]> granicus.if.org Git - python/commitdiff
Issue #19209: Remove import of copyreg from the os module to speed up
authorChristian Heimes <christian@cheimes.de>
Fri, 11 Oct 2013 23:27:08 +0000 (01:27 +0200)
committerChristian Heimes <christian@cheimes.de>
Fri, 11 Oct 2013 23:27:08 +0000 (01:27 +0200)
interpreter startup. stat_result and statvfs_result are now hard-coded to
reside in the os module.
The patch is based on Victor Stinner's patch.

Lib/os.py
Lib/test/test_os.py
Lib/test/test_site.py
Misc/NEWS
Modules/posixmodule.c

index 5fde36075cc3f371ea47d52e2f7fb45d4f5d4e75..1b1ce181e7b757407ab0b9fb03f841a0e0cc78ef 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -945,33 +945,6 @@ otherwise return -SIG, where SIG is the signal that killed it. """
     __all__.extend(["spawnlp", "spawnlpe"])
 
 
-import copyreg as _copyreg
-
-def _make_stat_result(tup, dict):
-    return stat_result(tup, dict)
-
-def _pickle_stat_result(sr):
-    (type, args) = sr.__reduce__()
-    return (_make_stat_result, args)
-
-try:
-    _copyreg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
-except NameError: # stat_result may not exist
-    pass
-
-def _make_statvfs_result(tup, dict):
-    return statvfs_result(tup, dict)
-
-def _pickle_statvfs_result(sr):
-    (type, args) = sr.__reduce__()
-    return (_make_statvfs_result, args)
-
-try:
-    _copyreg.pickle(statvfs_result, _pickle_statvfs_result,
-                     _make_statvfs_result)
-except NameError: # statvfs_result may not exist
-    pass
-
 # Supply os.popen()
 def popen(cmd, mode="r", buffering=-1):
     if not isinstance(cmd, str):
index 39b0e80125b9edfeec4bea9ad1efdf8b421f40da..ab44d011e5e886a8a78a065e522bd08df2af2b62 100644 (file)
@@ -26,6 +26,7 @@ import locale
 import codecs
 import decimal
 import fractions
+import pickle
 try:
     import threading
 except ImportError:
@@ -264,6 +265,13 @@ class StatAttributeTests(unittest.TestCase):
             warnings.simplefilter("ignore", DeprecationWarning)
             self.check_stat_attributes(fname)
 
+    def test_stat_result_pickle(self):
+        result = os.stat(self.fname)
+        p = pickle.dumps(result)
+        self.assertIn(b'\x03cos\nstat_result\n', p)
+        unpickled = pickle.loads(p)
+        self.assertEqual(result, unpickled)
+
     def test_statvfs_attributes(self):
         if not hasattr(os, "statvfs"):
             return
@@ -310,6 +318,20 @@ class StatAttributeTests(unittest.TestCase):
         except TypeError:
             pass
 
+    @unittest.skipUnless(hasattr(os, 'statvfs'),
+                         "need os.statvfs()")
+    def test_statvfs_result_pickle(self):
+        try:
+            result = os.statvfs(self.fname)
+        except OSError as e:
+            # On AtheOS, glibc always returns ENOSYS
+            if e.errno == errno.ENOSYS:
+                return
+        p = pickle.dumps(result)
+        self.assertIn(b'\x03cos\nstatvfs_result\n', p)
+        unpickled = pickle.loads(p)
+        self.assertEqual(result, unpickled)
+
     def test_utime_dir(self):
         delta = 1000000
         st = os.stat(support.TESTFN)
index d26060316de1035db176c1c2c6c0cfb9e0b4092f..5147edcf2923bd0178fcb940ab9e1a09519636a1 100644 (file)
@@ -431,10 +431,13 @@ class StartupImportTests(unittest.TestCase):
         modules = eval(stdout.decode('utf-8'))
         self.assertIn('site', modules)
 
+        # http://bugs.python.org/issue19205
         re_mods = {'re', '_sre', 'sre_compile', 'sre_constants', 'sre_parse'}
         self.assertFalse(modules.intersection(re_mods))
-
+        # http://bugs.python.org/issue9548
         self.assertNotIn('locale', modules)
+        # http://bugs.python.org/issue19209
+        self.assertNotIn('copyreg', modules)
 
 
 if __name__ == "__main__":
index c520bbe6365ff81884d69cf0b0b1c832728c2f20..921508a538c18c7cf8e3ce48b165425a82af3e87 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #19209: Remove import of copyreg from the os module to speed up
+  interpreter startup. stat_result and statvfs_result are now hard-coded to
+  reside in the os module.
+
 - Issue #19205: Don't import the 're' module in site and sysconfig module to
   to speed up interpreter start.
 
index df0d81b02ec1f7b02821cb09ec332c8c9fecd9d5..27798528be5488c438cda6b7a7460fc10a2e7d3e 100644 (file)
@@ -11974,7 +11974,7 @@ INITFUNC(void)
             return NULL;
 #endif
 
-        stat_result_desc.name = MODNAME ".stat_result";
+        stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
         stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
         stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
         stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
@@ -11983,7 +11983,7 @@ INITFUNC(void)
         structseq_new = StatResultType.tp_new;
         StatResultType.tp_new = statresult_new;
 
-        statvfs_result_desc.name = MODNAME ".statvfs_result";
+        statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
         if (PyStructSequence_InitType2(&StatVFSResultType,
                                        &statvfs_result_desc) < 0)
             return NULL;