8000 Last touch-ups (test and tiny fixes) · numpy/numpy@39d2e8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 39d2e8b

Browse files
committed
Last touch-ups (test and tiny fixes)
Lets defer further touch ups to later... One more run, since the last one errored (hopefully due to an old failure not merged correctly)
1 parent a9a44e9 commit 39d2e8b

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

numpy/core/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
NPY_RELAXED_STRIDES_DEBUG = NPY_RELAXED_STRIDES_DEBUG and NPY_RELAXED_STRIDES_CHECKING
2525

2626
# Set to True to use the new casting implementation as much as implemented.
27-
# Allows running the full test suit to excercise the new machinery until
27+
# Allows running the full test suit to exercise the new machinery until
2828
# it is used as default and the old version is eventually deleted.
29-
NPY_USE_NEW_CASTINGIMPL = os.environ.get('NPY_USE_NEW_CASTINGIMPL', "0") != 0
29+
NPY_USE_NEW_CASTINGIMPL = os.environ.get('NPY_USE_NEW_CASTINGIMPL', "1") != "0"
3030

3131
# XXX: ugly, we use a class to avoid calling twice some expensive functions in
3232
# config.h/numpyconfig.h. I don't see a better way because distutils force

numpy/core/src/multiarray/convert_datatype.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,20 +420,20 @@ PyArray_CanCastSafely(int fromtype, int totype)
420420
#if NPY_USE_NEW_CASTINGIMPL
421421
PyArray_DTypeMeta *from = PyArray_DTypeFromTypeNum(fromtype);
422422
if (from == NULL) {
423-
PyErr_WriteUnraisable(Py_None);
423+
PyErr_WriteUnraisable(NULL);
424424
return 0;
425425
}
426426
PyArray_DTypeMeta *to = PyArray_DTypeFromTypeNum(totype);
427427
if (to == NULL) {
428-
PyErr_WriteUnraisable(Py_None);
428+
PyErr_WriteUnraisable(NULL);
429429
return 0;
430430
}
431431
PyObject *castingimpl = PyArray_GetCastingImpl(from, to);
432432
Py_DECREF(from);
433433
Py_DECREF(to);
434434

435435
if (castingimpl == NULL) {
436-
PyErr_WriteUnraisable(Py_None);
436+
PyErr_WriteUnraisable(NULL);
437437
return 0;
438438
}
439439
else if (castingimpl == Py_None) {
@@ -2489,9 +2489,9 @@ structured_to_nonstructured_resolve_descriptors(
24892489
if (given_descrs[1] == NULL) {
24902490
loop_descrs[1] = dtypes[1]->default_descr(dtypes[1]);
24912491
/*
2492-
* Special case strings here, this is probably unnecessary and
2493-
* should be useless (i.e. it is necessary to use empty arrays to
2494-
* trigger this path.).
2492+
* Special case strings here, it should be useless (and only actually
2493+
* work for empty arrays). Possibly this should simply raise for
2494+
* all parametric DTypes.
24952495
*/
24962496
if (dtypes[1]->type_num == NPY_STRING) {
24972497
loop_descrs[1]->elsize = given_descrs[0]->elsize;
@@ -2744,7 +2744,7 @@ object_to_any_resolve_descriptors(
27442744
* here is that e.g. "M8" input is considered to be the DType class,
27452745
* and by allowing it here, we go back to the "M8" instance.
27462746
*/
2747-
if (dtypes[1]->parametric && !dtypes[1]->legacy) {
2747+
if (dtypes[1]->parametric) {
27482748
PyErr_Format(PyExc_TypeError,
27492749
"casting from object to the parametric DType %S requires "
27502750
"the specified output dtype instance. "
@@ -2756,11 +2756,6 @@ object_to_any_resolve_descriptors(
27562756
if (loop_descrs[1] == NULL) {
27572757
return -1;
27582758
}
2759-
if (dtypes[1]->type_num == NPY_VOID) {
2760-
/* NOTE: This appears to be behaviour as of 1.19 (void is not
2761-
* adjusted) */
2762-
loop_descrs[1]->elsize = sizeof(PyObject *);
2763-
}
27642759
}
27652760
else {
27662761
Py_INCREF(given_descrs[1]);
@@ -2791,6 +2786,8 @@ PyArray_GetObjectToGenericCastingImpl()
27912786
return PyErr_NoMemory();
27922787
}
27932788

2789+
method->nin = 1;
2790+
method->nout = 1;
27942791
method->name = "object_to_any_cast";
27952792
method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI;
27962793
method->casting = NPY_UNSAFE_CASTING;
@@ -2845,6 +2842,8 @@ PyArray_GetGenericToObjectCastingImpl()
28452842
return PyErr_NoMemory();
28462843
}
28472844

2845+
method->nin = 1;
2846+
method->nout = 1;
28482847
method->name = "any_to_object_cast";
28492848
method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI;
28502849
method->casting = NPY_SAFE_CASTING;

numpy/core/tests/test_casting_unittests.py

Lines changed: 17 additions & 0 deletions
+
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,20 @@ def test_string_to_string_cancast(self, other_dt, string_char):
282282
assert safety == expected_safety
283283
elif change_length > 0:
284284
assert safety == Casting.safe
285
286+
def test_void_to_string_special_case(self):
287+
# Cover a small special case in void to string casting that could
288+
# probably just as well be turned into an error (compare
289+
# `test_object_to_parametric_internal_error` below).
290+
assert np.array([], dtype="V5").astype("S").dtype.itemsize == 5
291+
assert np.array([], dtype="V5").astype("U").dtype.itemsize == 4 * 5
292+
293+
def test_object_to_parametric_internal_error(self):
294+
# We reject casting from object to a parametric type, without
295+
# figuring out the correct instance first.
296+
object_dtype = type(np.dtype(object))
297+
other_dtype = type(np.dtype(str))
298+
cast = get_castingimpl(object_dtype, other_dtype)
299+
with pytest.raises(TypeError,
300+
match="casting from object to the parametric DType"):
301+
cast._resolve_descriptors((np.dtype("O"), None))

0 commit comments

Comments
 (0)
0