summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-07-06 22:04:41 (GMT)
committerBrett Cannon <brett@python.org>2013-07-06 22:04:41 (GMT)
commit7e5d55705c2694e941047c5d3aac280211f566ce (patch)
tree0357fad36c3cda49c3c93f8892b1f6bd8e6adf59 /Lib/test
parent98054b4c1be933f5bbfb3c1a1f4ee3f556dd47ca (diff)
parenta53cca3fea655e19a9b98d14c514dcc4c2f780fe (diff)
downloadcpython-7e5d55705c2694e941047c5d3aac280211f566ce.zip
cpython-7e5d55705c2694e941047c5d3aac280211f566ce.tar.gz
cpython-7e5d55705c2694e941047c5d3aac280211f566ce.tar.bz2
merge for issue #18351.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_import.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 2351ba9..e6a4379 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -1,6 +1,7 @@
# We import importlib *ASAP* in order to test #15386
import importlib
import importlib.util
+from importlib._bootstrap import _get_sourcefile
import builtins
from test.test_importlib.import_ import util as importlib_util
import marshal
@@ -11,6 +12,7 @@ import random
import stat
import sys
import unittest
+import unittest.mock as mock
import textwrap
import errno
import shutil
@@ -851,6 +853,40 @@ class ImportlibBootstrapTests(unittest.TestCase):
self.assertIs(machinery.FileFinder, mod.FileFinder)
+@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):