| 1 |
"""Concurrent use of the public API from many threads. |
| 2 |
|
| 3 |
The library keeps no mutable module-level state (the lookup tables are |
| 4 |
read-only and ``uts46data`` is imported lazily under the import lock), so |
| 5 |
calling it from several threads at once must give exactly the results of |
| 6 |
calling it serially. This holds the library to that on every build, and |
| 7 |
on free-threaded CPython additionally checks that the GIL really is off |
| 8 |
when the environment asked for it, so that a dependency or extension |
| 9 |
silently re-enabling it would be noticed. |
| 10 |
""" |
| 11 |
|
| 12 |
import codecs |
| 13 |
import os |
| 14 |
import sys |
| 15 |
import unittest |
| 16 |
from concurrent.futures import ThreadPoolExecutor |
| 17 |
|
| 18 |
import idna |
| 19 |
import idna.codec # registers the idna2008 codec |
| 20 |
|
| 21 |
# A mix of paths: plain ASCII, U-labels, UTS #46 mapping, A-label decoding, |
| 22 |
# the codec, and inputs that must fail with a specific error code. |
| 23 |
_INPUTS = [ |
| 24 |
"example.com", |
| 25 |
"пример.рф", |
| 26 |
"παράδειγμα.δοκιμή", |
| 27 |
"उदाहरण.परीक्षा", |
| 28 |
"例え.テスト", |
| 29 |
"Bücher.Example", |
| 30 |
"xn--e1afmkfd.xn--p1ai", |
| 31 |
"xn--zckzah.xn--zckzah", |
| 32 |
"xn---bbk.example", |
| 33 |
"-bad.example", |
| 34 |
"a\u200cb.example", |
| 35 |
"a" * 64 + ".example", |
| 36 |
] |
| 37 |
|
| 38 |
|
| 39 |
def _outcome(fn, *args, **kwargs): |
| 40 |
try: |
| 41 |
return fn(*args, **kwargs) |
| 42 |
except idna.IDNAError as err: |
| 43 |
return ("error", err.code) |
| 44 |
|
| 45 |
|
| 46 |
def _work(rounds: int) -> list: |
| 47 |
"""Exercise the API ``rounds`` times and return every outcome, in order.""" |
| 48 |
results = [] |
| 49 |
for _ in range(rounds): |
| 50 |
for s in _INPUTS: |
| 51 |
results.append(_outcome(idna.encode, s)) |
| 52 |
results.append(_outcome(idna.encode, s, uts46=True)) |
| 53 |
results.append(_outcome(idna.decode, s)) |
| 54 |
results.append(_outcome(idna.decode, s, display=True)) |
| 55 |
results.append(_outcome(idna.uts46_remap, s)) |
| 56 |
results.append(_outcome(codecs.encode, s, "idna2008")) |
| 57 |
return results |
| 58 |
|
| 59 |
|
| 60 |
class ConcurrencyTests(unittest.TestCase): |
| 61 |
def test_threads_agree_with_serial_execution(self): |
| 62 |
expected = _work(1) |
| 63 |
with ThreadPoolExecutor(max_workers=8) as pool: |
| 64 |
for outcome in pool.map(lambda _: _work(20), range(16)): |
| 65 |
self.assertEqual(outcome, expected * 20) |
| 66 |
|
| 67 |
@unittest.skipUnless( |
| 68 |
os.environ.get("PYTHON_GIL") == "0" and hasattr(sys, "_is_gil_enabled"), |
| 69 |
"only meaningful when PYTHON_GIL=0 is set on a free-threaded build", |
| 70 |
) |
| 71 |
def test_gil_stays_disabled_when_requested(self): |
| 72 |
# getattr rather than direct access: the attribute only exists from |
| 73 |
# 3.13, and the type checkers evaluate against requires-python. |
| 74 |
is_gil_enabled = getattr(sys, "_is_gil_enabled", None) |
| 75 |
assert is_gil_enabled is not None |
| 76 |
self.assertFalse(is_gil_enabled()) |
| 77 |
|
| 78 |
|
| 79 |
if __name__ == "__main__": |
| 80 |
unittest.main() |