MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / run_as_contextmanager

Function run_as_contextmanager

lib/sqlalchemy/testing/util.py:156–178  ·  view source on GitHub ↗

Run the given function under the given contextmanager, simulating the behavior of 'with' to support older Python versions. This is not necessary anymore as we have placed 2.6 as minimum Python version, however some tests are still using this structure.

(ctx, fn, *arg, **kw)

Source from the content-addressed store, hash-verified

154
155
156def run_as_contextmanager(ctx, fn, *arg, **kw):
157 """Run the given function under the given contextmanager,
158 simulating the behavior of 'with' to support older
159 Python versions.
160
161 This is not necessary anymore as we have placed 2.6
162 as minimum Python version, however some tests are still using
163 this structure.
164
165 """
166
167 obj = ctx.__enter__()
168 try:
169 result = fn(obj, *arg, **kw)
170 ctx.__exit__(None, None, None)
171 return result
172 except:
173 exc_info = sys.exc_info()
174 raise_ = ctx.__exit__(*exc_info)
175 if not raise_:
176 raise
177 else:
178 return raise_
179
180
181def rowset(results):

Callers

nothing calls this directly

Calls 2

__enter__Method · 0.45
__exit__Method · 0.45

Tested by

no test coverage detected