Skip to content

Remove leftover Python 2 code and the mklrand_py_helper.h shim - #192

Merged
vlad-perevezentsev merged 14 commits into
masterfrom
remove-pystring-fallbacks
Oct 16, 2026
Merged

vlad-perevezentsev merged 14 commits into
masterfrom
remove-pystring-fallbacks

Conversation

@vlad-perevezentsev

@vlad-perevezentsev vlad-perevezentsev commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

The project requires Python 3.10+, so the mklrand_py_helper.h helpers only wrapped calls that Cython already declares in cpython.bytes; the header is gone and mklrand.pyx call PyBytes_* directly, which also drops its reliance on include order.
The remaining Python 2 leftovers go too: the no-op __future__ import, the str return type in the bytes docstring, and sys.maxint in the tomaxint example.

PyBytes_Check is used instead of isinstance(x, bytes) because the module-level bytes = _rand.bytes alias shadows the builtin here. No behavior change.

@antonwolfy antonwolfy left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me with small nits below.

Possible follow-ups outside this PR, for the same Python 2 cleanup:

  • mkl_random/__init__.py:26: from __future__ import absolute_import, division, print_function does nothing on Python 3.
  • mkl_random/mklrand.pyx:2664-2670: the bytes() docstring says out : str and shows a Python 2 string repr; it should be bytes with a b'...' example.
  • mkl_random/mklrand.pyx:3581-3583: the tomaxint example uses sys.maxint, which doesn't exist in Python 3 (sys.maxsize).

Comment thread mkl_random/src/mklrand_py_helper.h Outdated
Comment thread mkl_random/src/mklrand_py_helper.h Outdated
Comment thread CHANGELOG.md Outdated
With the Python 2 branches gone, the three helpers only wrapped calls
that Cython already declares in cpython.bytes, so the header had no
reason to exist. Dropping it also removes its hidden dependency on
include order: it used npy_intp while including only <Python.h>.

PyBytes_Check stands in for isinstance(x, bytes) because the
module-level `bytes = _rand.bytes` alias shadows the builtin here.
absolute_import, division and print_function are unconditional on
Python 3, and the project requires Python 3.10+.
@vlad-perevezentsev vlad-perevezentsev changed the title Remove dead PyString fallbacks from mklrand_py_helper.h Remove leftover Python 2 code and the mklrand_py_helper.h shim Oct 15, 2026
`bytes` documented its return as `str` and showed a repr without the `b`
prefix; `tomaxint` compared against `sys.maxint`. The comparison now uses
`np.iinfo("long").max`, matching the range in the summary line, and the
bool array drops the `dtype=bool` suffix NumPy 2 no longer prints.
Per review, "Removed" is for functionality users lose. Log the cleanup
under "Changed" and the docstring corrections under "Fixed".
The return type was corrected to `bytes`, but the line below it still
read "String of length `length`.", matching NumPy's own wording.

@jharlow-intel jharlow-intel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nice cleanup! The swap to the cpython.bytes cimports is one-to-one, nothing still references the removed header, and the existing get_state/set_state and bytes() tests cover the touched paths. Thanks for picking up Anton's follow-ups too.

@antonwolfy antonwolfy left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few Python 2 leftovers remain outside the diff, so either the description's "the remaining Python 2 leftovers go too" needs narrowing, or these could go in as well:

  • mklrand.pyx:2096 and :3148: <type 'float'> / <type 'int'> in the examples
  • mklrand.pyx:3103: "the C long type used by Python 2"
  • mklrand.pyx:2555: 'np.int' as the randint default (removed in NumPy 1.24)
  • src/mkl_distributions.cpp:1510,1513: sys.maxint in the Zipf comment

Out of scope, but worth an issue: np.long at mklrand.pyx:2829, :2844 and :7101 doesn't exist in NumPy 1.26, which numpy >=1.26.4 still allows, so choice(..., p=...) and permutation(int) raise AttributeError there. CI starts at NumPy 2.2, so it isn't caught.

Nit: "mklrand.pyx call" in the description should be "calls".

Comment thread CHANGELOG.md Outdated
Comment thread mkl_random/mklrand.pyx Outdated
Comment thread mkl_random/mklrand.pyx
Comment thread mkl_random/mklrand.pyx
The `random_sample` and `random_integers` examples printed `<type '...'>`
reprs, `random_integers` described `np.int` as Python 2's "short" integer
type, `randint` documented its default dtype as `'np.int'` (removed in
NumPy 1.24), and the `zipf` comment described the C long bounds as
`sys.maxint`. Wording follows NumPy's current docstrings.

Also marks the `tomaxint` example output as `# random`, which the removed
`sys.maxint` lines used to imply.
If irk_get_stream_size returns 0 or a negative MKL error code on the
first pass, the loop takes the save branch with bytesPtr never assigned,
which g++ reports under -Wmaybe-uninitialized. NULL also makes that path
well defined: vslSaveStreamM rejects a NULL buffer instead of writing.
The single entry read as if both docstrings had both defects; only
`bytes` documented a `str` return and only `tomaxint` used `sys.maxint`.
Same -Wmaybe-uninitialized case as the buffer pointer: `err` is assigned
inside the lock and read after it, so g++ cannot prove the assignment.
Zero is the "no error" value the checks below compare against.

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. Two follow-ups on the new random_integers docstring text below. I also reopened the set_state thread, since the length check is still missing.

The np.long uses at mklrand.pyx:2829, :2844 and :7102 (broken on NumPy 1.26) aren't tracked anywhere yet either; worth an issue together with the set_state one.

Comment thread mkl_random/mklrand.pyx Outdated
Comment thread mkl_random/mklrand.pyx Outdated
Per review: on NumPy 2 `numpy.int_` is `intp`, which is 64-bit on Windows,
while this returns a 32-bit C long there, so the wording borrowed from
NumPy is wrong. The example type was wrong too, in the same way the
original `<type 'int'>` was: `random_integers` delegates to
`randint(..., dtype="l")`, which returns a NumPy scalar, not a Python int.

Also fixes the neighbouring example, which passed a float `size` that
raises TypeError now, and marks the random output as NumPy does.

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlad-perevezentsev
vlad-perevezentsev merged commit 5eaa069 into master Oct 16, 2026
172 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants