本文整理汇总了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)