.. versionadded:: 3.4
-.. function:: cache_from_source(path, debug_override=None, *, optimization=None)
+.. function:: cache_from_source(path, *, optimization=None)
Return the :pep:`3147`/:pep:`488` path to the byte-compiled file associated
with the source *path*. For example, if *path* is ``/foo/bar/baz.py`` the return
``/foo/bar/__pycache__/baz.cpython-32.opt-2.pyc``. The string representation
of *optimization* can only be alphanumeric, else :exc:`ValueError` is raised.
- The *debug_override* parameter is deprecated and can be used to override
- the system's value for ``__debug__``. A ``True`` value is the equivalent of
- setting *optimization* to the empty string. A ``False`` value is the same as
- setting *optimization* to ``1``. If both *debug_override* an *optimization*
- are not ``None`` then :exc:`TypeError` is raised.
-
.. versionadded:: 3.4
.. versionchanged:: 3.5
.. versionchanged:: 3.6
Accepts a :term:`path-like object`.
+ .. versionchanged:: 3.15
+ The *debug_override* parameter was removed.
+
.. function:: source_from_cache(path)
# Deprecated.
DEBUG_BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES = BYTECODE_SUFFIXES
-def cache_from_source(path, debug_override=None, *, optimization=None):
+def cache_from_source(path, *, optimization=None):
"""Given the path to a .py file, return the path to its .pyc file.
The .py file does not need to exist; this simply returns the path to the
of the argument is taken and verified to be alphanumeric (else ValueError
is raised).
- The debug_override parameter is deprecated. If debug_override is not None,
- a True value is the same as setting 'optimization' to the empty string
- while a False value is equivalent to setting 'optimization' to '1'.
-
If sys.implementation.cache_tag is None then NotImplementedError is raised.
"""
- if debug_override is not None:
- _warnings.warn('the debug_override parameter is deprecated; use '
- "'optimization' instead", DeprecationWarning)
- if optimization is not 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('.')
self.assertEqual(self.util.cache_from_source(path, optimization=''),
expect)
- def test_cache_from_source_debug_override(self):
- # Given the path to a .py file, return the path to its PEP 3147/PEP 488
- # defined .pyc file (i.e. under __pycache__).
- path = os.path.join('foo', 'bar', 'baz', 'qux.py')
- with warnings.catch_warnings():
- warnings.simplefilter('ignore')
- self.assertEqual(self.util.cache_from_source(path, False),
- self.util.cache_from_source(path, optimization=1))
- self.assertEqual(self.util.cache_from_source(path, True),
- self.util.cache_from_source(path, optimization=''))
- with warnings.catch_warnings():
- warnings.simplefilter('error')
- with self.assertRaises(DeprecationWarning):
- self.util.cache_from_source(path, False)
- with self.assertRaises(DeprecationWarning):
- self.util.cache_from_source(path, True)
-
def test_cache_from_source_cwd(self):
path = 'foo.py'
expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
self.assertEqual(self.util.cache_from_source(path, optimization=''),
expect)
- def test_cache_from_source_override(self):
- # When debug_override is not None, it can be any true-ish or false-ish
- # value.
- path = os.path.join('foo', 'bar', 'baz.py')
- # However if the bool-ishness can't be determined, the exception
- # propagates.
- class Bearish:
- def __bool__(self): raise RuntimeError
- with warnings.catch_warnings():
- warnings.simplefilter('ignore')
- self.assertEqual(self.util.cache_from_source(path, []),
- self.util.cache_from_source(path, optimization=1))
- self.assertEqual(self.util.cache_from_source(path, [17]),
- self.util.cache_from_source(path, optimization=''))
- with self.assertRaises(RuntimeError):
- self.util.cache_from_source('/foo/bar/baz.py', Bearish())
-
-
def test_cache_from_source_optimization_empty_string(self):
# Setting 'optimization' to '' leads to no optimization tag (PEP 488).
path = 'foo.py'