1
1
2
- from cpython cimport PyTypeObject, PyErr_BadArgument
2
+ from cpython cimport PyTypeObject
3
3
4
4
cdef extern from * :
5
5
"""
@@ -18,7 +18,6 @@ cdef extern from "Python.h":
18
18
# Note: importing extern-style allows us to declare these as nogil
19
19
# functions, whereas `from cpython cimport` does not.
20
20
bint PyUnicode_Check(object obj) nogil
21
- bint PyBytes_Check(object obj) nogil
22
21
bint PyString_Check(object obj) nogil
23
22
bint PyBool_Check(object obj) nogil
24
23
bint PyFloat_Check(object obj) nogil
@@ -30,8 +29,8 @@ cdef extern from "Python.h":
30
29
# potentially allocate memory inside in unlikely case of when underlying
31
30
# unicode object was stored as non-utf8 and utf8 wasn't requested before.
32
31
bint PyBytes_AsStringAndSize(object obj, char ** buf,
33
- Py_ssize_t* length)
34
- char * PyUnicode_AsUTF8AndSize(object obj, Py_ssize_t* length)
32
+ Py_ssize_t* length) except - 1
33
+ char * PyUnicode_AsUTF8AndSize(object obj, Py_ssize_t* length) except NULL
35
34
36
35
from numpy cimport int64_t
37
36
@@ -238,18 +237,16 @@ cdef inline bint is_nan(object val):
238
237
return (is_float_object(val) or is_complex_object(val)) and val != val
239
238
240
239
241
- cdef inline const char * get_string_data(object s, Py_ssize_t * length):
240
+ cdef inline const char * get_c_string_buf_and_size(object s,
241
+ Py_ssize_t * length):
242
242
"""
243
- Extract internal char * buffer of unicode or bytes object `s` to `buf` with
243
+ Extract internal char * buffer of unicode or bytes object `s` with
244
244
getting length of this internal buffer saved in `length`.
245
- Returns `False` if it failed to extract such buffer for whatever reason,
246
- otherwise returns `True`.
247
245
248
246
Notes
249
247
-----
250
- Python object owns memory, `buf` should not be freed.
248
+ Python object owns memory, thus returned char* must not be freed.
251
249
`length` can be NULL if getting buffer length is not needed.
252
- This function should only raise exceptions in out-of-memory cases.
253
250
254
251
Parameters
255
252
----------
@@ -265,20 +262,10 @@ cdef inline const char* get_string_data(object s, Py_ssize_t *length):
265
262
266
263
if PyUnicode_Check(s):
267
264
buf = PyUnicode_AsUTF8AndSize(s, length)
268
- if PyBytes_Check(s):
269
- if PyBytes_AsStringAndSize(s, < char ** > & buf, length) != 0 :
270
- return NULL
265
+ else :
266
+ PyBytes_AsStringAndSize(s, < char ** > & buf, length)
271
267
return buf
272
268
273
269
274
- cdef inline const char * get_string_data_checked(object s, Py_ssize_t * length):
275
- """
276
- This is a wrapper for get_string_data() that raises TypeError
277
- when supplied with neither unicode nor bytes object
278
- """
279
- cdef:
280
- const char * buf = get_string_data(s, length)
281
-
282
- if not buf:
283
- PyErr_BadArgument()
284
- return buf
270
+ cdef inline const char * get_c_string(object s):
271
+ return get_c_string_buf_and_size(s, NULL )
0 commit comments