[Python-checkins] cpython (merge default -> default): merge

georg.brandl python-checkins at python.org
Sat Oct 12 22:56:44 CEST 2013


http://hg.python.org/cpython/rev/e45cb197f207
changeset:   86261:e45cb197f207
parent:      86260:fc09e5cff7ea
parent:      86258:142c62a490ce
user:        Georg Brandl <georg at python.org>
date:        Sat Oct 12 22:57:30 2013 +0200
summary:
  merge

files:
  Include/asdl.h           |     4 +-
  Include/marshal.h        |     2 +-
  Lib/test/test_marshal.py |     8 +-
  Misc/NEWS                |     6 +
  Parser/asdl_c.py         |     4 +-
  Python/Python-ast.c      |    96 +-
  Python/asdl.c            |     4 +-
  Python/ast.c             |    64 +-
  Python/errors.c          |     2 +-
  Python/fileutils.c       |     2 +-
  Python/importlib.h       |  6625 ++++++++++++-------------
  Python/marshal.c         |   390 +-
  12 files changed, 3565 insertions(+), 3642 deletions(-)


diff --git a/Include/asdl.h b/Include/asdl.h
--- a/Include/asdl.h
+++ b/Include/asdl.h
@@ -25,8 +25,8 @@
     int elements[1];
 } asdl_int_seq;
 
-asdl_seq *asdl_seq_new(Py_ssize_t size, PyArena *arena);
-asdl_int_seq *asdl_int_seq_new(Py_ssize_t size, PyArena *arena);
+asdl_seq *_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena);
+asdl_int_seq *_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena);
 
 #define asdl_seq_GET(S, I) (S)->elements[(I)]
 #define asdl_seq_LEN(S) ((S) == NULL ? 0 : (S)->size)
diff --git a/Include/marshal.h b/Include/marshal.h
--- a/Include/marshal.h
+++ b/Include/marshal.h
@@ -7,7 +7,7 @@
 extern "C" {
 #endif
 
-#define Py_MARSHAL_VERSION 3
+#define Py_MARSHAL_VERSION 4
 
 PyAPI_FUNC(void) PyMarshal_WriteLongToFile(long, FILE *, int);
 PyAPI_FUNC(void) PyMarshal_WriteObjectToFile(PyObject *, FILE *, int);
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -262,11 +262,11 @@
 
     def test_bad_reader(self):
         class BadReader(io.BytesIO):
-            def read(self, n=-1):
-                b = super().read(n)
+            def readinto(self, buf):
+                n = super().readinto(buf)
                 if n is not None and n > 4:
-                    b += b' ' * 10**6
-                return b
+                    n += 10**6
+                return n
         for value in (1.0, 1j, b'0123456789', '0123456789'):
             self.assertRaises(ValueError, marshal.load,
                               BadReader(marshal.dumps(value)))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,12 @@
 Core and Builtins
 -----------------
 
+- Issue #4555: All exported C symbols are now prefixed with either
+  "Py" or "_Py".
+
+- Issue #19219: Speed up marshal.loads(), and make pyc files slightly
+  (5% to 10%) smaller.
+
 - Issue #19221: Upgrade Unicode database to version 6.3.0.
 
 - Issue #16742: The result of the C callback PyOS_ReadlineFunctionPointer must
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -519,9 +519,9 @@
             self.emit("}", depth+1)
             self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
             if self.isSimpleType(field):
-                self.emit("%s = asdl_int_seq_new(len, arena);" % field.name, depth+1)
+                self.emit("%s = _Py_asdl_int_seq_new(len, arena);" % field.name, depth+1)
             else:
-                self.emit("%s = asdl_seq_new(len, arena);" % field.name, depth+1)
+                self.emit("%s = _Py_asdl_seq_new(len, arena);" % field.name, depth+1)
             self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
             self.emit("for (i = 0; i < len; i++) {", depth+1)
             self.emit("%s value;" % ctype, depth+2)
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -3612,7 +3612,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -3647,7 +3647,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -3704,7 +3704,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -3807,7 +3807,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -3831,7 +3831,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            decorator_list = asdl_seq_new(len, arena);
+            decorator_list = _Py_asdl_seq_new(len, arena);
             if (decorator_list == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -3894,7 +3894,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            bases = asdl_seq_new(len, arena);
+            bases = _Py_asdl_seq_new(len, arena);
             if (bases == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -3918,7 +3918,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            keywords = asdl_seq_new(len, arena);
+            keywords = _Py_asdl_seq_new(len, arena);
             if (keywords == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 keyword_ty value;
@@ -3962,7 +3962,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -3986,7 +3986,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            decorator_list = asdl_seq_new(len, arena);
+            decorator_list = _Py_asdl_seq_new(len, arena);
             if (decorator_list == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -4043,7 +4043,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            targets = asdl_seq_new(len, arena);
+            targets = _Py_asdl_seq_new(len, arena);
             if (targets == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -4079,7 +4079,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            targets = asdl_seq_new(len, arena);
+            targets = _Py_asdl_seq_new(len, arena);
             if (targets == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -4196,7 +4196,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4220,7 +4220,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            orelse = asdl_seq_new(len, arena);
+            orelse = _Py_asdl_seq_new(len, arena);
             if (orelse == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4268,7 +4268,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4292,7 +4292,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            orelse = asdl_seq_new(len, arena);
+            orelse = _Py_asdl_seq_new(len, arena);
             if (orelse == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4340,7 +4340,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4364,7 +4364,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            orelse = asdl_seq_new(len, arena);
+            orelse = _Py_asdl_seq_new(len, arena);
             if (orelse == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4400,7 +4400,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            items = asdl_seq_new(len, arena);
+            items = _Py_asdl_seq_new(len, arena);
             if (items == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 withitem_ty value;
@@ -4424,7 +4424,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4494,7 +4494,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4518,7 +4518,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            handlers = asdl_seq_new(len, arena);
+            handlers = _Py_asdl_seq_new(len, arena);
             if (handlers == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 excepthandler_ty value;
@@ -4542,7 +4542,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            orelse = asdl_seq_new(len, arena);
+            orelse = _Py_asdl_seq_new(len, arena);
             if (orelse == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4566,7 +4566,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            finalbody = asdl_seq_new(len, arena);
+            finalbody = _Py_asdl_seq_new(len, arena);
             if (finalbody == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -4635,7 +4635,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            names = asdl_seq_new(len, arena);
+            names = _Py_asdl_seq_new(len, arena);
             if (names == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 alias_ty value;
@@ -4682,7 +4682,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            names = asdl_seq_new(len, arena);
+            names = _Py_asdl_seq_new(len, arena);
             if (names == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 alias_ty value;
@@ -4727,7 +4727,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            names = asdl_seq_new(len, arena);
+            names = _Py_asdl_seq_new(len, arena);
             if (names == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 identifier value;
@@ -4762,7 +4762,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            names = asdl_seq_new(len, arena);
+            names = _Py_asdl_seq_new(len, arena);
             if (names == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 identifier value;
@@ -4903,7 +4903,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            values = asdl_seq_new(len, arena);
+            values = _Py_asdl_seq_new(len, arena);
             if (values == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5099,7 +5099,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            keys = asdl_seq_new(len, arena);
+            keys = _Py_asdl_seq_new(len, arena);
             if (keys == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5123,7 +5123,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            values = asdl_seq_new(len, arena);
+            values = _Py_asdl_seq_new(len, arena);
             if (values == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5158,7 +5158,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            elts = asdl_seq_new(len, arena);
+            elts = _Py_asdl_seq_new(len, arena);
             if (elts == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5205,7 +5205,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            generators = asdl_seq_new(len, arena);
+            generators = _Py_asdl_seq_new(len, arena);
             if (generators == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 comprehension_ty value;
@@ -5252,7 +5252,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            generators = asdl_seq_new(len, arena);
+            generators = _Py_asdl_seq_new(len, arena);
             if (generators == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 comprehension_ty value;
@@ -5311,7 +5311,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            generators = asdl_seq_new(len, arena);
+            generators = _Py_asdl_seq_new(len, arena);
             if (generators == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 comprehension_ty value;
@@ -5358,7 +5358,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            generators = asdl_seq_new(len, arena);
+            generators = _Py_asdl_seq_new(len, arena);
             if (generators == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 comprehension_ty value;
@@ -5449,7 +5449,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            ops = asdl_int_seq_new(len, arena);
+            ops = _Py_asdl_int_seq_new(len, arena);
             if (ops == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 cmpop_ty value;
@@ -5473,7 +5473,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            comparators = asdl_seq_new(len, arena);
+            comparators = _Py_asdl_seq_new(len, arena);
             if (comparators == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5523,7 +5523,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            args = asdl_seq_new(len, arena);
+            args = _Py_asdl_seq_new(len, arena);
             if (args == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5547,7 +5547,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            keywords = asdl_seq_new(len, arena);
+            keywords = _Py_asdl_seq_new(len, arena);
             if (keywords == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 keyword_ty value;
@@ -5862,7 +5862,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            elts = asdl_seq_new(len, arena);
+            elts = _Py_asdl_seq_new(len, arena);
             if (elts == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -5909,7 +5909,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            elts = asdl_seq_new(len, arena);
+            elts = _Py_asdl_seq_new(len, arena);
             if (elts == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 expr_ty value;
@@ -6074,7 +6074,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            dims = asdl_seq_new(len, arena);
+            dims = _Py_asdl_seq_new(len, arena);
             if (dims == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 slice_ty value;
@@ -6425,7 +6425,7 @@
             goto failed;
         }
         len = PyList_GET_SIZE(tmp);
-        ifs = asdl_seq_new(len, arena);
+        ifs = _Py_asdl_seq_new(len, arena);
         if (ifs == NULL) goto failed;
         for (i = 0; i < len; i++) {
             expr_ty value;
@@ -6520,7 +6520,7 @@
                 goto failed;
             }
             len = PyList_GET_SIZE(tmp);
-            body = asdl_seq_new(len, arena);
+            body = _Py_asdl_seq_new(len, arena);
             if (body == NULL) goto failed;
             for (i = 0; i < len; i++) {
                 stmt_ty value;
@@ -6566,7 +6566,7 @@
             goto failed;
         }
         len = PyList_GET_SIZE(tmp);
-        args = asdl_seq_new(len, arena);
+        args = _Py_asdl_seq_new(len, arena);
         if (args == NULL) goto failed;
         for (i = 0; i < len; i++) {
             arg_ty value;
@@ -6600,7 +6600,7 @@
             goto failed;
         }
         len = PyList_GET_SIZE(tmp);
-        kwonlyargs = asdl_seq_new(len, arena);
+        kwonlyargs = _Py_asdl_seq_new(len, arena);
         if (kwonlyargs == NULL) goto failed;
         for (i = 0; i < len; i++) {
             arg_ty value;
@@ -6624,7 +6624,7 @@
             goto failed;
         }
         len = PyList_GET_SIZE(tmp);
-        kw_defaults = asdl_seq_new(len, arena);
+        kw_defaults = _Py_asdl_seq_new(len, arena);
         if (kw_defaults == NULL) goto failed;
         for (i = 0; i < len; i++) {
             expr_ty value;
@@ -6658,7 +6658,7 @@
             goto failed;
         }
         len = PyList_GET_SIZE(tmp);
-        defaults = asdl_seq_new(len, arena);
+        defaults = _Py_asdl_seq_new(len, arena);
         if (defaults == NULL) goto failed;
         for (i = 0; i < len; i++) {
             expr_ty value;
diff --git a/Python/asdl.c b/Python/asdl.c
--- a/Python/asdl.c
+++ b/Python/asdl.c
@@ -2,7 +2,7 @@
 #include "asdl.h"
 
 asdl_seq *
-asdl_seq_new(Py_ssize_t size, PyArena *arena)
+_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
 {
     asdl_seq *seq = NULL;
     size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
@@ -33,7 +33,7 @@
 }
 
 asdl_int_seq *
-asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
+_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
 {
     asdl_int_seq *seq = NULL;
     size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
diff --git a/Python/ast.c b/Python/ast.c
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -696,7 +696,7 @@
     k = 0;
     switch (TYPE(n)) {
         case file_input:
-            stmts = asdl_seq_new(num_stmts(n), arena);
+            stmts = _Py_asdl_seq_new(num_stmts(n), arena);
             if (!stmts)
                 goto out;
             for (i = 0; i < NCH(n) - 1; i++) {
@@ -736,7 +736,7 @@
         }
         case single_input:
             if (TYPE(CHILD(n, 0)) == NEWLINE) {
-                stmts = asdl_seq_new(1, arena);
+                stmts = _Py_asdl_seq_new(1, arena);
                 if (!stmts)
                     goto out;
                 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
@@ -748,7 +748,7 @@
             else {
                 n = CHILD(n, 0);
                 num = num_stmts(n);
-                stmts = asdl_seq_new(num, arena);
+                stmts = _Py_asdl_seq_new(num, arena);
                 if (!stmts)
                     goto out;
                 if (num == 1) {
@@ -1099,7 +1099,7 @@
     int i;
     assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
 
-    seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
+    seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
     if (!seq)
         return NULL;
 
@@ -1279,22 +1279,22 @@
         if (TYPE(ch) == DOUBLESTAR) break;
         if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
     }
-    posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
+    posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
     if (!posargs && nposargs)
         return NULL;
     kwonlyargs = (nkwonlyargs ?
-                   asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
+                   _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
     if (!kwonlyargs && nkwonlyargs)
         return NULL;
     posdefaults = (nposdefaults ?
-                    asdl_seq_new(nposdefaults, c->c_arena) : NULL);
+                    _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
     if (!posdefaults && nposdefaults)
         return NULL;
     /* The length of kwonlyargs and kwdefaults are same
        since we set NULL as default for keyword only argument w/o default
        - we have sequence data structure, but no dictionary */
     kwdefaults = (nkwonlyargs ?
-                   asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
+                   _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
     if (!kwdefaults && nkwonlyargs)
         return NULL;
 
@@ -1462,7 +1462,7 @@
     int i;
 
     REQ(n, decorators);
-    decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
+    decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
     if (!decorator_seq)
         return NULL;
 
@@ -1658,7 +1658,7 @@
     if (n_fors == -1)
         return NULL;
 
-    comps = asdl_seq_new(n_fors, c->c_arena);
+    comps = _Py_asdl_seq_new(n_fors, c->c_arena);
     if (!comps)
         return NULL;
 
@@ -1699,7 +1699,7 @@
             if (n_ifs == -1)
                 return NULL;
 
-            ifs = asdl_seq_new(n_ifs, c->c_arena);
+            ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
             if (!ifs)
                 return NULL;
 
@@ -1921,7 +1921,7 @@
             /* it's a simple set */
             asdl_seq *elts;
             size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
-            elts = asdl_seq_new(size, c->c_arena);
+            elts = _Py_asdl_seq_new(size, c->c_arena);
             if (!elts)
                 return NULL;
             for (i = 0; i < NCH(ch); i += 2) {
@@ -1940,11 +1940,11 @@
         } else {
             /* it's a dict */
             size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
-            keys = asdl_seq_new(size, c->c_arena);
+            keys = _Py_asdl_seq_new(size, c->c_arena);
             if (!keys)
                 return NULL;
 
-            values = asdl_seq_new(size, c->c_arena);
+            values = _Py_asdl_seq_new(size, c->c_arena);
             if (!values)
                 return NULL;
 
@@ -2139,7 +2139,7 @@
             expr_ty e;
             int simple = 1;
             asdl_seq *slices, *elts;
-            slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
+            slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
             if (!slices)
                 return NULL;
             for (j = 0; j < NCH(n); j += 2) {
@@ -2155,7 +2155,7 @@
                                  Load, LINENO(n), n->n_col_offset, c->c_arena);
             }
             /* extract Index values and put them in a Tuple */
-            elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
+            elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
             if (!elts)
                 return NULL;
             for (j = 0; j < asdl_seq_LEN(slices); ++j) {
@@ -2288,7 +2288,7 @@
                 n = CHILD(n, 0);
                 goto loop;
             }
-            seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
+            seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
             if (!seq)
                 return NULL;
             for (i = 0; i < NCH(n); i += 2) {
@@ -2324,10 +2324,10 @@
                 expr_ty expression;
                 asdl_int_seq *ops;
                 asdl_seq *cmps;
-                ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
+                ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
                 if (!ops)
                     return NULL;
-                cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
+                cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
                 if (!cmps) {
                     return NULL;
                 }
@@ -2453,10 +2453,10 @@
         return NULL;
     }
 
-    args = asdl_seq_new(nargs + ngens, c->c_arena);
+    args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
     if (!args)
         return NULL;
-    keywords = asdl_seq_new(nkeywords, c->c_arena);
+    keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
     if (!keywords)
         return NULL;
     nargs = 0;
@@ -2633,7 +2633,7 @@
 
         /* a normal assignment */
         REQ(CHILD(n, 1), EQUAL);
-        targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
+        targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
         if (!targets)
             return NULL;
         for (i = 0; i < NCH(n) - 2; i += 2) {
@@ -2674,7 +2674,7 @@
 
     REQ(n, exprlist);
 
-    seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
+    seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
     if (!seq)
         return NULL;
     for (i = 0; i < NCH(n); i += 2) {
@@ -2904,7 +2904,7 @@
     if (TYPE(n) == import_name) {
         n = CHILD(n, 1);
         REQ(n, dotted_as_names);
-        aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
+        aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
         if (!aliases)
                 return NULL;
         for (i = 0; i < NCH(n); i += 2) {
@@ -2966,7 +2966,7 @@
             return NULL;
         }
 
-        aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
+        aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
         if (!aliases)
             return NULL;
 
@@ -3005,7 +3005,7 @@
     int i;
 
     REQ(n, global_stmt);
-    s = asdl_seq_new(NCH(n) / 2, c->c_arena);
+    s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
     if (!s)
         return NULL;
     for (i = 1; i < NCH(n); i += 2) {
@@ -3026,7 +3026,7 @@
     int i;
 
     REQ(n, nonlocal_stmt);
-    s = asdl_seq_new(NCH(n) / 2, c->c_arena);
+    s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
     if (!s)
         return NULL;
     for (i = 1; i < NCH(n); i += 2) {
@@ -3079,7 +3079,7 @@
     REQ(n, suite);
 
     total = num_stmts(n);
-    seq = asdl_seq_new(total, c->c_arena);
+    seq = _Py_asdl_seq_new(total, c->c_arena);
     if (!seq)
         return NULL;
     if (TYPE(CHILD(n, 0)) == simple_stmt) {
@@ -3198,7 +3198,7 @@
         if (has_else) {
             asdl_seq *suite_seq2;
 
-            orelse = asdl_seq_new(1, c->c_arena);
+            orelse = _Py_asdl_seq_new(1, c->c_arena);
             if (!orelse)
                 return NULL;
             expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
@@ -3222,7 +3222,7 @@
 
         for (i = 0; i < n_elif; i++) {
             int off = 5 + (n_elif - i - 1) * 4;
-            asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
+            asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
             if (!newobj)
                 return NULL;
             expression = ast_for_expr(c, CHILD(n, off));
@@ -3434,7 +3434,7 @@
     if (n_except > 0) {
         int i;
         /* process except statements to create a try ... except */
-        handlers = asdl_seq_new(n_except, c->c_arena);
+        handlers = _Py_asdl_seq_new(n_except, c->c_arena);
         if (handlers == NULL)
             return NULL;
 
@@ -3485,7 +3485,7 @@
     REQ(n, with_stmt);
 
     n_items = (NCH(n) - 2) / 2;
-    items = asdl_seq_new(n_items, c->c_arena);
+    items = _Py_asdl_seq_new(n_items, c->c_arena);
     if (!items)
         return NULL;
     for (i = 1; i < NCH(n) - 2; i += 2) {
diff --git a/Python/errors.c b/Python/errors.c
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1014,7 +1014,7 @@
    XXX The functionality of this function is quite similar to the
    functionality in tb_displayline() in traceback.c. */
 
-PyObject *
+static PyObject *
 err_programtext(FILE *fp, int lineno)
 {
     int i;
diff --git a/Python/fileutils.c b/Python/fileutils.c
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -566,7 +566,7 @@
 
 #endif
 
-int
+static int
 get_inheritable(int fd, int raise)
 {
 #ifdef MS_WINDOWS
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]
diff --git a/Python/marshal.c b/Python/marshal.c
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -51,6 +51,12 @@
 #define TYPE_FROZENSET          '>'
 #define FLAG_REF                '\x80' /* with a type, add obj to index */
 
+#define TYPE_ASCII              'a'
+#define TYPE_ASCII_INTERNED     'A'
+#define TYPE_SMALL_TUPLE        ')'
+#define TYPE_SHORT_ASCII        'z'
+#define TYPE_SHORT_ASCII_INTERNED 'Z'
+
 #define WFERR_OK 0
 #define WFERR_UNMARSHALLABLE 1
 #define WFERR_NESTEDTOODEEP 2
@@ -66,6 +72,8 @@
     PyObject *current_filename;
     char *ptr;
     char *end;
+    char *buf;
+    Py_ssize_t buf_size;
     PyObject *refs; /* dict on marshal, list on unmarshal */
     int version;
 } WFILE;
@@ -148,6 +156,13 @@
         w_string(s, n, p);
 }
 
+static void
+w_short_pstring(const char *s, Py_ssize_t n, WFILE *p)
+{
+    w_byte(n, p);
+    w_string(s, n, p);
+}
+
 /* We assume that Python ints are stored internally in base some power of
    2**15; for the sake of portability we'll always read and write them in base
    exactly 2**15. */
@@ -394,24 +409,51 @@
         w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
     }
     else if (PyUnicode_CheckExact(v)) {
-        PyObject *utf8;
-        utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
-        if (utf8 == NULL) {
-            p->depth--;
-            p->error = WFERR_UNMARSHALLABLE;
-            return;
+        if (p->version >= 4 && PyUnicode_IS_ASCII(v)) {
+            int is_short = PyUnicode_GET_LENGTH(v) < 256;
+            if (is_short) {
+                if (PyUnicode_CHECK_INTERNED(v))
+                    W_TYPE(TYPE_SHORT_ASCII_INTERNED, p);
+                else
+                    W_TYPE(TYPE_SHORT_ASCII, p);
+                w_short_pstring((char *) PyUnicode_1BYTE_DATA(v),
+                                PyUnicode_GET_LENGTH(v), p);
+            }
+            else {
+                if (PyUnicode_CHECK_INTERNED(v))
+                    W_TYPE(TYPE_ASCII_INTERNED, p);
+                else
+                    W_TYPE(TYPE_ASCII, p);
+                w_pstring((char *) PyUnicode_1BYTE_DATA(v),
+                          PyUnicode_GET_LENGTH(v), p);
+            }
         }
-        if (p->version >= 3 &&  PyUnicode_CHECK_INTERNED(v))
-            W_TYPE(TYPE_INTERNED, p);
-        else
-            W_TYPE(TYPE_UNICODE, p);
-        w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
-        Py_DECREF(utf8);
+        else {
+            PyObject *utf8;
+            utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
+            if (utf8 == NULL) {
+                p->depth--;
+                p->error = WFERR_UNMARSHALLABLE;
+                return;
+            }
+            if (p->version >= 3 &&  PyUnicode_CHECK_INTERNED(v))
+                W_TYPE(TYPE_INTERNED, p);
+            else
+                W_TYPE(TYPE_UNICODE, p);
+            w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
+            Py_DECREF(utf8);
+        }
     }
     else if (PyTuple_CheckExact(v)) {
-        W_TYPE(TYPE_TUPLE, p);
         n = PyTuple_Size(v);
-        W_SIZE(n, p);
+        if (p->version >= 4 && n < 256) {
+            W_TYPE(TYPE_SMALL_TUPLE, p);
+            w_byte(n, p);
+        }
+        else {
+            W_TYPE(TYPE_TUPLE, p);
+            W_SIZE(n, p);
+        }
         for (i = 0; i < n; i++) {
             w_object(PyTuple_GET_ITEM(v, i), p);
         }
@@ -537,59 +579,75 @@
 
 typedef WFILE RFILE; /* Same struct with different invariants */
 
-#define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
+static char *
+r_string(Py_ssize_t n, RFILE *p)
+{
+    Py_ssize_t read = -1;
 
-static Py_ssize_t
-r_string(char *s, Py_ssize_t n, RFILE *p)
-{
-    char *ptr;
-    Py_ssize_t read, left;
+    if (p->ptr != NULL) {
+        /* Fast path for loads() */
+        char *res = p->ptr;
+        Py_ssize_t left = p->end - p->ptr;
+        if (left < n) {
+            PyErr_SetString(PyExc_EOFError,
+                            "marshal data too short");
+            return NULL;
+        }
+        p->ptr += n;
+        return res;
+    }
+    if (p->buf == NULL) {
+        p->buf = PyMem_MALLOC(n);
+        if (p->buf == NULL) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+        p->buf_size = n;
+    }
+    else if (p->buf_size < n) {
+        p->buf = PyMem_REALLOC(p->buf, n);
+        if (p->buf == NULL) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+        p->buf_size = n;
+    }
+    if (!p->readable) {
+        assert(p->fp != NULL);
+        /* The result fits into int because it must be <=n. */
+        read = fread(p->buf, 1, n, p->fp);
+    }
+    else {
+        _Py_IDENTIFIER(readinto);
+        PyObject *res, *mview;
+        Py_buffer buf;
 
-    if (!p->readable) {
-        if (p->fp != NULL)
-            /* The result fits into int because it must be <=n. */
-            read = fread(s, 1, n, p->fp);
-        else {
-            left = p->end - p->ptr;
-            read = (left < n) ? left : n;
-            memcpy(s, p->ptr, read);
-            p->ptr += read;
+        if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1)
+            return NULL;
+        mview = PyMemoryView_FromBuffer(&buf);
+        if (mview == NULL)
+            return NULL;
+
+        res = _PyObject_CallMethodId(p->readable, &PyId_readinto, "N", mview);
+        if (res != NULL) {
+            read = PyNumber_AsSsize_t(res, PyExc_ValueError);
+            Py_DECREF(res);
         }
     }
-    else {
-        _Py_IDENTIFIER(read);
-
-        PyObject *data = _PyObject_CallMethodId(p->readable, &PyId_read, "n", n);
-        read = 0;
-        if (data != NULL) {
-            if (!PyBytes_Check(data)) {
-                PyErr_Format(PyExc_TypeError,
-                             "f.read() returned not bytes but %.100s",
-                             data->ob_type->tp_name);
-            }
-            else {
-                read = (int)PyBytes_GET_SIZE(data);
-                if (read > 0) {
-                    if (read > n) {
-                        PyErr_Format(PyExc_ValueError,
-                                    "read() returned too much data: "
-                                    "%zd bytes requested, %zd returned",
-                                    n, read);
-                        read = -1;
-                    }
-                    else {
-                        ptr = PyBytes_AS_STRING(data);
-                        memcpy(s, ptr, read);
-                    }
-                }
-            }
-            Py_DECREF(data);
+    if (read != n) {
+        if (!PyErr_Occurred()) {
+            if (read > n)
+                PyErr_Format(PyExc_ValueError,
+                             "read() returned too much data: "
+                             "%zd bytes requested, %zd returned",
+                             n, read);
+            else
+                PyErr_SetString(PyExc_EOFError,
+                                "EOF read where not expected");
         }
+        return NULL;
     }
-    if (!PyErr_Occurred() && (read < n)) {
-        PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
-    }
-    return read;
+    return p->buf;
 }
 
 
@@ -597,15 +655,20 @@
 r_byte(RFILE *p)
 {
     int c = EOF;
-    unsigned char ch;
-    Py_ssize_t n;
 
-    if (!p->readable)
-        c = p->fp ? getc(p->fp) : rs_byte(p);
+    if (p->ptr != NULL) {
+        if (p->ptr < p->end)
+            c = (unsigned char) *p->ptr++;
+        return c;
+    }
+    if (!p->readable) {
+        assert(p->fp);
+        c = getc(p->fp);
+    }
     else {
-        n = r_string((char *) &ch, 1, p);
-        if (n > 0)
-            c = ch;
+        char *ptr = r_string(1, p);
+        if (ptr != NULL)
+            c = *(unsigned char *) ptr;
     }
     return c;
 }
@@ -613,32 +676,36 @@
 static int
 r_short(RFILE *p)
 {
-    short x;
-    unsigned char buffer[2];
+    short x = -1;
+    unsigned char *buffer;
 
-    r_string((char *) buffer, 2, p);
-    x = buffer[0];
-    x |= buffer[1] << 8;
-    /* Sign-extension, in case short greater than 16 bits */
-    x |= -(x & 0x8000);
+    buffer = (unsigned char *) r_string(2, p);
+    if (buffer != NULL) {
+        x = buffer[0];
+        x |= buffer[1] << 8;
+        /* Sign-extension, in case short greater than 16 bits */
+        x |= -(x & 0x8000);
+    }
     return x;
 }
 
 static long
 r_long(RFILE *p)
 {
-    long x;
-    unsigned char buffer[4];
+    long x = -1;
+    unsigned char *buffer;
 
-    r_string((char *) buffer, 4, p);
-    x = buffer[0];
-    x |= (long)buffer[1] << 8;
-    x |= (long)buffer[2] << 16;
-    x |= (long)buffer[3] << 24;
+    buffer = (unsigned char *) r_string(4, p);
+    if (buffer != NULL) {
+        x = buffer[0];
+        x |= (long)buffer[1] << 8;
+        x |= (long)buffer[2] << 16;
+        x |= (long)buffer[3] << 24;
 #if SIZEOF_LONG > 4
-    /* Sign extension for 64-bit machines */
-    x |= -(x & 0x80000000L);
+        /* Sign extension for 64-bit machines */
+        x |= -(x & 0x80000000L);
 #endif
+    }
     return x;
 }
 
@@ -716,9 +783,7 @@
 r_ref_reserve(int flag, RFILE *p)
 {
     if (flag) { /* currently only FLAG_REF is defined */
-        Py_ssize_t idx = PyList_Size(p->refs);
-        if (idx < 0)
-            return -1;
+        Py_ssize_t idx = PyList_GET_SIZE(p->refs);
         if (idx >= 0x7ffffffe) {
             PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
             return -1;
@@ -742,12 +807,10 @@
 r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
 {
     if (o != NULL && flag) { /* currently only FLAG_REF is defined */
-        if (PyList_SetItem(p->refs, idx, o) < 0) {
-            Py_DECREF(o); /* release the new object */
-            return NULL;
-        } else {
-            Py_INCREF(o); /* a reference for the list */
-        }
+        PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
+        Py_INCREF(o);
+        PyList_SET_ITEM(p->refs, idx, o);
+        Py_DECREF(tmp);
     }
     return o;
 }
@@ -777,7 +840,7 @@
     Py_ssize_t idx = 0;
     long i, n;
     int type, code = r_byte(p);
-    int flag;
+    int flag, is_interned = 0;
     PyObject *retval;
 
     if (code == EOF) {
@@ -846,7 +909,7 @@
 
     case TYPE_FLOAT:
         {
-            char buf[256];
+            char buf[256], *ptr;
             double dx;
             retval = NULL;
             n = r_byte(p);
@@ -855,8 +918,10 @@
                     "EOF read where object expected");
                 break;
             }
-            if (r_string(buf, n, p) != n)
+            ptr = r_string(n, p);
+            if (ptr == NULL)
                 break;
+            memcpy(buf, ptr, n);
             buf[n] = '\0';
             dx = PyOS_string_to_double(buf, NULL, NULL);
             if (dx == -1.0 && PyErr_Occurred())
@@ -868,9 +933,10 @@
 
     case TYPE_BINARY_FLOAT:
         {
-            unsigned char buf[8];
+            unsigned char *buf;
             double x;
-            if (r_string((char*)buf, 8, p) != 8) {
+            buf = (unsigned char *) r_string(8, p);
+            if (buf == NULL) {
                 retval = NULL;
                 break;
             }
@@ -886,7 +952,7 @@
 
     case TYPE_COMPLEX:
         {
-            char buf[256];
+            char buf[256], *ptr;
             Py_complex c;
             retval = NULL;
             n = r_byte(p);
@@ -895,8 +961,10 @@
                     "EOF read where object expected");
                 break;
             }
-            if (r_string(buf, n, p) != n)
+            ptr = r_string(n, p);
+            if (ptr == NULL)
                 break;
+            memcpy(buf, ptr, n);
             buf[n] = '\0';
             c.real = PyOS_string_to_double(buf, NULL, NULL);
             if (c.real == -1.0 && PyErr_Occurred())
@@ -907,8 +975,10 @@
                     "EOF read where object expected");
                 break;
             }
-            if (r_string(buf, n, p) != n)
+            ptr = r_string(n, p);
+            if (ptr == NULL)
                 break;
+            memcpy(buf, ptr, n);
             buf[n] = '\0';
             c.imag = PyOS_string_to_double(buf, NULL, NULL);
             if (c.imag == -1.0 && PyErr_Occurred())
@@ -920,9 +990,10 @@
 
     case TYPE_BINARY_COMPLEX:
         {
-            unsigned char buf[8];
+            unsigned char *buf;
             Py_complex c;
-            if (r_string((char*)buf, 8, p) != 8) {
+            buf = (unsigned char *) r_string(8, p);
+            if (buf == NULL) {
                 retval = NULL;
                 break;
             }
@@ -931,7 +1002,8 @@
                 retval = NULL;
                 break;
             }
-            if (r_string((char*)buf, 8, p) != 8) {
+            buf = (unsigned char *) r_string(8, p);
+            if (buf == NULL) {
                 retval = NULL;
                 break;
             }
@@ -946,32 +1018,82 @@
         }
 
     case TYPE_STRING:
+        {
+            char *ptr;
+            n = r_long(p);
+            if (PyErr_Occurred()) {
+                retval = NULL;
+                break;
+            }
+            if (n < 0 || n > SIZE32_MAX) {
+                PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+                retval = NULL;
+                break;
+            }
+            v = PyBytes_FromStringAndSize((char *)NULL, n);
+            if (v == NULL) {
+                retval = NULL;
+                break;
+            }
+            ptr = r_string(n, p);
+            if (ptr == NULL) {
+                Py_DECREF(v);
+                retval = NULL;
+                break;
+            }
+            memcpy(PyBytes_AS_STRING(v), ptr, n);
+            retval = v;
+            R_REF(retval);
+            break;
+        }
+
+    case TYPE_ASCII_INTERNED:
+        is_interned = 1;
+    case TYPE_ASCII:
         n = r_long(p);
         if (PyErr_Occurred()) {
             retval = NULL;
             break;
         }
         if (n < 0 || n > SIZE32_MAX) {
-            PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+            PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
             retval = NULL;
             break;
         }
-        v = PyBytes_FromStringAndSize((char *)NULL, n);
-        if (v == NULL) {
-            retval = NULL;
+        goto _read_ascii;
+
+    case TYPE_SHORT_ASCII_INTERNED:
+        is_interned = 1;
+    case TYPE_SHORT_ASCII:
+        n = r_byte(p);
+        if (n == EOF) {
+            PyErr_SetString(PyExc_EOFError,
+                "EOF read where object expected");
             break;
         }
-        if (r_string(PyBytes_AS_STRING(v), n, p) != n) {
-            Py_DECREF(v);
-            retval = NULL;
+    _read_ascii:
+        {
+            char *ptr;
+            ptr = r_string(n, p);
+            if (ptr == NULL) {
+                retval = NULL;
+                break;
+            }
+            v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
+            if (v == NULL) {
+                retval = NULL;
+                break;
+            }
+            if (is_interned)
+                PyUnicode_InternInPlace(&v);
+            retval = v;
+            R_REF(retval);
             break;
         }
-        retval = v;
-        R_REF(retval);
-        break;
 
+    case TYPE_INTERNED:
+        is_interned = 1;
     case TYPE_UNICODE:
-    case TYPE_INTERNED:
         {
         char *buffer;
 
@@ -986,18 +1108,12 @@
             break;
         }
         if (n != 0) {
-            buffer = PyMem_NEW(char, n);
+            buffer = r_string(n, p);
             if (buffer == NULL) {
-                retval = PyErr_NoMemory();
-                break;
-            }
-            if (r_string(buffer, n, p) != n) {
-                PyMem_DEL(buffer);
                 retval = NULL;
                 break;
             }
             v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
-            PyMem_DEL(buffer);
         }
         else {
             v = PyUnicode_New(0, 0);
@@ -1006,13 +1122,16 @@
             retval = NULL;
             break;
         }
-        if (type == TYPE_INTERNED)
+        if (is_interned)
             PyUnicode_InternInPlace(&v);
         retval = v;
         R_REF(retval);
         break;
         }
 
+    case TYPE_SMALL_TUPLE:
+        n = (unsigned char) r_byte(p);
+        goto _read_tuple;
     case TYPE_TUPLE:
         n = r_long(p);
         if (PyErr_Occurred()) {
@@ -1024,6 +1143,7 @@
             retval = NULL;
             break;
         }
+    _read_tuple:
         v = PyTuple_New(n);
         R_REF(v);
         if (v == NULL) {
@@ -1304,23 +1424,33 @@
 PyMarshal_ReadShortFromFile(FILE *fp)
 {
     RFILE rf;
+    int res;
     assert(fp);
     rf.readable = NULL;
     rf.fp = fp;
     rf.current_filename = NULL;
     rf.end = rf.ptr = NULL;
-    return r_short(&rf);
+    rf.buf = NULL;
+    res = r_short(&rf);
+    if (rf.buf != NULL)
+        PyMem_FREE(rf.buf);
+    return res;
 }
 
 long
 PyMarshal_ReadLongFromFile(FILE *fp)
 {
     RFILE rf;
+    long res;
     rf.fp = fp;
     rf.readable = NULL;
     rf.current_filename = NULL;
     rf.ptr = rf.end = NULL;
-    return r_long(&rf);
+    rf.buf = NULL;
+    res = r_long(&rf);
+    if (rf.buf != NULL)
+        PyMem_FREE(rf.buf);
+    return res;
 }
 
 #ifdef HAVE_FSTAT
@@ -1379,11 +1509,14 @@
     rf.current_filename = NULL;
     rf.depth = 0;
     rf.ptr = rf.end = NULL;
+    rf.buf = NULL;
     rf.refs = PyList_New(0);
     if (rf.refs == NULL)
         return NULL;
     result = r_object(&rf);
     Py_DECREF(rf.refs);
+    if (rf.buf != NULL)
+        PyMem_FREE(rf.buf);
     return result;
 }
 
@@ -1397,12 +1530,15 @@
     rf.current_filename = NULL;
     rf.ptr = str;
     rf.end = str + len;
+    rf.buf = NULL;
     rf.depth = 0;
     rf.refs = PyList_New(0);
     if (rf.refs == NULL)
         return NULL;
     result = r_object(&rf);
     Py_DECREF(rf.refs);
+    if (rf.buf != NULL)
+        PyMem_FREE(rf.buf);
     return result;
 }
 
@@ -1516,9 +1652,13 @@
         rf.fp = NULL;
         rf.readable = f;
         rf.current_filename = NULL;
+        rf.ptr = rf.end = NULL;
+        rf.buf = NULL;
         if ((rf.refs = PyList_New(0)) != NULL) {
             result = read_object(&rf);
             Py_DECREF(rf.refs);
+            if (rf.buf != NULL)
+                PyMem_FREE(rf.buf);
         } else
             result = NULL;
     }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list