static int Rlite_init(hirlite_RliteObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "path", "encoding", NULL }; PyObject *encodingObj = NULL; char *path = ":memory:"; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO", kwlist, &path, &encodingObj)) return -1; if (encodingObj) { PyObject *encbytes; char *encstr; Py_ssize_t enclen; if (PyUnicode_Check(encodingObj)) encbytes = PyUnicode_AsASCIIString(encodingObj); else encbytes = PyObject_Bytes(encodingObj); if (encbytes == NULL) return -1; enclen = PyBytes_Size(encbytes); encstr = PyBytes_AsString(encbytes); self->encoding = (char*)malloc(enclen+1); memcpy(self->encoding, encstr, enclen); self->encoding[enclen] = '\0'; Py_DECREF(encbytes); } self->context = rliteConnect(path, 0); return 0; }
/*NUMPY_API * Convert any Python object, *obj*, to an NPY_CASTING enum. */ NPY_NO_EXPORT int PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting) { char *str = NULL; Py_ssize_t length = 0; if (PyUnicode_Check(obj)) { PyObject *str_obj; int ret; str_obj = PyUnicode_AsASCIIString(obj); if (str_obj == NULL) { return 0; } ret = PyArray_CastingConverter(str_obj, casting); Py_DECREF(str_obj); return ret; } if (PyBytes_AsStringAndSize(obj, &str, &length) == -1) { return 0; } if (length >= 2) switch (str[2]) { case 0: if (strcmp(str, "no") == 0) { *casting = NPY_NO_CASTING; return 1; } break; case 'u': if (strcmp(str, "equiv") == 0) { *casting = NPY_EQUIV_CASTING; return 1; } break; case 'f': if (strcmp(str, "safe") == 0) { *casting = NPY_SAFE_CASTING; return 1; } break; case 'm': if (strcmp(str, "same_kind") == 0) { *casting = NPY_SAME_KIND_CASTING; return 1; } break; case 's': if (strcmp(str, "unsafe") == 0) { *casting = NPY_UNSAFE_CASTING; return 1; } break; } PyErr_SetString(PyExc_ValueError, "casting must be one of 'no', 'equiv', 'safe', " "'same_kind', or 'unsafe'"); return 0; }
bool ChPythonEngine::GetString(const char* variable, std::string& return_val) { PyObject * module = PyImport_AddModule("__main__"); // borrowed reference assert(module); // __main__ should always exist PyObject * dictionary = PyModule_GetDict(module); // borrowed reference assert(dictionary); // __main__ should have a dictionary PyObject * result = PyDict_GetItemString(dictionary, variable); // borrowed reference if (!result) return false; if (PyBytes_Check(result)) { char* ret_string = PyBytes_AsString(result); return_val = ret_string; return true; } if (PyUnicode_Check(result)) { PyObject* mascii = PyUnicode_AsASCIIString(result); char* ret_string = PyBytes_AsString(mascii); return_val = ret_string; Py_DECREF(mascii); return true; } return false; }
/*NUMPY_API * Convert object to searchsorted side */ NPY_NO_EXPORT int PyArray_SearchsideConverter(PyObject *obj, void *addr) { NPY_SEARCHSIDE *side = (NPY_SEARCHSIDE *)addr; char *str; PyObject *tmp = NULL; if (PyUnicode_Check(obj)) { obj = tmp = PyUnicode_AsASCIIString(obj); } str = PyBytes_AsString(obj); if (!str || strlen(str) < 1) { PyErr_SetString(PyExc_ValueError, "expected nonempty string for keyword 'side'"); Py_XDECREF(tmp); return PY_FAIL; } if (str[0] == 'l' || str[0] == 'L') { *side = NPY_SEARCHLEFT; } else if (str[0] == 'r' || str[0] == 'R') { *side = NPY_SEARCHRIGHT; } else { PyErr_Format(PyExc_ValueError, "'%s' is an invalid value for keyword 'side'", str); Py_XDECREF(tmp); return PY_FAIL; } Py_XDECREF(tmp); return PY_SUCCEED; }
int PyUnicode_CopyPreferredEncoding(char *buffer, Py_ssize_t buffer_size) { PyObject *u = NULL; PyObject *b = NULL; Py_ssize_t len; #if (PY_MAJOR_VERSION >= 3) u = PyUnicode_GetPreferredEncoding(); if (u == NULL) goto error; b = PyUnicode_AsASCIIString(u); #else b = PyUnicode_GetPreferredEncoding(); #endif if (b == NULL) goto error; len = PyBytes_GET_SIZE(b); if (len >= buffer_size) len = buffer_size - 1; strncpy(buffer, PyBytes_AS_STRING(b), len); buffer[len] = '\0'; Py_XDECREF(u); Py_DECREF(b); return 1; error: Py_XDECREF(u); Py_XDECREF(b); return 0; }
int convert_offset_position(PyObject *obj, void *offsetp) { e_offset_position *offset = (e_offset_position *)offsetp; PyObject *offsetstrobj; char *offsetstr; *offset = OFFSET_POSITION_FIGURE; if (obj == NULL || obj == Py_None) { return 1; } offsetstrobj = PyUnicode_AsASCIIString(obj); if (offsetstrobj == NULL) { return 0; } offsetstr = PyBytes_AsString(offsetstrobj); if (offsetstr == NULL) { Py_DECREF(offsetstrobj); return 0; } if (strncmp(offsetstr, "data", 5) == 0) { *offset = OFFSET_POSITION_DATA; } Py_DECREF(offsetstrobj); return 1; }
/*NUMPY_API * Convert object to select kind */ NPY_NO_EXPORT int PyArray_SelectkindConverter(PyObject *obj, NPY_SELECTKIND *selectkind) { char *str; PyObject *tmp = NULL; if (PyUnicode_Check(obj)) { obj = tmp = PyUnicode_AsASCIIString(obj); } *selectkind = NPY_INTROSELECT; str = PyBytes_AsString(obj); if (!str) { Py_XDECREF(tmp); return NPY_FAIL; } if (strlen(str) < 1) { PyErr_SetString(PyExc_ValueError, "Select kind string must be at least length 1"); Py_XDECREF(tmp); return NPY_FAIL; } if (strcmp(str, "introselect") == 0) { *selectkind = NPY_INTROSELECT; } else { PyErr_Format(PyExc_ValueError, "%s is an unrecognized kind of select", str); Py_XDECREF(tmp); return NPY_FAIL; } Py_XDECREF(tmp); return NPY_SUCCEED; }
void py_new_msg (struct tgl_message *M) { if (!python_loaded) { return; } PyObject *msg; PyObject *arglist, *result; if(_py_new_msg == NULL) { logprintf("Callback not set for on_new_msg"); return; } msg = get_message (M); arglist = Py_BuildValue("(O)", msg); result = PyEval_CallObject(_py_new_msg, arglist); Py_DECREF(arglist); if(result == NULL) PyErr_Print(); else if(PyUnicode_Check(result)) logprintf ("python: %s\n", PyBytes_AsString(PyUnicode_AsASCIIString(result))); Py_XDECREF(result); }
static PyObject* convert( PyObject *py_result, std::string &result ) { PyObject *ascii_string = PyUnicode_AsASCIIString( py_result ); result = PyBytes_AsString( ascii_string ); Py_XDECREF( ascii_string ); return py_result; }
static PyObject* cms_profile_getattro(CmsProfileObject* self, PyObject* name) { PyObject* name_bytes; char* name_string; if (!PyUnicode_Check(name)) return NULL; if (!(name_bytes = PyUnicode_AsASCIIString(name))) return NULL; if (!(name_string = PyBytes_AsString(name_bytes))) return NULL; if (!strcmp(name_string, "product_name")) return PyUnicode_FromFormat("%s", cmsTakeProductName(self->profile)); if (!strcmp(name_string, "product_desc")) return PyUnicode_FromFormat("%s", cmsTakeProductDesc(self->profile)); if (!strcmp(name_string, "product_info")) return PyUnicode_FromFormat("%s", cmsTakeProductInfo(self->profile)); if (!strcmp(name_string, "rendering_intent")) return PyLong_FromLong(cmsTakeRenderingIntent(self->profile)); if (!strcmp(name_string, "pcs")) return PyUnicode_FromFormat("%s", findICmode(cmsGetPCS(self->profile))); if (!strcmp(name_string, "color_space")) return PyUnicode_FromFormat("%s", findICmode(cmsGetColorSpace(self->profile))); /* FIXME: add more properties (creation_datetime etc) */ return PyObject_GenericGetAttr((PyObject*)self, name); }
void py_chat_update (struct tgl_chat *C, unsigned flags) { if (!python_loaded) { return; } PyObject *peer, *types; PyObject *arglist, *result; if(_py_chat_update == NULL) { logprintf("Callback not set for on_chat_update"); return; } peer = get_peer (C->id, (void *)C); types = get_update_types (flags); arglist = Py_BuildValue("(OO)", peer, types); result = PyEval_CallObject(_py_chat_update, arglist); Py_DECREF(arglist); if(result == NULL) PyErr_Print(); else if(PyUnicode_Check(result)) logprintf ("python: %s\n", PyBytes_AsString(PyUnicode_AsASCIIString(result))); Py_XDECREF(result); }
static char *GetString( PyObject *value ) { /* * Name: * GetString * Purpose: * Get a pointer to a null terminated string from a PyObject. * It should be freed by the caller. * Stolen from pyast */ char *result = NULL; if( value && value != Py_None ) { #ifdef USE_PY3K PyObject *bytes = PyUnicode_AsASCIIString(value); #else PyObject *bytes = value; #endif if( bytes ) { size_t nbytes = PyBytes_Size( bytes ); const char * bytestr = PyBytes_AS_STRING(bytes); result = malloc( (nbytes+1) * sizeof(*result)); strcpy( result, bytestr ); #ifdef USE_PY3K Py_DECREF(bytes); #endif } } return result; }
static int __init__(PyObject *self, PyObject *args, PyObject *kwds) { LIGOTimeGPS *gps = &((pylal_LIGOTimeGPS *) self)->gps; PyObject *seconds; long long nanoseconds = 0; if(!PyArg_ParseTuple(args, "O|L", &seconds, &nanoseconds)) return -1; if(PyUnicode_Check(seconds)) { /* convert to ascii string */ PyObject *str = PyUnicode_AsASCIIString(seconds); if(!str) return -1; seconds = str; } if(PyString_Check(seconds)) { char *end, *str = PyString_AsString(seconds); int result = XLALStrToGPS(gps, str, &end); if((result < 0) || (end == str)) { PyErr_SetObject(PyExc_ValueError, seconds); return -1; } } else if(!pyobject_to_ligotimegps(seconds, gps)) { PyErr_SetObject(PyExc_ValueError, seconds); return -1; } XLALINT8NSToGPS(gps, XLALGPSToINT8NS(gps) + nanoseconds); return 0; }
static int string_from_pyobj(string *str,int *len,const string inistr,PyObject *obj,const char *errmess) { PyArrayObject *arr = NULL; PyObject *tmp = NULL; #ifdef DEBUGCFUNCS fprintf(stderr,"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\n",(char*)str,*len,(char *)inistr,obj); #endif if (obj == Py_None) { if (*len == -1) *len = strlen(inistr); /* Will this cause problems? */ STRINGMALLOC(*str,*len); STRINGCOPYN(*str,inistr,*len+1); return 1; } if (PyArray_Check(obj)) { if ((arr = (PyArrayObject *)obj) == NULL) goto capi_fail; if (!ISCONTIGUOUS(arr)) { PyErr_SetString(PyExc_ValueError,"array object is non-contiguous."); goto capi_fail; } if (*len == -1) *len = (arr->descr->elsize)*PyArray_SIZE(arr); STRINGMALLOC(*str,*len); STRINGCOPYN(*str,arr->data,*len+1); return 1; } if (PyString_Check(obj)) { tmp = obj; Py_INCREF(tmp); } #if PY_VERSION_HEX >= 0x03000000 else if (PyUnicode_Check(obj)) { tmp = PyUnicode_AsASCIIString(obj); } else { PyObject *tmp2; tmp2 = PyObject_Str(obj); if (tmp2) { tmp = PyUnicode_AsASCIIString(tmp2); Py_DECREF(tmp2); } else { tmp = NULL; } } #else else {
static void MemoryBinaryStream_Open(MemoryBinaryStream *pBinStrm, bp::str s) { char *pBuf; Py_ssize_t Size; //PyString_AsStringAndSize(s.ptr(), &pBuf, &Size); PyUnicode_AsASCIIString(s.ptr()); pBinStrm->Open(pBuf, static_cast<u32>(Size)); }
char *PythonScriptInterface::copyPythonString(PyObject *string) { if(string) { PyObject *bstr = PyUnicode_AsASCIIString(string); if(bstr && PyBytes_Check(bstr)) { return strdup(PyBytes_AsString(bstr)); } } return NULL; }
static int UnicodeResultProcessor_init(UnicodeResultProcessor *self, PyObject *args, PyObject *kwds) { PyObject *encoding, *errors = NULL; static char *kwlist[] = {"encoding", "errors", NULL}; #if PY_MAJOR_VERSION >= 3 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|U:__init__", kwlist, &encoding, &errors)) return -1; #else if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|S:__init__", kwlist, &encoding, &errors)) return -1; #endif #if PY_MAJOR_VERSION >= 3 encoding = PyUnicode_AsASCIIString(encoding); #else Py_INCREF(encoding); #endif self->encoding = encoding; if (errors) { #if PY_MAJOR_VERSION >= 3 errors = PyUnicode_AsASCIIString(errors); #else Py_INCREF(errors); #endif } else { #if PY_MAJOR_VERSION >= 3 errors = PyBytes_FromString("strict"); #else errors = PyString_FromString("strict"); #endif if (errors == NULL) return -1; } self->errors = errors; return 0; }
/*NUMPY_API * Convert an object to NPY_RAISE / NPY_CLIP / NPY_WRAP */ NPY_NO_EXPORT int PyArray_ClipmodeConverter(PyObject *object, NPY_CLIPMODE *val) { if (object == NULL || object == Py_None) { *val = NPY_RAISE; } else if (PyBytes_Check(object)) { char *str; str = PyBytes_AS_STRING(object); if (str[0] == 'C' || str[0] == 'c') { *val = NPY_CLIP; } else if (str[0] == 'W' || str[0] == 'w') { *val = NPY_WRAP; } else if (str[0] == 'R' || str[0] == 'r') { *val = NPY_RAISE; } else { PyErr_SetString(PyExc_TypeError, "clipmode not understood"); return NPY_FAIL; } } else if (PyUnicode_Check(object)) { PyObject *tmp; int ret; tmp = PyUnicode_AsASCIIString(object); if (tmp == NULL) { return NPY_FAIL; } ret = PyArray_ClipmodeConverter(tmp, val); Py_DECREF(tmp); return ret; } else { int number = PyArray_PyIntAsInt(object); if (error_converting(number)) { goto fail; } if (number <= (int) NPY_RAISE && number >= (int) NPY_CLIP) { *val = (NPY_CLIPMODE) number; } else { goto fail; } } return NPY_SUCCEED; fail: PyErr_SetString(PyExc_TypeError, "clipmode not understood"); return NPY_FAIL; }
/* * utility function for working with objects expected to be ASCII characters. */ PyObj PyObject_AsASCIIString(PyObj stringable_ob) { PyObj uni, rob; uni = PyObject_Str(stringable_ob); if (uni == NULL) return(NULL); rob = PyUnicode_AsASCIIString(uni); Py_DECREF(uni); return(rob); }
int set_string( const char* propname, PyObject* value, char* dest, Py_ssize_t maxlen) { char* buffer; Py_ssize_t len; PyObject* ascii_obj = NULL; int result = -1; if (check_delete(propname, value)) { return -1; } if (PyUnicode_Check(value)) { ascii_obj = PyUnicode_AsASCIIString(value); if (ascii_obj == NULL) { goto end; } if (PyBytes_AsStringAndSize(ascii_obj, &buffer, &len) == -1) { goto end; } } else if (PyBytes_Check(value)) { if (PyBytes_AsStringAndSize(value, &buffer, &len) == -1) { goto end; } } else { PyErr_SetString(PyExc_TypeError, "value must be bytes or unicode"); goto end; } if (len > maxlen) { PyErr_Format( PyExc_ValueError, "'%s' must be less than %u characters", propname, (unsigned int)maxlen); goto end; } strncpy(dest, buffer, (size_t)maxlen); result = 0; end: Py_XDECREF(ascii_obj); return result; }
size_t py_hex_to_git_oid (PyObject *py_oid, git_oid *oid) { PyObject *py_hex; int err; char *hex; Py_ssize_t len; #if PY_MAJOR_VERSION == 2 /* Bytes (only supported in Python 2) */ if (PyBytes_Check(py_oid)) { err = PyBytes_AsStringAndSize(py_oid, &hex, &len); if (err) return 0; err = git_oid_fromstrn(oid, hex, len); if (err < 0) { PyErr_SetObject(Error_type(err), py_oid); return 0; } return (size_t)len; } #endif /* Unicode */ if (PyUnicode_Check(py_oid)) { py_hex = PyUnicode_AsASCIIString(py_oid); if (py_hex == NULL) return 0; err = PyBytes_AsStringAndSize(py_hex, &hex, &len); if (err) { Py_DECREF(py_hex); return 0; } err = git_oid_fromstrn(oid, hex, len); Py_DECREF(py_hex); if (err < 0) { PyErr_SetObject(Error_type(err), py_oid); return 0; } return (size_t)len; } /* Type error */ PyErr_SetObject(PyExc_TypeError, py_oid); return 0; }
static PyObject * trie_with_prefix(trieobject *mp, PyObject *py_prefix) { const char *prefix; PyObject *py_list; #ifdef IS_PY3K PyObject* bytes; #endif /* Make sure prefix is a string. */ #ifdef IS_PY3K if(!PyUnicode_Check(py_prefix)) { #else if(!PyString_Check(py_prefix)) { #endif PyErr_SetString(PyExc_TypeError, "prefix must be a string"); return NULL; } #ifdef IS_PY3K bytes = PyUnicode_AsASCIIString(py_prefix); if(!bytes) { PyErr_SetString(PyExc_TypeError, "prefix must be an ASCII string"); return NULL; } prefix = PyBytes_AsString(bytes); #else prefix = PyString_AS_STRING(py_prefix); #endif if(!(py_list = PyList_New(0))) return NULL; Trie_with_prefix(mp->trie, prefix, _trie_with_prefix_helper, (void *)py_list); #ifdef IS_PY3K Py_DECREF(bytes); #endif if(PyErr_Occurred()) { Py_DECREF(py_list); return NULL; } return py_list; } static PyObject * trie_with_prefix_onearg(trieobject *mp, PyObject *py_args) { PyObject *py_arg; if(!PyArg_ParseTuple(py_args, "O", &py_arg)) return NULL; return trie_with_prefix(mp, py_arg); }
/*NUMPY_API * Convert object to endian */ NPY_NO_EXPORT int PyArray_ByteorderConverter(PyObject *obj, char *endian) { char *str; PyObject *tmp = NULL; if (PyUnicode_Check(obj)) { obj = tmp = PyUnicode_AsASCIIString(obj); } *endian = PyArray_SWAP; str = PyBytes_AsString(obj); if (!str) { Py_XDECREF(tmp); return PY_FAIL; } if (strlen(str) < 1) { PyErr_SetString(PyExc_ValueError, "Byteorder string must be at least length 1"); Py_XDECREF(tmp); return PY_FAIL; } *endian = str[0]; if (str[0] != PyArray_BIG && str[0] != PyArray_LITTLE && str[0] != PyArray_NATIVE && str[0] != PyArray_IGNORE) { if (str[0] == 'b' || str[0] == 'B') { *endian = PyArray_BIG; } else if (str[0] == 'l' || str[0] == 'L') { *endian = PyArray_LITTLE; } else if (str[0] == 'n' || str[0] == 'N') { *endian = PyArray_NATIVE; } else if (str[0] == 'i' || str[0] == 'I') { *endian = PyArray_IGNORE; } else if (str[0] == 's' || str[0] == 'S') { *endian = PyArray_SWAP; } else { PyErr_Format(PyExc_ValueError, "%s is an unrecognized byteorder", str); Py_XDECREF(tmp); return PY_FAIL; } } Py_XDECREF(tmp); return PY_SUCCEED; }
static PyObject *Rlite_command(hirlite_RliteObject *self, PyObject *args) { PyObject *object; int i, argc; char **argv; size_t *argvlen; PyObject *bytes; char *str; size_t len; rliteReply *reply; argc = (int)PyTuple_Size(args); argv = malloc(sizeof(char *) * argc); if (!argv) return NULL; argvlen = malloc(sizeof(size_t) * argc); if (!argvlen) { free(argv); return NULL; } for (i = 0; i < argc; i++) { object = PyTuple_GetItem(args, i); if (PyUnicode_Check(object)) bytes = PyUnicode_AsASCIIString(object); else bytes = PyObject_Bytes(object); if (bytes == NULL) return NULL; argvlen[i] = len = PyBytes_Size(bytes); str = PyBytes_AsString(bytes); argv[i] = (char*)malloc(len+1); memcpy(argv[i], str, len); argv[i][len] = '\0'; Py_DECREF(bytes); } reply = rliteCommandArgv(self->context, argc, argv, argvlen); object = replyToPyObject(self, reply); for (i = 0; i < argc; i++) { free(argv[i]); } free(argv); free(argvlen); rliteFreeReplyObject(reply); return object; }
/*NUMPY_API * Convert an object to FORTRAN / C / ANY / KEEP */ NPY_NO_EXPORT int PyArray_OrderConverter(PyObject *object, NPY_ORDER *val) { char *str; /* Leave the desired default from the caller for NULL/Py_None */ if (object == NULL || object == Py_None) { return NPY_SUCCEED; } else if (PyUnicode_Check(object)) { PyObject *tmp; int ret; tmp = PyUnicode_AsASCIIString(object); ret = PyArray_OrderConverter(tmp, val); Py_DECREF(tmp); return ret; } else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) { if (PyObject_IsTrue(object)) { *val = NPY_FORTRANORDER; } else { *val = NPY_CORDER; } if (PyErr_Occurred()) { return NPY_FAIL; } return NPY_SUCCEED; } else { str = PyBytes_AS_STRING(object); if (str[0] == 'C' || str[0] == 'c') { *val = NPY_CORDER; } else if (str[0] == 'F' || str[0] == 'f') { *val = NPY_FORTRANORDER; } else if (str[0] == 'A' || str[0] == 'a') { *val = NPY_ANYORDER; } else if (str[0] == 'K' || str[0] == 'k') { *val = NPY_KEEPORDER; } else { PyErr_SetString(PyExc_TypeError, "order not understood"); return NPY_FAIL; } } return NPY_SUCCEED; }
static PyObject* _create_bytes_object(PyObject* o) { PyObject* b; if (PyBytes_Check(o)) { return o; } if (!PyUnicode_Check(o)) { return NULL; } b = PyUnicode_AsASCIIString(o); if (!b) { PyErr_Clear(); return NULL; } return b; }
/*NUMPY_API * Convert object to sort kind */ NPY_NO_EXPORT int PyArray_SortkindConverter(PyObject *obj, NPY_SORTKIND *sortkind) { char *str; PyObject *tmp = NULL; if (PyUnicode_Check(obj)) { obj = tmp = PyUnicode_AsASCIIString(obj); if (obj == NULL) { return NPY_FAIL; } } *sortkind = NPY_QUICKSORT; str = PyBytes_AsString(obj); if (!str) { Py_XDECREF(tmp); return NPY_FAIL; } if (strlen(str) < 1) { PyErr_SetString(PyExc_ValueError, "Sort kind string must be at least length 1"); Py_XDECREF(tmp); return NPY_FAIL; } if (str[0] == 'q' || str[0] == 'Q') { *sortkind = NPY_QUICKSORT; } else if (str[0] == 'h' || str[0] == 'H') { *sortkind = NPY_HEAPSORT; } else if (str[0] == 'm' || str[0] == 'M') { *sortkind = NPY_MERGESORT; } else if (str[0] == 's' || str[0] == 'S') { /* mergesort is the only stable sorting method in numpy */ *sortkind = NPY_MERGESORT; } else { PyErr_Format(PyExc_ValueError, "%s is an unrecognized kind of sort", str); Py_XDECREF(tmp); return NPY_FAIL; } Py_XDECREF(tmp); return NPY_SUCCEED; }
static int arrayflags_setitem(PyArrayFlagsObject *self, PyObject *ind, PyObject *item) { char *key; char buf[16]; int n; if (PyUnicode_Check(ind)) { PyObject *tmp_str; tmp_str = PyUnicode_AsASCIIString(ind); key = PyBytes_AS_STRING(tmp_str); n = PyBytes_GET_SIZE(tmp_str); if (n > 16) n = 16; memcpy(buf, key, n); Py_DECREF(tmp_str); key = buf; } else if (PyBytes_Check(ind)) { key = PyBytes_AS_STRING(ind); n = PyBytes_GET_SIZE(ind); } else { goto fail; } if (((n==9) && (strncmp(key, "WRITEABLE", n) == 0)) || ((n==1) && (strncmp(key, "W", n) == 0))) { return arrayflags_writeable_set(self, item); } else if (((n==7) && (strncmp(key, "ALIGNED", n) == 0)) || ((n==1) && (strncmp(key, "A", n) == 0))) { return arrayflags_aligned_set(self, item); } else if (((n==12) && (strncmp(key, "UPDATEIFCOPY", n) == 0)) || ((n==1) && (strncmp(key, "U", n) == 0))) { return arrayflags_updateifcopy_set(self, item); } else if ((n==6) && (strncmp(key, "MASKNA", n) == 0)) { return arrayflags_maskna_set(self, item); } else if ((n==9) && (strncmp(key, "OWNMASKNA", n) == 0)) { return arrayflags_ownmaskna_set(self, item); } fail: PyErr_SetString(PyExc_KeyError, "Unknown flag"); return -1; }
char *GetCharFromDict(PyObject * dict, const char *key) { PyObject *o, *o2 = NULL; char *ps = NULL, *result = NULL; size_t length; o = PyDict_GetItemString(dict, key); if (o == NULL) { PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key); return NULL; } if (PyUnicode_Check(o)) { o2 = PyUnicode_AsASCIIString(o); if (o2 == NULL) { return NULL; } ps = PyBytes_AsString(o2); } #if PY_MAJOR_VERSION < 3 else if (PyString_Check(o)) { ps = PyString_AsString(o); } #endif if (ps == NULL) { PyErr_Format(PyExc_ValueError, "Can not get string value for key %s", key); goto out; } length = strlen(ps) + 1; result = (char *)malloc(length); if (result == NULL) { PyErr_Format(PyExc_ValueError, "Failed to allocate memory!"); goto out; } memcpy(result, ps, length); out: if (o2 != NULL) { Py_DECREF(o2); } return result; }
static void construct( PyObject *obj, converter::rvalue_from_python_stage1_data *data ) { void *storage = ((converter::rvalue_from_python_storage<std::string>*)data)->storage.bytes; PyObject *ascii = PyUnicode_AsASCIIString( obj ); if( ascii ) { new (storage) std::string( PyString_AsString( ascii ) ); // record successful construction data->convertible = storage; Py_DECREF( ascii ); } else { throw_error_already_set(); } }