Kaydet (Commit) 1aa78938 authored tarafından Victor Stinner's avatar Victor Stinner

Issue #26146: marshal.loads() now uses the empty frozenset singleton

üst 5ebe2c89
...@@ -135,6 +135,13 @@ class ContainerTestCase(unittest.TestCase, HelperMixin): ...@@ -135,6 +135,13 @@ class ContainerTestCase(unittest.TestCase, HelperMixin):
for constructor in (set, frozenset): for constructor in (set, frozenset):
self.helper(constructor(self.d.keys())) self.helper(constructor(self.d.keys()))
@support.cpython_only
def test_empty_frozenset_singleton(self):
# marshal.loads() must reuse the empty frozenset singleton
obj = frozenset()
obj2 = marshal.loads(marshal.dumps(obj))
self.assertIs(obj2, obj)
class BufferTestCase(unittest.TestCase, HelperMixin): class BufferTestCase(unittest.TestCase, HelperMixin):
......
...@@ -1266,6 +1266,16 @@ r_object(RFILE *p) ...@@ -1266,6 +1266,16 @@ r_object(RFILE *p)
PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)"); PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
break; break;
} }
if (n == 0 && type == TYPE_FROZENSET) {
/* call frozenset() to get the empty frozenset singleton */
v = PyObject_CallFunction((PyObject*)&PyFrozenSet_Type, NULL);
if (v == NULL)
break;
R_REF(v);
retval = v;
}
else {
v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL); v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
if (type == TYPE_SET) { if (type == TYPE_SET) {
R_REF(v); R_REF(v);
...@@ -1301,6 +1311,7 @@ r_object(RFILE *p) ...@@ -1301,6 +1311,7 @@ r_object(RFILE *p)
if (type != TYPE_SET) if (type != TYPE_SET)
v = r_ref_insert(v, idx, flag, p); v = r_ref_insert(v, idx, flag, p);
retval = v; retval = v;
}
break; break;
case TYPE_CODE: case TYPE_CODE:
......
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