static PyObject * cups_modelSort (PyObject *self, PyObject *args) { PyObject *Oa, *Ob, *a, *b; int len_a, len_b; size_t size_a, size_b; wchar_t *wca, *wcb; if (!PyArg_ParseTuple (args, "OO", &Oa, &Ob)) return NULL; a = PyUnicode_FromObject (Oa); b = PyUnicode_FromObject (Ob); if (a == NULL || b == NULL || !PyUnicode_Check (a) || !PyUnicode_Check (b)) { if (a) { Py_DECREF (a); } if (b) { Py_DECREF (b); } PyErr_SetString (PyExc_TypeError, "Unable to convert to Unicode"); return NULL; } len_a = 1 + PyUnicode_GetSize (a); size_a = len_a * sizeof (wchar_t); if ((size_a / sizeof (wchar_t)) != len_a) { Py_DECREF (a); Py_DECREF (b); PyErr_SetString (PyExc_RuntimeError, "String too long"); return NULL; } len_b = 1 + PyUnicode_GetSize (b); size_b = len_b * sizeof (wchar_t); if ((size_b / sizeof (wchar_t)) != len_b) { Py_DECREF (a); Py_DECREF (b); PyErr_SetString (PyExc_RuntimeError, "String too long"); return NULL; } wca = malloc (size_a); wcb = malloc (size_b); if (wca == NULL || wcb == NULL) { Py_DECREF (a); Py_DECREF (b); free (wca); free (wcb); PyErr_SetString (PyExc_RuntimeError, "Insufficient memory"); return NULL; } PyUnicode_AsWideChar ((PyUnicodeObject *) a, wca, size_a); PyUnicode_AsWideChar ((PyUnicodeObject *) b, wcb, size_b); Py_DECREF (a); Py_DECREF (b); return Py_BuildValue ("i", do_model_compare (wca, wcb)); }
static PyObject *string_strip(PyObject * self, PyObject *args) { PyObject *_argc0, *result; /*Unicode objects*/ PyUnicodeObject *unistr; if (!PyArg_ParseTuple(args,"O:XmlStrStrip",&_argc0)) return NULL; unistr = (PyUnicodeObject *)PyUnicode_FromObject(_argc0); if (!unistr) { if (PyErr_ExceptionMatches(PyExc_TypeError)) PyErr_Format(PyExc_TypeError, "argument must be unicode or string, %.80s found.", _argc0->ob_type->tp_name); return NULL; } result = XmlStrip(unistr, 1, 1); /*PyUnicode_FromObject always adds one REF so do a DECREF on it*/ Py_DECREF(unistr); /*Has a REF of one now so we don't need to INCREF it*/ return result; }
JCharString JPyString::asJCharString(PyObject* obj) { PyObject* torelease = NULL; TRACE_IN("JPyString::asJCharString"); if (PyBytes_Check(obj)) { PY_CHECK( obj = PyUnicode_FromObject(obj) ); torelease = obj; } Py_UNICODE* val = PyUnicode_AS_UNICODE(obj); Py_ssize_t length = JPyObject::length(obj); JCharString res(length); for (int i = 0; val[i] != 0; i++) { res[i] = (jchar)val[i]; } if (torelease != NULL) { Py_DECREF(torelease); } return res; TRACE_OUT; }
static PyObject *NormalizeWord(Normalizer *self,PyObject *word) { int i; PyObject *temp; if (PyString_Check(word)) { if (! (temp = PyUnicode_FromEncodedObject(word,self->encoding,"strict"))) { PyErr_SetString(PyExc_UnicodeError,"unicode conversion failed"); return NULL; } } else { temp = PyUnicode_FromObject(word); } for (i=0; i<PyList_Size(self->table); i++) { PyObject *s, *item, *key, *value; item = PySequence_Fast_GET_ITEM(self->table, i); key = PyTuple_GetItem(item,0); value = PyTuple_GetItem(item,1); if (! (s = PyUnicode_Replace( temp, key, value, -1))) return NULL; Py_DECREF(temp); temp = s; } return temp; }
static PyObject* validate_unicode_promote( Member* member, PyObject* owner, PyObject* oldvalue, PyObject* newvalue ) { if( PyUnicode_Check( newvalue ) ) return newref( newvalue ); if( PyString_Check( newvalue ) ) return PyUnicode_FromObject( newvalue ); return validate_type_fail( member, owner, newvalue, "unicode" ); }
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; }
bool py_to_qstr(PyObject *o, QString &str) { PyObject *u=NULL; if (!PyUnicode_Check(o)) { u=PyUnicode_FromObject(o); if (!u) return false; o=u; } #ifdef Py_UNICODE_WIDE str=QString::fromUcs4(PyUnicode_AsUnicode(o), PyUnicode_GetSize(o)); #else str.setUtf16(PyUnicode_AsUnicode(o), PyUnicode_GetSize(o)); #endif Py_XDECREF(u); return true; }
static PyObject *append(PyObject *self, PyObject *data) { int fail; if(PyUnicode_Check(data)) { fail = add_to_data((ligolw_Tokenizer *) self, data); } else if(PyString_Check(data)) { if(!(data = PyUnicode_FromObject(data))) return NULL; fail = add_to_data((ligolw_Tokenizer *) self, data); Py_DECREF(data); } else { PyErr_SetObject(PyExc_TypeError, data); return NULL; } if(fail < 0) return PyErr_NoMemory(); Py_INCREF(self); return self; }
PyObject* psutil_get_open_files(long pid, HANDLE processHandle) { _NtQuerySystemInformation NtQuerySystemInformation = GetLibraryProcAddress("ntdll.dll", "NtQuerySystemInformation"); _NtDuplicateObject NtDuplicateObject = GetLibraryProcAddress("ntdll.dll", "NtDuplicateObject"); _NtQueryObject NtQueryObject = GetLibraryProcAddress("ntdll.dll", "NtQueryObject"); NTSTATUS status; PSYSTEM_HANDLE_INFORMATION handleInfo; ULONG handleInfoSize = 0x10000; ULONG i; ULONG fileNameLength; PyObject *filesList = Py_BuildValue("[]"); PyObject *arg = NULL; PyObject *fileFromWchar = NULL; if (filesList == NULL) return NULL; handleInfo = (PSYSTEM_HANDLE_INFORMATION)malloc(handleInfoSize); if (handleInfo == NULL) { Py_DECREF(filesList); PyErr_NoMemory(); return NULL; } /* NtQuerySystemInformation won't give us the correct buffer size, so we guess by doubling the buffer size. */ while ((status = NtQuerySystemInformation( SystemHandleInformation, handleInfo, handleInfoSize, NULL )) == STATUS_INFO_LENGTH_MISMATCH) { handleInfo = (PSYSTEM_HANDLE_INFORMATION)realloc(handleInfo, handleInfoSize *= 2); } /* NtQuerySystemInformation stopped giving us STATUS_INFO_LENGTH_MISMATCH. */ if (!NT_SUCCESS(status)) { //printf("NtQuerySystemInformation failed!\n"); Py_DECREF(filesList); free(handleInfo); return NULL; } for (i = 0; i < handleInfo->HandleCount; i++) { SYSTEM_HANDLE handle = handleInfo->Handles[i]; HANDLE dupHandle = NULL; POBJECT_TYPE_INFORMATION objectTypeInfo = NULL; PVOID objectNameInfo; UNICODE_STRING objectName; ULONG returnLength; fileFromWchar = NULL; arg = NULL; /* Check if this handle belongs to the PID the user specified. */ if (handle.ProcessId != pid) continue; /* Skip handles with the following access codes as the next call to NtDuplicateObject() or NtQueryObject() might hang forever. */ if((handle.GrantedAccess == 0x0012019f) || (handle.GrantedAccess == 0x001a019f) || (handle.GrantedAccess == 0x00120189) || (handle.GrantedAccess == 0x00100000)) { continue; } /* Duplicate the handle so we can query it. */ if (!NT_SUCCESS(NtDuplicateObject( processHandle, handle.Handle, GetCurrentProcess(), &dupHandle, 0, 0, 0 ))) { //printf("[%#x] Error!\n", handle.Handle); continue; } /* Query the object type. */ objectTypeInfo = (POBJECT_TYPE_INFORMATION)malloc(0x1000); if (!NT_SUCCESS(NtQueryObject( dupHandle, ObjectTypeInformation, objectTypeInfo, 0x1000, NULL ))) { //printf("[%#x] Error!\n", handle.Handle); free(objectTypeInfo); CloseHandle(dupHandle); continue; } objectNameInfo = malloc(0x1000); if (!NT_SUCCESS(NtQueryObject( dupHandle, ObjectNameInformation, objectNameInfo, 0x1000, &returnLength ))) { /* Reallocate the buffer and try again. */ objectNameInfo = realloc(objectNameInfo, returnLength); if (!NT_SUCCESS(NtQueryObject( dupHandle, ObjectNameInformation, objectNameInfo, returnLength, NULL ))) { /* We have the type name, so just display that.*/ /* printf( "[%#x] %.*S: (could not get name)\n", handle.Handle, objectTypeInfo->Name.Length / 2, objectTypeInfo->Name.Buffer ); */ free(objectTypeInfo); free(objectNameInfo); CloseHandle(dupHandle); continue; } } /* Cast our buffer into an UNICODE_STRING. */ objectName = *(PUNICODE_STRING)objectNameInfo; /* Print the information! */ if (objectName.Length) { /* The object has a name. Make sure it is a file otherwise ignore it */ fileNameLength = objectName.Length / 2; if (wcscmp(objectTypeInfo->Name.Buffer, L"File") == 0) { //printf("%.*S\n", objectName.Length / 2, objectName.Buffer); fileFromWchar = PyUnicode_FromWideChar(objectName.Buffer, fileNameLength); if (fileFromWchar == NULL) goto error_py_fun; #if PY_MAJOR_VERSION >= 3 arg = Py_BuildValue("N", PyUnicode_AsUTF8String(fileFromWchar)); #else arg = Py_BuildValue("N", PyUnicode_FromObject(fileFromWchar)); #endif if (!arg) goto error_py_fun; Py_XDECREF(fileFromWchar); fileFromWchar = NULL; if (PyList_Append(filesList, arg)) goto error_py_fun; Py_XDECREF(arg); } /* printf( "[%#x] %.*S: %.*S\n", handle.Handle, objectTypeInfo->Name.Length / 2, objectTypeInfo->Name.Buffer, objectName.Length / 2, objectName.Buffer ); */ } else { /* Print something else. */ /* printf( "[%#x] %.*S: (unnamed)\n", handle.Handle, objectTypeInfo->Name.Length / 2, objectTypeInfo->Name.Buffer ); */ ;; } free(objectTypeInfo); free(objectNameInfo); CloseHandle(dupHandle); } free(handleInfo); CloseHandle(processHandle); return filesList; error_py_fun: Py_XDECREF(arg); Py_XDECREF(fileFromWchar); Py_DECREF(filesList); return NULL; }
/* * returns a Python list representing the arguments for the process * with given pid or NULL on error. */ PyObject * psutil_get_arg_list(long pid) { int nArgs, i; LPWSTR *szArglist = NULL; HANDLE hProcess = NULL; PVOID pebAddress; PVOID rtlUserProcParamsAddress; UNICODE_STRING commandLine; WCHAR *commandLineContents = NULL; PyObject *arg = NULL; PyObject *arg_from_wchar = NULL; PyObject *argList = NULL; hProcess = psutil_handle_from_pid(pid); if (hProcess == NULL) { return NULL; } pebAddress = psutil_get_peb_address(hProcess); // get the address of ProcessParameters #ifdef _WIN64 if (!ReadProcessMemory(hProcess, (PCHAR)pebAddress + 32, &rtlUserProcParamsAddress, sizeof(PVOID), NULL)) #else if (!ReadProcessMemory(hProcess, (PCHAR)pebAddress + 0x10, &rtlUserProcParamsAddress, sizeof(PVOID), NULL)) #endif { ////printf("Could not read the address of ProcessParameters!\n"); PyErr_SetFromWindowsErr(0); goto error; } // read the CommandLine UNICODE_STRING structure #ifdef _WIN64 if (!ReadProcessMemory(hProcess, (PCHAR)rtlUserProcParamsAddress + 112, &commandLine, sizeof(commandLine), NULL)) #else if (!ReadProcessMemory(hProcess, (PCHAR)rtlUserProcParamsAddress + 0x40, &commandLine, sizeof(commandLine), NULL)) #endif { PyErr_SetFromWindowsErr(0); goto error; } // allocate memory to hold the command line commandLineContents = (WCHAR *)malloc(commandLine.Length + 1); if (commandLineContents == NULL) { PyErr_NoMemory(); goto error; } // read the command line if (!ReadProcessMemory(hProcess, commandLine.Buffer, commandLineContents, commandLine.Length, NULL)) { PyErr_SetFromWindowsErr(0); goto error; } // Null-terminate the string to prevent wcslen from returning // incorrect length the length specifier is in characters, but // commandLine.Length is in bytes. commandLineContents[(commandLine.Length / sizeof(WCHAR))] = '\0'; // attempt tp parse the command line using Win32 API, fall back // on string cmdline version otherwise szArglist = CommandLineToArgvW(commandLineContents, &nArgs); if (NULL == szArglist) { // failed to parse arglist // encode as a UTF8 Python string object from WCHAR string arg_from_wchar = PyUnicode_FromWideChar(commandLineContents, commandLine.Length / 2); if (arg_from_wchar == NULL) goto error; #if PY_MAJOR_VERSION >= 3 argList = Py_BuildValue("N", PyUnicode_AsUTF8String(arg_from_wchar)); #else argList = Py_BuildValue("N", PyUnicode_FromObject(arg_from_wchar)); #endif if (!argList) goto error; } else { // arglist parsed as array of UNICODE_STRING, so convert each to // Python string object and add to arg list argList = Py_BuildValue("[]"); if (argList == NULL) goto error; for (i = 0; i < nArgs; i++) { arg_from_wchar = NULL; arg = NULL; arg_from_wchar = PyUnicode_FromWideChar(szArglist[i], wcslen(szArglist[i])); if (arg_from_wchar == NULL) goto error; #if PY_MAJOR_VERSION >= 3 arg = PyUnicode_FromObject(arg_from_wchar); #else arg = PyUnicode_AsUTF8String(arg_from_wchar); #endif if (arg == NULL) goto error; Py_XDECREF(arg_from_wchar); if (PyList_Append(argList, arg)) goto error; Py_XDECREF(arg); } } if (szArglist != NULL) LocalFree(szArglist); free(commandLineContents); CloseHandle(hProcess); return argList; error: Py_XDECREF(arg); Py_XDECREF(arg_from_wchar); Py_XDECREF(argList); if (hProcess != NULL) CloseHandle(hProcess); if (commandLineContents != NULL) free(commandLineContents); if (szArglist != NULL) LocalFree(szArglist); return NULL; }
static PyObject* PyLocale_strcoll(PyObject* self, PyObject* args) { #if !defined(HAVE_WCSCOLL) || !defined(Py_USING_UNICODE) char *s1,*s2; if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2)) return NULL; return PyInt_FromLong(strcoll(s1, s2)); #else PyObject *os1, *os2, *result = NULL; wchar_t *ws1 = NULL, *ws2 = NULL; int rel1 = 0, rel2 = 0, len1, len2; if (!PyArg_ParseTuple(args, "OO:strcoll", &os1, &os2)) return NULL; /* If both arguments are byte strings, use strcoll. */ if (PyString_Check(os1) && PyString_Check(os2)) return PyInt_FromLong(strcoll(PyString_AS_STRING(os1), PyString_AS_STRING(os2))); /* If neither argument is unicode, it's an error. */ if (!PyUnicode_Check(os1) && !PyUnicode_Check(os2)) { PyErr_SetString(PyExc_ValueError, "strcoll arguments must be strings"); } /* Convert the non-unicode argument to unicode. */ if (!PyUnicode_Check(os1)) { os1 = PyUnicode_FromObject(os1); if (!os1) return NULL; rel1 = 1; } if (!PyUnicode_Check(os2)) { os2 = PyUnicode_FromObject(os2); if (!os2) { Py_DECREF(os1); return NULL; } rel2 = 1; } /* 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 = PyInt_FromLong(wcscoll(ws1, ws2)); done: /* Deallocate everything. */ if (ws1) PyMem_FREE(ws1); if (ws2) PyMem_FREE(ws2); if (rel1) { Py_DECREF(os1); } if (rel2) { Py_DECREF(os2); } return result; #endif }
static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs) { UNUSED(self); Object pConnectString = 0; int fAutoCommit = 0; int fAnsi = 0; // force ansi int fUnicodeResults = 0; long timeout = 0; Py_ssize_t size = args ? PyTuple_Size(args) : 0; if (size > 1) { PyErr_SetString(PyExc_TypeError, "function takes at most 1 non-keyword argument"); return 0; } if (size == 1) { 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"); pConnectString.Attach(PyUnicode_FromObject(PyTuple_GetItem(args, 0))); if (!pConnectString.IsValid()) return 0; } if (kwargs && PyDict_Size(kwargs) > 0) { Object partsdict(PyDict_New()); if (!partsdict.IsValid()) return 0; Object unicodeT; // used to temporarily hold Unicode objects if we have to convert values to unicode Py_ssize_t pos = 0; PyObject* key = 0; PyObject* value = 0; while (PyDict_Next(kwargs, &pos, &key, &value)) { // Note: key and value are *borrowed*. // Check for the two non-connection string keywords we accept. (If we get many more of these, create something // table driven. Are we sure there isn't a Python function to parse keywords but leave those it doesn't know?) const char* szKey = PyString_AsString(key); if (_strcmpi(szKey, "autocommit") == 0) { fAutoCommit = PyObject_IsTrue(value); continue; } if (_strcmpi(szKey, "ansi") == 0) { fAnsi = PyObject_IsTrue(value); continue; } if (_strcmpi(szKey, "unicode_results") == 0) { fUnicodeResults = PyObject_IsTrue(value); continue; } if (_strcmpi(szKey, "timeout") == 0) { timeout = PyInt_AsLong(value); if (PyErr_Occurred()) return 0; continue; } // Anything else must be a string that is appended, along with the keyword to the connection string. if (!(PyString_Check(value) || PyUnicode_Check(value))) return PyErr_Format(PyExc_TypeError, "'%s' is not a string or unicode value'", szKey); // Map DB API recommended names to ODBC names (e.g. user --> uid). for (size_t i = 0; i < _countof(keywordmaps); i++) { if (_strcmpi(szKey, keywordmaps[i].oldname) == 0) { if (keywordmaps[i].newnameObject == 0) { keywordmaps[i].newnameObject = PyString_FromString(keywordmaps[i].newname); if (keywordmaps[i].newnameObject == 0) return 0; } key = keywordmaps[i].newnameObject; break; } } if (PyString_Check(value)) { unicodeT.Attach(PyUnicode_FromObject(value)); if (!unicodeT.IsValid()) return 0; value = unicodeT.Get(); } if (PyDict_SetItem(partsdict.Get(), key, value) == -1) return 0; unicodeT.Detach(); } if (PyDict_Size(partsdict.Get())) pConnectString.Attach(MakeConnectionString(pConnectString.Get(), partsdict)); } if (!pConnectString.IsValid()) return PyErr_Format(PyExc_TypeError, "no connection information was passed"); if (henv == SQL_NULL_HANDLE) { if (!AllocateEnv()) return 0; } return (PyObject*)Connection_New(pConnectString.Get(), fAutoCommit != 0, fAnsi != 0, fUnicodeResults != 0, timeout); }
static PyObject *py_normalize_token(PyObject *self, PyObject *args) { PyObject *s; uint32_t offset; uint32_t len; uint16_t type; uint64_t options; if (!PyArg_ParseTuple(args, "O(IIH)K:normalize", &s, &offset, &len, &type, &options)) { PyErr_SetString(PyExc_TypeError, "Error parsing arguments"); return 0; } token_t token = (token_t){(size_t)offset, (size_t)len, type}; PyObject *unistr = PyUnicode_FromObject(s); if (unistr == NULL) { PyErr_SetString(PyExc_TypeError, "Parameter could not be converted to unicode in scanner"); return 0; } #ifdef IS_PY3K // Python 3 encoding, supported by Python 3.3+ char *input = PyUnicode_AsUTF8(unistr); #else // Python 2 encoding PyObject *str = PyUnicode_AsEncodedString(unistr, "utf-8", "strict"); if (str == NULL) { PyErr_SetString(PyExc_ValueError, "Parameter could not be utf-8 encoded"); goto exit_decref_unistr; } char *input = PyBytes_AsString(str); #endif if (input == NULL) { goto exit_decref_str; } char_array *token_buffer = char_array_new_size(token.len); add_normalized_token(token_buffer, input, token, options); char *token_str = char_array_get_string(token_buffer); PyObject *result = PyUnicode_DecodeUTF8((const char *)token_str, token_buffer->n - 1, "strict"); if (result == NULL) { PyErr_SetString(PyExc_ValueError, "Error decoding token"); char_array_destroy(token_buffer); goto exit_decref_str; } char_array_destroy(token_buffer); #ifndef IS_PY3K Py_XDECREF(str); #endif Py_XDECREF(unistr); return result; exit_decref_str: #ifndef IS_PY3K Py_XDECREF(str); #endif exit_decref_unistr: Py_XDECREF(unistr); return 0; }
PyObject * init_marshal(void) { // init some stuff. // note that if any of this fails, we're probably horribly screwed. PyObject *m; int i, nl[] = {TYPE_TUPLE, TYPE_DICT, TYPE_LIST, TYPE_STRINGL, TYPE_STRINGR, TYPE_UNICODE, TYPE_GLOBAL, TYPE_STREAM, TYPE_UTF8, TYPE_LONG, TYPE_REF, TYPE_BLUE, TYPE_BUFFER, 0}; PycString_IMPORT; m = Py_InitModule("_blue.marshal", marshal_methods); if(!m) return NULL; Py_INCREF(m); for(i=0; nl[i]; i++) needlength[nl[i]] = 1; // populate constants table constants[TYPE_NONE] = (PyObject *)Py_None; constants[TYPE_TRUE] = (PyObject *)Py_True; constants[TYPE_FALSE] = (PyObject *)Py_False; constants[TYPE_MINUSONE] = (PyObject *)PyInt_FromLong(-1); constants[TYPE_ONE] = (PyObject *)PyInt_FromLong(1); constants[TYPE_ZERO] = (PyObject *)PyInt_FromLong(0); constants[TYPE_FLOAT0] = (PyObject *)PyFloat_FromDouble(0.0); constants[TYPE_STRING0] = (PyObject *)PyString_FromString(""); constants[TYPE_TUPLE0] = (PyObject *)PyTuple_New(0); constants[TYPE_UNICODE0] = (PyObject *)PyUnicode_FromObject(constants[TYPE_STRING0]); if(!(py__new__ = PyString_FromString("__new__"))) goto fail; if(!(py__setstate__ = PyString_FromString("__setstate__"))) goto fail; if(!(py__dict__ = PyString_FromString("__dict__"))) goto fail; if(!(pyappend = PyString_FromString("append"))) goto fail; if(!(global_cache = PyDict_New())) goto fail; if(!(string_table = PyList_New(0))) goto fail; if(!(UnmarshalError = PyErr_NewException("reverence.blue.marshal.UnmarshalError", NULL, NULL))) goto fail; PyModule_AddObject(m, "_stringtable", (PyObject*)string_table); PyModule_AddObject(m, "UnmarshalError", UnmarshalError); #if MARSHAL_DEBUG tokenname[TYPE_NONE] = "NONE"; tokenname[TYPE_GLOBAL] = "GLOBAL"; tokenname[TYPE_INT64] = "INT64"; tokenname[TYPE_INT32] = "INT32"; tokenname[TYPE_INT16] = "INT16"; tokenname[TYPE_INT8] = "INT8"; tokenname[TYPE_MINUSONE] = "MINUSONE"; tokenname[TYPE_ZERO] = "ZERO"; tokenname[TYPE_ONE] = "ONE"; tokenname[TYPE_FLOAT] = "FLOAT"; tokenname[TYPE_FLOAT0] = "FLOAT0"; tokenname[TYPE_STRINGL] = "STRINGL"; tokenname[TYPE_STRING0] = "STRING0"; tokenname[TYPE_STRING1] = "STRING1"; tokenname[TYPE_STRING] = "STRING"; tokenname[TYPE_STRINGR] = "STRINGR"; tokenname[TYPE_UNICODE] = "UNICODE"; tokenname[TYPE_BUFFER] = "BUFFER"; tokenname[TYPE_TUPLE] = "TUPLE"; tokenname[TYPE_LIST] = "LIST"; tokenname[TYPE_DICT] = "DICT"; tokenname[TYPE_INSTANCE] = "INSTANCE"; tokenname[TYPE_BLUE] = "BLUE"; tokenname[TYPE_REF] = "REF"; tokenname[TYPE_CHECKSUM] = "CHECKSUM"; tokenname[TYPE_TRUE] = "TRUE"; tokenname[TYPE_FALSE] = "FALSE"; tokenname[TYPE_PICKLER] = "PICKLER"; tokenname[TYPE_REDUCE] = "REDUCE"; tokenname[TYPE_NEWOBJ] = "NEWOBJ"; tokenname[TYPE_TUPLE0] = "TUPLE0"; tokenname[TYPE_TUPLE1] = "TUPLE1"; tokenname[TYPE_LIST0] = "LIST0"; tokenname[TYPE_LIST1] = "LIST1"; tokenname[TYPE_UNICODE0] = "UNICODE0"; tokenname[TYPE_UNICODE1] = "UNICODE1"; tokenname[TYPE_DBROW] = "DBROW"; tokenname[TYPE_STREAM] = "STREAM"; tokenname[TYPE_TUPLE2] = "TUPLE2"; tokenname[TYPE_MARK] = "MARK"; tokenname[TYPE_UTF8] = "UTF8"; tokenname[TYPE_LONG] = "LONG"; tokenname[TYPE_LIST_ITERATOR] = "LIST_ITERATOR"; tokenname[TYPE_DICT_ITERATOR] = "DICT_ITERATOR"; #endif // MARSHAL_DEBUG return m; fail: Py_XDECREF(py__new__); Py_XDECREF(py__setstate__); Py_XDECREF(py__dict__); Py_XDECREF(pyappend); Py_XDECREF(global_cache); Py_XDECREF(string_table); Py_XDECREF(UnmarshalError); return 0; }
static PyObject *py_normalize_string_latin(PyObject *self, PyObject *args) { PyObject *arg1; uint64_t options; if (!PyArg_ParseTuple(args, "OK:normalize", &arg1, &options)) { return 0; } PyObject *unistr = PyUnicode_FromObject(arg1); if (unistr == NULL) { PyErr_SetString(PyExc_TypeError, "Parameter could not be converted to unicode in scanner"); return 0; } #ifdef IS_PY3K // Python 3 encoding, supported by Python 3.3+ char *input = PyUnicode_AsUTF8(unistr); #else // Python 2 encoding PyObject *str = PyUnicode_AsEncodedString(unistr, "utf-8", "strict"); if (str == NULL) { PyErr_SetString(PyExc_TypeError, "Parameter could not be utf-8 encoded"); goto exit_decref_unistr; } char *input = PyBytes_AsString(str); #endif if (input == NULL) { goto exit_decref_str; } char *normalized = normalize_string_latin(input, strlen(input), options); PyObject *result = PyUnicode_DecodeUTF8((const char *)normalized, strlen(normalized), "strict"); free(normalized); if (result == NULL) { PyErr_SetString(PyExc_ValueError, "Result could not be utf-8 decoded"); goto exit_decref_str; } #ifndef IS_PY3K Py_XDECREF(str); #endif Py_XDECREF(unistr); return result; exit_decref_str: #ifndef IS_PY3K Py_XDECREF(str); #endif exit_decref_unistr: Py_XDECREF(unistr); return 0; }
static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs) { UNUSED(self); Object pConnectString; int fAutoCommit = 0; int fAnsi = 0; // force ansi int fReadOnly = 0; long timeout = 0; Object encoding; Object attrs_before; // Optional connect attrs set before connecting Py_ssize_t size = args ? PyTuple_Size(args) : 0; if (size > 1) { PyErr_SetString(PyExc_TypeError, "function takes at most 1 non-keyword argument"); return 0; } if (size == 1) { 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"); pConnectString.Attach(PyUnicode_FromObject(PyTuple_GetItem(args, 0))); if (!pConnectString.IsValid()) return 0; } if (kwargs && PyDict_Size(kwargs) > 0) { Object partsdict(PyDict_New()); if (!partsdict.IsValid()) return 0; Py_ssize_t pos = 0; PyObject* key = 0; PyObject* value = 0; Object okey; // in case we need to allocate a new key while (PyDict_Next(kwargs, &pos, &key, &value)) { if (!Text_Check(key)) return PyErr_Format(PyExc_TypeError, "Dictionary keys passed to connect must be strings"); // // Note: key and value are *borrowed*. // // // Check for the two non-connection string keywords we accept. (If we get many more of these, create something // // table driven. Are we sure there isn't a Python function to parse keywords but leave those it doesn't know?) // const char* szKey = PyString_AsString(key); if (Text_EqualsI(key, "autocommit")) { fAutoCommit = PyObject_IsTrue(value); continue; } if (Text_EqualsI(key, "ansi")) { fAnsi = PyObject_IsTrue(value); continue; } if (Text_EqualsI(key, "timeout")) { timeout = PyInt_AsLong(value); if (PyErr_Occurred()) return 0; continue; } if (Text_EqualsI(key, "readonly")) { fReadOnly = PyObject_IsTrue(value); continue; } if (Text_EqualsI(key, "attrs_before")) { attrs_before = _CheckAttrsDict(value); if (PyErr_Occurred()) return 0; continue; } if (Text_EqualsI(key, "encoding")) { #if PY_MAJOR_VERSION < 3 if (!PyString_Check(value) || !PyUnicode_Check(value)) return PyErr_Format(PyExc_TypeError, "encoding must be a string or unicode object"); #else if (!PyUnicode_Check(value)) return PyErr_Format(PyExc_TypeError, "encoding must be a string"); #endif encoding = value; continue; } // Map DB API recommended names to ODBC names (e.g. user --> uid). for (size_t i = 0; i < _countof(keywordmaps); i++) { if (Text_EqualsI(key, keywordmaps[i].oldname)) { if (keywordmaps[i].newnameObject == 0) { keywordmaps[i].newnameObject = PyString_FromString(keywordmaps[i].newname); if (keywordmaps[i].newnameObject == 0) return 0; } key = keywordmaps[i].newnameObject; break; } } PyObject* str = PyObject_Str(value); // convert if necessary if (!str) return 0; if (PyDict_SetItem(partsdict.Get(), key, str) == -1) { Py_XDECREF(str); return 0; } Py_XDECREF(str); } if (PyDict_Size(partsdict.Get())) pConnectString.Attach(MakeConnectionString(pConnectString.Get(), partsdict)); } if (!pConnectString.IsValid()) return PyErr_Format(PyExc_TypeError, "no connection information was passed"); if (henv == SQL_NULL_HANDLE) { if (!AllocateEnv()) return 0; } return (PyObject*)Connection_New(pConnectString.Get(), fAutoCommit != 0, fAnsi != 0, timeout, fReadOnly != 0, attrs_before, encoding); }
static int Py_Layout_init(Py_Layout *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"face", "text", "load_flags", NULL}; PyObject *face_obj = NULL; Py_Face *face = NULL; PyObject *text_obj; int load_flags = FT_LOAD_DEFAULT; PyObject *text = NULL; PyObject *decoded_text = NULL; char *decoded_text_buf; Py_ssize_t decoded_text_size; int result = -1; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|i:Layout.__init__", kwlist, &Py_Face_Type, &face_obj, &text_obj, &load_flags)) { goto exit; } face = (Py_Face *)face_obj; if (face->x->charmap == NULL || face->x->charmap->encoding != FT_ENCODING_UNICODE) { PyErr_SetString( PyExc_ValueError, "The layout only supports Unicode character map"); goto exit; } text = PyUnicode_FromObject(text_obj); if (text == NULL) { goto exit; } decoded_text = PyUnicode_AsUTF32String(text); if (decoded_text == NULL) { goto exit; } if (PyBytes_AsStringAndSize(decoded_text, &decoded_text_buf, &decoded_text_size)) { goto exit; } decoded_text_buf += 4; decoded_text_size = (decoded_text_size - 4) >> 2; if (ftpy_exc(ftpy_calculate_simple_layout( face->x, load_flags, (uint32_t *)decoded_text_buf, decoded_text_size, &self->x))) { goto exit; } Py_INCREF(face_obj); self->base.owner = face_obj; result = 0; exit: Py_XDECREF(text); Py_XDECREF(decoded_text); return result; }
int lightblueobex_addheaders(obex_t *obex, PyObject *headers, obex_object_t *obj) { uint8_t hi; obex_headerdata_t hv; PyObject *key, *value; Py_ssize_t pos = 0; int r = -1; DEBUG("%s()\n", __func__); if (headers == NULL || !PyDict_Check(headers)) { DEBUG("\taddheaders() arg must be dict\n"); return -1; } /* add connection-id first */ key = PyInt_FromLong(OBEX_HDR_CONNECTION); if (key != NULL) { value = PyDict_GetItem(headers, key); /* don't decref! */ Py_DECREF(key); key = NULL; if (value != NULL) { DEBUG("\tadding connection-id\n"); r = lightblueobex_add4byteheader(obex, obj, OBEX_HDR_CONNECTION, value); if (r < 0) { DEBUG("\terror adding connection-id header\n"); return -1; } } } /* add target header first (shouldn't have both conn-id and target) */ key = PyInt_FromLong(OBEX_HDR_TARGET); if (key != NULL) { value = PyDict_GetItem(headers, key); /* don't decref! */ Py_DECREF(key); key = NULL; if (value != NULL) { DEBUG("\tadding target\n"); r = lightblueobex_addbytestreamheader(obex, obj, OBEX_HDR_TARGET, value); if (r < 0) { DEBUG("\terror adding target header\n"); return -1; } } } while (PyDict_Next(headers, &pos, &key, &value)) { if (key == NULL || value == NULL) { DEBUG("\terror reading headers dict\n"); return -1; } if (!PyInt_Check(key)) { DEBUG("\theader id must be int, was %s\n", key->ob_type->tp_name); return -1; } hi = (uint8_t)PyInt_AsUnsignedLongMask(key); if (hi == OBEX_HDR_CONNECTION || hi == OBEX_HDR_TARGET) { /* these are already added */ continue; } DEBUG("\tadding header: 0x%02x\n", hi); switch (hi & OBEX_HI_MASK) { case OBEX_UNICODE: { PyObject *encoded = NULL; if (PyUnicode_Check(value)) { encoded = PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(value), PyUnicode_GET_SIZE(value), NULL, OBEX_BIG_ENDIAN); } else { /* try converting to unicode */ PyObject *tmp = NULL; tmp = PyUnicode_FromObject(value); if (tmp == NULL) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); /* let caller set exception */ } DEBUG("\tfailed to convert header value for id 0x%02x to unicode\n", hi); return -1; } encoded = PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(tmp), PyUnicode_GET_SIZE(tmp), NULL, OBEX_BIG_ENDIAN); Py_DECREF(tmp); } if (encoded == NULL) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); /* let caller set exception */ } DEBUG("\tfailed to encode value for header 0x%02x to UTF-16 big endian\n", hi); return -1; } r = lightblueobex_addunicodeheader(obex, obj, hi, encoded); Py_DECREF(encoded); break; } case OBEX_BYTE_STREAM: { r = lightblueobex_addbytestreamheader(obex, obj, hi, value); break; } case OBEX_BYTE: { long intvalue; if (!PyInt_Check(value)) { DEBUG("\theader value for id 0x%02x must be int, was %s\n", hi, value->ob_type->tp_name); return -1; } intvalue = PyInt_AsLong(value); if (PyErr_Occurred()) { DEBUG("\terror reading int value for 0x%02x\n", hi); PyErr_Clear(); return -1; } hv.bq1 = (uint8_t)intvalue; r = OBEX_ObjectAddHeader(obex, obj, hi, hv, 1, OBEX_FL_FIT_ONE_PACKET); break; } case OBEX_INT: { r = lightblueobex_add4byteheader(obex, obj, hi, value); break; } default: DEBUG("\tunknown header id encoding %d\n", (hi & OBEX_HI_MASK)); return -1; } if (r < 0) { DEBUG("\terror adding header 0x%02x\n", hi); return -1; } } return 1; }