(name, content='', *, pkg=False)
| 168 | |
| 169 | @contextlib.contextmanager |
| 170 | def 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 |
searching dependent graphs…