PyObject * _pyccn_cmd_content_to_bytes(PyObject *UNUSED(self), PyObject *arg) { PyObject *str; if (arg == Py_None) Py_RETURN_NONE; else if (PyFloat_Check(arg) || PyLong_Check(arg) || _pyccn_Int_Check(arg)) { PyObject *py_o; py_o = PyObject_Str(arg); if (!py_o) return NULL; #if PY_MAJOR_VERSION >= 3 str = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(py_o), PyUnicode_GET_SIZE(py_o), NULL); Py_DECREF(py_o); #else str = py_o; #endif return str; } else if (PyUnicode_Check(arg)) return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(arg), PyUnicode_GET_SIZE(arg), NULL); return PyObject_Bytes(arg); }
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; }
static PyObject *Proxy_bytes( ProxyObject *self, PyObject *args) { Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self); return PyObject_Bytes(self->wrapped); }
int do_unpickle(ObjectRow_PyObject *self) { PyObject *result; if (!self->has_pickle) { PyErr_Format(PyExc_KeyError, "Attribute exists but row pickle is not available"); return 0; } struct module_state *mstate = GETSTATE_FROMTYPE(self); PyObject *pickle_str = PyObject_Bytes(PySequence_Fast_GET_ITEM(self->row, self->query_info->pickle_idx)); PyObject *args = Py_BuildValue("(O)", pickle_str); // Custom unpickler, assigned to the module by db.py when imported PyObject *dbunpickle = PyObject_GetAttrString(mstate->module, "dbunpickle"); result = PyEval_CallObject(dbunpickle, args); Py_DECREF(args); Py_DECREF(pickle_str); Py_DECREF(dbunpickle); if (!result) { self->has_pickle = 0; return 0; } Py_DECREF(self->pickle); self->pickle = result; self->unpickled = 1; return 1; }
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; }
/* * Convert a Python object to a PostgreSQL bytea datum. This doesn't * go through the generic conversion function to circumvent problems * with embedded nulls. And it's faster this way. */ static Datum PLyObject_ToBytea(PLyObToDatum *arg, PyObject *plrv, bool *isnull, bool inarray) { PyObject *volatile plrv_so = NULL; Datum rv; if (plrv == Py_None) { *isnull = true; return (Datum) 0; } *isnull = false; plrv_so = PyObject_Bytes(plrv); if (!plrv_so) PLy_elog(ERROR, "could not create bytes representation of Python object"); PG_TRY(); { char *plrv_sc = PyBytes_AsString(plrv_so); size_t len = PyBytes_Size(plrv_so); size_t size = len + VARHDRSZ; bytea *result = palloc(size); SET_VARSIZE(result, size); memcpy(VARDATA(result), plrv_sc, len); rv = PointerGetDatum(result); } PG_CATCH(); { Py_XDECREF(plrv_so); PG_RE_THROW(); } PG_END_TRY(); Py_XDECREF(plrv_so); return rv; }
/* * Convert a Python object to a PostgreSQL bytea datum. This doesn't * go through the generic conversion function to circumvent problems * with embedded nulls. And it's faster this way. */ static Datum PLyObject_ToBytea(PLyObToDatum *arg, int32 typmod, PyObject *plrv) { PyObject *volatile plrv_so = NULL; Datum rv; Assert(plrv != Py_None); plrv_so = PyObject_Bytes(plrv); if (!plrv_so) PLy_elog(ERROR, "could not create bytes representation of Python object"); PG_TRY(); { char *plrv_sc = PyBytes_AsString(plrv_so); size_t len = PyBytes_Size(plrv_so); size_t size = len + VARHDRSZ; bytea *result = palloc(size); SET_VARSIZE(result, size); memcpy(VARDATA(result), plrv_sc, len); rv = PointerGetDatum(result); } PG_CATCH(); { Py_XDECREF(plrv_so); PG_RE_THROW(); } PG_END_TRY(); Py_XDECREF(plrv_so); if (get_typtype(arg->typoid) == TYPTYPE_DOMAIN) domain_check(rv, false, arg->typoid, &arg->typfunc.fn_extra, arg->typfunc.fn_mcxt); return rv; }
/* * Returns a copy of the given string (8-bit or Unicode) with the XML * control characters converted to XML character entities. * * If an 8-bit string is passed in, an 8-bit string is returned. If a * Unicode string is passed in, a Unicode string is returned. */ static PyObject* _escape_xml(PyObject* self, PyObject *args, const char** escapes) { PyObject* input_obj; PyObject* input_coerce = NULL; PyObject* output_obj; int count = 0; Py_UNICODE* uinput = NULL; char* input = NULL; Py_ssize_t input_len; Py_UNICODE* uoutput = NULL; char* output = NULL; Py_UNICODE* up = NULL; char* p = NULL; Py_ssize_t i; const char** esc; const char* ent; if (!PyArg_ParseTuple(args, "O:escape_xml", &input_obj)) { return NULL; } /* First, try as Unicode */ if (!PyBytes_Check(input_obj)) { input_coerce = PyObject_Str(input_obj); } if (input_coerce) { uinput = PyUnicode_AsUnicode(input_coerce); if (uinput == NULL) { Py_DECREF(input_coerce); return NULL; } input_len = PyUnicode_GetSize(input_coerce); for (i = 0; i < input_len; ++i) { for (esc = escapes; ; esc += 2) { if (uinput[i] > (Py_UNICODE)**esc) { break; } else if (uinput[i] == (Py_UNICODE)**esc) { ++count; break; } } } if (count) { uoutput = malloc((input_len + 1 + count * 5) * sizeof(Py_UNICODE)); if (uoutput == NULL) { Py_DECREF(input_coerce); PyErr_SetString(PyExc_MemoryError, "Out of memory"); return NULL; } up = uoutput; for (i = 0; i < input_len; ++i) { for (esc = escapes; ; esc += 2) { if (uinput[i] > (Py_UNICODE)**esc) { *(up++) = uinput[i]; break; } else if (uinput[i] == (Py_UNICODE)**esc) { for (ent = *(esc + 1); *ent != '\0'; ++ent) { *(up++) = (Py_UNICODE)*ent; } break; } } } *up = 0; Py_DECREF(input_coerce); output_obj = PyUnicode_FromUnicode(uoutput, up - uoutput); free(uoutput); return output_obj; } else { return input_coerce; } } /* Now try as bytes */ input_coerce = PyObject_Bytes(input_obj); if (input_coerce) { if (PyBytes_AsStringAndSize(input_coerce, &input, &input_len) == -1) { Py_DECREF(input_coerce); return NULL; } for (i = 0; i < input_len; ++i) { for (esc = escapes; ; esc += 2) { if (input[i] > **esc) { break; } else if (input[i] == **esc) { ++count; break; } } } if (count) { output = malloc((input_len + 1 + count * 5) * sizeof(char)); if (output == NULL) { Py_DECREF(input_coerce); PyErr_SetString(PyExc_MemoryError, "Out of memory"); return NULL; } p = output; for (i = 0; i < input_len; ++i) { for (esc = escapes; ; esc += 2) { if (input[i] > **esc) { *(p++) = input[i]; break; } else if (input[i] == **esc) { for (ent = *(esc + 1); *ent != '\0'; ++ent) { *(p++) = *ent; } break; } } } *p = 0; Py_DECREF(input_coerce); output_obj = PyBytes_FromStringAndSize(output, p - output); free(output); return output_obj; } else { return input_coerce; } } PyErr_SetString(PyExc_TypeError, "must be convertible to str or bytes"); return NULL; }