1 /* ------------------------------------------------------------ 2 * utility methods for char strings 3 * ------------------------------------------------------------ */ 4 %fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { 5 SWIGINTERN int 6 SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) 7 { 8 %#if PY_VERSION_HEX>=0x03000000 9 if (PyUnicode_Check(obj)) 10 %#else 11 if (PyString_Check(obj)) 12 %#endif 13 { 14 char *cstr; Py_ssize_t len; 15 %#if PY_VERSION_HEX>=0x03000000 16 if (!alloc && cptr) { 17 /* We can't allow converting without allocation, since the internal 18 representation of string in Python 3 is UCS-2/UCS-4 but we require 19 a UTF-8 representation. 20 TODO(bhy) More detailed explanation */ 21 return SWIG_RuntimeError; 22 } 23 obj = PyUnicode_AsUTF8String(obj); 24 PyBytes_AsStringAndSize(obj, &cstr, &len); 25 if(alloc) *alloc = SWIG_NEWOBJ; 26 %#else 27 PyString_AsStringAndSize(obj, &cstr, &len); 28 %#endif 29 if (cptr) { 30 if (alloc) { 31 /* 32 In python the user should not be able to modify the inner 33 string representation. To warranty that, if you define 34 SWIG_PYTHON_SAFE_CSTRINGS, a new/copy of the python string 35 buffer is always returned. 36 37 The default behavior is just to return the pointer value, 38 so, be careful. 39 */ 40 %#if defined(SWIG_PYTHON_SAFE_CSTRINGS) 41 if (*alloc != SWIG_OLDOBJ) 42 %#else 43 if (*alloc == SWIG_NEWOBJ) 44 %#endif 45 { 46 *cptr = %new_copy_array(cstr, len + 1, char); 47 *alloc = SWIG_NEWOBJ; 48 } 49 else { 50 *cptr = cstr; 51 *alloc = SWIG_OLDOBJ; 52 } 53 } else { 54 %#if PY_VERSION_HEX>=0x03000000 55 assert(0); /* Should never reach here in Python 3 */ 56 %#endif 57 *cptr = SWIG_Python_str_AsChar(obj); 58 } 59 } 60 if (psize) *psize = len + 1; 61 %#if PY_VERSION_HEX>=0x03000000 62 Py_XDECREF(obj); 63 %#endif 64 return SWIG_OK; 65 } else { 66 swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); 67 if (pchar_descriptor) { 68 void* vptr = 0; 69 if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) { 70 if (cptr) *cptr = (char *) vptr; 71 if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0; 72 if (alloc) *alloc = SWIG_OLDOBJ; 73 return SWIG_OK; 74 } 75 } 76 } 77 return SWIG_TypeError; 78 } 79 } 80 81 %fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { 82 SWIGINTERNINLINE PyObject * 83 SWIG_FromCharPtrAndSize(const char* carray, size_t size) 84 { 85 if (carray) { 86 if (size > INT_MAX) { 87 swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); 88 return pchar_descriptor ? 89 SWIG_InternalNewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void(); 90 } else { 91 %#if PY_VERSION_HEX >= 0x03000000 92 return PyUnicode_FromStringAndSize(carray, %numeric_cast(size,int)); 93 %#else 94 return PyString_FromStringAndSize(carray, %numeric_cast(size,int)); 95 %#endif 96 } 97 } else { 98 return SWIG_Py_Void(); 99 } 100 } 101 } 102 103 104