MCPcopy Index your code
hub / github.com/python/cpython / temp_module

Function temp_module

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

Source from the content-addressed store, hash-verified

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…