| 440 | } |
| 441 | |
| 442 | static int |
| 443 | string_converter_helper( |
| 444 | PyObject *object, |
| 445 | void *out, |
| 446 | int (*str_func)(char const*, Py_ssize_t, void*), |
| 447 | char const *name, |
| 448 | char const *message) |
| 449 | { |
| 450 | /* allow bytes for compatibility */ |
| 451 | PyObject *str_object = NULL; |
| 452 | if (PyBytes_Check(object)) { |
| 453 | str_object = PyUnicode_FromEncodedObject(object, NULL, NULL); |
| 454 | if (str_object == NULL) { |
| 455 | PyErr_Format(PyExc_ValueError, |
| 456 | "%s %s (got %R)", name, message, object); |
| 457 | return NPY_FAIL; |
| 458 | } |
| 459 | } |
| 460 | else if (PyUnicode_Check(object)) { |
| 461 | str_object = object; |
| 462 | Py_INCREF(str_object); |
| 463 | } |
| 464 | else { |
| 465 | PyErr_Format(PyExc_TypeError, |
| 466 | "%s must be str, not %s", name, Py_TYPE(object)->tp_name); |
| 467 | return NPY_FAIL; |
| 468 | } |
| 469 | |
| 470 | Py_ssize_t length; |
| 471 | char const *str = PyUnicode_AsUTF8AndSize(str_object, &length); |
| 472 | if (str == NULL) { |
| 473 | Py_DECREF(str_object); |
| 474 | return NPY_FAIL; |
| 475 | } |
| 476 | |
| 477 | int ret = str_func(str, length, out); |
| 478 | Py_DECREF(str_object); |
| 479 | if (ret < 0) { |
| 480 | /* str_func returns -1 without an exception if the value is wrong */ |
| 481 | if (!PyErr_Occurred()) { |
| 482 | PyErr_Format(PyExc_ValueError, |
| 483 | "%s %s (got %R)", name, message, object); |
| 484 | } |
| 485 | return NPY_FAIL; |
| 486 | } |
| 487 | return NPY_SUCCEED; |
| 488 | } |
| 489 | |
| 490 | static int byteorder_parser(char const *str, Py_ssize_t length, void *data) |
| 491 | { |
no outgoing calls
no test coverage detected