Skip to content

Commit 011078a

Browse files
author
Anselm Kruis
committed
Stackless issue python#81: adapt the interpreter shutdown test to newer versions of Python 3.
The 2.7 detector hook (cleanup of codecs search path) does not work with Python 3.3 and later. Adapt the test cases to use a different hook (builtins._).
1 parent 04dd9c1 commit 011078a

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Stackless/test/test_interpreter_shutdown.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import _thread as thread
44
import stackless
5-
import codecs
5+
import builtins
66
import os
77
import time
88
import collections
@@ -63,13 +63,14 @@ def __init__(self, out, checks, tasklets):
6363
self.checks = checks
6464
self.tasklets = tasklets
6565

66-
# register the detector in the codecs search path
67-
# it will be cleared after clearing the thread states
68-
# this way the detector runs very late
69-
def dummy_codecs_search_function(name):
70-
return None
71-
codecs.register(dummy_codecs_search_function)
72-
dummy_codecs_search_function.x = self
66+
# In Py_Finalize() the PyImport_Cleanup() runs shortly after
67+
# slp_kill_tasks_with_stacks(NULL).
68+
# As very first action of PyImport_Cleanup() the Python
69+
# interpreter sets builtins._ to None.
70+
# Therefore we can use this assignment to trigger the execution
71+
# of this detector. At this point the interpreter is still almost fully
72+
# intact.
73+
builtins._ = self
7374

7475
def __del__(self):
7576
if self.debug:

Stackless/unittests/test_shutdown.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from __future__ import print_function, absolute_import, division
3636
import sys
3737
import stackless
38-
import codecs
38+
import builtins
3939
import os
4040
import time
4141
import collections
@@ -220,13 +220,14 @@ def __init__(self, out, checks, tasklets):
220220
self.checks = checks
221221
self.tasklets = tasklets
222222

223-
# register the detector in the codecs search path
224-
# it will be cleared after clearing the thread states
225-
# this way the detector runs very late
226-
def dummy_codecs_search_function(name):
227-
return None
228-
codecs.register(dummy_codecs_search_function)
229-
dummy_codecs_search_function.x = self
223+
# In Py_Finalize() the PyImport_Cleanup() runs shortly after
224+
# slp_kill_tasks_with_stacks(NULL).
225+
# As very first action of PyImport_Cleanup() the Python
226+
# interpreter sets builtins._ to None.
227+
# Therefore we can use this assignment to trigger the execution
228+
# of this detector. At this point the interpreter is still almost fully
229+
# intact.
230+
builtins._ = self
230231

231232
def __del__(self):
232233
if self.debug:
@@ -405,7 +406,7 @@ def interpreter_shutdown_test():
405406
test = Test()
406407
detector = Detector(test.out, test.checks, test.tasklets)
407408
assert sys.getrefcount(detector) - sys.getrefcount(object()) == 2
408-
detector = None # the last ref is now in the codecs search path
409+
detector = None # the last ref is now in bultins._
409410
thread.start_new_thread(test.other_thread_main, ())
410411
ready.acquire() # Be sure the other thread is ready.
411412
# print("at end")

0 commit comments

Comments
 (0)