Skip to content

Commit 737f690

Browse files
committed
BUG: In Python 3K PyComplex can't convert byte strings.
This may be more a workaround for a Python 3K inconsistency or bug, but it is probably better to use unicode for these sorts of calls. The fix itself is a bit of a hack, but so is the *_to_* function. It really needs a rewrite.
1 parent 95a52ab commit 737f690

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,12 +1327,15 @@ OBJECT_to_@TOTYPE@(PyObject **ip, @totype@ *op, npy_intp n,
13271327
* 0*23#
13281328
* #convstr = (Int*9, Long*2, Float*4, Complex*3, Tuple*3, Long*2)*3#
13291329
*/
1330+
1331+
#define IS_@from@
1332+
13301333
static void
13311334
@from@_to_@to@(@fromtyp@ *ip, @totyp@ *op, npy_intp n, PyArrayObject *aip,
13321335
PyArrayObject *aop)
13331336
{
13341337
npy_intp i;
1335-
PyObject *temp = NULL;
1338+
PyObject *temp = NULL, *new;
13361339
int skip = PyArray_DESCR(aip)->elsize;
13371340
int oskip = @oskip@;
13381341

@@ -1341,24 +1344,35 @@ static void
13411344
if (temp == NULL) {
13421345
return;
13431346
}
1344-
/* convert from Python object to needed one */
13451347
#if @convert@
1348+
1349+
#if defined(NPY_PY3K) && defined(IS_STRING)
1350+
/* Work around some Python 3K */
1351+
new = PyUnicode_FromEncodedObject(temp, "ascii", "strict");
1352+
Py_DECREF(temp);
1353+
temp = new;
1354+
if (temp == NULL) {
1355+
return;
1356+
}
1357+
#endif
1358+
/* convert from Python object to needed one */
13461359
{
1347-
PyObject *new, *args;
1348-
/* call out to the Python builtin given by convstr */
1349-
args = Py_BuildValue("(N)", temp);
1360+
PyObject *args;
1361+
1362+
/* call out to the Python builtin given by convstr */
1363+
args = Py_BuildValue("(N)", temp);
13501364
#if defined(NPY_PY3K)
13511365
#define PyInt_Type PyLong_Type
13521366
#endif
1353-
new = Py@convstr@_Type.tp_new(&Py@convstr@_Type, args, NULL);
1367+
new = Py@convstr@_Type.tp_new(&Py@convstr@_Type, args, NULL);
13541368
#if defined(NPY_PY3K)
13551369
#undef PyInt_Type
13561370
#endif
1357-
Py_DECREF(args);
1358-
temp = new;
1359-
if (temp == NULL) {
1360-
return;
1361-
}
1371+
Py_DECREF(args);
1372+
temp = new;
1373+
if (temp == NULL) {
1374+
return;
1375+
}
13621376
}
13631377
#endif /* @convert@ */
13641378
if (@to@_setitem(temp,(char *)op, aop)) {
@@ -1368,6 +1382,9 @@ static void
13681382
Py_DECREF(temp);
13691383
}
13701384
}
1385+
1386+
#undef IS_@from@
1387+
13711388
/**end repeat**/
13721389

13731390

0 commit comments

Comments
 (0)