8000 BUG: Fix unicode(unicode_array_0d) on python 2.7 by eric-wieser · Pull Request #9201 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix unicode(unicode_array_0d) on python 2.7 #9201

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 1 commit into from
Nov 13, 2017
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
6 changes: 6 additions & 0 deletions numpy/core/src/multiarray/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,12 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
(PyCFunction)array_ufunc,
METH_VARARGS | METH_KEYWORDS, NULL},

#ifndef 10000 NPY_PY3K
{"__unicode__",
(PyCFunction)array_unicode,
METH_NOARGS, NULL},
#endif

/* for the sys module */
{"__sizeof__",
(PyCFunction) array_sizeof,
Expand Down
32 changes: 32 additions & 0 deletions numpy/core/src/multiarray/strfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,35 @@ array_format(PyArrayObject *self, PyObject *args)
);
}
}

#ifndef NPY_PY3K

NPY_NO_EXPORT PyObject *
array_unicode(PyArrayObject *self)
{
PyObject *uni;

if (PyArray_NDIM(self) == 0) {
PyObject *item = PyArray_ToScalar(PyArray_DATA(self), self);
if (item == NULL){
return NULL;
}

/* defer to invoking `unicode` on the scalar */
uni = PyObject_CallFunctionObjArgs(
(PyObject *)&PyUnicode_Type, item, NULL);
Py_DECREF(item);
}
else {
/* Do what unicode(self) would normally do */
PyObject *str = PyObject_Str((PyObject *)self);
if (str == NULL){
return NULL;
}
uni = PyUnicode_FromObject(str);
Py_DECREF(str);
}
return uni;
}

#endif
5 changes: 5 additions & 0 deletions numpy/core/src/multiarray/strfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ array_str(PyArrayObject *self);
NPY_NO_EXPORT PyObject *
array_format(PyArrayObject *self, PyObject *args);

#ifndef NPY_PY3K
NPY_NO_EXPORT PyObject *
array_unicode(PyArrayObject *self);
#endif

#endif
4 changes: 3 additions & 1 deletion numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ def test_formatter_reset(self):
assert_equal(repr(x), "array([0., 1., 2.])")

def test_0d_arrays(self):
unicode = type(u'')
assert_equal(unicode(np.array(u'café', np.unicode_)), u'café')

if sys.version_info[0] >= 3:
assert_equal(str(np.array('café', np.unicode_)), 'café')
assert_equal(repr(np.array('café', np.unicode_)),
"array('café',\n dtype='<U4')")
else:
Expand Down
8 changes: 8 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3867,6 +3867,10 @@ def _insert_masked_print(self):
def __str__(self):
return str(self._insert_masked_print())

if sys.version_info.major < 3:
def __unicode__(self):
return unicode(self._insert_masked_print())

def __repr__(self):
"""
Literal string representation.
Expand Down Expand Up @@ -6238,6 +6242,10 @@ def __array_wrap__(self, obj, context=None):
def __str__(self):
return str(masked_print_option._display)

if sys.version_info.major < 3:
def __unicode__(self):
return unicode(masked_print_option._display)

def __repr__(self):
if self is MaskedConstant.__singleton:
return 'masked'
Expand Down
10 changes: 10 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,16 @@ def test_str_repr(self):
' fill_value = 999999)\n'
)

def test_0d_unicode(self):
u = u'caf\xe9'
utype = type(u)

arr_nomask = np.ma.array(u)
arr_masked = np.ma.array(u, mask=True)

assert_equal(utype(arr_nomask), u)
assert_equal(utype(arr_masked), u'--')

def test_pickling(self):
# Tests pickling
for dtype in (int, float, str, object):
Expand Down
0