本文整理匯總了Python中contextlib._GeneratorContextManager方法的典型用法代碼示例。如果您正苦於以下問題:Python contextlib._GeneratorContextManager方法的具體用法?Python contextlib._GeneratorContextManager怎麽用?Python contextlib._GeneratorContextManager使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類contextlib
的用法示例。
在下文中一共展示了contextlib._GeneratorContextManager方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __call__
# 需要導入模塊: import contextlib [as 別名]
# 或者: from contextlib import _GeneratorContextManager [as 別名]
def __call__(self, func):
if isgeneratorfunction(func):
def inner(*args, **kwds):
with self._recreate_cm():
yield from func(*args, **kwds)
elif is_contextmanager(func):
@contextmanager
def inner(*args, **kwds):
with self._recreate_cm():
with func(*args, **kwds) as ret:
yield ret
else:
def inner(*args, **kwds):
with self._recreate_cm():
return func(*args, **kwds)
return wraps(func)(inner)
# Some python version have a different signature for '_GeneratorContextManager.__init__', so we must adapt:
示例2: test_open_and_close
# 需要導入模塊: import contextlib [as 別名]
# 或者: from contextlib import _GeneratorContextManager [as 別名]
def test_open_and_close(self):
# ProfileStore.open() returns a context manager that closes the
# database on exit.
config_file = self.makeDir().joinpath("config")
config = ProfileStore.open(config_file)
self.assertIsInstance(config, contextlib._GeneratorContextManager)
with config as config:
self.assertIsInstance(config, ProfileStore)
self.assertEqual((1,), config.database.execute("SELECT 1").fetchone())
self.assertRaises(sqlite3.ProgrammingError, config.database.execute, "SELECT 1")
示例3: test_open_and_close
# 需要導入模塊: import contextlib [as 別名]
# 或者: from contextlib import _GeneratorContextManager [as 別名]
def test_open_and_close(self):
# ProfileConfig.open() returns a context manager that closes the
# database on exit.
config_file = os.path.join(self.make_dir(), "config")
config = api.ProfileConfig.open(config_file, create=True)
self.assertIsInstance(config, contextlib._GeneratorContextManager)
with config as config:
self.assertIsInstance(config, api.ProfileConfig)
with config.cursor() as cursor:
self.assertEqual((1,), cursor.execute("SELECT 1").fetchone())
self.assertRaises(sqlite3.ProgrammingError, config.cursor)
示例4: test_open_and_close
# 需要導入模塊: import contextlib [as 別名]
# 或者: from contextlib import _GeneratorContextManager [as 別名]
def test_open_and_close(self):
# ConfigurationDatabase.open() returns a context manager that closes
# the database on exit.
config_file = os.path.join(self.make_dir(), "config")
config = ConfigurationDatabase.open_for_update(config_file)
self.assertIsInstance(config, contextlib._GeneratorContextManager)
with config as config:
self.assertIsInstance(config, ConfigurationDatabase)
with config.cursor() as cursor:
self.assertEqual((1,), cursor.execute("SELECT 1").fetchone())
self.assertRaises(sqlite3.ProgrammingError, config.cursor)