MCPcopy Index your code
hub / github.com/RustPython/RustPython / import_state

Function import_state

Lib/test/test_importlib/util.py:196–222  ·  view source on GitHub ↗

Context manager to manage the various importers and stored state in the sys module. The 'modules' attribute is not supported as the interpreter state stores a pointer to the dict that the interpreter uses internally; reassigning to sys.modules does not have the desired effect.

(**kwargs)

Source from the content-addressed store, hash-verified

194
195@contextlib.contextmanager
196def import_state(**kwargs):
197 """Context manager to manage the various importers and stored state in the
198 sys module.
199
200 The 'modules' attribute is not supported as the interpreter state stores a
201 pointer to the dict that the interpreter uses internally;
202 reassigning to sys.modules does not have the desired effect.
203
204 """
205 originals = {}
206 try:
207 for attr, default in (('meta_path', []), ('path', []),
208 ('path_hooks', []),
209 ('path_importer_cache', {})):
210 originals[attr] = getattr(sys, attr)
211 if attr in kwargs:
212 new_value = kwargs[attr]
213 del kwargs[attr]
214 else:
215 new_value = default
216 setattr(sys, attr, new_value)
217 if len(kwargs):
218 raise ValueError('unrecognized arguments: {}'.format(kwargs))
219 yield
220 finally:
221 for attr, value in originals.items():
222 setattr(sys, attr, value)
223
224
225class _ImporterMock:

Callers 1

create_modulesFunction · 0.85

Calls 5

getattrFunction · 0.85
setattrFunction · 0.85
lenFunction · 0.85
formatMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected