diff options
author | Brett Cannon <brett@python.org> | 2016-09-08 01:39:18 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2016-09-08 01:39:18 (GMT) |
commit | 035a1003820c0148b9a12f3034829fcc655a92bb (patch) | |
tree | 70e69c549aee6c15150b402951999419135984cb /Lib | |
parent | d5f92239818eef182fadc7c6f81f70912090573d (diff) | |
download | cpython-035a1003820c0148b9a12f3034829fcc655a92bb.zip cpython-035a1003820c0148b9a12f3034829fcc655a92bb.tar.gz cpython-035a1003820c0148b9a12f3034829fcc655a92bb.tar.bz2 |
Issue #26667: Add path-like object support to importlib.util.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 4 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_spec.py | 6 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_util.py | 19 |
3 files changed, 29 insertions, 0 deletions
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index d36e4ac..828246c 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -279,6 +279,7 @@ def cache_from_source(path, debug_override=None, *, optimization=None): message = 'debug_override or optimization must be set to None' raise TypeError(message) optimization = '' if debug_override else 1 + path = _os.fspath(path) head, tail = _path_split(path) base, sep, rest = tail.rpartition('.') tag = sys.implementation.cache_tag @@ -309,6 +310,7 @@ def source_from_cache(path): """ if sys.implementation.cache_tag is None: raise NotImplementedError('sys.implementation.cache_tag is None') + path = _os.fspath(path) head, pycache_filename = _path_split(path) head, pycache = _path_split(head) if pycache != _PYCACHE: @@ -536,6 +538,8 @@ def spec_from_file_location(name, location=None, *, loader=None, location = loader.get_filename(name) except ImportError: pass + else: + location = _os.fspath(location) # If the location is on the filesystem, but doesn't actually exist, # we could return None here, indicating that the location is not diff --git a/Lib/test/test_importlib/test_spec.py b/Lib/test/test_importlib/test_spec.py index 8b333e8..5a16a03 100644 --- a/Lib/test/test_importlib/test_spec.py +++ b/Lib/test/test_importlib/test_spec.py @@ -5,6 +5,7 @@ machinery = test_util.import_importlib('importlib.machinery') util = test_util.import_importlib('importlib.util') import os.path +import pathlib from test.support import CleanImport import unittest import sys @@ -659,6 +660,11 @@ class FactoryTests: self.assertEqual(spec.cached, self.cached) self.assertTrue(spec.has_location) + def test_spec_from_file_location_path_like_arg(self): + spec = self.util.spec_from_file_location(self.name, + pathlib.PurePath(self.path)) + self.assertEqual(spec.origin, self.path) + def test_spec_from_file_location_default_without_location(self): spec = self.util.spec_from_file_location(self.name) diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py index 41ca333..2aa1131 100644 --- a/Lib/test/test_importlib/test_util.py +++ b/Lib/test/test_importlib/test_util.py @@ -5,6 +5,7 @@ machinery = util.import_importlib('importlib.machinery') importlib_util = util.import_importlib('importlib.util') import os +import pathlib import string import sys from test import support @@ -677,6 +678,15 @@ class PEP3147Tests: '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) @unittest.skipUnless(sys.implementation.cache_tag is not None, + 'requires sys.implementation.cache_tag not be None') + def test_source_from_cache_path_like_arg(self): + path = pathlib.PurePath('foo', 'bar', 'baz', 'qux.py') + expect = os.path.join('foo', 'bar', 'baz', '__pycache__', + 'qux.{}.pyc'.format(self.tag)) + self.assertEqual(self.util.cache_from_source(path, optimization=''), + expect) + + @unittest.skipUnless(sys.implementation.cache_tag is not None, 'requires sys.implementation.cache_tag to not be ' 'None') def test_source_from_cache(self): @@ -738,6 +748,15 @@ class PEP3147Tests: with self.assertRaises(ValueError): self.util.source_from_cache(path) + @unittest.skipUnless(sys.implementation.cache_tag is not None, + 'requires sys.implementation.cache_tag to not be ' + 'None') + def test_source_from_cache_path_like_arg(self): + path = pathlib.PurePath('foo', 'bar', 'baz', '__pycache__', + 'qux.{}.pyc'.format(self.tag)) + expect = os.path.join('foo', 'bar', 'baz', 'qux.py') + self.assertEqual(self.util.source_from_cache(path), expect) + (Frozen_PEP3147Tests, Source_PEP3147Tests |