summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_import.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-07-06 21:56:43 (GMT)
committerBrett Cannon <brett@python.org>2013-07-06 21:56:43 (GMT)
commita53cca3fea655e19a9b98d14c514dcc4c2f780fe (patch)
tree1305cb295dc134ad5e13797c0d7a6203fcb99ca5 /Lib/test/test_import.py
parent2a99d5df63395149ef7393ef5e8a4fb141f64b55 (diff)
downloadcpython-a53cca3fea655e19a9b98d14c514dcc4c2f780fe.zip
cpython-a53cca3fea655e19a9b98d14c514dcc4c2f780fe.tar.gz
cpython-a53cca3fea655e19a9b98d14c514dcc4c2f780fe.tar.bz2
Issue #18351: Fix various issues with
importlib._bootstrap._get_sourcefile(). Thanks to its only use by the C API, it was never properly tested until now. Thanks to Neal Norwitz for discovering the bug and Madison May for the patch.
Diffstat (limited to 'Lib/test/test_import.py')
-rw-r--r--Lib/test/test_import.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 8be66a1..e710122 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -1,5 +1,6 @@
# We import importlib *ASAP* in order to test #15386
import importlib
+from importlib._bootstrap import _get_sourcefile
import builtins
import imp
from test.test_importlib.import_ import util as importlib_util
@@ -11,6 +12,7 @@ import random
import stat
import sys
import unittest
+import unittest.mock as mock
import textwrap
import errno
import shutil
@@ -864,6 +866,40 @@ class ImportlibBootstrapTests(unittest.TestCase):
self.assertIs(imp.new_module, mod.new_module)
+@cpython_only
+class GetSourcefileTests(unittest.TestCase):
+
+ """Test importlib._bootstrap._get_sourcefile() as used by the C API.
+
+ Because of the peculiarities of the need of this function, the tests are
+ knowingly whitebox tests.
+
+ """
+
+ def test_get_sourcefile(self):
+ # Given a valid bytecode path, return the path to the corresponding
+ # source file if it exists.
+ with mock.patch('importlib._bootstrap._path_isfile') as _path_isfile:
+ _path_isfile.return_value = True;
+ path = TESTFN + '.pyc'
+ expect = TESTFN + '.py'
+ self.assertEqual(_get_sourcefile(path), expect)
+
+ def test_get_sourcefile_no_source(self):
+ # Given a valid bytecode path without a corresponding source path,
+ # return the original bytecode path.
+ with mock.patch('importlib._bootstrap._path_isfile') as _path_isfile:
+ _path_isfile.return_value = False;
+ path = TESTFN + '.pyc'
+ self.assertEqual(_get_sourcefile(path), path)
+
+ def test_get_sourcefile_bad_ext(self):
+ # Given a path with an invalid bytecode extension, return the
+ # bytecode path passed as the argument.
+ path = TESTFN + '.bad_ext'
+ self.assertEqual(_get_sourcefile(path), path)
+
+
class ImportTracebackTests(unittest.TestCase):
def setUp(self):
@@ -1028,7 +1064,7 @@ def test_main(verbose=None):
run_unittest(ImportTests, PycacheTests, FilePermissionTests,
PycRewritingTests, PathsTests, RelativeImportTests,
OverridingImportBuiltinTests,
- ImportlibBootstrapTests,
+ ImportlibBootstrapTests, GetSourcefileTests,
TestSymbolicallyLinkedPackage,
ImportTracebackTests)