static long __Pyx__PyObject_Ord(PyObject* c) { Py_ssize_t size; if (PyBytes_Check(c)) { size = PyBytes_GET_SIZE(c); if (likely(size == 1)) { return (unsigned char) PyBytes_AS_STRING(c)[0]; } #if PY_MAJOR_VERSION < 3 } else if (PyUnicode_Check(c)) { return (long)__Pyx_PyUnicode_AsPy_UCS4(c); #endif #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE)) } else if (PyByteArray_Check(c)) { size = PyByteArray_GET_SIZE(c); if (likely(size == 1)) { return (unsigned char) PyByteArray_AS_STRING(c)[0]; } #endif } else { // FIXME: support character buffers - but CPython doesn't support them either PyErr_Format(PyExc_TypeError, "ord() expected string of length 1, but %.200s found", c->ob_type->tp_name); return (long)(Py_UCS4)-1; } PyErr_Format(PyExc_TypeError, "ord() expected a character, but string of length %zd found", size); return (long)(Py_UCS4)-1; }
static char * fp_readl(char *s, int size, struct tok_state *tok) { PyObject* bufobj; const char *buf; Py_ssize_t buflen; /* Ask for one less byte so we can terminate it */ assert(size > 0); size--; if (tok->decoding_buffer) { bufobj = tok->decoding_buffer; Py_INCREF(bufobj); } else { bufobj = PyObject_CallObject(tok->decoding_readline, NULL); if (bufobj == NULL) goto error; } if (PyUnicode_CheckExact(bufobj)) { buf = _PyUnicode_AsStringAndSize(bufobj, &buflen); if (buf == NULL) { goto error; } } else { buf = PyByteArray_AsString(bufobj); if (buf == NULL) { goto error; } buflen = PyByteArray_GET_SIZE(bufobj); } Py_XDECREF(tok->decoding_buffer); if (buflen > size) { /* Too many chars, the rest goes into tok->decoding_buffer */ tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size, buflen-size); if (tok->decoding_buffer == NULL) goto error; buflen = size; } else tok->decoding_buffer = NULL; memcpy(s, buf, buflen); s[buflen] = '\0'; if (buflen == 0) /* EOF */ s = NULL; Py_DECREF(bufobj); return s; error: Py_XDECREF(bufobj); return error_ret(tok); }
// from Python/bltinmodule.c static const char * source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy) { const char *str; Py_ssize_t size; Py_buffer view; *cmd_copy = NULL; if (PyUnicode_Check(cmd)) { cf->cf_flags |= PyCF_IGNORE_COOKIE; str = PyUnicode_AsUTF8AndSize(cmd, &size); if (str == NULL) return NULL; } else if (PyBytes_Check(cmd)) { str = PyBytes_AS_STRING(cmd); size = PyBytes_GET_SIZE(cmd); } else if (PyByteArray_Check(cmd)) { str = PyByteArray_AS_STRING(cmd); size = PyByteArray_GET_SIZE(cmd); } else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) { /* Copy to NUL-terminated buffer. */ *cmd_copy = PyBytes_FromStringAndSize( (const char *)view.buf, view.len); PyBuffer_Release(&view); if (*cmd_copy == NULL) { return NULL; } str = PyBytes_AS_STRING(*cmd_copy); size = PyBytes_GET_SIZE(*cmd_copy); } else { PyErr_Format(PyExc_TypeError, "%s() arg 1 must be a %s object", funcname, what); return NULL; } if (strlen(str) != (size_t)size) { PyErr_SetString(PyExc_ValueError, "source code string cannot contain null bytes"); Py_CLEAR(*cmd_copy); return NULL; } return str; }
// Helper for Map.__init__(), Map.set() static int bad_mapbytes(PyObject * py_mapbytes, int size_pixels, const char * methodname) { if (!PyByteArray_Check(py_mapbytes)) { return error_on_raise_argument_exception_with_details("Map", methodname, "argument is not a byte array"); } if (PyByteArray_GET_SIZE(py_mapbytes) != (size_pixels * size_pixels)) { return error_on_raise_argument_exception_with_details("Map", methodname, "mapbytes are wrong size"); } return 0; }
static PyObject *py_JpegObject_setBlock(py_JpegObject *self, PyObject *args) { int x,y,comp; PyObject *coefObj; if (!PyArg_ParseTuple(args, "iiiO!", &x, &y, &comp, &PyByteArray_Type, &coefObj)) { return NULL; } Py_ssize_t size = PyByteArray_GET_SIZE(coefObj); if ((uint)size < BLOCKSIZE) { PyErr_SetString(PyExc_ValueError, "Byte array too small"); return NULL; } JCOEFPTR ptr = get_coeff_block(&self->stuff, x, y, comp); if (ptr == NULL) { PyErr_SetString(PyExc_ValueError, "Coordinates or component out of range"); return NULL; } char *coeffs = PyByteArray_AS_STRING(coefObj); memcpy(ptr, coeffs, BLOCKSIZE); Py_RETURN_NONE; }
static PyObject * c_set(void *ptr, PyObject *value, Py_ssize_t size) { if (PyUnicode_Check(value)) { value = PyUnicode_AsEncodedString(value, _ctypes_conversion_encoding, _ctypes_conversion_errors); if (value == NULL) return NULL; if (PyBytes_GET_SIZE(value) != 1) { Py_DECREF(value); goto error; } *(char *)ptr = PyBytes_AS_STRING(value)[0]; Py_DECREF(value); _RET(value); } if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) { *(char *)ptr = PyBytes_AS_STRING(value)[0]; _RET(value); } if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) { *(char *)ptr = PyByteArray_AS_STRING(value)[0]; _RET(value); } if (PyLong_Check(value)) { long longval = PyLong_AS_LONG(value); if (longval < 0 || longval >= 256) goto error; *(char *)ptr = (char)longval; _RET(value); } error: PyErr_Format(PyExc_TypeError, "one character string expected"); return NULL; }
static PyObject * c_set(void *ptr, PyObject *value, Py_ssize_t size) { if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) { *(char *)ptr = PyBytes_AS_STRING(value)[0]; _RET(value); } if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) { *(char *)ptr = PyByteArray_AS_STRING(value)[0]; _RET(value); } if (PyLong_Check(value)) { long longval = PyLong_AS_LONG(value); if (longval < 0 || longval >= 256) goto error; *(char *)ptr = (char)longval; _RET(value); } error: PyErr_Format(PyExc_TypeError, "one character string expected"); return NULL; }
static PyObject * iobase_readline(PyObject *self, PyObject *args) { /* For backwards compatibility, a (slowish) readline(). */ Py_ssize_t limit = -1; int has_peek = 0; PyObject *buffer, *result; Py_ssize_t old_size = -1; if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) { return NULL; } if (PyObject_HasAttrString(self, "peek")) has_peek = 1; buffer = PyByteArray_FromStringAndSize(NULL, 0); if (buffer == NULL) return NULL; while (limit < 0 || Py_SIZE(buffer) < limit) { Py_ssize_t nreadahead = 1; PyObject *b; if (has_peek) { PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1); if (readahead == NULL) goto fail; if (!PyBytes_Check(readahead)) { PyErr_Format(PyExc_IOError, "peek() should have returned a bytes object, " "not '%.200s'", Py_TYPE(readahead)->tp_name); Py_DECREF(readahead); goto fail; } if (PyBytes_GET_SIZE(readahead) > 0) { Py_ssize_t n = 0; const char *buf = PyBytes_AS_STRING(readahead); if (limit >= 0) { do { if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) break; if (buf[n++] == '\n') break; } while (1); } else { do { if (n >= PyBytes_GET_SIZE(readahead)) break; if (buf[n++] == '\n') break; } while (1); } nreadahead = n; } Py_DECREF(readahead); } b = PyObject_CallMethod(self, "read", "n", nreadahead); if (b == NULL) goto fail; if (!PyBytes_Check(b)) { PyErr_Format(PyExc_IOError, "read() should have returned a bytes object, " "not '%.200s'", Py_TYPE(b)->tp_name); Py_DECREF(b); goto fail; } if (PyBytes_GET_SIZE(b) == 0) { Py_DECREF(b); break; } old_size = PyByteArray_GET_SIZE(buffer); PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)); memcpy(PyByteArray_AS_STRING(buffer) + old_size, PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); Py_DECREF(b); if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') break; } result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), PyByteArray_GET_SIZE(buffer)); Py_DECREF(buffer); return result; fail: Py_DECREF(buffer); return NULL; }
static PyObject * _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) /*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/ { /* For backwards compatibility, a (slowish) readline(). */ PyObject *peek, *buffer, *result; Py_ssize_t old_size = -1; if (_PyObject_LookupAttr(self, _PyIO_str_peek, &peek) < 0) { return NULL; } buffer = PyByteArray_FromStringAndSize(NULL, 0); if (buffer == NULL) { Py_XDECREF(peek); return NULL; } while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) { Py_ssize_t nreadahead = 1; PyObject *b; if (peek != NULL) { PyObject *readahead = PyObject_CallFunctionObjArgs(peek, _PyLong_One, NULL); if (readahead == NULL) { /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR occurs so we needn't do it ourselves. */ if (_PyIO_trap_eintr()) { continue; } goto fail; } if (!PyBytes_Check(readahead)) { PyErr_Format(PyExc_OSError, "peek() should have returned a bytes object, " "not '%.200s'", Py_TYPE(readahead)->tp_name); Py_DECREF(readahead); goto fail; } if (PyBytes_GET_SIZE(readahead) > 0) { Py_ssize_t n = 0; const char *buf = PyBytes_AS_STRING(readahead); if (limit >= 0) { do { if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) break; if (buf[n++] == '\n') break; } while (1); } else { do { if (n >= PyBytes_GET_SIZE(readahead)) break; if (buf[n++] == '\n') break; } while (1); } nreadahead = n; } Py_DECREF(readahead); } b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); if (b == NULL) { /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR occurs so we needn't do it ourselves. */ if (_PyIO_trap_eintr()) { continue; } goto fail; } if (!PyBytes_Check(b)) { PyErr_Format(PyExc_OSError, "read() should have returned a bytes object, " "not '%.200s'", Py_TYPE(b)->tp_name); Py_DECREF(b); goto fail; } if (PyBytes_GET_SIZE(b) == 0) { Py_DECREF(b); break; } old_size = PyByteArray_GET_SIZE(buffer); if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) { Py_DECREF(b); goto fail; } memcpy(PyByteArray_AS_STRING(buffer) + old_size, PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); Py_DECREF(b); if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') break; } result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), PyByteArray_GET_SIZE(buffer)); Py_XDECREF(peek); Py_DECREF(buffer); return result; fail: Py_XDECREF(peek); Py_DECREF(buffer); return NULL; }
static int encode_common(PyObject **o, void **buf, size_t *nbuf, lcb_uint32_t flags) { PyObject *bytesobj; Py_ssize_t plen; int rv; if (flags == PYCBC_FMT_UTF8) { #if PY_MAJOR_VERSION == 2 if (PyString_Check(*o)) { #else if (0) { #endif bytesobj = *o; Py_INCREF(*o); } else { if (!PyUnicode_Check(*o)) { PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Must be unicode or string", *o); return -1; } bytesobj = PyUnicode_AsUTF8String(*o); } } else if (flags == PYCBC_FMT_BYTES) { if (PyBytes_Check(*o) || PyByteArray_Check(*o)) { bytesobj = *o; Py_INCREF(*o); } else { PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Must be bytes or bytearray", *o); return -1; } } else { PyObject *args = NULL; PyObject *helper; if (flags == PYCBC_FMT_PICKLE) { helper = pycbc_helpers.pickle_encode; } else if (flags == PYCBC_FMT_JSON) { helper = pycbc_helpers.json_encode; } else { PYCBC_EXC_WRAP(PYCBC_EXC_ARGUMENTS, 0, "Unrecognized format"); return -1; } args = PyTuple_Pack(1, *o); bytesobj = PyObject_CallObject(helper, args); Py_DECREF(args); if (!bytesobj) { PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Couldn't encode value", *o); return -1; } if (!PyBytes_Check(bytesobj)) { PyObject *old = bytesobj; bytesobj = convert_to_bytesobj(old); Py_DECREF(old); if (!bytesobj) { return -1; } } } if (PyByteArray_Check(bytesobj)) { *buf = PyByteArray_AS_STRING(bytesobj); plen = PyByteArray_GET_SIZE(bytesobj); rv = 0; } else { rv = PyBytes_AsStringAndSize(bytesobj, (char**)buf, &plen); } if (rv < 0) { Py_DECREF(bytesobj); PYCBC_EXC_WRAP(PYCBC_EXC_ENCODING, 0, "Couldn't encode value"); return -1; } *nbuf = plen; *o = bytesobj; return 0; } static int decode_common(PyObject **vp, const char *buf, size_t nbuf, lcb_uint32_t flags) { PyObject *decoded = NULL; lcb_U32 c_flags, l_flags; c_flags = flags & PYCBC_FMT_COMMON_MASK; l_flags = flags & PYCBC_FMT_LEGACY_MASK; #define FMT_MATCHES(fmtbase) \ (c_flags == PYCBC_FMT_COMMON_##fmtbase || l_flags == PYCBC_FMT_LEGACY_##fmtbase) if (FMT_MATCHES(UTF8)) { decoded = convert_to_string(buf, nbuf, CONVERT_MODE_UTF8_ONLY); if (!decoded) { return -1; } } else if (FMT_MATCHES(BYTES)) { GT_BYTES: decoded = convert_to_string(buf, nbuf, CONVERT_MODE_BYTES_ONLY); pycbc_assert(decoded); } else { PyObject *converter = NULL; PyObject *args = NULL; PyObject *first_arg = NULL; if (FMT_MATCHES(PICKLE)) { converter = pycbc_helpers.pickle_decode; first_arg = convert_to_string(buf, nbuf, CONVERT_MODE_BYTES_ONLY); pycbc_assert(first_arg); } else if (FMT_MATCHES(JSON)) { converter = pycbc_helpers.json_decode; first_arg = convert_to_string(buf, nbuf, CONVERT_MODE_UTF8_ONLY); if (!first_arg) { return -1; } } else { PyErr_Warn(PyExc_UserWarning, "Unrecognized flags. Forcing bytes"); goto GT_BYTES; } pycbc_assert(first_arg); args = PyTuple_Pack(1, first_arg); decoded = PyObject_CallObject(converter, args); Py_DECREF(args); Py_DECREF(first_arg); } if (!decoded) { PyObject *bytes_tmp = PyBytes_FromStringAndSize(buf, nbuf); PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Failed to decode bytes", bytes_tmp); Py_XDECREF(bytes_tmp); return -1; } *vp = decoded; return 0; #undef FMT_MATCHES }
static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT if ( #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII __Pyx_sys_getdefaultencoding_not_ascii && #endif PyUnicode_Check(o)) { #if PY_VERSION_HEX < 0x03030000 char* defenc_c; // borrowed, cached reference PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); if (!defenc) return NULL; defenc_c = PyBytes_AS_STRING(defenc); #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII { char* end = defenc_c + PyBytes_GET_SIZE(defenc); char* c; for (c = defenc_c; c < end; c++) { if ((unsigned char) (*c) >= 128) { // raise the error PyUnicode_AsASCIIString(o); return NULL; } } } #endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/ *length = PyBytes_GET_SIZE(defenc); return defenc_c; #else /* PY_VERSION_HEX < 0x03030000 */ if (PyUnicode_READY(o) == -1) return NULL; #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII if (PyUnicode_IS_ASCII(o)) { // cached for the lifetime of the object *length = PyUnicode_GET_DATA_SIZE(o); return PyUnicode_AsUTF8(o); } else { // raise the error PyUnicode_AsASCIIString(o); return NULL; } #else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ return PyUnicode_AsUTF8AndSize(o, length); #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ #endif /* PY_VERSION_HEX < 0x03030000 */ } else #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */ #if PY_VERSION_HEX >= 0x02060000 if (PyByteArray_Check(o)) { *length = PyByteArray_GET_SIZE(o); return PyByteArray_AS_STRING(o); } else #endif { char* result; int r = PyBytes_AsStringAndSize(o, &result, length); if (unlikely(r < 0)) { return NULL; } else { return result; } } }
LLBC_PacketHeaderParts *pyllbc_Service::BuildCLayerParts(PyObject *pyLayerParts) { // Python layer parts(dict type) convert rules describe: // python type c++ type // -------------------------- // int/long/bool --> sint64 // float4/8 --> float/double // str/bytearray --> LLBC_String if (!PyDict_Check(pyLayerParts)) { pyllbc_SetError("parts instance not dict type"); return NULL; } LLBC_PacketHeaderParts *cLayerParts = LLBC_New(LLBC_PacketHeaderParts); Py_ssize_t pos = 0; PyObject *key, *value; while (PyDict_Next(pyLayerParts, &pos, &key, &value)) // key & value are borrowed. { const int serialNo = static_cast<int>(PyInt_AsLong(key)); if (UNLIKELY(serialNo == -1 && PyErr_Occurred())) { pyllbc_TransferPyError("When fetch header part serial no"); LLBC_Delete(cLayerParts); return NULL; } // Value type check order: // int-> // str-> // float-> // long-> // bool-> // bytearray-> // other objects if (PyInt_CheckExact(value)) { const sint64 cValue = PyInt_AS_LONG(value); cLayerParts->SetPart<sint64>(serialNo, cValue); } else if (PyString_CheckExact(value)) { char *strBeg; Py_ssize_t strLen; if (UNLIKELY(PyString_AsStringAndSize(value, &strBeg, &strLen) == -1)) { pyllbc_TransferPyError("When fetch header part value"); LLBC_Delete(cLayerParts); return NULL; } cLayerParts->SetPart(serialNo, strBeg, strLen); } else if (PyFloat_CheckExact(value)) { const double cValue = PyFloat_AS_DOUBLE(value); cLayerParts->SetPart<double>(serialNo, cValue); } else if (PyLong_CheckExact(value)) { const sint64 cValue = PyLong_AsLongLong(value); cLayerParts->SetPart<sint64>(serialNo, cValue); } else if (PyBool_Check(value)) { const int pyBoolCheck = PyObject_IsTrue(value); if (UNLIKELY(pyBoolCheck == -1)) { pyllbc_TransferPyError("when fetch header part value"); LLBC_Delete(cLayerParts); return NULL; } cLayerParts->SetPart<uint8>(serialNo, pyBoolCheck); } else if (PyByteArray_CheckExact(value)) { char *bytesBeg = PyByteArray_AS_STRING(value); Py_ssize_t bytesLen = PyByteArray_GET_SIZE(value); cLayerParts->SetPart(serialNo, bytesBeg, bytesLen); } else // Other types, we simple get the object string representations. { LLBC_String strRepr = pyllbc_ObjUtil::GetObjStr(value); if (UNLIKELY(strRepr.empty() && PyErr_Occurred())) { LLBC_Delete(cLayerParts); return NULL; } cLayerParts->SetPart(serialNo, strRepr.data(), strRepr.size()); } } return cLayerParts; }
static uint32_t pointless_export_py_rec(pointless_export_state_t* state, PyObject* py_object, uint32_t depth) { // don't go too deep if (depth >= POINTLESS_MAX_DEPTH) { PyErr_SetString(PyExc_ValueError, "structure is too deep"); state->is_error = 1; printf("line: %i\n", __LINE__); state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // check simple types first uint32_t handle = POINTLESS_CREATE_VALUE_FAIL; // return an error on failure #define RETURN_OOM(state) {PyErr_NoMemory(); (state)->is_error = 1; printf("line: %i\n", __LINE__); state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL;} #define RETURN_OOM_IF_FAIL(handle, state) if ((handle) == POINTLESS_CREATE_VALUE_FAIL) RETURN_OOM(state); // booleans, need this above integer check, cause PyInt_Check return 1 for booleans if (PyBool_Check(py_object)) { if (py_object == Py_True) handle = pointless_create_boolean_true(&state->c); else handle = pointless_create_boolean_false(&state->c); RETURN_OOM_IF_FAIL(handle, state); // integer } else if (PyInt_Check(py_object)) { long v = PyInt_AS_LONG(py_object); // unsigned if (v >= 0) { if (v > UINT32_MAX) { PyErr_Format(PyExc_ValueError, "integer too large for mere 32 bits"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_u32(&state->c, (uint32_t)v); // signed } else { if (!(INT32_MIN <= v && v <= INT32_MAX)) { PyErr_Format(PyExc_ValueError, "integer too large for mere 32 bits with a sign"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_i32(&state->c, (int32_t)v); } RETURN_OOM_IF_FAIL(handle, state); // long } else if (PyLong_Check(py_object)) { // this will raise an overflow error if number is outside the legal range of PY_LONG_LONG PY_LONG_LONG v = PyLong_AsLongLong(py_object); // if there was an exception, clear it, and set our own if (PyErr_Occurred()) { PyErr_Clear(); PyErr_SetString(PyExc_ValueError, "value of long is way beyond what we can store right now"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // unsigned if (v >= 0) { if (v > UINT32_MAX) { PyErr_Format(PyExc_ValueError, "long too large for mere 32 bits"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_u32(&state->c, (uint32_t)v); // signed } else { if (!(INT32_MIN <= v && v <= INT32_MAX)) { PyErr_Format(PyExc_ValueError, "long too large for mere 32 bits with a sign"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_i32(&state->c, (int32_t)v); } RETURN_OOM_IF_FAIL(handle, state); // None object } else if (py_object == Py_None) { handle = pointless_create_null(&state->c); RETURN_OOM_IF_FAIL(handle, state); } else if (PyFloat_Check(py_object)) { handle = pointless_create_float(&state->c, (float)PyFloat_AS_DOUBLE(py_object)); RETURN_OOM_IF_FAIL(handle, state); } if (handle != POINTLESS_CREATE_VALUE_FAIL) return handle; // remaining types are containers/big-values, which we track // either for space-savings or maintaining circular references // if object has been seen before, return its handle handle = pointless_export_get_seen(state, py_object); if (handle != POINTLESS_CREATE_VALUE_FAIL) return handle; // list/tuple object if (PyList_Check(py_object) || PyTuple_Check(py_object)) { // create and cache handle assert(is_container(py_object)); handle = pointless_create_vector_value(&state->c); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // populate vector Py_ssize_t i, n_items = PyList_Check(py_object) ? PyList_GET_SIZE(py_object) : PyTuple_GET_SIZE(py_object); for (i = 0; i < n_items; i++) { PyObject* child = PyList_Check(py_object) ? PyList_GET_ITEM(py_object, i) : PyTuple_GET_ITEM(py_object, i); uint32_t child_handle = pointless_export_py_rec(state, child, depth + 1); if (child_handle == POINTLESS_CREATE_VALUE_FAIL) return child_handle; if (pointless_create_vector_value_append(&state->c, handle, child_handle) == POINTLESS_CREATE_VALUE_FAIL) { RETURN_OOM(state); } } // pointless value vectors } else if (PyPointlessVector_Check(py_object)) { // currently, we only support value vectors, they are simple PyPointlessVector* v = (PyPointlessVector*)py_object; const char* error = 0; switch(v->v->type) { case POINTLESS_VECTOR_VALUE: case POINTLESS_VECTOR_VALUE_HASHABLE: handle = pointless_recreate_value(&v->pp->p, v->v, &state->c, &error); if (handle == POINTLESS_CREATE_VALUE_FAIL) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; PyErr_Format(PyExc_ValueError, "pointless_recreate_value(): %s", error); return POINTLESS_CREATE_VALUE_FAIL; } break; case POINTLESS_VECTOR_I8: handle = pointless_create_vector_i8_owner(&state->c, pointless_reader_vector_i8(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U8: handle = pointless_create_vector_u8_owner(&state->c, pointless_reader_vector_u8(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_I16: handle = pointless_create_vector_i16_owner(&state->c, pointless_reader_vector_i16(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U16: handle = pointless_create_vector_u16_owner(&state->c, pointless_reader_vector_u16(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_I32: handle = pointless_create_vector_i32_owner(&state->c, pointless_reader_vector_i32(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U32: handle = pointless_create_vector_u32_owner(&state->c, pointless_reader_vector_u32(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_I64: handle = pointless_create_vector_i64_owner(&state->c, pointless_reader_vector_i64(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U64: handle = pointless_create_vector_u64_owner(&state->c, pointless_reader_vector_u64(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_FLOAT: handle = pointless_create_vector_float_owner(&state->c, pointless_reader_vector_float(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_EMPTY: handle = pointless_create_vector_value(&state->c); break; default: state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; PyErr_SetString(PyExc_ValueError, "internal error: illegal type for primitive vector"); return POINTLESS_CREATE_VALUE_FAIL; } RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // python bytearray } else if (PyByteArray_Check(py_object)) { // create handle and hand over the memory Py_ssize_t n_items = PyByteArray_GET_SIZE(py_object); if (n_items > UINT32_MAX) { PyErr_SetString(PyExc_ValueError, "bytearray has too many items"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_vector_u8_owner(&state->c, (uint8_t*)PyByteArray_AS_STRING(py_object), (uint32_t)n_items); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // primitive vectors } else if (PyPointlessPrimVector_Check(py_object)) { // we just hand over the memory PyPointlessPrimVector* prim_vector = (PyPointlessPrimVector*)py_object; uint32_t n_items = pointless_dynarray_n_items(&prim_vector->array); void* data = prim_vector->array._data; switch (prim_vector->type) { case POINTLESS_PRIM_VECTOR_TYPE_I8: handle = pointless_create_vector_i8_owner(&state->c, (int8_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U8: handle = pointless_create_vector_u8_owner(&state->c, (uint8_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_I16: handle = pointless_create_vector_i16_owner(&state->c, (int16_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U16: handle = pointless_create_vector_u16_owner(&state->c, (uint16_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_I32: handle = pointless_create_vector_i32_owner(&state->c, (int32_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U32: handle = pointless_create_vector_u32_owner(&state->c, (uint32_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_I64: handle = pointless_create_vector_i64_owner(&state->c, (int64_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U64: handle = pointless_create_vector_u64_owner(&state->c, (uint64_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_FLOAT: handle = pointless_create_vector_float_owner(&state->c, (float*)data, n_items); break; default: PyErr_SetString(PyExc_ValueError, "internal error: illegal type for primitive vector"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // unicode object } else if (PyUnicode_Check(py_object)) { // get it from python Py_UNICODE* python_buffer = PyUnicode_AS_UNICODE(py_object); // string must not contain zero's Py_ssize_t s_len_python = PyUnicode_GET_SIZE(py_object); #if Py_UNICODE_SIZE == 4 uint32_t s_len_pointless = pointless_ucs4_len(python_buffer); #else uint32_t s_len_pointless = pointless_ucs2_len(python_buffer); #endif if (s_len_python < 0 || (uint64_t)s_len_python != s_len_pointless) { PyErr_SetString(PyExc_ValueError, "unicode string contains a zero, where it shouldn't"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } #if Py_UNICODE_SIZE == 4 if (state->unwiden_strings && pointless_is_ucs4_ascii((uint32_t*)python_buffer)) handle = pointless_create_string_ucs4(&state->c, python_buffer); else handle = pointless_create_unicode_ucs4(&state->c, python_buffer); #else if (state->unwiden_strings && pointless_is_ucs2_ascii(python_buffer)) handle = pointless_create_string_ucs2(&state->c, python_buffer); else handle = pointless_create_unicode_ucs2(&state->c, python_buffer); #endif RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // string object } else if (PyString_Check(py_object)) { // get it from python uint8_t* python_buffer = (uint8_t*)PyString_AS_STRING(py_object); // string must not contain zero's Py_ssize_t s_len_python = PyString_GET_SIZE(py_object); uint32_t s_len_pointless = pointless_ascii_len(python_buffer); if (s_len_python < 0 || (uint64_t)s_len_python != s_len_pointless) { PyErr_SetString(PyExc_ValueError, "string contains a zero, where it shouldn't"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_string_ascii(&state->c, python_buffer); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // dict object } else if (PyDict_Check(py_object)) { handle = pointless_create_map(&state->c); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } PyObject* key = 0; PyObject* value = 0; Py_ssize_t pos = 0; while (PyDict_Next(py_object, &pos, &key, &value)) { uint32_t key_handle = pointless_export_py_rec(state, key, depth + 1); uint32_t value_handle = pointless_export_py_rec(state, value, depth + 1); if (key_handle == POINTLESS_CREATE_VALUE_FAIL || value_handle == POINTLESS_CREATE_VALUE_FAIL) break; if (pointless_create_map_add(&state->c, handle, key_handle, value_handle) == POINTLESS_CREATE_VALUE_FAIL) { PyErr_SetString(PyExc_ValueError, "error adding key/value pair to map"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; break; } } if (state->is_error) { return POINTLESS_CREATE_VALUE_FAIL; } // set object } else if (PyAnySet_Check(py_object)) { PyObject* iterator = PyObject_GetIter(py_object); PyObject* item = 0; if (iterator == 0) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // get a handle handle = pointless_create_set(&state->c); RETURN_OOM_IF_FAIL(handle, state); // cache object if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // iterate over it while ((item = PyIter_Next(iterator)) != 0) { uint32_t item_handle = pointless_export_py_rec(state, item, depth + 1); if (item_handle == POINTLESS_CREATE_VALUE_FAIL) break; if (pointless_create_set_add(&state->c, handle, item_handle) == POINTLESS_CREATE_VALUE_FAIL) { PyErr_SetString(PyExc_ValueError, "error adding item to set"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; break; } } Py_DECREF(iterator); if (PyErr_Occurred()) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // bitvector } else if (PyPointlessBitvector_Check(py_object)) { PyPointlessBitvector* bitvector = (PyPointlessBitvector*)py_object; if (bitvector->is_pointless) { uint32_t i, n_bits = pointless_reader_bitvector_n_bits(&bitvector->pointless_pp->p, bitvector->pointless_v); void* bits = pointless_calloc(ICEIL(n_bits, 8), 1); if (bits == 0) { RETURN_OOM(state); } for (i = 0; i < n_bits; i++) { if (pointless_reader_bitvector_is_set(&bitvector->pointless_pp->p, bitvector->pointless_v, i)) bm_set_(bits, i); } if (state->normalize_bitvector) handle = pointless_create_bitvector(&state->c, bits, n_bits); else handle = pointless_create_bitvector_no_normalize(&state->c, bits, n_bits); pointless_free(bits); bits = 0; } else { if (state->normalize_bitvector) handle = pointless_create_bitvector(&state->c, bitvector->primitive_bits, bitvector->primitive_n_bits); else handle = pointless_create_bitvector_no_normalize(&state->c, bitvector->primitive_bits, bitvector->primitive_n_bits); } RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } } else if (PyPointlessSet_Check(py_object)) { PyPointlessSet* set = (PyPointlessSet*)py_object; const char* error = 0; handle = pointless_recreate_value(&set->pp->p, set->v, &state->c, &error); if (handle == POINTLESS_CREATE_VALUE_FAIL) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; PyErr_Format(PyExc_ValueError, "pointless_recreate_value(): %s", error); return POINTLESS_CREATE_VALUE_FAIL; } if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } } else if (PyPointlessMap_Check(py_object)) { PyPointlessMap* map = (PyPointlessMap*)py_object; const char* error = 0; handle = pointless_recreate_value(&map->pp->p, map->v, &state->c, &error); if (handle == POINTLESS_CREATE_VALUE_FAIL) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; PyErr_Format(PyExc_ValueError, "pointless_recreate_value(): %s", error); return POINTLESS_CREATE_VALUE_FAIL; } if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // type not supported } else { PyErr_Format(PyExc_ValueError, "type <%s> not supported", py_object->ob_type->tp_name); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; } #undef RETURN_OOM #undef RETURN_IF_OOM return handle; }