inline PyBytesHolder Utf8FromPyUnicode(PyObject* pyStr) { #if PY_VERSION_HEX >= 0x03000000 PyObjectHolder py_bytes(PyUnicode_AsUTF8String(pyStr)); #else PyObjectHolder py_unicode(PyObject_Unicode(pyStr)); PyObjectHolder py_bytes(PyUnicode_AsUTF8String(py_unicode)); #endif PYTHON_CHECK(py_bytes, "PyUnicode_AsUTF8String failed!"); return py_bytes; }
//------------------------------------------------------------------------- static bool py_do_get_bytes( PyObject **out_py_bytes, PyObject **out_py_mask, ea_t ea, unsigned int size, int gmb_flags) { PYW_GIL_CHECK_LOCKED_SCOPE(); bool has_mask = out_py_mask != NULL; do { if ( size <= 0 ) break; // Allocate memory via Python newref_t py_bytes(PyString_FromStringAndSize(NULL, Py_ssize_t(size))); if ( py_bytes == NULL ) break; bytevec_t mask; if ( has_mask ) mask.resize((size + 7) / 8, 0); // Read bytes int code = get_bytes(PyString_AsString(py_bytes.o), size, ea, gmb_flags, has_mask ? mask.begin() : NULL); if ( code < 0 ) break; // note: specify size, as '0' bytes would otherwise cut the mask short if ( has_mask ) { newref_t py_mask(PyString_FromStringAndSize( (const char *) mask.begin(), mask.size())); if ( py_mask == NULL ) break; py_mask.incref(); *out_py_mask = py_mask.o; } py_bytes.incref(); *out_py_bytes = py_bytes.o; return true; } while ( false ); return false; }
inline bool PyObjectToStringNoExcept(PyObject* obj, std::string& result) { if (!obj) return false; #if PY_VERSION_HEX >= 0x03000000 PyObjectHolder obj_str(PyObject_Str(obj)); #else PyObjectHolder obj_str(PyObject_Unicode(obj)); #endif if (!obj_str) return false; PyObjectHolder py_bytes(PyUnicode_AsUTF8String(obj_str)); if (!py_bytes) return false; const char* content = PyBytes_AsString(py_bytes); if (!content) return false; result = content; return true; }