Skip to content

Commit f2944a2

Browse files
committed
fix(utilities): leave GraalPy's bootstrap frames out of the importer's level, and gate its remaining limits (#1963)
- _importer_stacklevel skips <frozen _frozen_importlib> and <frozen _frozen_importlib_external>, GraalPy's names for importlib's bootstrap frames, which its warnings.warn already leaves out; the import-time ConfigWarnings named <sys>:0 there - new ImporterStacklevelTests pins both interpreters' names on CPython - test_sentinel_py_abc_1960_unit: GraalPy takes the _py_abc fallback unaided, and its _py_abc imports from _abc, so the child blocks _abc only off GraalPy - test_compat: GraalPy words a UnicodeDecodeError's reason its own way, so there only the octet and position are compared; this is what failed UnmaskedOrderTests' unittest child - test_tier_guard_xdist: skip XdistSubprocessTests and ProbeWorkerCrashTests on GraalPy, whose nested xdist sessions idled the leg's other workers Touched modules pass on CPython 3.14 and GraalPy 25.4.4; the tier guard passes. Refs #1963
1 parent 279f3f0 commit f2944a2

5 files changed

Lines changed: 73 additions & 5 deletions

File tree

‎pcapkit/utilities/warnings.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,10 @@ def _importer_stacklevel() -> 'int':
483483
level then runs past the outermost frame, and the warning is attributed to
484484
``<sys>:0``. This counts frames as :func:`warnings.warn` does, and
485485
:mod:`logging` does from Python 3.11; on earlier versions the log record is
486-
attributed to a frame inside the import.
486+
attributed to a frame inside the import. GraalPy names the bootstrap's frames
487+
``<frozen _frozen_importlib>`` and ``<frozen _frozen_importlib_external>``,
488+
which its :func:`warnings.warn` leaves out too, so those are not counted
489+
either (:issue:`1963`).
487490
488491
"""
489492
pcapkit = f'{os.path.sep}pcapkit{os.path.sep}'
@@ -493,7 +496,8 @@ def _importer_stacklevel() -> 'int':
493496
frame = frame.f_back if frame is not None else None # the caller, level 1
494497
while frame is not None:
495498
filename = frame.f_code.co_filename
496-
if not ('importlib' in filename and '_bootstrap' in filename):
499+
graalpy_bootstrap = filename.startswith('<frozen _frozen_importlib')
500+
if not ('importlib' in filename and ('_bootstrap' in filename or graalpy_bootstrap)):
497501
level += 1
498502
outermost = level
499503
if pcapkit in filename:

‎tests/corekit/test_sentinel_py_abc_1960_unit.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
:class:`PyABCFallbackTests` runs the checks in a child interpreter that really
1313
takes the fallback. It starts with ``-S``, so that no module but :mod:`io` has
1414
imported :mod:`abc` yet, drops both from :data:`sys.modules`, blocks ``_abc``
15-
and imports them again. The child reports whether :class:`abc.ABCMeta` is
15+
(except on GraalPy, which takes the fallback unaided, #1963), and imports
16+
them again. The child reports whether :class:`abc.ABCMeta` is
1617
``_py_abc``'s and whether ``_collections_abc`` was already loaded, rather than
1718
trusting the block. :class:`CModuleTests` pins that, under the C module, the
1819
names let through for ``_py_abc`` are still refused.
@@ -60,7 +61,11 @@
6061
preloaded = sorted(name for name in ('abc', 'io', '_collections_abc') if name in sys.modules)
6162
for name in ('abc', 'io'):
6263
sys.modules.pop(name, None)
63-
sys.modules['_abc'] = None
64+
# GraalPy's ``_abc`` holds only the two helpers its own ``_py_abc`` imports,
65+
# so :mod:`abc` takes the fallback there unaided, and blocking ``_abc``
66+
# would break ``_py_abc`` too (#1963).
67+
if sys.implementation.name != 'graalpy':
68+
sys.modules['_abc'] = None
6469
6570
import abc
6671
import io # noqa: F401 # rebuilt on the fallback before anything uses it

‎tests/test_tier_guard_xdist.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
#: Test tier (#1958): checks the guard in xdist workers on each leg's interpreter.
8282
__tier__ = 'ALL'
8383

84+
#: Why the nested-session probes skip GraalPy (#1963): each starts a whole
85+
#: ``pytest -n`` session of fresh GraalPy interpreters, which took minutes a test
86+
#: on the experimental leg while the other workers idled. What they pin is the
87+
#: guard's xdist handling, which no interpreter changes, so the CPython legs cover it.
88+
SKIP_NESTED_ON_GRAALPY = 'a nested pytest-xdist session is too slow on GraalPy (#1963)'
89+
8490
#: Capture name the probe module in :class:`XdistSubprocessTests` reads. Not
8591
#: tracked by git -- see :func:`tests._tiers.committed_captures` -- so it is a
8692
#: tier violation regardless of what ``make samples`` has built locally.
@@ -177,6 +183,7 @@ def _child_environ() -> 'dict[str, str]':
177183

178184
@unittest.skipUnless(importlib.util.find_spec('xdist') is not None,
179185
'pytest-xdist not installed')
186+
@unittest.skipIf(sys.implementation.name == 'graalpy', SKIP_NESTED_ON_GRAALPY)
180187
class XdistSubprocessTests(unittest.TestCase):
181188
"""End-to-end reproduction: a real violation, a real ``pytest`` subprocess.
182189
@@ -300,6 +307,7 @@ def pytest_runtest_setup(item):
300307

301308
@unittest.skipUnless(importlib.util.find_spec('xdist') is not None,
302309
'pytest-xdist not installed')
310+
@unittest.skipIf(sys.implementation.name == 'graalpy', SKIP_NESTED_ON_GRAALPY)
303311
class ProbeWorkerCrashTests(unittest.TestCase):
304312
"""A worker dying mid-probe fails that one test, not the session (#1756).
305313

‎tests/utilities/test_compat.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@
158158
#: assertion is aimed there.
159159
OWN_MODULE_PREFIX = 'pcapkit'
160160

161+
#: Whether a :exc:`UnicodeDecodeError` row's message is compared past the octet
162+
#: and position it names. GraalPy's codecs word the reason their own way --
163+
#: ``malformed input`` for the rows below -- so there the reason is left out (#1963).
164+
DECODE_REASON_COMPARED = sys.implementation.name != 'graalpy'
165+
161166
#: ``(value, encoding, errors, outcome)`` for ``compat.unquote``, each as Python
162167
#: 3.9's :func:`urllib.parse.unquote` has it: the text returned, or the exception
163168
#: type and message raised. ``None`` keywords leave the defaults in place. Before
@@ -340,7 +345,11 @@ def assert_unquotes_as_python39(self, unquote: 'Callable[..., str]') -> None:
340345
else:
341346
with self.assertRaises(outcome[0]) as raised:
342347
unquote(value, **kwargs)
343-
self.assertEqual(str(raised.exception), outcome[1])
348+
message, expected = str(raised.exception), outcome[1]
349+
if outcome[0] is UnicodeDecodeError and not DECODE_REASON_COMPARED:
350+
message = message.rpartition(': ')[0]
351+
expected = expected.rpartition(': ')[0]
352+
self.assertEqual(message, expected)
344353

345354
def test_unquote_decodes_bytes_as_python39_does(self) -> None:
346355
"""From 3.9 the name is :func:`urllib.parse.unquote` itself (#1693)."""

‎tests/utilities/test_config_unit.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,5 +590,47 @@ def test_an_error_filter_makes_the_import_raise(self) -> None:
590590
self.assertIn(f"ConfigWarning: PCAPKIT_{name}={text!r}", result.stderr)
591591

592592

593+
class ImporterStacklevelTests(unittest.TestCase):
594+
"""GitHub issue #1963: the importer's level leaves out the bootstrap, however it is named.
595+
596+
CPython names :mod:`importlib`'s bootstrap frames ``<frozen importlib._bootstrap>``
597+
and ``<frozen importlib._bootstrap_external>``, GraalPy ``<frozen _frozen_importlib>``
598+
and ``<frozen _frozen_importlib_external>``, and each one's :func:`warnings.warn`
599+
leaves its own out. Counting GraalPy's ran the level past the outermost frame,
600+
so the import-time warnings above named ``<sys>:0`` there.
601+
602+
"""
603+
604+
def level_under(self, bootstrap: str) -> int:
605+
"""The level a :mod:`pcapkit` frame gets, imported through ``bootstrap`` frames.
606+
607+
The chain is that of an import: this test, a bootstrap frame, a package
608+
body, a bootstrap frame, then the reporting module. Leaving the bootstrap
609+
out, the test is the third frame.
610+
611+
"""
612+
pcapkit_warnings = importlib.import_module('pcapkit.utilities.warnings')
613+
namespace = {'_importer_stacklevel': getattr(pcapkit_warnings, '_importer_stacklevel')}
614+
package = os.path.join(os.sep, 'site-packages', 'pcapkit', 'probe_1963.py')
615+
reporter = ('def report():\n return _importer_stacklevel()\n'
616+
'def body():\n return load_inner()\n')
617+
importer = ('def load_inner():\n return report()\n'
618+
'def load_outer():\n return body()\n')
619+
exec(compile(reporter, package, 'exec'), namespace) # nosec: B102 # pylint: disable=exec-used
620+
exec(compile(importer, bootstrap, 'exec'), namespace) # nosec: B102 # pylint: disable=exec-used
621+
return namespace['load_outer']()
622+
623+
def test_each_interpreters_bootstrap_frames_are_left_out(self) -> None:
624+
"""Both CPython's names and GraalPy's leave the test the third frame."""
625+
for bootstrap in ('<frozen importlib._bootstrap>', '<frozen importlib._bootstrap_external>',
626+
'<frozen _frozen_importlib>', '<frozen _frozen_importlib_external>'):
627+
with self.subTest(bootstrap=bootstrap):
628+
self.assertEqual(self.level_under(bootstrap), 3)
629+
630+
def test_any_other_frame_is_counted(self) -> None:
631+
"""The control: frames named otherwise are counted, so the level is one further out."""
632+
self.assertEqual(self.level_under('<string>'), 4)
633+
634+
593635
if __name__ == '__main__':
594636
unittest.main()

0 commit comments

Comments
 (0)