pystrings.swg 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. The default behavior is just to return the pointer value,
  37. so, be careful.
  38. */
  39. %#if defined(SWIG_PYTHON_SAFE_CSTRINGS)
  40. if (*alloc != SWIG_OLDOBJ)
  41. %#else
  42. if (*alloc == SWIG_NEWOBJ)
  43. %#endif
  44. {
  45. *cptr = %new_copy_array(cstr, len + 1, char);
  46. *alloc = SWIG_NEWOBJ;
  47. } else {
  48. *cptr = cstr;
  49. *alloc = SWIG_OLDOBJ;
  50. }
  51. } else {
  52. %#if PY_VERSION_HEX>=0x03000000
  53. assert(0); /* Should never reach here in Python 3 */
  54. %#endif
  55. *cptr = SWIG_Python_str_AsChar(obj);
  56. }
  57. }
  58. if (psize) *psize = len + 1;
  59. %#if PY_VERSION_HEX>=0x03000000
  60. Py_XDECREF(obj);
  61. %#endif
  62. return SWIG_OK;
  63. } else {
  64. %#if defined(SWIG_PYTHON_2_UNICODE)
  65. %#if PY_VERSION_HEX<0x03000000
  66. if (PyUnicode_Check(obj)) {
  67. char *cstr; Py_ssize_t len;
  68. if (!alloc && cptr) {
  69. return SWIG_RuntimeError;
  70. }
  71. obj = PyUnicode_AsUTF8String(obj);
  72. if (PyString_AsStringAndSize(obj, &cstr, &len) != -1) {
  73. if (cptr) {
  74. if (alloc) *alloc = SWIG_NEWOBJ;
  75. *cptr = %new_copy_array(cstr, len + 1, char);
  76. }
  77. if (psize) *psize = len + 1;
  78. Py_XDECREF(obj);
  79. return SWIG_OK;
  80. } else {
  81. Py_XDECREF(obj);
  82. }
  83. }
  84. %#endif
  85. %#endif
  86. swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
  87. if (pchar_descriptor) {
  88. void* vptr = 0;
  89. if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) {
  90. if (cptr) *cptr = (char *) vptr;
  91. if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0;
  92. if (alloc) *alloc = SWIG_OLDOBJ;
  93. return SWIG_OK;
  94. }
  95. }
  96. }
  97. return SWIG_TypeError;
  98. }
  99. }
  100. %fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
  101. SWIGINTERNINLINE PyObject *
  102. SWIG_FromCharPtrAndSize(const char* carray, size_t size)
  103. {
  104. if (carray) {
  105. if (size > INT_MAX) {
  106. swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
  107. return pchar_descriptor ?
  108. SWIG_InternalNewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void();
  109. } else {
  110. %#if PY_VERSION_HEX >= 0x03000000
  111. %#if PY_VERSION_HEX >= 0x03010000
  112. return PyUnicode_DecodeUTF8(carray, %numeric_cast(size, Py_ssize_t), "surrogateescape");
  113. %#else
  114. return PyUnicode_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t));
  115. %#endif
  116. %#else
  117. return PyString_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t));
  118. %#endif
  119. }
  120. } else {
  121. return SWIG_Py_Void();
  122. }
  123. }
  124. }