Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG,MAINT: Remove incorrect special case in string to number casts #15766

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 0 additions & 63 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1506,70 +1506,8 @@ OBJECT_to_@TOTYPE@(void *input, void *output, npy_intp n,
* #oskip = 1*18,(PyArray_DESCR(aop)->elsize)*3,1*2,
* 1*18,(PyArray_DESCR(aop)->elsize)*3,1*2,
* 1*18,(PyArray_DESCR(aop)->elsize)*3,1*2#
* #convert = 1*18, 0*3, 1*2,
* 1*18, 0*3, 1*2,
* 0*23#
* #convstr = (Long*9, Long*2, Float*4, Complex*3, Tuple*3, Long*2)*3#
*/

#if @convert@

#define IS_@from@

static void
@from@_to_@to@(void *input, void *output, npy_intp n,
void *vaip, void *aop)
{
@fromtyp@ *ip = input;
@totyp@ *op = output;
PyArrayObject *aip = vaip;

npy_intp i;
int skip = PyArray_DESCR(aip)->elsize;
int oskip = @oskip@;

for (i = 0; i < n; i++, ip+=skip, op+=oskip) {
PyObject *new;
PyObject *temp = PyArray_Scalar(ip, PyArray_DESCR(aip), (PyObject *)aip);
if (temp == NULL) {
return;
}

#if defined(IS_STRING)
/* Work around some Python 3K */
new = PyUnicode_FromEncodedObject(temp, "ascii", "strict");
Py_DECREF(temp);
temp = new;
if (temp == NULL) {
return;
}
#endif
/* convert from Python object to needed one */
{
PyObject *args;

/* call out to the Python builtin given by convstr */
args = Py_BuildValue("(N)", temp);
new = Py@convstr@_Type.tp_new(&Py@convstr@_Type, args, NULL);
Py_DECREF(args);
temp = new;
if (temp == NULL) {
return;
}
}

if (@to@_setitem(temp, op, aop)) {
Py_DECREF(temp);
return;
}
Py_DECREF(temp);
}
}

#undef IS_@from@

#else

static void
@from@_to_@to@(void *input, void *output, npy_intp n,
void *vaip, void *aop)
Expand All @@ -1595,7 +1533,6 @@ static void
}
}

#endif

/**end repeat**/

Expand Down
8 changes: 6 additions & 2 deletions numpy/core/src/multiarray/convert_datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,17 @@ PyArray_AdaptFlexibleDType(PyObject *data_obj, PyArray_Descr *data_dtype,
case NPY_HALF:
case NPY_FLOAT:
case NPY_DOUBLE:
case NPY_LONGDOUBLE:
size = 32;
break;
case NPY_LONGDOUBLE:
size = 48;
break;
case NPY_CFLOAT:
case NPY_CDOUBLE:
size = 2 * 32;
break;
case NPY_CLONGDOUBLE:
size = 64;
size = 2 * 48;
break;
case NPY_OBJECT:
size = 64;
Expand Down
34 changes: 24 additions & 10 deletions numpy/core/tests/test_longdouble.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,36 @@ def test_repr_roundtrip():
assert_equal(np.longdouble(repr(o)), o, "repr was %s" % repr(o))


def test_unicode():
np.longdouble(u"1.2")
@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
def test_repr_roundtrip_bytes():
o = 1 + LD_INFO.eps
assert_equal(np.longdouble(repr(o).encode("ascii")), o)


def test_string():
np.longdouble("1.2")
@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
@pytest.mark.parametrize("strtype", (np.str_, np.bytes_, str, bytes))
def test_array_and_stringlike_roundtrip(strtype):
"""
Test that string representations of long-double roundtrip both
for array casting and scalar coercion, see also gh-15608.
"""
o = 1 + LD_INFO.eps

if strtype in (np.bytes_, bytes):
o_str = strtype(repr(o).encode("ascii"))
else:
o_str = strtype(repr(o))

def test_bytes():
np.longdouble(b"1.2")
# Test that `o` is correctly coerced from the string-like
assert o == np.longdouble(o_str)

# Test that arrays also roundtrip correctly:
o_strarr = np.asarray([o] * 3, dtype=strtype)
assert (o == o_strarr.astype(np.longdouble)).all()

@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
def test_repr_roundtrip_bytes():
o = 1 + LD_INFO.eps
assert_equal(np.longdouble(repr(o).encode("ascii")), o)
# And array coercion and casting to string give the same as scalar repr:
assert (o_strarr == o_str).all()
assert (np.asarray([o] * 3).astype(strtype) == o_str).all()


def test_bogus_string():
Expand Down