File tree Expand file tree Collapse file tree
Misc/NEWS.d/next/Core_and_Builtins Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66import textwrap
77import time
88import unittest
9+ from test import support
910from test .support import script_helper
1011
1112
@@ -351,9 +352,9 @@ def test_reference_cycle(self):
351352 # was cleared first, the structseq instance would crash in the
352353 # destructor.
353354 script_helper .assert_python_ok ("-c" , textwrap .dedent (r"""
354- import time
355- t = time.gmtime( )
356- type(t).refcyle = t
355+ import os
356+ t = os.stat('.' )
357+ type(t).refcycle = t
357358 """ ))
358359
359360 def test_replace_gc_tracked (self ):
@@ -365,5 +366,8 @@ def test_replace_gc_tracked(self):
365366
366367 self .assertTrue (gc .is_tracked (replaced_struct ))
367368
369+ def test_struct_time_type_immutable (self ):
370+ support .check_immutable_type (self , type (time .gmtime ()))
371+
368372if __name__ == "__main__" :
369373 unittest .main ()
Original file line number Diff line number Diff line change 1+ Make the :class: `time.struct_time ` type immutable to prevent reference cycles between the type and its instances. Patch by Shamil Abdulaev.
Original file line number Diff line number Diff line change 55#include "pycore_moduleobject.h" // _PyModule_GetState()
66#include "pycore_namespace.h" // _PyNamespace_New()
77#include "pycore_runtime.h" // _Py_ID()
8+ #include "pycore_structseq.h" // _PyStructSequence_NewType()
89#include "pycore_time.h" // _PyTimeFraction
910
1011#include <time.h> // clock()
@@ -2164,7 +2165,8 @@ time_exec(PyObject *module)
21642165 }
21652166
21662167 // struct_time type
2167- state -> struct_time_type = PyStructSequence_NewType (& struct_time_type_desc );
2168+ state -> struct_time_type = _PyStructSequence_NewType (
2169+ & struct_time_type_desc , Py_TPFLAGS_IMMUTABLETYPE );
21682170 if (state -> struct_time_type == NULL ) {
21692171 return -1 ;
21702172 }
You can’t perform that action at this time.
0 commit comments