Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move this import below?


from test import support
from test.support import is_apple_mobile, os_helper, requires_debug_ranges, is_emscripten
from test.support.script_helper import assert_python_ok
Expand Down Expand Up @@ -872,6 +874,55 @@ def test_read_object_from_file(self):
_testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)

@support.cpython_only
class GCTrackingTestCase(unittest.TestCase):

def _not_tracked_instantly(self, t):
new = marshal.loads(marshal.dumps(t))

self.assertFalse(gc.is_tracked(t), t)
self.assertFalse(gc.is_tracked(new), new)

def _not_tracked(self, t):
# Nested tuples can take several collections to untrack
gc.collect()
gc.collect()

new = marshal.loads(marshal.dumps(t))

self.assertFalse(gc.is_tracked(t), t)
self.assertFalse(gc.is_tracked(new), new)

def _tracked(self, t):
new = marshal.loads(marshal.dumps(t))

self.assertTrue(gc.is_tracked(t), t)
self.assertTrue(gc.is_tracked(new), new)

def testTuple(self):
x, y, z = 1.5, "a", []

self._not_tracked_instantly(())
self._not_tracked_instantly((1,))
self._not_tracked_instantly((1, 2))
self._not_tracked_instantly((1, 2, "a"))
self._not_tracked_instantly((12, 10**10, 'a_' * 100))

# Test for _PyTuple_Concat
self._not_tracked_instantly((1, 2) + (2, 3))

# Test for _PyTuple_Repeat
self._not_tracked_instantly((1, 2) * 5)

self._not_tracked(((1, x), y, (2, 3)))
self._not_tracked((1, 2, (True, False, ())))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, something like this?

t = (True, False, ())
self._not_tracked((1, 2, t))

Because for now self._not_tracked_instantly((1, 2, (True, False, ()))) is also correct.


self._tracked(([],))
self._tracked(([1],))
self._tracked(({},))
self._tracked((set(),))
self._tracked((x, y, z))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Untrack tuples from the GC when possible during unmarshalling. Patch by
Sergey Miryanov.
13 changes: 13 additions & 0 deletions Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ r_object(RFILE *p)
int type, code = r_byte(p);
int flag, is_interned = 0;
PyObject *retval = NULL;
bool track_tuple = false;

if (code == EOF) {
if (PyErr_ExceptionMatches(PyExc_EOFError)) {
Expand Down Expand Up @@ -1422,16 +1423,28 @@ r_object(RFILE *p)
if (v == NULL)
break;

// empty tuples are untracked, and we can check if n > 0,
// but using PyObject_GC_UnTrack is clearer
PyObject_GC_UnTrack(v);
track_tuple = false;

for (i = 0; i < n; i++) {
v2 = r_object(p);
if ( v2 == NULL ) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_TypeError,
"NULL object in marshal data for tuple");
Py_SETREF(v, NULL);
track_tuple = false;
break;
}
PyTuple_SET_ITEM(v, i, v2);
if (!track_tuple && PyObject_GC_IsTracked(v2)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not _PyObject_GC_MAY_BE_TRACKED, as in _PyObject_GC_TRACK?
Is there a significant difference between these functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyObject_GC_IsTracked wider than _PyObject_GC_MAY_BE_TRACKED, I'm not sure we should narrow our checks only to tuples as _PyObject_GC_MAY_BE_TRACKED do.

track_tuple = true;
}
}
if (track_tuple) {
_PyObject_GC_TRACK(v);
}
retval = v;
break;
Expand Down
Loading