mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
gh-120198: Fix race condition when editing __class__ with an audit hook active (GH-120195)
(cherry picked from commit 203565b2f9
)
Co-authored-by: Ken Jin <kenjin@python.org>
This commit is contained in:
parent
f5289c450a
commit
a6c4080aaa
4 changed files with 38 additions and 2 deletions
|
@ -1,9 +1,10 @@
|
|||
"""Unit tests for zero-argument super() & related machinery."""
|
||||
|
||||
import textwrap
|
||||
import threading
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from test.support import import_helper
|
||||
from test.support import import_helper, threading_helper
|
||||
|
||||
|
||||
ADAPTIVE_WARMUP_DELAY = 2
|
||||
|
@ -505,6 +506,38 @@ class TestSuper(unittest.TestCase):
|
|||
for _ in range(ADAPTIVE_WARMUP_DELAY):
|
||||
C.some(C)
|
||||
|
||||
@threading_helper.requires_working_threading()
|
||||
def test___class___modification_multithreaded(self):
|
||||
""" Note: this test isn't actually testing anything on its own.
|
||||
It requires a sys audithook to be set to crash on older Python.
|
||||
This should be the case anyways as our test suite sets
|
||||
an audit hook.
|
||||
"""
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
class Bar:
|
||||
pass
|
||||
|
||||
thing = Foo()
|
||||
def work():
|
||||
foo = thing
|
||||
for _ in range(5000):
|
||||
foo.__class__ = Bar
|
||||
type(foo)
|
||||
foo.__class__ = Foo
|
||||
type(foo)
|
||||
|
||||
|
||||
threads = []
|
||||
for _ in range(6):
|
||||
thread = threading.Thread(target=work)
|
||||
thread.start()
|
||||
threads.append(thread)
|
||||
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue