mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-30814: Fixed a race condition when import a submodule from a package. (#2580)
This commit is contained in:
parent
1ccbad9c95
commit
b4baacee1a
7 changed files with 352 additions and 337 deletions
|
@ -10,6 +10,8 @@ import py_compile
|
|||
import random
|
||||
import stat
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
import textwrap
|
||||
|
@ -380,6 +382,32 @@ class ImportTests(unittest.TestCase):
|
|||
self.assertEqual(str(cm.exception),
|
||||
"cannot import name 'does_not_exist' from '<unknown module name>' (unknown location)")
|
||||
|
||||
def test_concurrency(self):
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))
|
||||
try:
|
||||
exc = None
|
||||
def run():
|
||||
event.wait()
|
||||
try:
|
||||
import package
|
||||
except BaseException as e:
|
||||
nonlocal exc
|
||||
exc = e
|
||||
|
||||
for i in range(10):
|
||||
event = threading.Event()
|
||||
threads = [threading.Thread(target=run) for x in range(2)]
|
||||
try:
|
||||
with test.support.start_threads(threads, event.set):
|
||||
time.sleep(0)
|
||||
finally:
|
||||
sys.modules.pop('package', None)
|
||||
sys.modules.pop('package.submodule', None)
|
||||
if exc is not None:
|
||||
raise exc
|
||||
finally:
|
||||
del sys.path[0]
|
||||
|
||||
|
||||
@skip_if_dont_write_bytecode
|
||||
class FilePermissionTests(unittest.TestCase):
|
||||
|
|
2
Lib/test/test_import/data/package/__init__.py
Normal file
2
Lib/test/test_import/data/package/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
import package.submodule
|
||||
package.submodule
|
0
Lib/test/test_import/data/package/submodule.py
Normal file
0
Lib/test/test_import/data/package/submodule.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue