Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f1913ca

Browse files
committed
marshal: optimize parsing of empty Unicode strings
Don't create a temporary buffer of zeroy byte nor call r_string() if the length is zero, create directly the empty string.
1 parent 79278bd commit f1913ca

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

Python/marshal.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,20 +979,25 @@ r_object(RFILE *p)
979979
retval = NULL;
980980
break;
981981
}
982-
buffer = PyMem_NEW(char, n);
983-
if (buffer == NULL) {
984-
retval = PyErr_NoMemory();
985-
break;
986-
}
987-
if (r_string(buffer, n, p) != n) {
982+
if (n != 0) {
983+
buffer = PyMem_NEW(char, n);
984+
if (buffer == NULL) {
985+
retval = PyErr_NoMemory();
986+
break;
987+
}
988+
if (r_string(buffer, n, p) != n) {
989+
PyMem_DEL(buffer);
990+
PyErr_SetString(PyExc_EOFError,
991+
"EOF read where object expected");
992+
retval = NULL;
993+
break;
994+
}
995+
v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
988996
PyMem_DEL(buffer);
989-
PyErr_SetString(PyExc_EOFError,
990-
"EOF read where object expected");
991-
retval = NULL;
992-
break;
993997
}
994-
v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
995-
PyMem_DEL(buffer);
998+
else {
999+
v = PyUnicode_New(0, 0);
1000+
}
9961001
if (type == TYPE_INTERNED)
9971002
PyUnicode_InternInPlace(&v);
9981003
retval = v;

0 commit comments

Comments
 (0)