Skip to content

Commit 2f25051

Browse files
authored
Merge pull request #374 from bashtage/restore-mode
Restore mode
2 parents 434e3da + a4f2509 commit 2f25051

38 files changed

+405
-168
lines changed

.pep8speaks.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
scanner:
22
diff_only: False
3-
linter: flake8 # Other option is flake8
3+
linter: pycodestyle # Other option is flake8
44

55
pycodestyle: # Same as scanner.linter value. Other option is flake8
66
max-line-length: 99 # Default is 79 in PEP 8
77
ignore: # Errors and warnings to ignore
88
- E203 # Whitespace before ':'
99
- W503 # Line break occurred before a binary operator (W503)
10+
- E301
11+
- E302
12+
- E305
13+
- E501
14+
- E701
1015

1116
no_blank_comment: False # If True, no comment is made on PR without any errors.

randomgen/_deprecated_value.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class _DeprecatedValueType:
2+
"""Special keyword value for deprecated arguments..
3+
4+
The instance of this class may be used as the default value assigned to a
5+
keyword if the parameter is deprecated.
6+
"""
7+
8+
__instance = None
9+
10+
def __new__(cls):
11+
# ensure that only one instance exists
12+
if not cls.__instance:
13+
cls.__instance = super().__new__(cls)
14+
return cls.__instance
15+
16+
def __repr__(self):
17+
return "<deprecated>"
18+
19+
20+
_DeprecatedValue = _DeprecatedValueType()
21+
22+
__all__ = ["_DeprecatedValue"]

randomgen/_seed_sequence.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ except ImportError:
4444
randbits = SystemRandom().getrandbits
4545

4646
import numpy as np
47-
cimport numpy as np
4847

48+
cimport numpy as np
4949
from libc.stdint cimport uint32_t
5050

5151
__all__ = ["SeedSequence", "SeedlessSeedSequence", "ISeedSequence",

randomgen/aes.pyx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33

44
from randomgen.common cimport *
5-
5+
from randomgen._deprecated_value import _DeprecatedValue
66

77
__all__ = ["AESCounter"]
88

@@ -17,7 +17,7 @@ cdef double aes_double(void* st) noexcept nogil:
1717

1818
cdef class AESCounter(BitGenerator):
1919
"""
20-
AESCounter(seed=None, *, counter=None, key=None)
20+
AESCounter(seed=None, *, counter=None, key=None, mode=<deprecated>)
2121
2222
Container for the AES Counter pseudo-random number generator.
2323
@@ -38,6 +38,12 @@ cdef class AESCounter(BitGenerator):
3838
another RNG before use, the value in key is directly set. Can be either
3939
a Python int in [0, 2**128) or a 2-element uint64 array.
4040
key and seed cannot both be used.
41+
mode : {None, "sequence"}
42+
Deprecated parameter. Do not use.
43+
44+
.. deprecated: 2.0.0
45+
46+
Starting in version 2, only seed sequences are supported.
4147
4248
Attributes
4349
----------
@@ -123,8 +129,8 @@ cdef class AESCounter(BitGenerator):
123129
.. [1] Advanced Encryption Standard. (n.d.). In Wikipedia. Retrieved
124130
June 1, 2019, from https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
125131
"""
126-
def __init__(self, seed=None, *, counter=None, key=None):
127-
BitGenerator.__init__(self, seed)
132+
def __init__(self, seed=None, *, counter=None, key=None, mode=_DeprecatedValue):
133+
BitGenerator.__init__(self, seed, mode=mode)
128134
# Calloc since ctr needs to be 0
129135
self.rng_state = <aesctr_state_t *>PyArray_calloc_aligned(
130136
sizeof(aesctr_state_t), 1

randomgen/chacha.pyx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import numpy as np
33

44
from randomgen.common cimport *
55

6+
from randomgen._deprecated_value import _DeprecatedValue
7+
68
__all__ = ["ChaCha"]
79

810
cdef uint64_t chacha_uint64(void* st) noexcept nogil:
@@ -16,7 +18,7 @@ cdef double chacha_double(void* st) noexcept nogil:
1618

1719
cdef class ChaCha(BitGenerator):
1820
"""
19-
ChaCha(seed=None, *, counter=None, key=None, rounds=20)
21+
ChaCha(seed=None, *, counter=None, key=None, rounds=20, mode=<deprecated>)
2022
2123
Container for the ChaCha family of Counter pseudo-random number generators
2224
@@ -43,6 +45,12 @@ cdef class ChaCha(BitGenerator):
4345
The standard number of rounds in 20. Smaller values, usually 8 or
4446
more, can be used to reduce security properties of the random stream
4547
while improving performance.
48+
mode : {None, "sequence"}
49+
Deprecated parameter. Do not use.
50+
51+
.. deprecated: 2.0.0
52+
53+
Starting in version 2, only seed sequences are supported.
4654
4755
Attributes
4856
----------
@@ -127,8 +135,8 @@ cdef class ChaCha(BitGenerator):
127135
.. [1] Bernstein, D. J.. ChaCha, a variant of Salsa20.
128136
http://cr.yp.to/papers.html#chacha. 2008.01.28.
129137
"""
130-
def __init__(self, seed=None, *, counter=None, key=None, rounds=20):
131-
BitGenerator.__init__(self, seed)
138+
def __init__(self, seed=None, *, counter=None, key=None, rounds=20, mode=_DeprecatedValue):
139+
BitGenerator.__init__(self, seed, mode=mode)
132140
self.rng_state = <chacha_state_t *>PyArray_malloc_aligned(
133141
sizeof(chacha_state_t)
134142
)

randomgen/common.pyx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
#!python
2-
import sys
32
from collections import namedtuple
3+
import sys
4+
import warnings
5+
46
try:
57
from threading import Lock
68
except ImportError:
79
from dummy_threading import Lock
810

911
import numpy as np
10-
from numpy.random.bit_generator cimport BitGenerator as _BitGenerator
11-
from cpython cimport PyFloat_AsDouble
12+
1213
cimport numpy as np
14+
from cpython cimport PyFloat_AsDouble
15+
from numpy.random.bit_generator cimport BitGenerator as _BitGenerator
1316

14-
from randomgen.common cimport *
1517
from randomgen cimport api
18+
from randomgen.common cimport *
19+
20+
from randomgen._deprecated_value import _DeprecatedValue
1621
from randomgen.seed_sequence import ISeedSequence
1722

1823
ISEED_SEQUENCES = (ISeedSequence,)
@@ -43,14 +48,23 @@ cdef class BitGenerator(_BitGenerator):
4348
"""
4449
Abstract class for all BitGenerators
4550
"""
46-
def __init__(self, seed, mode="sequence"):
47-
if mode is not None and (not isinstance(mode, str) or mode.lower() not in self._supported_modes()):
51+
def __init__(self, seed, *, numpy_seed=False, mode=_DeprecatedValue):
52+
if mode is not _DeprecatedValue:
53+
msg = ("mode is deprecated and will be removed in a future version. "
54+
"Seeding defaults to a numpy.random.SeedSequence instance.")
55+
if "numpy" in self._supported_modes():
56+
msg += " Use numpy_seed=True to enforce numpy-compatible seeding."
57+
warnings.warn(msg, FutureWarning)
58+
if mode is not _DeprecatedValue and (
59+
not isinstance(mode, str) or mode.lower() not in self._supported_modes()
60+
):
4861
if len(self._supported_modes()) == 1:
4962
msg = f"mode must be {self._supported_modes()[0]}"
5063
else:
5164
modes = ", ".join(f"\"{mode}\"" for mode in self._supported_modes())
5265
raise ValueError(f"mode must be one of: {modes}.")
53-
self.mode = mode.lower()
66+
mode = mode.lower() if isinstance(mode, str) else mode
67+
self.mode = "numpy" if (numpy_seed or mode == "numpy") else "sequence"
5468
super().__init__(seed)
5569

5670
def _supported_modes(self):

randomgen/dsfmt.pyx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import operator
33

44
import numpy as np
55
cimport numpy as np
6-
6+
from randomgen._deprecated_value import _DeprecatedValue
77
from randomgen.common cimport *
88

99
__all__ = ["DSFMT"]
@@ -27,7 +27,7 @@ cdef uint64_t dsfmt_raw(void *st) noexcept nogil:
2727

2828
cdef class DSFMT(BitGenerator):
2929
"""
30-
DSFMT(seed=None)
30+
DSFMT(seed=None, *, mode=<deprecated>)
3131
3232
Container for the SIMD-based Mersenne Twister pseudo RNG.
3333
@@ -40,6 +40,12 @@ cdef class DSFMT(BitGenerator):
4040
``None`` (the default). If `seed` is ``None``, then 764 32-bit unsigned
4141
integers are read from ``/dev/urandom`` (or the Windows analog) if
4242
available. If unavailable, a hash of the time and process ID is used.
43+
mode : {None, "sequence"}
44+
Deprecated parameter. Do not use.
45+
46+
.. deprecated: 2.0.0
47+
48+
Starting in version 2, only seed sequences are supported.
4349
4450
Attributes
4551
----------
@@ -110,8 +116,8 @@ cdef class DSFMT(BitGenerator):
110116
Sequences and Their Applications - SETA, 290--298, 2008.
111117
"""
112118

113-
def __init__(self, seed=None):
114-
BitGenerator.__init__(self, seed)
119+
def __init__(self, seed=None, *, mode=_DeprecatedValue):
120+
BitGenerator.__init__(self, seed, mode=mode)
115121
self.rng_state.state = <dsfmt_t *>PyArray_malloc_aligned(sizeof(dsfmt_t))
116122
self.rng_state.buffered_uniforms = <double *>PyArray_calloc_aligned(
117123
DSFMT_N64, sizeof(double)

randomgen/examples/cython/extending.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#cython: language_level=3
2+
from cpython.pycapsule cimport PyCapsule_GetPointer, PyCapsule_IsValid
23
from libc.stdint cimport uint32_t
3-
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
44

55
import numpy as np
6-
cimport numpy as np
6+
77
cimport cython
8+
cimport numpy as np
89

910
from randomgen.common cimport bitgen_t
11+
1012
from randomgen.xoroshiro128 import Xoroshiro128
1113

1214
np.import_array()

randomgen/examples/cython/extending_distributions.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#cython: language_level=3
22
import numpy as np
3-
cimport numpy as np
3+
44
cimport cython
5-
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
5+
cimport numpy as np
6+
from cpython.pycapsule cimport PyCapsule_GetPointer, PyCapsule_IsValid
7+
68
from randomgen.common cimport *
79
from randomgen.distributions cimport random_gauss_zig
10+
811
from randomgen.xoroshiro128 import Xoroshiro128
912

1013

randomgen/examples/cython/low_level.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#cython: language_level=3, boundscheck=False, wraparound=False
2+
from cpython.pycapsule cimport PyCapsule_GetPointer, PyCapsule_IsValid
23
from libc.stdint cimport uint32_t
3-
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
44

55
import numpy as np
6-
cimport numpy as np
6+
77
cimport cython
8+
cimport numpy as np
89

910
from randomgen.common cimport bitgen_t, uint64_to_double
1011
from randomgen.xoshiro256 cimport Xoshiro256, xoshiro256_next64
12+
1113
from randomgen.xoshiro256 import Xoshiro256
1214

1315
np.import_array()

0 commit comments

Comments
 (0)