31 if ( PyBytes_Check( pyobject ) )
35 PyBufferProcs* bufprocs = Py_TYPE(pyobject)->tp_as_buffer;
37 PySequenceMethods* seqmeths = Py_TYPE(pyobject)->tp_as_sequence;
38 if ( seqmeths != 0 && bufprocs != 0
39 && bufprocs->bf_getbuffer != 0
44 (*(bufprocs->bf_getbuffer))( pyobject, &bufinfo, PyBUF_WRITABLE );
45 buf = (
char*)bufinfo.buf;
46 Py_ssize_t buflen = bufinfo.len;
48 if ( buf && check == kTRUE ) {
50 PyObject* pytc = PyObject_GetAttrString( pyobject,
"typecode");
52 const char* s = PyUnicode_AsUTF8AndSize( pytc,
nullptr );
53 if ( s && s[0] !=
tc )
56 }
else if ( seqmeths->sq_length &&
57 (
int)(buflen / (*(seqmeths->sq_length))( pyobject )) == size ) {
60 }
else if ( buflen == size ) {
67 PyObject* pytype = 0, *pyvalue = 0, *pytrace = 0;
68 PyErr_Fetch( &pytype, &pyvalue, &pytrace );
69 const char* s = PyUnicode_AsUTF8AndSize( pyvalue,
nullptr );
71 PyObject* pyvalue2 = PyUnicode_FromFormat(
72 (
char*)
"%s and given element size (%ld) do not match needed (%d)",
74 seqmeths->sq_length ? (Long_t)(buflen / (*(seqmeths->sq_length))( pyobject )) : (Long_t)buflen,
77 PyErr_Restore( pytype, pyvalue2, pytrace );