Skip to content

Commit 0a94476

Browse files
committed
fix(core): don't crash import cuda.core on a non-integer opt-out value
`cuda/core/__init__.py` reads `CUDA_CORE_DONT_FIX_TAB_COMPLETION` with a bare `int(os.environ.get(..., "0"))` at import time. `int()` raises for any value that is not a base-10 integer, and `os.environ.get` returns the empty string (not the `"0"` default) when the variable is set but empty, so: export CUDA_CORE_DONT_FIX_TAB_COMPLETION= python -c "import cuda.core" ValueError: invalid literal for int() with base 10: '' Clearing a variable with `export VAR=` is the usual way to neutralize it in a shell profile, a Dockerfile, or a CI job spec, and `=true` / `=yes` are the obvious guesses for a boolean-looking opt-out. All of them make the whole package unimportable, which is a hard failure for a knob whose only purpose is to skip an optional `rlcompleter` patch. Parse the value leniently instead. Integer values keep their existing meaning (non-zero opts out, so `0` and `00` still install the patch), while a non-integer, non-empty value is honored as an opt-out rather than being silently ignored. Unset and empty/whitespace-only both mean "not set". Also document the variable, which was not listed on the environment variables page, and drop the stale "only installed in interactive mode" comment: the interactivity gate was intentionally removed in #2055 ("Always install the monkeypatch"), so the patch has been unconditional since then. The new parametrized test asserts the resulting behavior for eight values; four of them ("", " ", "true", "yes") fail on main because the subprocess exits non-zero with the ValueError above.
1 parent 3bd069a commit 0a94476

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎cuda_core/cuda/core/__init__.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ def _patch_rlcompleter_for_cython_properties() -> None:
3636
# which rlcompleter's narrow isinstance(..., property) check misses; the
3737
# fallback getattr() then invokes the descriptor and any non-AttributeError
3838
# it raises kills tab completion. Extend that isinstance check to also
39-
# match getset_descriptor / member_descriptor. Only installed in
40-
# interactive mode so library users running scripts see no global
41-
# rlcompleter side effect.
39+
# match getset_descriptor / member_descriptor. Installed unconditionally
40+
# (the patch is scoped to the rlcompleter module, so non-interactive users
41+
# only pay for the import).
4242
import os
4343

44-
if int(os.environ.get("CUDA_CORE_DONT_FIX_TAB_COMPLETION", "0")):
44+
raw_opt_out = os.environ.get("CUDA_CORE_DONT_FIX_TAB_COMPLETION", "").strip()
45+
try:
46+
opt_out = int(raw_opt_out) != 0
47+
except ValueError:
48+
opt_out = raw_opt_out != ""
49+
if opt_out:
4550
# Explicit opt-out for users who don't want the global rlcompleter
4651
# side effect, even in an interactive session.
4752
return

‎cuda_core/docs/source/environment_variables.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ Runtime Environment Variables
2424
warnings about CUDA major version mismatches between ``cuda-bindings`` and
2525
the installed driver. This warning occurs when ``cuda-bindings`` was built
2626
for a newer CUDA major version than the installed driver supports.
27+
28+
- ``CUDA_CORE_DONT_FIX_TAB_COMPLETION`` : When set to 1, ``import cuda.core``
29+
does not patch the standard library's :mod:`rlcompleter` module. The patch
30+
works around a CPython bug (fixed in Python 3.13.13, 3.14.6 and 3.15) that
31+
makes tab completion fail on Cython properties, and it changes global
32+
interpreter state; set this variable to opt out. Unset, empty, and ``0``
33+
leave the patch enabled; any other value disables it.

‎cuda_core/tests/test_rlcompleter_patch.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,61 @@ def test_opt_out_env_var_disables_patch_even_when_interactive():
107107
result = _run_probe(pythoninspect=True, opt_out=True)
108108
assert result.returncode == 0, f"stderr: {result.stderr}\nstdout: {result.stdout}"
109109
assert "crash: RuntimeError" in result.stdout, result.stdout
110+
111+
112+
# Imports cuda.core and reports whether the rlcompleter patch was installed.
113+
# No CUDA device is needed: the opt-out is evaluated at import time. The
114+
# stdlib rlcompleter module has no `property` attribute of its own, so its
115+
# presence is exactly the signal that the patch ran.
116+
_OPT_OUT_PROBE_SCRIPT = textwrap.dedent("""
117+
import rlcompleter
118+
119+
import cuda.core # noqa: F401
120+
121+
print(f"patched: {hasattr(rlcompleter, 'property')}")
122+
""")
123+
124+
125+
@pytest.mark.agent_authored(model="claude-opus-5")
126+
@pytest.mark.parametrize(
127+
("value", "expect_patched"),
128+
[
129+
# Empty / whitespace-only means "not set": `export VAR=` is the usual
130+
# way to neutralize a variable in a shell profile or container spec.
131+
("", True),
132+
(" ", True),
133+
# Integer values keep their long-standing meaning.
134+
("0", True),
135+
("00", True),
136+
("1", False),
137+
("2", False),
138+
# Non-integer values are honored as an opt-out.
139+
("true", False),
140+
("yes", False),
141+
],
142+
)
143+
def test_opt_out_env_var_values(value, expect_patched):
144+
"""`CUDA_CORE_DONT_FIX_TAB_COMPLETION` must never break `import cuda.core`.
145+
146+
The opt-out used to be read with a bare `int(...)` at import time, so any
147+
value that is not a base-10 integer -- including the empty string -- raised
148+
`ValueError: invalid literal for int() with base 10: ''` out of
149+
`cuda/core/__init__.py` and made the package unimportable.
150+
"""
151+
env = os.environ.copy()
152+
env.pop("PYTHONPATH", None)
153+
env["CUDA_CORE_DONT_FIX_TAB_COMPLETION"] = value
154+
# Run from a neutral directory so a source tree next to the test run
155+
# cannot shadow the installed package (see _run_probe).
156+
with tempfile.TemporaryDirectory() as tmpdir:
157+
result = subprocess.run( # noqa: S603
158+
[sys.executable, "-c", _OPT_OUT_PROBE_SCRIPT],
159+
capture_output=True,
160+
text=True,
161+
env=env,
162+
check=False,
163+
stdin=subprocess.DEVNULL,
164+
cwd=tmpdir,
165+
)
166+
assert result.returncode == 0, f"stderr: {result.stderr}\nstdout: {result.stdout}"
167+
assert result.stdout.strip() == f"patched: {expect_patched}", result.stdout

0 commit comments

Comments
 (0)