summaryrefslogtreecommitdiff
path: root/numpy/core/src
diff options
context:
space:
mode:
authorAllan Haldane <allan.haldane@gmail.com>2018-03-17 19:41:15 -0400
committerAllan Haldane <allan.haldane@gmail.com>2018-03-19 14:09:15 -0400
commit0d6296a660b2b05a8b364fbdf7c5c290de957b8c (patch)
tree382a30bc19f725cd1bd94bbd329a93299e4351a0 /numpy/core/src
parent5324067c702a41e901354a01a5f0d05ff49b6cb4 (diff)
downloadnumpy-0d6296a660b2b05a8b364fbdf7c5c290de957b8c.tar.gz
BUG: floating types should override tp_print
Fixes #10753
Diffstat (limited to 'numpy/core/src')
-rw-r--r--numpy/core/src/multiarray/scalartypes.c.src37
1 files changed, 37 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src
index ee83206de..3e17ec040 100644
--- a/numpy/core/src/multiarray/scalartypes.c.src
+++ b/numpy/core/src/multiarray/scalartypes.c.src
@@ -4169,6 +4169,37 @@ initialize_casting_tables(void)
}
}
+#ifndef NPY_PY3K
+/*
+ * In python2, the `float` and `complex` types still implement the obsolete
+ * "tp_print" method, which uses CPython's float-printing routines to print the
+ * float. Numpy's float_/cfloat inherit from Python float/complex, but
+ * override its tp_repr and tp_str methods. In order to avoid an inconsistency
+ * with the inherited tp_print, we need to override it too.
+ *
+ * In python3 the tp_print method is reserved/unused.
+ */
+static int
+doubletype_print(PyObject *o, FILE *fp, int flags)
+{
+ int ret;
+ PyObject *to_print;
+ if (flags & Py_PRINT_RAW) {
+ to_print = PyObject_Str(o);
+ }
+ else {
+ to_print = PyObject_Repr(o);
+ }
+
+ if (to_print == NULL) {
+ return -1;
+ }
+
+ ret = PyObject_Print(to_print, fp, flags);
+ Py_DECREF(to_print);
+ return ret;
+}
+#endif
static PyNumberMethods longdoubletype_as_number;
static PyNumberMethods clongdoubletype_as_number;
@@ -4221,6 +4252,12 @@ initialize_numeric_types(void)
/**end repeat**/
+#ifndef NPY_PY3K
+ PyDoubleArrType_Type.tp_print = &doubletype_print;
+ PyCDoubleArrType_Type.tp_print = &doubletype_print;
+#endif
+
+
PyBoolArrType_Type.tp_as_number->nb_index = (unaryfunc)bool_index;
PyStringArrType_Type.tp_alloc = NULL;