File tree Expand file tree Collapse file tree 2 files changed +31
-34
lines changed Expand file tree Collapse file tree 2 files changed +31
-34
lines changed Original file line number Diff line number Diff line change @@ -7389,40 +7389,6 @@ def gen():
73897389            res  =  self .assert_python_in_subinterp (True , script )
73907390            self .assertFalse (res .err )
73917391
7392-         script  =  textwrap .dedent (""" 
7393-             import gc 
7394-             import sys 
7395-             import _testcapi 
7396- 
7397-             def emulate_interp_restart(): 
7398-                 del sys.modules['_datetime'] 
7399-                 try: 
7400-                     del sys.modules['datetime'] 
7401-                 except KeyError: 
7402-                     pass 
7403-                 gc.collect()  # unload 
7404- 
7405-             _testcapi.test_datetime_capi()  # only once 
7406-             timedelta = _testcapi.get_capi_types()['timedelta'] 
7407-             emulate_interp_restart() 
7408-             timedelta(days=1) 
7409-             emulate_interp_restart() 
7410- 
7411-             def gen(): 
7412-                 try: 
7413-                     yield 
7414-                 finally: 
7415-                     # Exceptions are ignored here 
7416-                     assert not sys.modules 
7417-                     timedelta(days=1) 
7418- 
7419-             it = gen() 
7420-             next(it) 
7421-             """ )
7422-         with  self .subTest ('MainInterpreter Restart' ):
7423-             res  =  script_helper .assert_python_ok ('-c' , script )
7424-             self .assertIn (b'ImportError: sys.meta_path is None' , res .err )
7425- 
74267392        script  =  textwrap .dedent (""" 
74277393            import sys 
74287394            timedelta = _testcapi.get_capi_types()['timedelta'] 
Original file line number Diff line number Diff line change @@ -440,6 +440,37 @@ def test_datetime_reset_strptime(self):
440440        out , err  =  self .run_embedded_interpreter ("test_repeated_init_exec" , code )
441441        self .assertEqual (out , '20000101\n '  *  INIT_LOOPS )
442442
443+     def  test_datetime_capi_at_shutdown (self ):
444+         # gh-120782: Test the case where PyDateTime_IMPORT is called only once 
445+         code  =  textwrap .dedent (""" 
446+             import sys 
447+             import _testcapi 
448+             if not _testcapi.get_capi_types(): 
449+                 _testcapi.test_datetime_capi() 
450+                 assert '_datetime' in sys.modules 
451+             else: 
452+                 assert '_datetime' not in sys.modules 
453+             timedelta = _testcapi.get_capi_types()['timedelta'] 
454+ 
455+             def gen(): 
456+                 try: 
457+                     yield 
458+                 finally: 
459+                     assert not sys.modules 
460+                     res = 1 
461+                     try: 
462+                         timedelta(days=1) 
463+                     except ImportError as e: 
464+                         res =  2 if 'sys.meta_path is None' in e.msg else 3 
465+                     assert not sys.modules 
466+                     print(res) 
467+ 
468+             it = gen() 
469+             next(it) 
470+         """ )
471+         out , err  =  self .run_embedded_interpreter ("test_repeated_init_exec" , code )
472+         self .assertEqual (out , '1\n '  +  '2\n '  *  (INIT_LOOPS  -  1 ))
473+ 
443474    def  test_static_types_inherited_slots (self ):
444475        script  =  textwrap .dedent (""" 
445476            import test.support 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments