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

Function temp_module

Lib/test/test_importlib/util.py:172–192  ·  view source on GitHub ↗
(name, content='', *, pkg=False)

Source from the content-addressed store, hash-verified

170
171@contextlib.contextmanager
172def temp_module(name, content='', *, pkg=False):
173 conflicts = [n for n in sys.modules if n.partition('.')[0] == name]
174 with os_helper.temp_cwd(None) as cwd:
175 with uncache(name, *conflicts):
176 with import_helper.DirsOnSysPath(cwd):
177 invalidate_caches()
178
179 location = os.path.join(cwd, name)
180 if pkg:
181 modpath = os.path.join(location, '__init__.py')
182 os.mkdir(name)
183 else:
184 modpath = location + '.py'
185 if content is None:
186 # Make sure the module file gets created.
187 content = ''
188 if content is not None:
189 # not a namespace package
190 with open(modpath, 'w', encoding='utf-8') as modfile:
191 modfile.write(content)
192 yield location
193
194
195@contextlib.contextmanager

Callers 1

setup_moduleFunction · 0.90

Calls 7

invalidate_cachesFunction · 0.90
uncacheFunction · 0.85
openFunction · 0.50
partitionMethod · 0.45
joinMethod · 0.45
mkdirMethod · 0.45
writeMethod · 0.45

Tested by 1

setup_moduleFunction · 0.72