NUITKA_MAY_BE_UNUSED static bool UNICODE_ADD_INCREMENTAL(PyObject **operand1, PyObject *operand2) { Py_ssize_t operand2_size = PyUnicode_GET_SIZE(operand2); if (operand2_size == 0) return true; #if PYTHON_VERSION < 300 Py_ssize_t operand1_size = PyUnicode_GET_SIZE(*operand1); 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; } memcpy(PyUnicode_AS_UNICODE(*operand1) + operand1_size, PyUnicode_AS_UNICODE(operand2), operand2_size * sizeof(Py_UNICODE)); return true; #else assert(!PyUnicode_CHECK_INTERNED(*operand1)); return UNICODE_APPEND(operand1, operand2); #endif }
static PyObject *NormalizeSpace(PyObject *str) { Py_UNICODE *start, *end, *p; PyObject *result; if (!PyUnicode_CheckExact(str)) { PyErr_SetString(PyExc_TypeError, "unicode object expected"); return NULL; } start = PyUnicode_AS_UNICODE(str); end = start + PyUnicode_GET_SIZE(str); /* look for runs of contiguous whitespace */ for (p = start; p < end; p++) { if (IS_XMLSPACE(p[0]) && IS_XMLSPACE(p[1])) break; } if (p == end && !IS_XMLSPACE(start[0]) && !IS_XMLSPACE(end[-1])) { /* no whitespace to remove; return original string */ Py_INCREF(str); return str; } /* skip over leading whitespace */ while (start < end && IS_XMLSPACE(start[0])) start++; /* skip over trailing whitespace */ while (end > start && IS_XMLSPACE(end[-1])) end--; /* create the result string (it may be shrunk later) */ result = PyUnicode_FromUnicode(start, (Py_ssize_t)(end - start)); if (result) { start = PyUnicode_AS_UNICODE(result); end = start + PyUnicode_GET_SIZE(result); for (p = start; p < end; p++) { if (IS_XMLSPACE(*p)) { /* replace contiguous whitespace with a single space */ *start++ = 0x20; p++; while (p < end && IS_XMLSPACE(*p)) p++; } *start++ = *p; } if (start != end) { Py_ssize_t newsize = (Py_ssize_t)(start - PyUnicode_AS_UNICODE(result)); if (PyUnicode_Resize(&result, newsize) < 0) { Py_DECREF(result); return NULL; } } } return result; }
PyObject* DetachValue() { // At this point, Trim should have been called by PostRead. if (bytesUsed == SQL_NULL_DATA || buffer == 0) Py_RETURN_NONE; if (usingStack) { if (dataType == SQL_C_CHAR || dataType == SQL_C_BINARY) return PyString_FromStringAndSize(buffer, bytesUsed); if (sizeof(wchar_t) == Py_UNICODE_SIZE) return PyUnicode_FromUnicode((const Py_UNICODE*)buffer, bytesUsed / element_size); return PyUnicode_FromWideChar((const wchar_t*)buffer, bytesUsed / element_size); } if (PyString_CheckExact(bufferOwner)) { if (_PyString_Resize(&bufferOwner, bytesUsed) == -1) return 0; PyObject* tmp = bufferOwner; bufferOwner = 0; buffer = 0; return tmp; } if (PyUnicode_CheckExact(bufferOwner)) { if (PyUnicode_Resize(&bufferOwner, bytesUsed / element_size) == -1) return 0; PyObject* tmp = bufferOwner; bufferOwner = 0; buffer = 0; return tmp; } // We have allocated our own wchar_t buffer and must now copy it to a Unicode object. PyObject* result = PyUnicode_FromWideChar((const wchar_t*)buffer, bytesUsed / element_size); if (result == 0) return false; free(buffer); buffer = 0; return result; }
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* getenvironment(PyObject* environment) { int i; Py_ssize_t envsize; PyObject* out = NULL; PyObject* keys; PyObject* values; Py_UNICODE* p; /* convert environment dictionary to windows enviroment string */ if (! PyMapping_Check(environment)) { PyErr_SetString( PyExc_TypeError, "environment must be dictionary or None"); return NULL; } envsize = PyMapping_Length(environment); keys = PyMapping_Keys(environment); values = PyMapping_Values(environment); if (!keys || !values) goto error; out = PyUnicode_FromUnicode(NULL, 2048); if (! out) goto error; p = PyUnicode_AS_UNICODE(out); for (i = 0; i < envsize; i++) { Py_ssize_t ksize, vsize, totalsize; PyObject* key = PyList_GET_ITEM(keys, i); PyObject* value = PyList_GET_ITEM(values, i); if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { PyErr_SetString(PyExc_TypeError, "environment can only contain strings"); goto error; } ksize = PyUnicode_GET_SIZE(key); vsize = PyUnicode_GET_SIZE(value); totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 + vsize + 1 + 1; if (totalsize > PyUnicode_GET_SIZE(out)) { Py_ssize_t offset = p - PyUnicode_AS_UNICODE(out); PyUnicode_Resize(&out, totalsize + 1024); p = PyUnicode_AS_UNICODE(out) + offset; } Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize); p += ksize; *p++ = '='; Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize); p += vsize; *p++ = '\0'; } /* add trailing null byte */ *p++ = '\0'; PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out)); /* PyObject_Print(out, stdout, 0); */ Py_XDECREF(keys); Py_XDECREF(values); return out; error: Py_XDECREF(out); Py_XDECREF(keys); Py_XDECREF(values); return NULL; }
PyObject * PyFile_GetLine(PyObject *f, int n) { PyObject *result; if (f == NULL) { PyErr_BadInternalCall(); return NULL; } { PyObject *reader; PyObject *args; reader = PyObject_GetAttrString(f, "readline"); if (reader == NULL) return NULL; if (n <= 0) args = PyTuple_New(0); else args = Py_BuildValue("(i)", n); if (args == NULL) { Py_DECREF(reader); return NULL; } result = PyEval_CallObject(reader, args); Py_DECREF(reader); Py_DECREF(args); if (result != NULL && !PyBytes_Check(result) && !PyUnicode_Check(result)) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_TypeError, "object.readline() returned non-string"); } } if (n < 0 && result != NULL && PyBytes_Check(result)) { char *s = PyBytes_AS_STRING(result); Py_ssize_t len = PyBytes_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (s[len-1] == '\n') { if (result->ob_refcnt == 1) _PyBytes_Resize(&result, len-1); else { PyObject *v; v = PyBytes_FromStringAndSize(s, len-1); Py_DECREF(result); result = v; } } } if (n < 0 && result != NULL && PyUnicode_Check(result)) { Py_UNICODE *s = PyUnicode_AS_UNICODE(result); Py_ssize_t len = PyUnicode_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (s[len-1] == '\n') { if (result->ob_refcnt == 1) PyUnicode_Resize(&result, len-1); else { PyObject *v; v = PyUnicode_FromUnicode(s, len-1); Py_DECREF(result); result = v; } } } return result; }
PyObject* DetachValue() { // At this point, Trim should have been called by PostRead. if (bytesUsed == SQL_NULL_DATA || buffer == 0) Py_RETURN_NONE; if (usingStack) { if (dataType == SQL_C_CHAR) return PyBytes_FromStringAndSize(buffer, bytesUsed); if (dataType == SQL_C_BINARY) { #if PY_VERSION_HEX >= 0x02060000 return PyByteArray_FromStringAndSize(buffer, bytesUsed); #else return PyBytes_FromStringAndSize(buffer, bytesUsed); #endif } if (sizeof(SQLWCHAR) == Py_UNICODE_SIZE) return PyUnicode_FromUnicode((const Py_UNICODE*)buffer, bytesUsed / element_size); return PyUnicode_FromSQLWCHAR((const SQLWCHAR*)buffer, bytesUsed / element_size); } if (bufferOwner && PyUnicode_CheckExact(bufferOwner)) { if (PyUnicode_Resize(&bufferOwner, bytesUsed / element_size) == -1) return 0; PyObject* tmp = bufferOwner; bufferOwner = 0; buffer = 0; return tmp; } if (bufferOwner && PyBytes_CheckExact(bufferOwner)) { if (_PyBytes_Resize(&bufferOwner, bytesUsed) == -1) return 0; PyObject* tmp = bufferOwner; bufferOwner = 0; buffer = 0; return tmp; } #if PY_VERSION_HEX >= 0x02060000 if (bufferOwner && PyByteArray_CheckExact(bufferOwner)) { if (PyByteArray_Resize(bufferOwner, bytesUsed) == -1) return 0; PyObject* tmp = bufferOwner; bufferOwner = 0; buffer = 0; return tmp; } #endif // We have allocated our own SQLWCHAR buffer and must now copy it to a Unicode object. I(bufferOwner == 0); PyObject* result = PyUnicode_FromSQLWCHAR((const SQLWCHAR*)buffer, bytesUsed / element_size); if (result == 0) return false; pyodbc_free(buffer); buffer = 0; return result; }
bool AllocateMore(SQLLEN cbAdd) { // cbAdd // The number of bytes (cb --> count of bytes) to add. if (cbAdd == 0) return true; SQLLEN newSize = bufferSize + cbAdd; if (usingStack) { // This is the first call and `buffer` points to stack memory. Allocate a new object and copy the stack // data into it. char* stackBuffer = buffer; if (dataType == SQL_C_CHAR) { bufferOwner = PyBytes_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyBytes_AS_STRING(bufferOwner) : 0; } else if (dataType == SQL_C_BINARY) { #if PY_VERSION_HEX >= 0x02060000 bufferOwner = PyByteArray_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyByteArray_AS_STRING(bufferOwner) : 0; #else bufferOwner = PyBytes_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyBytes_AS_STRING(bufferOwner) : 0; #endif } else if (sizeof(SQLWCHAR) == Py_UNICODE_SIZE) { // Allocate directly into a Unicode object. bufferOwner = PyUnicode_FromUnicode(0, newSize / element_size); buffer = bufferOwner ? (char*)PyUnicode_AsUnicode(bufferOwner) : 0; } else { // We're Unicode, but SQLWCHAR and Py_UNICODE don't match, so maintain our own SQLWCHAR buffer. bufferOwner = 0; buffer = (char*)pyodbc_malloc((size_t)newSize); } if (buffer == 0) return false; usingStack = false; memcpy(buffer, stackBuffer, (size_t)bufferSize); bufferSize = newSize; return true; } if (bufferOwner && PyUnicode_CheckExact(bufferOwner)) { if (PyUnicode_Resize(&bufferOwner, newSize / element_size) == -1) return false; buffer = (char*)PyUnicode_AsUnicode(bufferOwner); } #if PY_VERSION_HEX >= 0x02060000 else if (bufferOwner && PyByteArray_CheckExact(bufferOwner)) { if (PyByteArray_Resize(bufferOwner, newSize) == -1) return false; buffer = PyByteArray_AS_STRING(bufferOwner); } #else else if (bufferOwner && PyBytes_CheckExact(bufferOwner)) { if (_PyBytes_Resize(&bufferOwner, newSize) == -1) return false; buffer = PyBytes_AS_STRING(bufferOwner); } #endif else { char* tmp = (char*)realloc(buffer, (size_t)newSize); if (tmp == 0) return false; buffer = tmp; } bufferSize = newSize; return true; }
bool AllocateMore(SQLLEN cbAdd) { if (cbAdd == 0) return true; SQLLEN newSize = bufferSize + cbAdd; if (usingStack) { // This is the first call and `buffer` points to stack memory. Allocate a new object and copy the stack // data into it. char* stackBuffer = buffer; if (dataType == SQL_C_CHAR || dataType == SQL_C_BINARY) { bufferOwner = PyString_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyString_AS_STRING(bufferOwner) : 0; } else if (sizeof(wchar_t) == Py_UNICODE_SIZE) { // Allocate directly into a Unicode object. bufferOwner = PyUnicode_FromUnicode(0, newSize / element_size); buffer = bufferOwner ? (char*)PyUnicode_AsUnicode(bufferOwner) : 0; } else { // We're Unicode, but wchar_t and Py_UNICODE don't match, so maintain our own wchar_t buffer. buffer = (char*)malloc(newSize); } usingStack = false; if (buffer == 0) return false; memcpy(buffer, stackBuffer, bufferSize); bufferSize = newSize; return true; } if (PyString_CheckExact(bufferOwner)) { if (_PyString_Resize(&bufferOwner, newSize) == -1) return false; buffer = PyString_AS_STRING(bufferOwner); } else if (PyUnicode_CheckExact(bufferOwner)) { if (PyUnicode_Resize(&bufferOwner, newSize / element_size) == -1) return false; buffer = (char*)PyUnicode_AsUnicode(bufferOwner); } else { char* tmp = (char*)realloc(buffer, newSize); if (tmp == 0) return false; buffer = tmp; } bufferSize = newSize; return true; }