Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-121921: Update Lib/test/crashers #126360

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
31 changes: 0 additions & 31 deletions Lib/test/crashers/mutation_inside_cyclegc.py

This file was deleted.

27 changes: 0 additions & 27 deletions Lib/test/crashers/trace_at_recursion_limit.py

This file was deleted.

39 changes: 39 additions & 0 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,45 @@ def test_ast_fini(self):
assert_python_ok("-c", code)


@requires_gil_enabled("This test hangs and occasionally segfaults on the Free-threading build")
class MutationInsideCycleGCTests(unittest.TestCase):
def setUp(self):
# This test requires the gc to be enabled.
gc.enable()
self.addCleanup(gc.disable)

def test_does_not_crash(self):
# Moved from Lib/test/crashers as it does not crash anymore.
# This test ensures that the code does not start crashing again.
# Original comment:

# The cycle GC collector can be executed when any GC-tracked object is
# allocated, e.g. during a call to PyList_New(), PyDict_New(), ...
# Moreover, it can invoke arbitrary Python code via a weakref callback.
# This means that there are many places in the source where an arbitrary
# mutation could unexpectedly occur.

# The example below shows list_slice() not expecting the call to
# PyList_New to mutate the input list. (Of course there are many
# more examples like this one.)
class A(object):
pass

def callback(x):
del lst[:]

keepalive = []

for i in range(100):
lst = [str(i)]
a = A()
a.cycle = a
keepalive.append(weakref.ref(a, callback))
del a
while lst:
keepalive.append(lst[:])


def setUpModule():
global enabled, debug
enabled = gc.isenabled()
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3146,5 +3146,37 @@ def func(arg = 1):
sys.settrace(None)


class TestTraceAtRecursionLimit(unittest.TestCase):
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())

def test_does_not_crash(self):
# Moved from Lib/test/crashers as it does not crash anymore.
# This test ensures that the code does not start crashing again.
# Original comment:

# From http://bugs.python.org/issue6717

# A misbehaving trace hook can trigger a segfault by exceeding the recursion
# limit.
def x():
pass

def g(*args):
if True: # change to True to crash interpreter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but I copied it verbatim from the original crasher. I can remove it though if you prefer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and removed the comment

try:
x()
except:
pass
return g

def f():
f()

sys.settrace(g)
with self.assertRaises(RecursionError):
f()


if __name__ == "__main__":
unittest.main()
Loading