Commit 2481899a authored by Benjamin Peterson's avatar Benjamin Peterson

merge heads

parents aa14dc3b cb3ae4b1
...@@ -886,8 +886,8 @@ are always available. They are listed here in alphabetical order. ...@@ -886,8 +886,8 @@ are always available. They are listed here in alphabetical order.
*buffering* is an optional integer used to set the buffering policy. Pass 0 *buffering* is an optional integer used to set the buffering policy. Pass 0
to switch buffering off (only allowed in binary mode), 1 to select line to switch buffering off (only allowed in binary mode), 1 to select line
buffering (only usable in text mode), and an integer > 1 to indicate the size buffering (only usable in text mode), and an integer > 1 to indicate the size
of a fixed-size chunk buffer. When no *buffering* argument is given, the in bytes of a fixed-size chunk buffer. When no *buffering* argument is
default buffering policy works as follows: given, the default buffering policy works as follows:
* Binary files are buffered in fixed-size chunks; the size of the buffer is * Binary files are buffered in fixed-size chunks; the size of the buffer is
chosen using a heuristic trying to determine the underlying device's "block chosen using a heuristic trying to determine the underlying device's "block
......
...@@ -181,7 +181,7 @@ of which this module provides three different variants: ...@@ -181,7 +181,7 @@ of which this module provides three different variants:
.. versionchanged:: 3.4 .. versionchanged:: 3.4
The error response includes a Content-Length header. The error response includes a Content-Length header.
explain argument was added. Added the *explain* argument.
.. method:: send_response(code, message=None) .. method:: send_response(code, message=None)
......
...@@ -86,9 +86,9 @@ class BuildDumbTestCase(support.TempdirManager, ...@@ -86,9 +86,9 @@ class BuildDumbTestCase(support.TempdirManager,
fp.close() fp.close()
contents = sorted(os.path.basename(fn) for fn in contents) contents = sorted(os.path.basename(fn) for fn in contents)
wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
'foo.%s.pyc' % imp.get_tag(), if not sys.dont_write_bytecode:
'foo.py'] wanted.append('foo.%s.pyc' % imp.get_tag())
self.assertEqual(contents, sorted(wanted)) self.assertEqual(contents, sorted(wanted))
def test_suite(): def test_suite():
......
...@@ -215,8 +215,8 @@ def compile(pattern, flags=0): ...@@ -215,8 +215,8 @@ def compile(pattern, flags=0):
def purge(): def purge():
"Clear the regular expression caches" "Clear the regular expression caches"
_compile.cache_clear() _cache.clear()
_compile_repl.cache_clear() _cache_repl.clear()
def template(pattern, flags=0): def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object" "Compile a template pattern, returning a pattern object"
...@@ -259,11 +259,18 @@ def escape(pattern): ...@@ -259,11 +259,18 @@ def escape(pattern):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# internals # internals
_cache = {}
_cache_repl = {}
_pattern_type = type(sre_compile.compile("", 0)) _pattern_type = type(sre_compile.compile("", 0))
@functools.lru_cache(maxsize=512, typed=True) _MAXCACHE = 512
def _compile(pattern, flags): def _compile(pattern, flags):
# internal: compile pattern # internal: compile pattern
try:
return _cache[type(pattern), pattern, flags]
except KeyError:
pass
if isinstance(pattern, _pattern_type): if isinstance(pattern, _pattern_type):
if flags: if flags:
raise ValueError( raise ValueError(
...@@ -271,12 +278,23 @@ def _compile(pattern, flags): ...@@ -271,12 +278,23 @@ def _compile(pattern, flags):
return pattern return pattern
if not sre_compile.isstring(pattern): if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern") raise TypeError("first argument must be string or compiled pattern")
return sre_compile.compile(pattern, flags) p = sre_compile.compile(pattern, flags)
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[type(pattern), pattern, flags] = p
return p
@functools.lru_cache(maxsize=512)
def _compile_repl(repl, pattern): def _compile_repl(repl, pattern):
# internal: compile replacement pattern # internal: compile replacement pattern
return sre_parse.parse_template(repl, pattern) try:
return _cache_repl[repl, pattern]
except KeyError:
pass
p = sre_parse.parse_template(repl, pattern)
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl[repl, pattern] = p
return p
def _expand(pattern, match, template): def _expand(pattern, match, template):
# internal: match.expand implementation hook # internal: match.expand implementation hook
......
...@@ -162,8 +162,10 @@ class ImportTests(unittest.TestCase): ...@@ -162,8 +162,10 @@ class ImportTests(unittest.TestCase):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore') warnings.simplefilter('ignore')
mod = imp.load_compiled( if not sys.dont_write_bytecode:
temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) mod = imp.load_compiled(
temp_mod_name,
imp.cache_from_source(temp_mod_name + '.py'))
self.assertEqual(mod.a, 1) self.assertEqual(mod.a, 1)
if not os.path.exists(test_package_name): if not os.path.exists(test_package_name):
......
...@@ -24,6 +24,10 @@ from test.support import ( ...@@ -24,6 +24,10 @@ from test.support import (
from test import script_helper from test import script_helper
skip_if_dont_write_bytecode = unittest.skipIf(
sys.dont_write_bytecode,
"test meaningful only when writing bytecode")
def remove_files(name): def remove_files(name):
for f in (name + ".py", for f in (name + ".py",
name + ".pyc", name + ".pyc",
...@@ -120,6 +124,7 @@ class ImportTests(unittest.TestCase): ...@@ -120,6 +124,7 @@ class ImportTests(unittest.TestCase):
finally: finally:
del sys.path[0] del sys.path[0]
@skip_if_dont_write_bytecode
def test_bug7732(self): def test_bug7732(self):
source = TESTFN + '.py' source = TESTFN + '.py'
os.mkdir(source) os.mkdir(source)
...@@ -230,6 +235,7 @@ class ImportTests(unittest.TestCase): ...@@ -230,6 +235,7 @@ class ImportTests(unittest.TestCase):
remove_files(TESTFN) remove_files(TESTFN)
unload(TESTFN) unload(TESTFN)
@skip_if_dont_write_bytecode
def test_file_to_source(self): def test_file_to_source(self):
# check if __file__ points to the source file where available # check if __file__ points to the source file where available
source = TESTFN + ".py" source = TESTFN + ".py"
...@@ -316,6 +322,7 @@ class ImportTests(unittest.TestCase): ...@@ -316,6 +322,7 @@ class ImportTests(unittest.TestCase):
self.fail("fromlist must allow bogus names") self.fail("fromlist must allow bogus names")
@skip_if_dont_write_bytecode
class FilePermissionTests(unittest.TestCase): class FilePermissionTests(unittest.TestCase):
# tests for file mode on cached .pyc/.pyo files # tests for file mode on cached .pyc/.pyo files
...@@ -642,6 +649,7 @@ class PycacheTests(unittest.TestCase): ...@@ -642,6 +649,7 @@ class PycacheTests(unittest.TestCase):
del sys.path[0] del sys.path[0]
self._clean() self._clean()
@skip_if_dont_write_bytecode
def test_import_pyc_path(self): def test_import_pyc_path(self):
self.assertFalse(os.path.exists('__pycache__')) self.assertFalse(os.path.exists('__pycache__'))
__import__(TESTFN) __import__(TESTFN)
...@@ -654,6 +662,7 @@ class PycacheTests(unittest.TestCase): ...@@ -654,6 +662,7 @@ class PycacheTests(unittest.TestCase):
"test meaningful only on posix systems") "test meaningful only on posix systems")
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
"due to varying filesystem permission semantics (issue #11956)") "due to varying filesystem permission semantics (issue #11956)")
@skip_if_dont_write_bytecode
def test_unwritable_directory(self): def test_unwritable_directory(self):
# When the umask causes the new __pycache__ directory to be # When the umask causes the new __pycache__ directory to be
# unwritable, the import still succeeds but no .pyc file is written. # unwritable, the import still succeeds but no .pyc file is written.
...@@ -663,6 +672,7 @@ class PycacheTests(unittest.TestCase): ...@@ -663,6 +672,7 @@ class PycacheTests(unittest.TestCase):
self.assertFalse(os.path.exists(os.path.join( self.assertFalse(os.path.exists(os.path.join(
'__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag)))) '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag))))
@skip_if_dont_write_bytecode
def test_missing_source(self): def test_missing_source(self):
# With PEP 3147 cache layout, removing the source but leaving the pyc # With PEP 3147 cache layout, removing the source but leaving the pyc
# file does not satisfy the import. # file does not satisfy the import.
...@@ -673,6 +683,7 @@ class PycacheTests(unittest.TestCase): ...@@ -673,6 +683,7 @@ class PycacheTests(unittest.TestCase):
forget(TESTFN) forget(TESTFN)
self.assertRaises(ImportError, __import__, TESTFN) self.assertRaises(ImportError, __import__, TESTFN)
@skip_if_dont_write_bytecode
def test_missing_source_legacy(self): def test_missing_source_legacy(self):
# Like test_missing_source() except that for backward compatibility, # Like test_missing_source() except that for backward compatibility,
# when the pyc file lives where the py file would have been (and named # when the pyc file lives where the py file would have been (and named
...@@ -694,6 +705,7 @@ class PycacheTests(unittest.TestCase): ...@@ -694,6 +705,7 @@ class PycacheTests(unittest.TestCase):
pyc_file = imp.cache_from_source(TESTFN + '.py') pyc_file = imp.cache_from_source(TESTFN + '.py')
self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file)) self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file))
@skip_if_dont_write_bytecode
def test___cached___legacy_pyc(self): def test___cached___legacy_pyc(self):
# Like test___cached__() except that for backward compatibility, # Like test___cached__() except that for backward compatibility,
# when the pyc file lives where the py file would have been (and named # when the pyc file lives where the py file would have been (and named
...@@ -709,6 +721,7 @@ class PycacheTests(unittest.TestCase): ...@@ -709,6 +721,7 @@ class PycacheTests(unittest.TestCase):
self.assertEqual(m.__cached__, self.assertEqual(m.__cached__,
os.path.join(os.curdir, os.path.relpath(pyc_file))) os.path.join(os.curdir, os.path.relpath(pyc_file)))
@skip_if_dont_write_bytecode
def test_package___cached__(self): def test_package___cached__(self):
# Like test___cached__ but for packages. # Like test___cached__ but for packages.
def cleanup(): def cleanup():
......
...@@ -159,7 +159,8 @@ class SimpleTest(unittest.TestCase): ...@@ -159,7 +159,8 @@ class SimpleTest(unittest.TestCase):
finally: finally:
os.unlink(file_path) os.unlink(file_path)
pycache = os.path.dirname(imp.cache_from_source(file_path)) pycache = os.path.dirname(imp.cache_from_source(file_path))
shutil.rmtree(pycache) if os.path.exists(pycache):
shutil.rmtree(pycache)
def test_timestamp_overflow(self): def test_timestamp_overflow(self):
# When a modification timestamp is larger than 2**32, it should be # When a modification timestamp is larger than 2**32, it should be
......
...@@ -258,12 +258,13 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin): ...@@ -258,12 +258,13 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin):
importlib.invalidate_caches() importlib.invalidate_caches()
__import__(mod_name) __import__(mod_name)
os.remove(mod_fname) os.remove(mod_fname)
make_legacy_pyc(mod_fname) if not sys.dont_write_bytecode:
unload(mod_name) # In case loader caches paths make_legacy_pyc(mod_fname)
importlib.invalidate_caches() unload(mod_name) # In case loader caches paths
if verbose > 1: print("Running from compiled:", mod_name) importlib.invalidate_caches()
self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) if verbose > 1: print("Running from compiled:", mod_name)
self.check_code_execution(create_ns, expected_ns) self._fix_ns_for_legacy_pyc(expected_ns, alter_sys)
self.check_code_execution(create_ns, expected_ns)
finally: finally:
self._del_pkg(pkg_dir, depth, mod_name) self._del_pkg(pkg_dir, depth, mod_name)
if verbose > 1: print("Module executed successfully") if verbose > 1: print("Module executed successfully")
...@@ -293,12 +294,13 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin): ...@@ -293,12 +294,13 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin):
importlib.invalidate_caches() importlib.invalidate_caches()
__import__(mod_name) __import__(mod_name)
os.remove(mod_fname) os.remove(mod_fname)
make_legacy_pyc(mod_fname) if not sys.dont_write_bytecode:
unload(mod_name) # In case loader caches paths make_legacy_pyc(mod_fname)
if verbose > 1: print("Running from compiled:", pkg_name) unload(mod_name) # In case loader caches paths
importlib.invalidate_caches() if verbose > 1: print("Running from compiled:", pkg_name)
self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) importlib.invalidate_caches()
self.check_code_execution(create_ns, expected_ns) self._fix_ns_for_legacy_pyc(expected_ns, alter_sys)
self.check_code_execution(create_ns, expected_ns)
finally: finally:
self._del_pkg(pkg_dir, depth, pkg_name) self._del_pkg(pkg_dir, depth, pkg_name)
if verbose > 1: print("Package executed successfully") if verbose > 1: print("Package executed successfully")
...@@ -351,16 +353,17 @@ from ..uncle.cousin import nephew ...@@ -351,16 +353,17 @@ from ..uncle.cousin import nephew
importlib.invalidate_caches() importlib.invalidate_caches()
__import__(mod_name) __import__(mod_name)
os.remove(mod_fname) os.remove(mod_fname)
make_legacy_pyc(mod_fname) if not sys.dont_write_bytecode:
unload(mod_name) # In case the loader caches paths make_legacy_pyc(mod_fname)
if verbose > 1: print("Running from compiled:", mod_name) unload(mod_name) # In case the loader caches paths
importlib.invalidate_caches() if verbose > 1: print("Running from compiled:", mod_name)
d2 = run_module(mod_name, run_name=run_name) # Read from bytecode importlib.invalidate_caches()
self.assertEqual(d2["__name__"], expected_name) d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
self.assertEqual(d2["__package__"], pkg_name) self.assertEqual(d2["__name__"], expected_name)
self.assertIn("sibling", d2) self.assertEqual(d2["__package__"], pkg_name)
self.assertIn("nephew", d2) self.assertIn("sibling", d2)
del d2 # Ensure __loader__ entry doesn't keep file open self.assertIn("nephew", d2)
del d2 # Ensure __loader__ entry doesn't keep file open
finally: finally:
self._del_pkg(pkg_dir, depth, mod_name) self._del_pkg(pkg_dir, depth, mod_name)
if verbose > 1: print("Module executed successfully") if verbose > 1: print("Module executed successfully")
...@@ -513,9 +516,10 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin): ...@@ -513,9 +516,10 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
script_name = self._make_test_script(script_dir, mod_name) script_name = self._make_test_script(script_dir, mod_name)
compiled_name = py_compile.compile(script_name, doraise=True) compiled_name = py_compile.compile(script_name, doraise=True)
os.remove(script_name) os.remove(script_name)
legacy_pyc = make_legacy_pyc(script_name) if not sys.dont_write_bytecode:
self._check_script(script_dir, "<run_path>", legacy_pyc, legacy_pyc = make_legacy_pyc(script_name)
script_dir) self._check_script(script_dir, "<run_path>", legacy_pyc,
script_dir)
def test_directory_error(self): def test_directory_error(self):
with temp_dir() as script_dir: with temp_dir() as script_dir:
......
...@@ -283,6 +283,9 @@ Core and Builtins ...@@ -283,6 +283,9 @@ Core and Builtins
Library Library
------- -------
- Issue #16389: Fixed a performance regression relative to Python 3.1 in the
caching of compiled regular expressions.
- Added missing FeedParser and BytesFeedParser to email.parser.__all__. - Added missing FeedParser and BytesFeedParser to email.parser.__all__.
- Issue #17431: Fix missing import of BytesFeedParser in email.parser. - Issue #17431: Fix missing import of BytesFeedParser in email.parser.
...@@ -935,6 +938,9 @@ Extension Modules ...@@ -935,6 +938,9 @@ Extension Modules
Tests Tests
----- -----
- Issue #11420: make test suite pass with -B/DONTWRITEBYTECODE set.
Initial patch by Thomas Wouters.
- Issue #10652: make tcl/tk tests run after __all__ test, patch by - Issue #10652: make tcl/tk tests run after __all__ test, patch by
Zachary Ware. Zachary Ware.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment