static char *GetString( PyObject *value ) { /* * Name: * GetString * Purpose: * Get a pointer to a null terminated string from a PyObject. * It should be freed by the caller. * Stolen from pyast */ char *result = NULL; if( value && value != Py_None ) { #ifdef USE_PY3K PyObject *bytes = PyUnicode_AsASCIIString(value); #else PyObject *bytes = value; #endif if( bytes ) { size_t nbytes = PyBytes_Size( bytes ); const char * bytestr = PyBytes_AS_STRING(bytes); result = malloc( (nbytes+1) * sizeof(*result)); strcpy( result, bytestr ); #ifdef USE_PY3K Py_DECREF(bytes); #endif } } return result; }
size_t OnSysRead(void *buffer, size_t bufsize) { if (bufsize == 0) return 0; wxPyThreadBlocker blocker; PyObject* arglist = Py_BuildValue("(i)", bufsize); PyObject* result = PyEval_CallObject(m_read, arglist); Py_DECREF(arglist); size_t o = 0; if ((result != NULL) && PyBytes_Check(result)) { o = PyBytes_Size(result); if (o == 0) m_lasterror = wxSTREAM_EOF; if (o > bufsize) o = bufsize; memcpy((char*)buffer, PyBytes_AsString(result), o); // strings only, not unicode... Py_DECREF(result); } else m_lasterror = wxSTREAM_READ_ERROR; return o; }
/* Given the contents of a .py[co] file in a buffer, unmarshal the data and return the code object. Return None if it the magic word doesn't match (we do this instead of raising an exception as we fall back to .py if available and we don't want to mask other errors). Returns a new reference. */ static PyObject * unmarshal_code(char *pathname, PyObject *data) { PyObject *code; char *buf = PyBytes_AsString(data); Py_ssize_t size = PyBytes_Size(data); if (size <= 9) { PyErr_SetString(PyExc_TypeError, "bad pyc data"); return NULL; } if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) { if (Py_VerboseFlag) PySys_WriteStderr("# %s has bad magic\n", pathname); Py_INCREF(Py_None); return NULL; } code = PyMarshal_ReadObjectFromString(buf + 8, size - 8); if (code == NULL) return NULL; if (!PyCode_Check(code)) { Py_DECREF(code); PyErr_Format(PyExc_TypeError, "compiled module %.200s is not a code object", pathname); return NULL; } return code; }
static PyObject *dash_getpowhash(PyObject *self, PyObject *args) { char *output; PyObject *value; #if PY_MAJOR_VERSION >= 3 PyBytesObject *input; #else PyStringObject *input; #endif if (!PyArg_ParseTuple(args, "S", &input)) return NULL; Py_INCREF(input); output = PyMem_Malloc(32); #if PY_MAJOR_VERSION >= 3 dash_hash((char *)PyBytes_AsString((PyObject*) input), (int)PyBytes_Size((PyObject*) input), output); #else dash_hash((char *)PyString_AsString((PyObject*) input), (int)PyString_Size((PyObject*) input), output); #endif Py_DECREF(input); #if PY_MAJOR_VERSION >= 3 value = Py_BuildValue("y#", output, 32); #else value = Py_BuildValue("s#", output, 32); #endif PyMem_Free(output); return value; }
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 int chunker_fill(Chunker *c) { size_t n; memmove(c->data, c->data + c->last, c->position + c->remaining - c->last); c->position -= c->last; c->last = 0; n = c->buf_size - c->position - c->remaining; if(c->eof || n == 0) { return 1; } PyObject *data = PyObject_CallMethod(c->fd, "read", "i", n); if(!data) { return 0; } n = PyBytes_Size(data); if(n) { memcpy(c->data + c->position + c->remaining, PyBytes_AsString(data), n); c->remaining += n; c->bytes_read += n; } else { c->eof = 1; } Py_DECREF(data); return 1; }
static int chunker_fill(Chunker *c) { ssize_t n; size_t offset, length; PyObject *data; memmove(c->data, c->data + c->last, c->position + c->remaining - c->last); c->position -= c->last; c->last = 0; n = c->buf_size - c->position - c->remaining; if(c->eof || n == 0) { return 1; } if(c->fh >= 0) { offset = c->bytes_read; // if we have a os-level file descriptor, use os-level API n = read(c->fh, c->data + c->position + c->remaining, n); if(n > 0) { c->remaining += n; c->bytes_read += n; } else if(n == 0) { c->eof = 1; } else { // some error happened return 0; } length = c->bytes_read - offset; #if ( _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L ) // We tell the OS that we do not need the data that we just have read any // more (that it maybe has in the cache). This avoids that we spoil the // complete cache with data that we only read once and (due to cache // size limit) kick out data from the cache that might be still useful // for the OS or other processes. if (length > 0) { posix_fadvise(c->fh, (off_t) offset, (off_t) length, POSIX_FADV_DONTNEED); } #endif } else { // no os-level file descriptor, use Python file object API data = PyObject_CallMethod(c->fd, "read", "i", n); if(!data) { return 0; } n = PyBytes_Size(data); if(n) { memcpy(c->data + c->position + c->remaining, PyBytes_AsString(data), n); c->remaining += n; c->bytes_read += n; } else { c->eof = 1; } Py_DECREF(data); } return 1; }
static int mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v) { const char *buf; CHECK_VALID(-1); if (i < 0 || i >= self->size) { PyErr_SetString(PyExc_IndexError, "mmap index out of range"); return -1; } if (v == NULL) { PyErr_SetString(PyExc_TypeError, "mmap object doesn't support item deletion"); return -1; } if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) { PyErr_SetString(PyExc_IndexError, "mmap assignment must be length-1 bytes()"); return -1; } if (!is_writable(self)) return -1; buf = PyBytes_AsString(v); self->data[i] = buf[0]; return 0; }
PyObject * vm_set_mem(JitCpu *self, PyObject *args) { PyObject *py_addr; PyObject *py_buffer; Py_ssize_t py_length; char *buffer; Py_ssize_t pysize; uint64_t addr; int ret = 0x1337; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) return NULL; PyGetInt_uint64_t(py_addr, addr); if(!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); pysize = PyBytes_Size(py_buffer); if (pysize < 0) { RAISE(PyExc_TypeError,"Python error"); } PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length); ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError,"arg must be str"); Py_INCREF(Py_None); return Py_None; }
// 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]; } }
PyObject* vm_set_mem(VmMngr* self, PyObject* args) { PyObject *py_addr; PyObject *py_buffer; Py_ssize_t py_length; char * buffer; Py_ssize_t pysize; uint64_t addr; int ret; if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer)) RAISE(PyExc_TypeError,"Cannot parse arguments"); PyGetInt_uint64_t(py_addr, addr); if (!PyBytes_Check(py_buffer)) RAISE(PyExc_TypeError,"arg must be bytes"); pysize = PyBytes_Size(py_buffer); if (pysize < 0) { RAISE(PyExc_TypeError,"Python error"); } PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length); ret = vm_write_mem(&self->vm_mngr, addr, buffer, pysize); if (ret < 0) RAISE(PyExc_TypeError, "Error in set_mem"); add_mem_write(&self->vm_mngr, addr, (size_t)pysize); check_invalid_code_blocs(&self->vm_mngr); Py_INCREF(Py_None); return Py_None; }
static void setstate(IM& im, const object & state) { if(!PyBytes_Check(state.ptr())) throw std::invalid_argument("Failed to unpickle, unexpected type!"); const int N = im.W()*im.H()*im.C()*sizeof(value_type); if( PyBytes_Size(state.ptr()) != N ) throw std::invalid_argument("Failed to unpickle, unexpected size!"); memcpy( im.data(), PyBytes_AS_STRING(state.ptr()), N ); }
static int np_char(char *p, PyObject *v, const formatdef *f) { if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) { PyErr_SetString(StructError, "char format requires a bytes object of length 1"); return -1; } *p = *PyBytes_AsString(v); return 0; }
static PyObject *_internal_stream_load(PyObject *args, unsigned int blocking) { PyObject *decoder = NULL; PyObject *stream = NULL; PyObject *buffer = NULL; PyObject *result = NULL; #ifdef IS_PYTHON3 PyObject *bufferstring = NULL; #endif if (!PyArg_ParseTuple(args, "O", &stream)) { goto bad_type; } if (__read == NULL) { __read = PyUnicode_FromString("read"); } if (!PyObject_HasAttr(stream, __read)) { goto bad_type; } buffer = PyObject_CallMethodObjArgs(stream, __read, NULL); if (!buffer) return NULL; #ifdef IS_PYTHON3 bufferstring = PyUnicode_AsUTF8String(buffer); if (!bufferstring) return NULL; #endif decoder = PyObject_Call((PyObject *)(&YajlDecoderType), NULL, NULL); if (decoder == NULL) { return NULL; } #ifdef IS_PYTHON3 result = _internal_decode((_YajlDecoder *)decoder, PyBytes_AsString(bufferstring), PyBytes_Size(bufferstring)); Py_XDECREF(bufferstring); #else result = _internal_decode((_YajlDecoder *)decoder, PyString_AsString(buffer), PyString_Size(buffer)); #endif Py_XDECREF(decoder); Py_XDECREF(buffer); return result; bad_type: PyErr_SetObject(PyExc_TypeError, PyUnicode_FromString("Must pass a single stream object")); return NULL; }
PyObject* vm_add_memory_page(VmMngr* self, PyObject* args) { PyObject *addr; PyObject *access; PyObject *item_str; PyObject *name=NULL; uint64_t buf_size; size_t buf_size_st; char* buf_data; Py_ssize_t length; uint64_t page_addr; uint64_t page_access; const char *name_ptr; struct memory_page_node * mpn; if (!PyArg_ParseTuple(args, "OOO|O", &addr, &access, &item_str, &name)) RAISE(PyExc_TypeError,"Cannot parse arguments"); PyGetInt_uint64_t(addr, page_addr); PyGetInt_uint64_t(access, page_access); if(!PyBytes_Check(item_str)) RAISE(PyExc_TypeError,"arg must be bytes"); buf_size = PyBytes_Size(item_str); PyBytes_AsStringAndSize(item_str, &buf_data, &length); if (name == NULL) { name_ptr = (char*)""; } else { PyGetStr(name_ptr, name); } mpn = create_memory_page_node(page_addr, (unsigned int)buf_size, (unsigned int)page_access, name_ptr); if (mpn == NULL) RAISE(PyExc_TypeError,"cannot create page"); if (is_mpn_in_tab(&self->vm_mngr, mpn)) { free(mpn->ad_hp); free(mpn); RAISE(PyExc_TypeError,"known page in memory"); } if (buf_size > SIZE_MAX) { fprintf(stderr, "Size too big\n"); exit(EXIT_FAILURE); } buf_size_st = (size_t) buf_size; memcpy(mpn->ad_hp, buf_data, buf_size_st); add_memory_page(&self->vm_mngr, mpn); Py_INCREF(Py_None); return Py_None; }
static void w_more(int c, WFILE *p) { Py_ssize_t size, newsize; if (p->str == NULL) return; /* An error already occurred */ size = PyBytes_Size(p->str); newsize = size + size + 1024; if (newsize > 32*1024*1024) { newsize = size + (size >> 3); /* 12.5% overallocation */ }
/* Given the contents of a .pyc file in a buffer, unmarshal the data and return the code object. Return None if it the magic word doesn't match (we do this instead of raising an exception as we fall back to .py if available and we don't want to mask other errors). Returns a new reference. */ static PyObject * unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime) { PyObject *code; unsigned char *buf = (unsigned char *)PyBytes_AsString(data); Py_ssize_t size = PyBytes_Size(data); if (size < 16) { PyErr_SetString(ZipImportError, "bad pyc data"); return NULL; } if (get_uint32(buf) != (unsigned int)PyImport_GetMagicNumber()) { if (Py_VerboseFlag) { PySys_FormatStderr("# %R has bad magic\n", pathname); } Py_RETURN_NONE; /* signal caller to try alternative */ } uint32_t flags = get_uint32(buf + 4); if (flags != 0) { // Hash-based pyc. We currently refuse to handle checked hash-based // pycs. We could validate hash-based pycs against the source, but it // seems likely that most people putting hash-based pycs in a zipfile // will use unchecked ones. if (strcmp(_Py_CheckHashBasedPycsMode, "never") && (flags != 0x1 || !strcmp(_Py_CheckHashBasedPycsMode, "always"))) Py_RETURN_NONE; } else if ((mtime != 0 && !eq_mtime(get_uint32(buf + 8), mtime))) { if (Py_VerboseFlag) { PySys_FormatStderr("# %R has bad mtime\n", pathname); } Py_RETURN_NONE; /* signal caller to try alternative */ } /* XXX the pyc's size field is ignored; timestamp collisions are probably unimportant with zip files. */ code = PyMarshal_ReadObjectFromString((char *)buf + 16, size - 16); if (code == NULL) { return NULL; } if (!PyCode_Check(code)) { Py_DECREF(code); PyErr_Format(PyExc_TypeError, "compiled module %R is not a code object", pathname); return NULL; } return code; }
int get_bytes_or_bytearray_ln(bp::object buf) { PyObject *py_ba; py_ba = buf.ptr(); if (PyByteArray_Check(py_ba)) return PyByteArray_Size(py_ba); else if (PyBytes_Check(py_ba)) return PyBytes_Size(py_ba); else throw_ba_exception(); return 0; }
/* Convert a 4-char string object argument to an OSType value */ int AE_GetOSType(PyObject *v, OSType *pr) { uint32_t tmp; if (!PyBytes_Check(v) || PyBytes_Size(v) != 4) { PyErr_SetString(PyExc_TypeError, "OSType arg must be a bytes object, 4 bytes in length."); return 0; } memcpy((char *)&tmp, PyBytes_AsString(v), 4); *pr = (OSType)CFSwapInt32HostToBig(tmp); return 1; }
static PyObject * rpmts_Match(rpmtsObject * s, PyObject * args, PyObject * kwds) { PyObject *Key = NULL; PyObject *str = NULL; PyObject *mio = NULL; char *key = NULL; /* XXX lkey *must* be a 32 bit integer, int "works" on all known platforms. */ int lkey = 0; int len = 0; rpmDbiTagVal tag = RPMDBI_PACKAGES; char * kwlist[] = {"tagNumber", "key", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O:Match", kwlist, tagNumFromPyObject, &tag, &Key)) return NULL; if (Key) { if (PyInt_Check(Key)) { lkey = PyInt_AsLong(Key); key = (char *)&lkey; len = sizeof(lkey); } else if (PyLong_Check(Key)) { lkey = PyLong_AsLong(Key); key = (char *)&lkey; len = sizeof(lkey); } else if (utf8FromPyObject(Key, &str)) { key = PyBytes_AsString(str); len = PyBytes_Size(str); } else { PyErr_SetString(PyExc_TypeError, "unknown key type"); return NULL; } /* One of the conversions above failed, exception is set already */ if (PyErr_Occurred()) goto exit; } /* XXX If not already opened, open the database O_RDONLY now. */ /* XXX FIXME: lazy default rdonly open also done by rpmtsInitIterator(). */ if (rpmtsGetRdb(s->ts) == NULL) { int rc = rpmtsOpenDB(s->ts, O_RDONLY); if (rc || rpmtsGetRdb(s->ts) == NULL) { PyErr_SetString(pyrpmError, "rpmdb open failed"); goto exit; } } mio = rpmmi_Wrap(&rpmmi_Type, rpmtsInitIterator(s->ts, tag, key, len), (PyObject*)s); exit: Py_XDECREF(str); return mio; }
/* * Generic conversion function: Convert PyObject to cstring and * cstring into PostgreSQL type. */ static Datum PLyObject_ToDatum(PLyObToDatum *arg, int32 typmod, PyObject *plrv) { PyObject *volatile plrv_bo = NULL; Datum rv; Assert(plrv != Py_None); if (PyUnicode_Check(plrv)) plrv_bo = PLyUnicode_Bytes(plrv); else { #if PY_MAJOR_VERSION >= 3 PyObject *s = PyObject_Str(plrv); plrv_bo = PLyUnicode_Bytes(s); Py_XDECREF(s); #else plrv_bo = PyObject_Str(plrv); #endif } if (!plrv_bo) PLy_elog(ERROR, "could not create string representation of Python object"); PG_TRY(); { char *plrv_sc = PyBytes_AsString(plrv_bo); size_t plen = PyBytes_Size(plrv_bo); size_t slen = strlen(plrv_sc); if (slen < plen) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes"))); else if (slen > plen) elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length"); pg_verifymbstr(plrv_sc, slen, false); rv = InputFunctionCall(&arg->typfunc, plrv_sc, arg->typioparam, typmod); } PG_CATCH(); { Py_XDECREF(plrv_bo); PG_RE_THROW(); } PG_END_TRY(); Py_XDECREF(plrv_bo); return rv; }
static PyObject* Instruction_setOpcodes(PyObject* self, PyObject* opc) { if (!PyBytes_Check(opc)) return PyErr_Format(PyExc_TypeError, "setOpcodes(): expected a bytes array as argument"); if (PyBytes_Size(opc) >= 32) return PyErr_Format(PyExc_TypeError, "setOpcodes(): Invalid size (too big)"); PyInstruction_AsInstruction(self)->setOpcodes(reinterpret_cast<triton::uint8*>(PyBytes_AsString(opc)), PyBytes_Size(opc)); Py_INCREF(Py_None); return Py_None; }
/* * Convert Python object to C string in server encoding. */ char * PLyObject_AsString(PyObject *plrv) { PyObject *plrv_bo; char *plrv_sc; size_t plen; size_t slen; if (PyUnicode_Check(plrv)) plrv_bo = PLyUnicode_Bytes(plrv); else if (PyFloat_Check(plrv)) { /* use repr() for floats, str() is lossy */ #if PY_MAJOR_VERSION >= 3 PyObject *s = PyObject_Repr(plrv); plrv_bo = PLyUnicode_Bytes(s); Py_XDECREF(s); #else plrv_bo = PyObject_Repr(plrv); #endif } else { #if PY_MAJOR_VERSION >= 3 PyObject *s = PyObject_Str(plrv); plrv_bo = PLyUnicode_Bytes(s); Py_XDECREF(s); #else plrv_bo = PyObject_Str(plrv); #endif } if (!plrv_bo) PLy_elog(ERROR, "could not create string representation of Python object"); plrv_sc = pstrdup(PyBytes_AsString(plrv_bo)); plen = PyBytes_Size(plrv_bo); slen = strlen(plrv_sc); Py_XDECREF(plrv_bo); if (slen < plen) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes"))); else if (slen > plen) elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length"); pg_verifymbstr(plrv_sc, slen, false); return plrv_sc; }
static void setstate(std::vector<Polygons> & vp, const object & state) { if(!PyBytes_Check(state.ptr())) throw std::invalid_argument("Failed to unpickle, unexpected type!"); std::stringstream ss( std::string( PyBytes_AS_STRING(state.ptr()), PyBytes_Size(state.ptr()) ) ); unsigned int s; ss.read( (char*)&s, sizeof(s) ); vp.resize( s ); for( Polygons & p: vp ) { ss.read( (char*)&s, sizeof(s) ); p.resize( s ); for( Polygon & v: p ) loadMatrixX( ss, v ); } }
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; }
static PyObject *blocks(PyObject *self, PyObject *args) { PyObject *sa, *sb, *rl = NULL, *m; struct line *a, *b; struct hunk l, *h; int an, bn, count, pos = 0; if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) return NULL; an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); if (!a || !b) goto nomem; l.next = NULL; count = diff(a, an, b, bn, &l); if (count < 0) goto nomem; rl = PyList_New(count); if (!rl) goto nomem; for (h = l.next; h; h = h->next) { m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); PyList_SetItem(rl, pos, m); pos++; } nomem: free(a); free(b); freehunks(l.next); return rl ? rl : PyErr_NoMemory(); }
int PyCode_Addr2Line(PyCodeObject *co, int addrq) { Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2; unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab); int line = co->co_firstlineno; int addr = 0; while (--size >= 0) { addr += *p++; if (addr > addrq) break; line += *p++; } return line; }
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; }
static int w_reserve(WFILE *p, Py_ssize_t needed) { Py_ssize_t pos, size, delta; if (p->ptr == NULL) return 0; /* An error already occurred */ if (p->fp != NULL) { w_flush(p); return needed <= p->end - p->ptr; } assert(p->str != NULL); pos = p->ptr - p->buf; size = PyBytes_Size(p->str); if (size > 16*1024*1024) delta = (size >> 3); /* 12.5% overallocation */ else
static int np_char(char *p, PyObject *v, const formatdef *f) { if (PyUnicode_Check(v)) { v = _PyUnicode_AsDefaultEncodedString(v, NULL); if (v == NULL) return -1; } if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) { PyErr_SetString(StructError, "char format requires bytes or string of length 1"); return -1; } *p = *PyBytes_AsString(v); return 0; }