Skip to content

Commit 79cfb42

Browse files
Rogdhamlangston-barrett
authored andcommitted
1 parent 36cf7de commit 79cfb42

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

sphinxcontrib/autoprogram.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import os
1919
import re
2020
import sys
21+
import tempfile
2122
from functools import reduce
2223
from typing import Any, Dict, Iterable, List, Optional, Tuple
2324
import unittest
@@ -144,16 +145,16 @@ def import_object(import_name: str):
144145
# the script, if there is a script named module_name. Otherwise, raise
145146
# an ImportError as it did before.
146147
import glob
147-
import sys
148148
import os
149-
import imp
149+
import sys
150+
import types
150151

151152
for p in sys.path:
152153
f = glob.glob(os.path.join(p, module_name))
153154
if len(f) > 0:
154155
with open(f[0]) as fobj:
155156
codestring = fobj.read()
156-
foo = imp.new_module("foo")
157+
foo = types.ModuleType("foo")
157158
exec(codestring, foo.__dict__)
158159

159160
sys.modules["foo"] = foo
@@ -590,6 +591,14 @@ def test_import_object(self) -> None:
590591
)
591592
self.assertIsInstance(instance, UtilTestCase)
592593

594+
def test_import_object_filename(self) -> None:
595+
with tempfile.TemporaryDirectory() as tmpdirname:
596+
filename = os.path.join(tmpdirname, 'somefile')
597+
with open(filename, 'w') as fp:
598+
fp.write("bar = 42\n")
599+
value = import_object("{}:bar".format(filename))
600+
self.assertTrue(value == 42)
601+
593602
if not hasattr(unittest.TestCase, "assertIsInstance"):
594603

595604
def assertIsInstance(self, instance, cls) -> None: # type: ignore[override]

0 commit comments

Comments
 (0)