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
10 changes: 7 additions & 3 deletions Lib/test/test_structseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import textwrap
import time
import unittest
from test import support
from test.support import script_helper


Expand Down Expand Up @@ -351,9 +352,9 @@ def test_reference_cycle(self):
# was cleared first, the structseq instance would crash in the
# destructor.
script_helper.assert_python_ok("-c", textwrap.dedent(r"""
import time
t = time.gmtime()
type(t).refcyle = t
import os
t = os.stat('.')
type(t).refcycle = t
"""))

def test_replace_gc_tracked(self):
Expand All @@ -365,5 +366,8 @@ def test_replace_gc_tracked(self):

self.assertTrue(gc.is_tracked(replaced_struct))

def test_struct_time_type_immutable(self):
support.check_immutable_type(self, type(time.gmtime()))

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the :class:`time.struct_time` type immutable to prevent reference cycles between the type and its instances. Patch by Shamil Abdulaev.
4 changes: 3 additions & 1 deletion Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_namespace.h" // _PyNamespace_New()
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_structseq.h" // _PyStructSequence_NewType()
#include "pycore_time.h" // _PyTimeFraction

#include <time.h> // clock()
Expand Down Expand Up @@ -2110,7 +2111,8 @@ time_exec(PyObject *module)
}

// struct_time type
state->struct_time_type = PyStructSequence_NewType(&struct_time_type_desc);
state->struct_time_type = _PyStructSequence_NewType(
&struct_time_type_desc, Py_TPFLAGS_IMMUTABLETYPE);
if (state->struct_time_type == NULL) {
return -1;
}
Expand Down
Loading