FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-155728: Untrack tuples while unmarshalling by sergey-miryanov · Pull Request #156423 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
51 changes: 51 additions & 0 deletions Lib/test/test_marshal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from test.support import threading_helper
from test.support.script_helper import assert_python_ok
import array
import gc
import io
import marshal
import struct
Expand Down Expand Up @@ -1340,6 +1341,56 @@ def work():
self.assertEqual(len(results), 80)
self.assertEqual(set(results), {expected})

@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", []
t = (True, False, ())

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, t))

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


if __name__ == "__main__":
unittest.main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,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 @@ -1670,16 +1671,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)) {
Comment thread
sergey-miryanov marked this conversation as resolved.
track_tuple = true;
}
}
if (track_tuple) {
_PyObject_GC_TRACK(v);
}
retval = r_ref_insert(v, idx, flag, p);
break;
Expand Down
Loading

Back | FazBrowse Home | New Git URL