Skip to content

Commit d1dc0ee

Browse files
committed
Refactor _make_mod() so we can use it to create package modules too
1 parent 61ba389 commit d1dc0ee

1 file changed

Lines changed: 24 additions & 36 deletions

File tree

Lib/test/test_site.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -944,14 +944,26 @@ def _make_pth(self, content, name='testpkg'):
944944
f.write(content)
945945
return basename
946946

947-
def _make_mod(self, contents, name='mod'):
948-
"""Write an importable <mod>.py, returning the module directory."""
947+
def _make_mod(self, contents, name='mod', *, package=False, on_path=False):
948+
"""Write an importable module (or package), returning its parent dir."""
949949
extdir = os.path.join(self.sitedir, 'extdir')
950-
os.mkdir(extdir)
951-
modpath = os.path.join(extdir, f'{name}.py')
950+
os.makedirs(extdir, exist_ok=True)
951+
952+
# Put the code in a package's dunder-init or flat module.
953+
if package:
954+
pkgdir = os.path.join(extdir, name)
955+
os.mkdir(pkgdir)
956+
modpath = os.path.join(pkgdir, '__init__.py')
957+
else:
958+
modpath = os.path.join(extdir, f'{name}.py')
959+
952960
with open(modpath, 'w') as fp:
953961
fp.write(contents)
962+
954963
self.addCleanup(sys.modules.pop, name, None)
964+
if on_path:
965+
# Don't worry, DirsOnSysPath() in setUp() will clean this up.
966+
sys.path.insert(0, extdir)
955967
return extdir
956968

957969
def _all_entrypoints(self):
@@ -1178,18 +1190,12 @@ def test_read_pth_file_locale_fallback(self):
11781190

11791191
def test_execute_entrypoints_with_callable(self):
11801192
# Entrypoint with callable is invoked.
1181-
mod_dir = os.path.join(self.sitedir, 'epmod')
1182-
os.mkdir(mod_dir)
1183-
init_file = os.path.join(mod_dir, '__init__.py')
1184-
with open(init_file, 'w') as f:
1185-
f.write("""\
1193+
self._make_mod("""\
11861194
called = False
11871195
def startup():
11881196
global called
11891197
called = True
1190-
""")
1191-
sys.path.insert(0, self.sitedir)
1192-
self.addCleanup(sys.modules.pop, 'epmod', None)
1198+
""", name='epmod', package=True, on_path=True)
11931199
fullname = os.path.join(self.sitedir, 'epmod.start')
11941200
site._pending_entrypoints[fullname] = ['epmod:startup']
11951201
site._execute_start_entrypoints()
@@ -1228,16 +1234,10 @@ def test_execute_entrypoints_strict_syntax_rejection(self):
12281234

12291235
def test_execute_entrypoints_callable_error(self):
12301236
# Callable that raises prints traceback but continues.
1231-
mod_dir = os.path.join(self.sitedir, 'badmod')
1232-
os.mkdir(mod_dir)
1233-
init_file = os.path.join(mod_dir, '__init__.py')
1234-
with open(init_file, 'w') as f:
1235-
f.write("""\
1237+
self._make_mod("""\
12361238
def fail():
12371239
raise RuntimeError("boom")
1238-
""")
1239-
sys.path.insert(0, self.sitedir)
1240-
self.addCleanup(sys.modules.pop, 'badmod', None)
1240+
""", name='badmod', package=True, on_path=True)
12411241
fullname = os.path.join(self.sitedir, 'badmod.start')
12421242
site._pending_entrypoints[fullname] = ['badmod:fail']
12431243
with captured_stderr() as err:
@@ -1247,18 +1247,12 @@ def fail():
12471247

12481248
def test_execute_entrypoints_duplicates_called_twice(self):
12491249
# PEP 829: duplicate entry points execute multiple times.
1250-
mod_dir = os.path.join(self.sitedir, 'countmod')
1251-
os.mkdir(mod_dir)
1252-
init_file = os.path.join(mod_dir, '__init__.py')
1253-
with open(init_file, 'w') as f:
1254-
f.write("""\
1250+
self._make_mod("""\
12551251
call_count = 0
12561252
def bump():
12571253
global call_count
12581254
call_count += 1
1259-
""")
1260-
sys.path.insert(0, self.sitedir)
1261-
self.addCleanup(sys.modules.pop, 'countmod', None)
1255+
""", name='countmod', package=True, on_path=True)
12621256
fullname = os.path.join(self.sitedir, 'countmod.start')
12631257
site._pending_entrypoints[fullname] = [
12641258
'countmod:bump', 'countmod:bump']
@@ -1289,18 +1283,12 @@ def test_exec_imports_not_suppressed_by_different_start(self):
12891283
def test_exec_imports_suppressed_by_empty_matching_start(self):
12901284
self._make_start("", name='foo')
12911285
self._make_pth("import epmod; epmod.startup()", name='foo')
1292-
mod_dir = os.path.join(self.sitedir, 'epmod')
1293-
os.mkdir(mod_dir)
1294-
init_file = os.path.join(mod_dir, '__init__.py')
1295-
with open(init_file, 'w') as f:
1296-
f.write("""\
1286+
self._make_mod("""\
12971287
called = False
12981288
def startup():
12991289
global called
13001290
called = True
1301-
""")
1302-
sys.path.insert(0, self.sitedir)
1303-
self.addCleanup(sys.modules.pop, 'epmod', None)
1291+
""", name='epmod', package=True, on_path=True)
13041292
site._read_pth_file(self.sitedir, 'foo.pth', set())
13051293
site._read_start_file(self.sitedir, 'foo.start')
13061294
site._exec_imports()

0 commit comments

Comments
 (0)