PyObject * PyObject_Unicode(PyObject *v) { PyObject *res; PyObject *func; PyObject *str; static PyObject *unicodestr; if (v == NULL) { res = PyString_FromString("<NULL>"); if (res == NULL) return NULL; str = PyUnicode_FromEncodedObject(res, NULL, "strict"); Py_DECREF(res); return str; } else if (PyUnicode_CheckExact(v)) { Py_INCREF(v); return v; } /* XXX As soon as we have a tp_unicode slot, we should check this before trying the __unicode__ method. */ if (unicodestr == NULL) { unicodestr= PyString_InternFromString("__unicode__"); if (unicodestr == NULL) return NULL; } func = PyObject_GetAttr(v, unicodestr); if (func != NULL) { res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); } else { PyErr_Clear(); if (PyUnicode_Check(v)) { /* For a Unicode subtype that's didn't overwrite __unicode__, return a true Unicode object with the same data. */ return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), PyUnicode_GET_SIZE(v)); } if (PyString_CheckExact(v)) { Py_INCREF(v); res = v; } else { if (v->ob_type->tp_str != NULL) res = (*v->ob_type->tp_str)(v); else res = PyObject_Repr(v); } } if (res == NULL) return NULL; if (!PyUnicode_Check(res)) { str = PyUnicode_FromEncodedObject(res, NULL, "strict"); Py_DECREF(res); res = str; } return res; }
// Initialize the global decimal character and thousands separator character, used when parsing decimal // objects. // static void init_locale_info() { Object module = PyImport_ImportModule("locale"); if (!module) { PyErr_Clear(); return; } Object ldict = PyObject_CallMethod(module, "localeconv", 0); if (!ldict) { PyErr_Clear(); return; } PyObject* value = PyDict_GetItemString(ldict, "decimal_point"); if (value) { if (PyBytes_Check(value) && PyBytes_Size(value) == 1) chDecimal = (Py_UNICODE)PyBytes_AS_STRING(value)[0]; if (PyUnicode_Check(value) && PyUnicode_GET_SIZE(value) == 1) chDecimal = PyUnicode_AS_UNICODE(value)[0]; } }
static bool Connect(PyObject* pConnectString, HDBC hdbc, bool fAnsi) { // This should have been checked by the global connect function. I(PyString_Check(pConnectString) || PyUnicode_Check(pConnectString)); const int cchMax = 600; if (PySequence_Length(pConnectString) >= cchMax) { PyErr_SetString(PyExc_TypeError, "connection string too long"); return false; } // The driver manager determines if the app is a Unicode app based on whether we call SQLDriverConnectA or // SQLDriverConnectW. Some drivers, notably Microsoft Access/Jet, change their behavior based on this, so we try // the Unicode version first. (The Access driver only supports Unicode text, but SQLDescribeCol returns SQL_CHAR // instead of SQL_WCHAR if we connect with the ANSI version. Obviously this causes lots of errors since we believe // what it tells us (SQL_CHAR).) // Python supports only UCS-2 and UCS-4, so we shouldn't need to worry about receiving surrogate pairs. However, // Windows does use UCS-16, so it is possible something would be misinterpreted as one. We may need to examine // this more. SQLRETURN ret; if (!fAnsi) { SQLWCHAR szConnectW[cchMax]; if (PyUnicode_Check(pConnectString)) { Py_UNICODE* p = PyUnicode_AS_UNICODE(pConnectString); for (int i = 0, c = PyUnicode_GET_SIZE(pConnectString); i <= c; i++) szConnectW[i] = (wchar_t)p[i]; } else { const char* p = PyString_AS_STRING(pConnectString); for (int i = 0, c = PyString_GET_SIZE(pConnectString); i <= c; i++) szConnectW[i] = (wchar_t)p[i]; } Py_BEGIN_ALLOW_THREADS ret = SQLDriverConnectW(hdbc, 0, szConnectW, SQL_NTS, 0, 0, 0, SQL_DRIVER_NOPROMPT); Py_END_ALLOW_THREADS if (SQL_SUCCEEDED(ret)) return true; // The Unicode function failed. If the error is that the driver doesn't have a Unicode version (IM001), continue // to the ANSI version. PyObject* error = GetErrorFromHandle("SQLDriverConnectW", hdbc, SQL_NULL_HANDLE); if (!HasSqlState(error, "IM001")) { RaiseErrorFromException(error); return false; } Py_XDECREF(error); }
static NUMBA_INLINE Py_UCS4 __Numba_PyObject_AsPy_UCS4(PyObject* x) { long ival; if (PyUnicode_Check(x)) { Py_ssize_t length; #if CYTHON_PEP393_ENABLED length = PyUnicode_GET_LENGTH(x); if (likely(length == 1)) { return PyUnicode_READ_CHAR(x, 0); } #else length = PyUnicode_GET_SIZE(x); if (likely(length == 1)) { return PyUnicode_AS_UNICODE(x)[0]; } #if Py_UNICODE_SIZE == 2 else if (PyUnicode_GET_SIZE(x) == 2) { Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0]; if (high_val >= 0xD800 && high_val <= 0xDBFF) { Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1]; if (low_val >= 0xDC00 && low_val <= 0xDFFF) { return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1))); } } } #endif #endif PyErr_Format(PyExc_ValueError, "only single character unicode strings can be converted to Py_UCS4, " "got length %" CYTHON_FORMAT_SSIZE_T "d", length); return (Py_UCS4)-1; } ival = __Numba_PyInt_AsLong(x); if (unlikely(ival < 0)) { if (!PyErr_Occurred()) PyErr_SetString(PyExc_OverflowError, "cannot convert negative value to Py_UCS4"); return (Py_UCS4)-1; } else if (unlikely(ival > 1114111)) { PyErr_SetString(PyExc_OverflowError, "value too large to convert to Py_UCS4"); return (Py_UCS4)-1; } return (Py_UCS4)ival; }
static Box* _intNew(Box* val, Box* base) { if (isSubclass(val->cls, int_cls)) { RELEASE_ASSERT(!base, ""); BoxedInt* n = static_cast<BoxedInt*>(val); if (val->cls == int_cls) return n; return new BoxedInt(n->n); } else if (isSubclass(val->cls, str_cls)) { int base_n; if (!base) base_n = 10; else { RELEASE_ASSERT(base->cls == int_cls, ""); base_n = static_cast<BoxedInt*>(base)->n; } BoxedString* s = static_cast<BoxedString*>(val); RELEASE_ASSERT(s->size() == strlen(s->data()), ""); Box* r = PyInt_FromString(s->data(), NULL, base_n); if (!r) throwCAPIException(); return r; } else if (isSubclass(val->cls, unicode_cls)) { int base_n; if (!base) base_n = 10; else { RELEASE_ASSERT(base->cls == int_cls, ""); base_n = static_cast<BoxedInt*>(base)->n; } Box* r = PyInt_FromUnicode(PyUnicode_AS_UNICODE(val), PyUnicode_GET_SIZE(val), base_n); if (!r) throwCAPIException(); return r; } else if (val->cls == float_cls) { RELEASE_ASSERT(!base, ""); double d = static_cast<BoxedFloat*>(val)->d; return new BoxedInt(d); } else { RELEASE_ASSERT(!base, ""); static const std::string int_str("__int__"); Box* r = callattr(val, &int_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = true }), ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); if (!r) { fprintf(stderr, "TypeError: int() argument must be a string or a number, not '%s'\n", getTypeName(val)); raiseExcHelper(TypeError, ""); } if (!isSubclass(r->cls, int_cls) && !isSubclass(r->cls, long_cls)) { raiseExcHelper(TypeError, "__int__ returned non-int (type %s)", r->cls->tp_name); } return r; }
static PyObject * complex_subtype_from_string(PyTypeObject *type, PyObject *v) { const char *s; PyObject *result = NULL; #ifdef Py_USING_UNICODE char *s_buffer = NULL; #endif Py_ssize_t len; if (PyString_Check(v)) { s = PyString_AS_STRING(v); len = PyString_GET_SIZE(v); } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(v)) { s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1); if (s_buffer == NULL) return PyErr_NoMemory(); if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), PyUnicode_GET_SIZE(v), s_buffer, NULL)) goto exit; s = s_buffer; len = strlen(s); } #endif else { PyErr_SetString(PyExc_TypeError, "complex() arg is not a string"); return NULL; } result = _Py_string_to_number_with_underscores(s, len, "complex", v, type, complex_from_string_inner); exit: #ifdef Py_USING_UNICODE if (s_buffer) PyMem_FREE(s_buffer); #endif return result; }
static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen) { PyObject *obj = (PyObject *) _obj; PyObject *newObj = PyUnicode_EncodeUTF8 (PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), NULL); GET_TC(tc)->newObj = newObj; *_outLen = PyString_GET_SIZE(newObj); return PyString_AS_STRING(newObj); }
// @object PROPSPEC|Identifies a property. Can be either an int property id, or a str/unicode property name. BOOL PyObject_AsPROPSPECs( PyObject *ob, PROPSPEC **ppRet, ULONG *pcRet) { BOOL ret=FALSE; DWORD len, i; PyObject *tuple=PyWinSequence_Tuple(ob, &len); if (tuple==NULL) return FALSE; // First count the items, and the total string space we need. size_t cChars = 0; for (i=0;i<len;i++) { PyObject *sub = PyTuple_GET_ITEM(tuple, i); if (PyUnicode_Check(sub)) cChars += PyUnicode_GET_SIZE(sub) + 1; else if (PyString_Check(sub)) cChars += PyString_Size(sub) + 1; else if (PyInt_Check(sub)) ; // PROPID is a ULONG, so this may fail for values that require a python long else { PyErr_SetString(PyExc_TypeError, "PROPSPECs must be a sequence of strings or integers"); goto cleanup; } } size_t numBytes; numBytes = (sizeof(PROPSPEC) * len) + (sizeof(WCHAR) * cChars); PROPSPEC *pRet; pRet = (PROPSPEC *)malloc(numBytes); if (pRet==NULL) { PyErr_SetString(PyExc_MemoryError, "allocating PROPSPECs"); goto cleanup; } WCHAR *curBuf; curBuf = (WCHAR *)(pRet+len); for (i=0;i<len;i++) { PyObject *sub = PyTuple_GET_ITEM(tuple, i); BSTR bstr; if (PyWinObject_AsBstr(sub, &bstr)) { pRet[i].ulKind = PRSPEC_LPWSTR; pRet[i].lpwstr = curBuf; wcscpy( curBuf, bstr); curBuf += wcslen(curBuf) + 1; PyWinObject_FreeBstr(bstr); } else { PyErr_Clear(); pRet[i].ulKind = PRSPEC_PROPID; pRet[i].propid = PyInt_AsLong(sub); } } ret=TRUE; *ppRet = pRet; *pcRet = len; cleanup: Py_DECREF(tuple); return ret; }
static Tcl_Obj* AsObj(PyObject *value) { Tcl_Obj *result; if (PyString_Check(value)) return Tcl_NewStringObj(PyString_AS_STRING(value), PyString_GET_SIZE(value)); else if (PyInt_Check(value)) return Tcl_NewLongObj(PyInt_AS_LONG(value)); else if (PyFloat_Check(value)) return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); else if (PyTuple_Check(value)) { Tcl_Obj **argv = (Tcl_Obj**) ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*)); int i; if(!argv) return 0; for(i=0;i<PyTuple_Size(value);i++) argv[i] = AsObj(PyTuple_GetItem(value,i)); result = Tcl_NewListObj(PyTuple_Size(value), argv); ckfree(FREECAST argv); return result; } else if (PyUnicode_Check(value)) { #if TKMAJORMINOR <= 8001 /* In Tcl 8.1 we must use UTF-8 */ PyObject* utf8 = PyUnicode_AsUTF8String(value); if (!utf8) return 0; result = Tcl_NewStringObj(PyString_AS_STRING(utf8), PyString_GET_SIZE(utf8)); Py_DECREF(utf8); return result; #else /* TKMAJORMINOR > 8001 */ /* In Tcl 8.2 and later, use Tcl_NewUnicodeObj() */ if (sizeof(Py_UNICODE) != sizeof(Tcl_UniChar)) { /* XXX Should really test this at compile time */ PyErr_SetString(PyExc_SystemError, "Py_UNICODE and Tcl_UniChar differ in size"); return 0; } return Tcl_NewUnicodeObj(PyUnicode_AS_UNICODE(value), PyUnicode_GET_SIZE(value)); #endif /* TKMAJORMINOR > 8001 */ } else { PyObject *v = PyObject_Str(value); if (!v) return 0; result = AsObj(v); Py_DECREF(v); return result; } }
// String conversions // Convert a Python object to a WCHAR - allow embedded NULLs, None, etc. BOOL PyWinObject_AsWCHAR(PyObject *stringObject, WCHAR **pResult, BOOL bNoneOK /*= FALSE*/,DWORD *pResultLen /*= NULL*/) { BOOL rc = TRUE; int resultLen = 0; #if (PY_VERSION_HEX < 0x03000000) // Do NOT accept 'bytes' object when a plain 'WCHAR' is needed on py3k. if (PyString_Check(stringObject)) { int size=PyString_Size(stringObject); const char *buf = PyString_AsString(stringObject); if (buf==NULL) return FALSE; /* We assume that we dont need more 'wide characters' for the result then the number of bytes in the input. Often we will need less, as the input may contain multi-byte chars, but we should never need more */ *pResult = (LPWSTR)PyMem_Malloc((size+1)*sizeof(WCHAR)); if (*pResult==NULL) { PyErr_SetString(PyExc_MemoryError, "No memory for wide string buffer"); return FALSE; } /* convert and get the final character size */ resultLen = MultiByteToWideChar(CP_ACP, 0, buf, size, *pResult, size); /* terminate the string */ (*pResult)[resultLen] = L'\0'; } else #endif // py3k if (PyUnicode_Check(stringObject)) { resultLen = PyUnicode_GET_SIZE(stringObject); size_t cb = sizeof(WCHAR) * (resultLen+1); *pResult = (WCHAR *)PyMem_Malloc(cb); if (*pResult==NULL) { PyErr_SetString(PyExc_MemoryError, "Allocating WCHAR array"); return FALSE; } // copy the value, including embedded NULLs memcpy(*pResult, PyUnicode_AsUnicode(stringObject), cb); } else if (stringObject == Py_None) { if (bNoneOK) { *pResult = NULL; } else { PyErr_SetString(PyExc_TypeError, "None is not a valid string in this context"); rc = FALSE; } } else { const char *tp_name = stringObject && stringObject->ob_type ? stringObject->ob_type->tp_name : "<NULL!!>"; PyErr_Format(PyExc_TypeError, "Objects of type '%s' can not be converted to Unicode.", tp_name); rc = FALSE; } if (rc && pResultLen) *pResultLen = resultLen; return rc; }
static int add_to_data(ligolw_Tokenizer *tokenizer, PyObject *unicode) { Py_ssize_t n = PyUnicode_GET_SIZE(unicode); if(n) { if(tokenizer->length - tokenizer->data + n > tokenizer->allocation) { /* * convert pointers to integer offsets */ ptrdiff_t pos = tokenizer->pos - tokenizer->data; ptrdiff_t length = tokenizer->length - tokenizer->data; /* * increase buffer size, adding 1 to leave room for * the null terminator */ Py_UNICODE *old_data = tokenizer->data; tokenizer->data = realloc(tokenizer->data, (tokenizer->allocation + n + 1) * sizeof(*tokenizer->data)); if(!tokenizer->data) { /* * memory failure, restore pointer and exit */ tokenizer->data = old_data; return -1; } tokenizer->allocation += n; /* * convert integer offsets back to pointers */ tokenizer->pos = &tokenizer->data[pos]; tokenizer->length = &tokenizer->data[length]; } /* * copy data from unicode into buffer, appending null * terminator */ memcpy(tokenizer->length, PyUnicode_AsUnicode(unicode), n * sizeof(*tokenizer->length)); tokenizer->length += n; *tokenizer->length = 0; } /* * success */ return 0; }
static PyObject * complex__format__(PyObject* self, PyObject* args) { PyObject *format_spec; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; return _PyComplex_FormatAdvanced(self, PyUnicode_AS_UNICODE(format_spec), PyUnicode_GET_SIZE(format_spec)); }
EXPORT UnicodeString &PyObject_AsUnicodeString(PyObject *object, const char *encoding, const char *mode, UnicodeString &string) { if (PyUnicode_Check(object)) { if (sizeof(Py_UNICODE) == sizeof(UChar)) string.setTo((const UChar *) PyUnicode_AS_UNICODE(object), (int32_t) PyUnicode_GET_SIZE(object)); else { int32_t len = (int32_t) PyUnicode_GET_SIZE(object); Py_UNICODE *pchars = PyUnicode_AS_UNICODE(object); UChar *chars = new UChar[len * 3]; UErrorCode status = U_ZERO_ERROR; int32_t dstLen; u_strFromUTF32(chars, len * 3, &dstLen, (const UChar32 *) pchars, len, &status); if (U_FAILURE(status)) { delete[] chars; throw ICUException(status); } string.setTo((const UChar *) chars, (int32_t) dstLen); delete[] chars; } } else if (PyBytes_Check(object)) PyBytes_AsUnicodeString(object, encoding, mode, string); else { PyErr_SetObject(PyExc_TypeError, object); throw ICUException(); } return string; }
static PyObject * Splitter_split(Splitter *self, PyObject *args) { PyObject *doc; char *encoding = "iso-8859-15"; Py_XDECREF(self->list); self->list = PyList_New(0); if (! (PyArg_ParseTuple(args,"O|s",&doc, &encoding))) return NULL; if (PyBytes_Check(doc)) { if (strlen(encoding) == 0 || !strcmp(encoding,"ascii")) splitString(self, doc); else { PyObject *doc1; if (! (doc1 = PyUnicode_FromEncodedObject(doc, encoding, "strict"))) { PyErr_SetString(PyExc_UnicodeError,"unicode conversion failed (maybe wrong encoding parameter)"); return NULL; } splitUnicodeString(self, doc1); Py_XDECREF(doc1); } } else if (PyUnicode_Check(doc)) { PyObject *doc1; // create a *real* copy since we need to modify the string doc1 = PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(doc)); Py_UNICODE_COPY(PyUnicode_AS_UNICODE(doc1), PyUnicode_AS_UNICODE(doc), PyUnicode_GET_SIZE(doc)); splitUnicodeString(self, doc1); Py_DECREF(doc1); } else { PyErr_SetString(PyExc_TypeError, "first argument must be string or unicode"); return NULL; } Py_XINCREF(self->list); return self->list; }
Py_ssize_t len(PyObject* str) { if (str == Py_None) return 0; if (PyUnicode_Check(str)) return PyUnicode_GET_SIZE(str); if (PyBytes_Check(str)) return PyBytes_GET_SIZE(str); return 0; }
static PyObject * unquote_internal_unicode(PyObject *string, int plus) { PyObject *result; Py_UNICODE *su, *ru; Py_ssize_t j, slen, tlen, sentinel; if (!PyUnicode_CheckExact(string)) { if (!(string = PyObject_Unicode(string))) return NULL; } else Py_INCREF(string); su = PyUnicode_AS_UNICODE(string); slen = tlen = PyUnicode_GET_SIZE(string); for (j=0, sentinel=slen-2; j<sentinel; ++j) { if ( WTF_IS_LATIN1(su[j]) && (su[j] & 0xFF) == '%' && WTF_IS_LATIN1(su[j+1]) && WTF_IS_HEX_DIGIT(su[j+1]) && WTF_IS_LATIN1(su[j+2]) && WTF_IS_HEX_DIGIT(su[j+2])) { tlen -= 2; j += 2; } } if (slen == tlen && !plus) /* shortcut: nothing to unquote */ return string; if (!(result = PyUnicode_FromUnicode(NULL, tlen))) goto done; ru = PyUnicode_AS_UNICODE(result); for (j=0, sentinel=slen-2; j<slen; ++j) { if ( j < sentinel && WTF_IS_LATIN1(su[j]) && (su[j] & 0xFF) == '%' && WTF_IS_LATIN1(su[j+1]) && WTF_IS_HEX_DIGIT(su[j+1]) && WTF_IS_LATIN1(su[j+2]) && WTF_IS_HEX_DIGIT(su[j+2])) { *ru++ = (WTF_HEX_VALUE(su[j+1]) << 4) + (WTF_HEX_VALUE(su[j+2])); j += 2; } else if (plus && su[j] == (unsigned char)'+') { *ru++ = (unsigned char)' '; } else { *ru++ = su[j]; } } done: Py_DECREF(string); return result; }
static PyObject* automaton_iter(PyObject* self, PyObject* args) { #define automaton ((Automaton*)self) if (automaton->kind != AHOCORASICK) { PyErr_SetString(PyExc_AttributeError, "not an automaton yet; add some words and call make_automaton"); return NULL; } PyObject* object; ssize_t start; ssize_t end; object = PyTuple_GetItem(args, 0); if (object) { #ifdef AHOCORASICK_UNICODE if (PyUnicode_Check(object)) { start = 0; end = PyUnicode_GET_SIZE(object); } else { PyErr_SetString(PyExc_TypeError, "string required"); return NULL; } #else if (PyBytes_Check(object)) { start = 0; end = PyBytes_GET_SIZE(object); } else { PyErr_SetString(PyExc_TypeError, "bytes required"); return NULL; } #endif } else return NULL; if (pymod_parse_start_end(args, 1, 2, start, end, &start, &end)) return NULL; return automaton_search_iter_new( automaton, object, start, end ); #undef automaton }
static int IsSpace(PyObject *str) { Py_UNICODE *p, *e; if (!PyUnicode_CheckExact(str)) return -1; p = PyUnicode_AS_UNICODE(str); e = p + PyUnicode_GET_SIZE(str); while (p < e) { if (!IS_XMLSPACE(*p)) return 0; p++; } return 1; }
PyObject* unicodeTest(PyObject* self, PyObject* args) { PyObject* uni = PyTuple_GET_ITEM(args, 0); // puts("unicodeTest"); //"str" // puts(uni->ob_type->tp_name); //puts(PyString_AS_STRING(PyObject_Repr(uni))); // puts(PyString_AS_STRING(uni->ob_type->tp_repr(uni))); Py_UNICODE* unc = PyUnicode_AS_UNICODE(uni); //PyObject* ret = PyUnicode_FromUnicode(unc, PyUnicode_GET_SIZE(uni)); //Py_RETURN_NONE; return PyUnicode_FromUnicode(unc, PyUnicode_GET_SIZE(uni)); }
char * PyUnicode_AsPgString(PyObject *p_unicode) { Py_ssize_t unicode_size = PyUnicode_GET_SIZE(p_unicode); char *message = NULL; PyObject *pTempStr = PyUnicode_Encode(PyUnicode_AsUnicode(p_unicode), unicode_size, GetDatabaseEncodingName(), NULL); errorCheck(); message = strdup(PyBytes_AsString(pTempStr)); errorCheck(); Py_DECREF(pTempStr); return message; }
NUITKA_MAY_BE_UNUSED static bool UNICODE_ADD_INCREMENTAL( PyObject **operand1, PyObject *operand2 ) { #if PYTHON_VERSION < 330 Py_ssize_t operand1_size = PyUnicode_GET_SIZE( *operand1 ); Py_ssize_t operand2_size = PyUnicode_GET_SIZE( operand2 ); Py_ssize_t new_size = operand1_size + operand2_size; if (unlikely( new_size < 0 )) { PyErr_Format( PyExc_OverflowError, "strings are too large to concat" ); return false; } if (unlikely( PyUnicode_Resize( operand1, new_size ) != 0 )) { return false; } /* copy 'w' into the newly allocated area of 'v' */ memcpy( PyUnicode_AS_UNICODE( *operand1 ) + operand1_size, PyUnicode_AS_UNICODE( operand2 ), operand2_size * sizeof( Py_UNICODE ) ); return true; #else PyUnicode_Append( operand1, operand2 ); return !ERROR_OCCURRED(); #endif }
static PyObject* PyLocale_strcoll(PyObject* self, PyObject* args) { PyObject *os1, *os2, *result = NULL; wchar_t *ws1 = NULL, *ws2 = NULL; Py_ssize_t len1, len2; if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2)) return NULL; /* Convert the unicode strings to wchar[]. */ len1 = PyUnicode_GET_SIZE(os1) + 1; ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t)); if (!ws1) { PyErr_NoMemory(); goto done; } if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1) goto done; ws1[len1 - 1] = 0; len2 = PyUnicode_GET_SIZE(os2) + 1; ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t)); if (!ws2) { PyErr_NoMemory(); goto done; } if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1) goto done; ws2[len2 - 1] = 0; /* Collate the strings. */ result = PyLong_FromLong(wcscoll(ws1, ws2)); done: /* Deallocate everything. */ if (ws1) PyMem_FREE(ws1); if (ws2) PyMem_FREE(ws2); return result; }
//----------------------------------------------------------------------------- // Variable_TypeByValue() // Return a variable type given a Python object or NULL if the Python // object does not have a corresponding variable type. //----------------------------------------------------------------------------- static udt_VariableType *Variable_TypeByValue( PyObject* value, // Python type SQLUINTEGER* size) // size to use (OUT) { if (value == Py_None) { *size = 1; return &ceString_VariableType; } if (ceString_Check(value)) { *size = ceString_GetSize(value); return &ceString_VariableType; } #if PY_MAJOR_VERSION < 3 if (PyUnicode_Check(value)) { *size = PyUnicode_GET_SIZE(value); return &vt_Unicode; } #endif if (ceBinary_Check(value)) { udt_StringBuffer temp; if (StringBuffer_FromBinary(&temp, value) < 0) return NULL; *size = temp.size; StringBuffer_Clear(&temp); return &vt_Binary; } if (PyBool_Check(value)) return &vt_Bit; if (PyInt_Check(value)) return &vt_Integer; if (PyLong_Check(value)) return &vt_BigInteger; if (PyFloat_Check(value)) return &vt_Double; if (Py_TYPE(value) == (PyTypeObject*) g_DecimalType) return &vt_Decimal; if (PyTime_Check(value)) return &vt_Time; if (PyDateTime_Check(value)) return &vt_Timestamp; if (PyDate_Check(value)) return &vt_Timestamp; PyErr_Format(g_NotSupportedErrorException, "Variable_TypeByValue(): unhandled data type %s", Py_TYPE(value)->tp_name); return NULL; }
static PyObject * syslog_get_argv(void) { /* Figure out what to use for as the program "ident" for openlog(). * This swallows exceptions and continues rather than failing out, * because the syslog module can still be used because openlog(3) * is optional. */ Py_ssize_t argv_len, scriptlen; PyObject *scriptobj; Py_UNICODE *atslash, *atstart; PyObject *argv = PySys_GetObject("argv"); if (argv == NULL) { return(NULL); } argv_len = PyList_Size(argv); if (argv_len == -1) { PyErr_Clear(); return(NULL); } if (argv_len == 0) { return(NULL); } scriptobj = PyList_GetItem(argv, 0); if (!PyUnicode_Check(scriptobj)) { return(NULL); } scriptlen = PyUnicode_GET_SIZE(scriptobj); if (scriptlen == 0) { return(NULL); } atstart = PyUnicode_AS_UNICODE(scriptobj); atslash = Py_UNICODE_strrchr(atstart, SEP); if (atslash) { return(PyUnicode_FromUnicode(atslash + 1, scriptlen - (atslash - atstart) - 1)); } else { Py_INCREF(scriptobj); return(scriptobj); } return(NULL); }
static PyObject* mod_setdecimalsep(PyObject* self, PyObject* args) { UNUSED(self); if (!PyString_Check(PyTuple_GET_ITEM(args, 0)) && !PyUnicode_Check(PyTuple_GET_ITEM(args, 0))) return PyErr_Format(PyExc_TypeError, "argument 1 must be a string or unicode object"); PyObject* value = PyUnicode_FromObject(PyTuple_GetItem(args, 0)); if (value) { if (PyBytes_Check(value) && PyBytes_Size(value) == 1) chDecimal = (Py_UNICODE)PyBytes_AS_STRING(value)[0]; if (PyUnicode_Check(value) && PyUnicode_GET_SIZE(value) == 1) chDecimal = PyUnicode_AS_UNICODE(value)[0]; } Py_RETURN_NONE; }
// Convert a Python object to a BSTR - allow embedded NULLs, None, etc. BOOL PyWinObject_AsBstr(PyObject *stringObject, BSTR *pResult, BOOL bNoneOK /*= FALSE*/,DWORD *pResultLen /*= NULL*/) { BOOL rc = TRUE; if (PyString_Check(stringObject)) rc = PyString_AsBstr(stringObject, pResult); else if (PyUnicode_Check(stringObject)) { // copy the value, including embedded NULLs int nchars = PyUnicode_GET_SIZE(stringObject); *pResult = SysAllocStringLen(NULL, nchars); if (*pResult) { #if (PY_VERSION_HEX < 0x03020000) #define PUAWC_TYPE PyUnicodeObject * #else #define PUAWC_TYPE PyObject * #endif if (PyUnicode_AsWideChar((PUAWC_TYPE)stringObject, *pResult, nchars)==-1) { rc = FALSE; } else { // The SysAllocStringLen docs indicate that nchars+1 bytes are allocated, // and that normally a \0 is appened by the function. It also states // the \0 is not necessary! While it seems to work fine without it, // we do copy it, as the previous code, which used SysAllocStringLen // with a non-NULL arg is documented clearly as appending the \0. (*pResult)[nchars] = 0; } } } else if (stringObject == Py_None) { if (bNoneOK) { *pResult = NULL; } else { PyErr_SetString(PyExc_TypeError, "None is not a valid string in this context"); rc = FALSE; } } else { const char *tp_name = stringObject && stringObject->ob_type ? stringObject->ob_type->tp_name : "<NULL!!>"; PyErr_Format(PyExc_TypeError, "Objects of type '%s' can not be converted to Unicode.", tp_name); rc = FALSE; } if (rc && !pResult) { PyErr_SetString(PyExc_MemoryError, "Allocating BSTR"); return FALSE; } if (rc && pResultLen) *pResultLen = SysStringLen(*pResult); return rc; }
static int lightblueobex_addunicodeheader(obex_t *obex, obex_object_t *obj, uint8_t hi, PyObject *utf16string) { obex_headerdata_t hv; Py_ssize_t len = PyUnicode_GET_SIZE(utf16string); uint8_t bytes[len+2]; DEBUG("%s()\n", __func__); /* need to add 2-byte null terminator */ memcpy(bytes, (const uint8_t*)PyString_AsString(utf16string), len); bytes[len] = 0x00; bytes[len+1] = 0x00; hv.bs = bytes; return OBEX_ObjectAddHeader(obex, obj, (uint8_t)hi, hv, len+2, OBEX_FL_FIT_ONE_PACKET); }
static PyObject * BSTR_set(void *ptr, PyObject *value, Py_ssize_t size) { BSTR bstr; /* convert value into a PyUnicodeObject or NULL */ if (Py_None == value) { value = NULL; } else if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, _ctypes_conversion_encoding, _ctypes_conversion_errors); if (!value) return NULL; } else if (PyUnicode_Check(value)) { Py_INCREF(value); /* for the descref below */ } else { PyErr_Format(PyExc_TypeError, "unicode string expected instead of %s instance", value->ob_type->tp_name); return NULL; } /* create a BSTR from value */ if (value) { Py_ssize_t size = PyUnicode_GET_SIZE(value); if ((unsigned) size != size) { PyErr_SetString(PyExc_ValueError, "String too long for BSTR"); return NULL; } bstr = SysAllocStringLen(PyUnicode_AS_UNICODE(value), (unsigned)size); Py_DECREF(value); } else bstr = NULL; /* free the previous contents, if any */ if (*(BSTR *)ptr) SysFreeString(*(BSTR *)ptr); /* and store it */ *(BSTR *)ptr = bstr; /* We don't need to keep any other object */ _RET(value); }
BOOL PyWinObject_AsPfnAllocatedWCHAR(PyObject *stringObject, void *(*pfnAllocator)(ULONG), WCHAR **ppResult, BOOL bNoneOK /*= FALSE*/,DWORD *pResultLen /*= NULL*/) { BOOL rc = TRUE; if (PyString_Check(stringObject)) { int cch=PyString_Size(stringObject); const char *buf = PyString_AsString(stringObject); if (buf==NULL) return FALSE; /* We assume that we dont need more 'wide characters' for the result then the number of bytes in the input. Often we will need less, as the input may contain multi-byte chars, but we should never need more */ *ppResult = (LPWSTR)(*pfnAllocator)((cch+1)*sizeof(WCHAR)); if (*ppResult) /* convert and get the final character size */ cch = MultiByteToWideChar(CP_ACP, 0, buf, cch+1, *ppResult, cch+1); if (*ppResult && pResultLen) *pResultLen = cch; } else if (PyUnicode_Check(stringObject)) { // copy the value, including embedded NULLs WCHAR *v = (WCHAR *)PyUnicode_AS_UNICODE(stringObject); UINT cch = PyUnicode_GET_SIZE(stringObject); *ppResult = (WCHAR *)pfnAllocator((cch+1) * sizeof(WCHAR)); if (*ppResult) memcpy(*ppResult, v, (cch+1) * sizeof(WCHAR)); if (*ppResult && pResultLen) *pResultLen = cch; } else if (stringObject == Py_None) { if (bNoneOK) { *ppResult = NULL; } else { PyErr_SetString(PyExc_TypeError, "None is not a valid string in this context"); rc = FALSE; } } else { const char *tp_name = stringObject && stringObject->ob_type ? stringObject->ob_type->tp_name : "<NULL!!>"; PyErr_Format(PyExc_TypeError, "Objects of type '%s' can not be converted to Unicode.", tp_name); rc = FALSE; } if (rc && !ppResult) { PyErr_SetString(PyExc_MemoryError, "Allocating WCHAR"); return FALSE; } return rc; }
static bool GetUnicodeInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInfo& info) { Py_UNICODE* pch = PyUnicode_AsUnicode(param); Py_ssize_t len = PyUnicode_GET_SIZE(param); info.ValueType = SQL_C_WCHAR; info.ColumnSize = (SQLUINTEGER)max(len, 1); if (len <= cur->cnxn->wvarchar_maxlength) { if (SQLWCHAR_SIZE == Py_UNICODE_SIZE) { info.ParameterValuePtr = pch; } else { // SQLWCHAR and Py_UNICODE are not the same size, so we need to allocate and copy a buffer. if (len > 0) { info.ParameterValuePtr = SQLWCHAR_FromUnicode(pch, len); if (info.ParameterValuePtr == 0) return false; info.allocated = true; } else { info.ParameterValuePtr = pch; } } info.ParameterType = SQL_WVARCHAR; info.StrLen_or_Ind = (SQLINTEGER)(len * sizeof(SQLWCHAR)); } else { // Too long to pass all at once, so we'll provide the data at execute. info.ParameterType = SQL_WLONGVARCHAR; info.StrLen_or_Ind = cur->cnxn->need_long_data_len ? SQL_LEN_DATA_AT_EXEC((SQLLEN)len * sizeof(SQLWCHAR)) : SQL_DATA_AT_EXEC; info.ParameterValuePtr = param; } return true; }