summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2022-06-23 07:05:56 -0700
committerGitHub <noreply@github.com>2022-06-23 07:05:56 -0700
commit4b49a8a41fd1ac3f9eb4959d99c078f58f24f410 (patch)
tree3410e1ea7d48b8d2c77a3ee801f13c583398e944
parentdf7ccc4034e04fd3fadaa33710c727589dc0d938 (diff)
parentf9cc4bf70c5c6f26ad421962bf5c608558b1035c (diff)
downloadnumpy-4b49a8a41fd1ac3f9eb4959d99c078f58f24f410.tar.gz
Merge pull request #21797 from karlotness/enh-np-require
MAINT: Reduce object construction in np.require
-rw-r--r--numpy/core/_asarray.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/numpy/core/_asarray.py b/numpy/core/_asarray.py
index 89d422e99..cbaab8c3f 100644
--- a/numpy/core/_asarray.py
+++ b/numpy/core/_asarray.py
@@ -14,6 +14,15 @@ from .multiarray import array, asanyarray
__all__ = ["require"]
+POSSIBLE_FLAGS = {
+ 'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C',
+ 'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F',
+ 'A': 'A', 'ALIGNED': 'A',
+ 'W': 'W', 'WRITEABLE': 'W',
+ 'O': 'O', 'OWNDATA': 'O',
+ 'E': 'E', 'ENSUREARRAY': 'E'
+}
+
def _require_dispatcher(a, dtype=None, requirements=None, *, like=None):
return (like,)
@@ -36,7 +45,7 @@ def require(a, dtype=None, requirements=None, *, like=None):
The required data-type. If None preserve the current dtype. If your
application requires the data to be in native byteorder, include
a byteorder specification as a part of the dtype specification.
- requirements : str or list of str
+ requirements : str or sequence of str
The requirements list can be any of the following
* 'F_CONTIGUOUS' ('F') - ensure a Fortran-contiguous array
@@ -97,16 +106,10 @@ def require(a, dtype=None, requirements=None, *, like=None):
like=like,
)
- possible_flags = {'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C',
- 'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F',
- 'A': 'A', 'ALIGNED': 'A',
- 'W': 'W', 'WRITEABLE': 'W',
- 'O': 'O', 'OWNDATA': 'O',
- 'E': 'E', 'ENSUREARRAY': 'E'}
if not requirements:
return asanyarray(a, dtype=dtype)
- else:
- requirements = {possible_flags[x.upper()] for x in requirements}
+
+ requirements = {POSSIBLE_FLAGS[x.upper()] for x in requirements}
if 'E' in requirements:
requirements.remove('E')
@@ -128,8 +131,7 @@ def require(a, dtype=None, requirements=None, *, like=None):
for prop in requirements:
if not arr.flags[prop]:
- arr = arr.copy(order)
- break
+ return arr.copy(order)
return arr