Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ENH: Create helper for conversion to arrays#24214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ngoldbaum merged 4 commits intonumpy:mainfromseberg:creation-helper
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
MAINT: Allow PyArray_FromAny_int to signal if the input was a scalar
  • Loading branch information
@seberg
seberg committedJan 20, 2024
commit27fa00b6b9bc0ac0ef2999e4c944d8430715cda6
23 changes: 18 additions & 5 deletionsnumpy/_core/src/multiarray/ctors.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1530,8 +1530,10 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
return NULL;
}

PyObject* ret = PyArray_FromAny_int(op, dt_info.descr, dt_info.dtype,
min_depth, max_depth, flags, context);
int was_scalar;
PyObject* ret = PyArray_FromAny_int(
op, dt_info.descr, dt_info.dtype,
min_depth, max_depth, flags, context, &was_scalar);

Py_XDECREF(dt_info.descr);
Py_XDECREF(dt_info.dtype);
Expand All@@ -1541,12 +1543,19 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
/*
* Internal version of PyArray_FromAny that accepts a dtypemeta. Borrows
* references to the descriptor and dtype.
*
* The `was_scalar` output returns 1 when the object was a "scalar".
* This means it was:
* - Recognized as a scalar by a/the dtype. This can be DType specific,
* for example a tuple may be a scalar, but only for structured dtypes.
* - Anything not recognized as an instance of a DType's scalar type but also not
* convertable to an array. (no __array__ protocol, etc.)
* these must map to `dtype=object` (if a dtype wasn't specified).
*/

NPY_NO_EXPORT PyObject *
PyArray_FromAny_int(PyObject *op, PyArray_Descr *in_descr,
PyArray_DTypeMeta *in_DType, int min_depth, int max_depth,
int flags, PyObject *context)
int flags, PyObject *context, int *was_scalar)
{
/*
* This is the main code to make a NumPy array from a Python
Expand All@@ -1571,6 +1580,9 @@ PyArray_FromAny_int(PyObject *op, PyArray_Descr *in_descr,
return NULL;
}

/* If the cache is NULL, then the object is considered a scalar */
*was_scalar = (cache == NULL);

if (dtype == NULL) {
dtype = PyArray_DescrFromType(NPY_DEFAULT_TYPE);
}
Expand DownExpand Up@@ -1827,8 +1839,9 @@ PyArray_CheckFromAny_int(PyObject *op, PyArray_Descr *in_descr,
}
}

int was_scalar;
obj = PyArray_FromAny_int(op, in_descr, in_DType, min_depth,
max_depth, requires, context);
max_depth, requires, context, &was_scalar);
if (obj == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletionnumpy/_core/src/multiarray/ctors.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -56,7 +56,7 @@ _array_from_array_like(PyObject *op,
NPY_NO_EXPORT PyObject *
PyArray_FromAny_int(PyObject *op, PyArray_Descr *in_descr,
PyArray_DTypeMeta *in_DType, int min_depth, int max_depth,
int flags, PyObject *context);
int flags, PyObject *context, int *was_scalar);

NPY_NO_EXPORT PyObject *
PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp