@@ -63,52 +63,32 @@ common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
6363 return (PyArray_DTypeMeta * )Py_NotImplemented ;
6464}
6565
66+ // returns a new reference to the string "value" of
67+ // `scalar`. If scalar is not already a string, __str__
68+ // is called to convert it to a string. If the scalar
69+ // is the na_object for the dtype class, return
70+ // a new reference to the na_object.
71+
6672static PyObject *
6773get_value (PyObject * scalar , StringDType_type * cls )
6874{
69- PyObject * na_object = cls -> na_object ;
70- PyObject * ret = NULL ;
7175 PyTypeObject * expected_scalar_type = cls -> base .scalar_type ;
7276 PyTypeObject * scalar_type = Py_TYPE (scalar );
73- // FIXME: handle bytes too
74- if ((scalar_type == & PyUnicode_Type ) ||
75- (scalar_type == expected_scalar_type )) {
76- // attempt to decode as UTF8
77- ret = PyUnicode_AsUTF8String (scalar );
78- if (ret == NULL ) {
79- PyErr_SetString (
80- PyExc_TypeError ,
81- "Can only store UTF8 text in a StringDType array." );
77+ if (scalar == cls -> na_object ) {
78+ Py_INCREF (scalar );
79+ return scalar ;
80+ }
81+ else if (!((scalar_type == & PyUnicode_Type ) ||
82+ (scalar_type == expected_scalar_type ))) {
83+ // attempt to coerce to str
84+ scalar = PyObject_Str (scalar );
85+ if (scalar == NULL ) {
86+ // __str__ raised an exception
8287 return NULL ;
8388 }
8489 }
85- else if (scalar == na_object ) {
86- ret = scalar ;
87- Py_INCREF (ret );
88- }
89- // store np.nan as NA
90- else if (scalar_type == & PyFloat_Type ) {
91- double scalar_val = PyFloat_AsDouble (scalar );
92- if ((scalar_val == -1.0 ) && PyErr_Occurred ()) {
93- return NULL ;
94- }
95- if (npy_isnan (scalar_val )) {
96- ret = na_object ;
97- Py_INCREF (ret );
98- }
99- else {
100- PyErr_SetString (
101- PyExc_TypeError ,
102- "Can only store UTF8 text in a StringDType array." );
103- return NULL ;
104- }
105- }
106- else {
107- PyErr_SetString (PyExc_TypeError ,
108- "Can only store String text in a StringDType array." );
109- return NULL ;
110- }
111- return ret ;
90+ // attempt to decode as UTF8
91+ return PyUnicode_AsUTF8String (scalar );
11292}
11393
11494// For a given python object, this function returns a borrowed reference
0 commit comments