cpython/Lib/packaging/tests/test_mixin2to3.py
Éric Araujo 880801501b Improve byte-compilation in packaging to be independent of -O or -B.
The code I fixed to comply with PEP 3147 still had one bug: When run
under python -O, some paths for pyc files would be pyo, because I called
imp.cache_from_source without explicit debug_override argument in some
places, and under -O that would return .pyo (this is well explained in
the imp docs).  Now all code (util.byte_compile, build_py, install_lib)
can create .pyo files according to options given by users,
without interference from the calling Python’s own optimize mode.

On a related topic, I also removed the code that prevented byte
compilation under python -B.  The rationale is that packaging gives
control over the creation of pyc files to the user with its own explicit
option, and the behavior should not be changed if the calling Python
happens to run with -B for whatever reason.  I will argue that this is a
bug fix and ask to be allowed to backport this change to distutils.

Finally, I moved one nugget of information about the --compile and
--optimize options from the source into the doc.  It clears up a
misunderstanding that I (and maybe other people) had.
2011-11-03 05:08:28 +01:00

81 lines
2 KiB
Python

import textwrap
from packaging.tests import unittest, support
from packaging.compat import Mixin2to3
class Mixin2to3TestCase(support.TempdirManager,
support.LoggingCatcher,
unittest.TestCase):
@support.skip_2to3_optimize
def test_convert_code_only(self):
# used to check if code gets converted properly.
code = "print 'test'"
with self.mktempfile() as fp:
fp.write(code)
mixin2to3 = Mixin2to3()
mixin2to3._run_2to3([fp.name])
expected = "print('test')"
with open(fp.name) as fp:
converted = fp.read()
self.assertEqual(expected, converted)
def test_doctests_only(self):
# used to check if doctests gets converted properly.
doctest = textwrap.dedent('''\
"""Example docstring.
>>> print test
test
It works.
"""''')
with self.mktempfile() as fp:
fp.write(doctest)
mixin2to3 = Mixin2to3()
mixin2to3._run_2to3([fp.name])
expected = textwrap.dedent('''\
"""Example docstring.
>>> print(test)
test
It works.
"""\n''')
with open(fp.name) as fp:
converted = fp.read()
self.assertEqual(expected, converted)
def test_additional_fixers(self):
# used to check if use_2to3_fixers works
code = 'type(x) is not T'
with self.mktempfile() as fp:
fp.write(code)
mixin2to3 = Mixin2to3()
mixin2to3._run_2to3(files=[fp.name], doctests=[fp.name],
fixers=['packaging.tests.fixer'])
expected = 'not isinstance(x, T)'
with open(fp.name) as fp:
converted = fp.read()
self.assertEqual(expected, converted)
def test_suite():
return unittest.makeSuite(Mixin2to3TestCase)
if __name__ == "__main__":
unittest.main(defaultTest="test_suite")