8000 BUG: add nb_* conversions to fix issue #9972 by mattip · Pull Request #10040 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: add nb_* conversions to fix issue #9972 #10040

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions numpy/core/include/numpy/npy_3kcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static NPY_INLINE int PyInt_Check(PyObject *op) {
}

#define PyInt_FromLong PyLong_FromLong
#define PyInt_FromString PyLong_FromString
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AS_LONG PyLong_AsLong
#define PyInt_AsSsize_t PyLong_AsSsize_t
Expand Down
10 changes: 10 additions & 0 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4602,6 +4602,16 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict))

DUAL_INHERIT2(String, String, Character);
DUAL_INHERIT2(Unicode, Unicode, Character);
/*
since PyStringArrType_Type, PyUnicodeArrType_Type have a tp_as_number, all
th nb_* functions will be filled in by looking up the mro. They will be
erroneously filled by PyGenericArrType_Type's values. Zero out the ones
we care about
*/
PyStringArrType_Type.tp_as_number->nb_add = NULL;
PyStringArrType_Type.tp_as_number->nb_multiply = NULL;
PyUnicodeArrType_Type.tp_as_number->nb_add = NULL;
PyUnicodeArrType_Type.tp_as_number->nb_multiply = NULL;

SINGLE_INHERIT(Void, Flexible);

Expand Down
72 changes: 72 additions & 0 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -4000,8 +4000,70 @@ initialize_casting_tables(void)

static PyNumberMethods longdoubletype_as_number;
static PyNumberMethods clongdoubletype_as_number;
static PyNumberMethods stringtype_as_number;
static PyNumberMethods unicodetype_as_number;
static void init_basetypes(void);

/* Copied from CPython abstract.c int_from_string*/
static PyObject*
int_from_string(PyObject * o) {
const char *s;
Py_ssize_t len;
char *end;
PyObject *x;
if (!PyString_Check(o)) {
PyErr_SetString(PyExc_ValueError,
"non-string in int_from_string");
return NULL;
}
s = PyString_AS_STRING(o);
len = PyString_GET_SIZE(o);
x = PyInt_FromString((char*)s, &end, 10);
if (x == NULL)
return NULL;
if (end != s + len) {
PyErr_SetString(PyExc_ValueError,
"null byte in argument for int()");
Py_DECREF(x);
return NULL;
}
return x;
};

static PyObject*
int_from_unicode(PyObject *o) {
PyObject *x;
Py_UNICODE *s;
Py_ssize_t length;
char *buffer;
if (!PyUnicode_Check(o)) {
PyErr_SetString(PyExc_ValueError,
"non-unicode in int_from_unicode");
return NULL;
}
s = PyUnicode_AS_UNICODE(o);
length = PyUnicode_GET_SIZE(o);
buffer = (char *)PyMem_MALLOC(length+1);
if (buffer == NULL)
return PyErr_NoMemory();

if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
PyMem_FREE(buffer);
return NULL;
}
x = PyInt_FromString(buffer, NULL, 10);
PyMem_FREE(buffer);
return x;
};

static PyObject*
float_from_string(PyObject* x) {
#if PY_MAJOR_VERSION >= 3
return PyFloat_FromString(x);
#else
return PyFloat_FromString(x, NULL);
#endif
}

NPY_NO_EXPORT void
initialize_numeric_types(void)
Expand Down Expand Up @@ -4056,9 +4118,19 @@ initialize_numeric_types(void)

PyStringArrType_Type.tp_repr = stringtype_repr;
PyStringArrType_Type.tp_str = stringtype_str;
if (PyString_Type.tp_as_number)
stringtype_as_number = *PyString_Type.tp_as_number;
stringtype_as_number.nb_int = int_from_string;
stringtype_as_number.nb_float = float_from_string;
Copy link
Member
@eric-wieser eric-wieser Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you just use PyString_Type.nb_float here? - hmm, I see it doesn't have one :(

>>> "".__float__
AttributeError: 'str' object has no attribute '__float__'
>>> "".__int__
AttributeError: 'str' object has no attribute '__int__'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just use PyNumber_Int(PyObject_Str(self)) here instead (swapping str for unicode, and int for float as needed)`

PyStringArrType_Type.tp_as_number = &stringtype_as_number;

PyUnicodeArrType_Type.tp_repr = unicodetype_repr;
PyUnicodeArrType_Type.tp_str = unicodetype_str;
if (PyUnicode_Type.tp_as_number)
unicodetype_as_number = *PyUnicode_Type.tp_as_number;
unicodetype_as_number.nb_int = int_from_unicode;
unicodetype_as_number.nb_float = float_from_string;
PyUnicodeArrType_Type.tp_as_number = &unicodetype_as_number;

PyVoidArrType_Type.tp_methods = voidtype_methods;
PyVoidArrType_Type.tp_getset = voidtype_getsets;
Expand Down
0