Repository navigation
Remove leftover Python 2 code and the mklrand_py_helper.h shim - #192
Conversation
There was a problem hiding this comment.
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_functiondoes nothing on Python 3.mkl_random/mklrand.pyx:2664-2670: thebytes()docstring saysout : strand shows a Python 2 string repr; it should bebyteswith ab'...'example.mkl_random/mklrand.pyx:3581-3583: thetomaxintexample usessys.maxint, which doesn't exist in Python 3 (sys.maxsize).
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+.
`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
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:2096and:3148:<type 'float'>/<type 'int'>in the examplesmklrand.pyx:3103: "the C long type used by Python 2"mklrand.pyx:2555:'np.int'as therandintdefault (removed in NumPy 1.24)src/mkl_distributions.cpp:1510,1513:sys.maxintin 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".
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
left a comment
There was a problem hiding this comment.
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.
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
left a comment
There was a problem hiding this comment.
Thank you @vlad-perevezentsev
The project requires Python 3.10+, so the
mklrand_py_helper.hhelpers only wrapped calls that Cython already declares incpython.bytes; the header is gone andmklrand.pyxcallPyBytes_*directly, which also drops its reliance on include order.The remaining Python 2 leftovers go too: the no-op
__future__import, thestrreturn type in thebytesdocstring, andsys.maxintin thetomaxintexample.PyBytes_Checkis used instead ofisinstance(x, bytes)because the module-levelbytes = _rand.bytesalias shadows the builtin here. No behavior change.