PyObject * string_count(PyStringObject *self, PyObject *args) { PyObject *sub_obj; const char *str = PyString_AS_STRING(self), *sub; Py_ssize_t sub_len; Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end)) return NULL; if (PyString_Check(sub_obj)) { sub = PyString_AS_STRING(sub_obj); sub_len = PyString_GET_SIZE(sub_obj); } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(sub_obj)) { Py_ssize_t count; count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); if (count == -1) return NULL; else return PyInt_FromSsize_t(count); } #endif else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) return NULL; ADJUST_INDICES(start, end, PyString_GET_SIZE(self)); return PyInt_FromSsize_t( stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) ); }
NUITKA_MAY_BE_UNUSED static bool STRING_ADD_INCREMENTAL( PyObject **operand1, PyObject *operand2 ) { assert( PyString_CheckExact( *operand1 ) ); assert( !PyString_CHECK_INTERNED( *operand1 ) ); assert( PyString_CheckExact( operand2 ) ); Py_ssize_t operand1_size = PyString_GET_SIZE( *operand1 ); Py_ssize_t operand2_size = PyString_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( STRING_RESIZE( operand1, new_size ) == false )) { return false; } memcpy( PyString_AS_STRING( *operand1 ) + operand1_size, PyString_AS_STRING( operand2 ), operand2_size ); return true; }
string_find_internal(PyStringObject *self, PyObject *args, int dir) { PyObject *subobj; const char *sub; Py_ssize_t sub_len; Py_ssize_t start=0, end=PY_SSIZE_T_MAX; if (!stringlib_parse_args_finds("find/rfind/index/rindex", args, &subobj, &start, &end)) return -2; if (PyString_Check(subobj)) { sub = PyString_AS_STRING(subobj); sub_len = PyString_GET_SIZE(subobj); } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) return PyUnicode_Find( (PyObject *)self, subobj, start, end, dir); #endif else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) /* XXX - the "expected a character buffer object" is pretty confusing for a non-expert. remap to something else ? */ return -2; if (dir > 0) return stringlib_find_slice( PyString_AS_STRING(self), PyString_GET_SIZE(self), sub, sub_len, start, end); else return stringlib_rfind_slice( PyString_AS_STRING(self), PyString_GET_SIZE(self), sub, sub_len, start, end); }
static size_t wsgi_getheaders(Request* request, PyObject* buf) { char* bufp = PyString_AS_STRING(buf); Py_ssize_t i; #define buf_write(src, len) \ do { \ size_t n = len; \ const char* s = src; \ while(n--) *bufp++ = *s++; \ } while(0) #define buf_write2(src) buf_write(src, strlen(src)) buf_write2("HTTP/1.1 "); buf_write(PyString_AS_STRING(request->status), PyString_GET_SIZE(request->status)); for(i=0; i<PyList_GET_SIZE(request->headers); ++i) { PyObject *tuple = PyList_GET_ITEM(request->headers, i); PyObject *field = PyTuple_GET_ITEM(tuple, 0), *value = PyTuple_GET_ITEM(tuple, 1); buf_write2("\r\n"); buf_write(PyString_AS_STRING(field), PyString_GET_SIZE(field)); buf_write2(": "); buf_write(PyString_AS_STRING(value), PyString_GET_SIZE(value)); } if(request->state.chunked_response) buf_write2("\r\nTransfer-Encoding: chunked"); buf_write2("\r\n\r\n"); return bufp - PyString_AS_STRING(buf); }
static bool send_chunk(Request* request) { Py_ssize_t bytes_sent; static uv_buf_t resbuf; uv_write_t * wr; wr = (uv_buf_t*) malloc(sizeof *wr); //dprint("发送chunk:\n%s",PyString_AS_STRING(request->current_chunk) + request->current_chunk_p); dprint("发送大小:%d",PyString_GET_SIZE(request->current_chunk) - request->current_chunk_p); assert(request->current_chunk != NULL); assert(!(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk) && PyString_GET_SIZE(request->current_chunk) != 0)); resbuf = uv_buf_init(PyString_AS_STRING(request->current_chunk) + request->current_chunk_p, PyString_GET_SIZE(request->current_chunk) - request->current_chunk_p); bytes_sent = uv_write( wr, request->ev_watcher, &resbuf, 1, after_write); if(bytes_sent == -1){ dprint("发送数据出错"); dprint("chunk:\n%s",PyString_AS_STRING(request->current_chunk) + request->current_chunk_p); return handle_nonzero_errno(request); } request->current_chunk_p += resbuf.len; if(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk)) { Py_CLEAR(request->current_chunk); request->current_chunk_p = 0; return false; } //@@@@@@@@@@@@@@@@@ Py_CLEAR(request->current_chunk); //@@@@@@@@@@@@@@@@@ free(resbuf.base); return true; }
static bool send_chunk(Request* request) { Py_ssize_t bytes_sent; assert(request->current_chunk != NULL); assert(!(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk) && PyString_GET_SIZE(request->current_chunk) != 0)); bytes_sent = write( request->client_fd, PyString_AS_STRING(request->current_chunk) + request->current_chunk_p, PyString_GET_SIZE(request->current_chunk) - request->current_chunk_p ); if(bytes_sent == -1) return handle_nonzero_errno(request); request->current_chunk_p += bytes_sent; if(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk)) { Py_CLEAR(request->current_chunk); request->current_chunk_p = 0; return false; } return true; }
static bool wsgi_senditer(Request* request) { #define ITER_MAXSEND 1024*4 PyObject* item = request->iterable_next; if(!item) return true; ssize_t sent = 0; while(item && sent < ITER_MAXSEND) { TYPECHECK(item, PyString, "wsgi iterable items", true); if(!sendall(request, PyString_AS_STRING(item), PyString_GET_SIZE(item))) return true; sent += PyString_GET_SIZE(item); Py_DECREF(item); item = PyIter_Next(request->response); if(PyErr_Occurred()) { /* TODO: What to do here? Parts of the response are already sent */ return true; } } if(item) { request->iterable_next = item; return false; } else { return true; } }
PyObject* string_rsplit(PyStringObject* self, PyObject* args) { Py_ssize_t len = PyString_GET_SIZE(self), n; Py_ssize_t maxsplit = -1; const char* s = PyString_AS_STRING(self), *sub; PyObject* subobj = Py_None; if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) return NULL; if (maxsplit < 0) maxsplit = PY_SSIZE_T_MAX; if (subobj == Py_None) return stringlib_rsplit_whitespace((PyObject*)self, s, len, maxsplit); if (PyString_Check(subobj)) { sub = PyString_AS_STRING(subobj); n = PyString_GET_SIZE(subobj); } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) return PyUnicode_RSplit((PyObject*)self, subobj, maxsplit); #endif else if (PyObject_AsCharBuffer(subobj, &sub, &n)) return NULL; return stringlib_rsplit((PyObject*)self, s, len, sub, n, maxsplit); }
/* Write stuff to the write buffer (possibly flush it) */ static int generic_write(genericstreamobject *self, PyObject *data) { PyObject *datastr; bufitem *item; Py_ssize_t size; if (!(datastr = PyObject_Str(data))) return -1; if (!(item = bufitem_new())) goto error; size = self->wbuf_size + PyString_GET_SIZE(datastr); if ((size < self->wbuf_size) && (generic_flush(self, 0) == -1)) goto error_item; item->load = datastr; item->next = self->wbuf; self->wbuf_size += PyString_GET_SIZE(datastr); self->wbuf = item; if ((self->wbuf_size > self->chunk_size) && (generic_flush(self, 0) == -1)) return -1; return 0; error_item: (void)bufitem_del(item); error: Py_DECREF(datastr); return -1; }
static Tcl_Obj* AsObj(PyObject *value) { Tcl_Obj *result; if (PyString_Check(value)) return Tcl_NewStringObj(PyString_AS_STRING(value), PyString_GET_SIZE(value)); else if (PyInt_Check(value)) return Tcl_NewLongObj(PyInt_AS_LONG(value)); else if (PyFloat_Check(value)) return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); else if (PyTuple_Check(value)) { Tcl_Obj **argv = (Tcl_Obj**) ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*)); int i; if(!argv) return 0; for(i=0;i<PyTuple_Size(value);i++) argv[i] = AsObj(PyTuple_GetItem(value,i)); result = Tcl_NewListObj(PyTuple_Size(value), argv); ckfree(FREECAST argv); return result; } else if (PyUnicode_Check(value)) { #if TKMAJORMINOR <= 8001 /* In Tcl 8.1 we must use UTF-8 */ PyObject* utf8 = PyUnicode_AsUTF8String(value); if (!utf8) return 0; result = Tcl_NewStringObj(PyString_AS_STRING(utf8), PyString_GET_SIZE(utf8)); Py_DECREF(utf8); return result; #else /* TKMAJORMINOR > 8001 */ /* In Tcl 8.2 and later, use Tcl_NewUnicodeObj() */ if (sizeof(Py_UNICODE) != sizeof(Tcl_UniChar)) { /* XXX Should really test this at compile time */ PyErr_SetString(PyExc_SystemError, "Py_UNICODE and Tcl_UniChar differ in size"); return 0; } return Tcl_NewUnicodeObj(PyUnicode_AS_UNICODE(value), PyUnicode_GET_SIZE(value)); #endif /* TKMAJORMINOR > 8001 */ } else { PyObject *v = PyObject_Str(value); if (!v) return 0; result = AsObj(v); Py_DECREF(v); return result; } }
static PyObject* MakeConnectionString(PyObject* existing, PyObject* parts) { // Creates a connection string from an optional existing connection string plus a dictionary of keyword value // pairs. The keywords must be String objects and the values must be Unicode objects. Py_ssize_t length = 0; if (existing) length = PyUnicode_GET_SIZE(existing) + 1; // + 1 to add a trailing Py_ssize_t pos = 0; PyObject* key = 0; PyObject* value = 0; while (PyDict_Next(parts, &pos, &key, &value)) { length += PyString_GET_SIZE(key) + 1 + PyUnicode_GET_SIZE(value) + 1; // key=value; } PyObject* result = PyUnicode_FromUnicode(0, length); if (!result) return 0; Py_UNICODE* buffer = PyUnicode_AS_UNICODE(result); Py_ssize_t offset = 0; if (existing) { memcpy(&buffer[offset], PyUnicode_AS_UNICODE(existing), PyUnicode_GET_SIZE(existing) * sizeof(Py_UNICODE)); offset += PyUnicode_GET_SIZE(existing); buffer[offset++] = (Py_UNICODE)';'; } pos = 0; while (PyDict_Next(parts, &pos, &key, &value)) { const char* szKey = PyString_AS_STRING(key); for (int i = 0; i < PyString_GET_SIZE(key); i++) buffer[offset++] = (Py_UNICODE)szKey[i]; buffer[offset++] = (Py_UNICODE)'='; memcpy(&buffer[offset], PyUnicode_AS_UNICODE(value), PyUnicode_GET_SIZE(value) * sizeof(Py_UNICODE)); offset += PyUnicode_GET_SIZE(value); buffer[offset++] = (Py_UNICODE)';'; } I(offset == length); return result; }
// _tnetstring_loads: parse tnetstring-format value from a string. // static PyObject* _tnetstring_loads(PyObject* self, PyObject *args) { PyObject *string = NULL; PyObject *encoding = Py_None; PyObject *val = NULL; tns_ops *ops = &_tnetstring_ops_bytes; char *data; size_t len; if(!PyArg_UnpackTuple(args, "loads", 1, 2, &string, &encoding)) { return NULL; } if(!PyString_Check(string)) { PyErr_SetString(PyExc_TypeError, "arg must be a string"); return NULL; } Py_INCREF(string); if(encoding == Py_None) { data = PyString_AS_STRING(string); len = PyString_GET_SIZE(string); val = tns_parse(ops, data, len, NULL); } else { if(!PyString_Check(encoding)) { PyErr_SetString(PyExc_TypeError, "encoding must be a string"); goto error; } Py_INCREF(encoding); ops = _tnetstring_get_unicode_ops(encoding); if(ops == NULL) { Py_DECREF(encoding); goto error; } data = PyString_AS_STRING(string); len = PyString_GET_SIZE(string); val = tns_parse(ops, data, len, NULL); free(ops); Py_DECREF(encoding); } Py_DECREF(string); return val; error: Py_DECREF(string); return NULL; }
static PyObject* _tnetstring_loads(PyObject* self, PyObject *args) { PyObject *string, *val; char *data; size_t len; if(!PyArg_UnpackTuple(args, "loads", 1, 1, &string)) { return NULL; } if(!PyString_Check(string)) { PyErr_SetString(PyExc_TypeError, "arg must be a string"); return NULL; } Py_INCREF(string); data = PyString_AS_STRING(string); len = PyString_GET_SIZE(string); val = tns_parse(data, len, NULL); Py_DECREF(string); if(val == NULL) { return NULL; } return val; }
static int _set_char(const char *name, char *target, PyObject *src, char dflt) { if (src == NULL) *target = dflt; else { *target = '\0'; if (src != Py_None) { Py_ssize_t len; if (!PyString_Check(src)) { PyErr_Format(PyExc_TypeError, "\"%s\" must be string, not %.200s", name, src->ob_type->tp_name); return -1; } len = PyString_GET_SIZE(src); if (len > 1) { PyErr_Format(PyExc_TypeError, "\"%s\" must be an 1-character string", name); return -1; } if (len > 0) *target = *PyString_AS_STRING(src); } } return 0; }
static int future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) { int i, found_docstring = 0, done = 0, prev_line = 0; if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) return 1; /* A subsequent pass will detect future imports that don't appear at the beginning of the file. There's one case, however, that is easier to handle here: A series of imports joined by semi-colons, where the first import is a future statement but some subsequent import has the future form but is preceded by a regular import. */ for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) { stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); if (done && s->lineno > prev_line) return 1; prev_line = s->lineno; /* The tests below will return from this function unless it is still possible to find a future statement. The only things that can precede a future statement are another future statement and a doc string. */ if (s->kind == ImportFrom_kind) { identifier modname = s->v.ImportFrom.module; if (modname && PyString_GET_SIZE(modname) == 10 && !strcmp(PyString_AS_STRING(modname), "__future__")) { if (done) { PyErr_SetString(PyExc_SyntaxError, ERR_LATE_FUTURE); PyErr_SyntaxLocation(filename, s->lineno); return 0; } if (!future_check_features(ff, s, filename)) return 0; ff->ff_lineno = s->lineno; } else done = 1; } else if (s->kind == Expr_kind && !found_docstring) { expr_ty e = s->v.Expr.value; if (e->kind != Str_kind) done = 1; else found_docstring = 1; } else done = 1; } return 1; }
:See: `EncoderInterface`"); static PyObject * TDI_SoupEncoderType_endtag(tdi_soup_encoder_t *self, PyObject *args) { PyObject *name, *result; char *cresult; Py_ssize_t size; if (!(PyArg_ParseTuple(args, "S", &name))) return NULL; if (!PyString_CheckExact(name)) { if (!(name = PyObject_Str(name))) return NULL; } else Py_INCREF(name); size = PyString_GET_SIZE(name); if (!(result = PyString_FromStringAndSize(NULL, size + 3))) { /* </> */ Py_DECREF(name); return NULL; } cresult = PyString_AS_STRING(result); *cresult++ = '<'; *cresult++ = '/'; (void)memcpy(cresult, PyString_AS_STRING(name), (size_t)size); Py_DECREF(name); cresult += size; *cresult = '>'; return result; }
static inline bool inspect_headers(Request* request) { Py_ssize_t i; PyObject* tuple; for(i=0; i<PyList_GET_SIZE(request->headers); ++i) { tuple = PyList_GET_ITEM(request->headers, i); if(!PyTuple_Check(tuple) || PyTuple_GET_SIZE(tuple) != 2) goto err; PyObject* field = PyTuple_GET_ITEM(tuple, 0); PyObject* value = PyTuple_GET_ITEM(tuple, 1); if(!PyString_Check(field) || !PyString_Check(value)) goto err; if(!strncasecmp(PyString_AS_STRING(field), "Content-Length", PyString_GET_SIZE(field))) request->state.response_length_unknown = false; } return true; err: TYPE_ERROR_INNER("start_response argument 2", "a list of 2-tuples", "(found invalid '%.200s' object at position %zd)", Py_TYPE(tuple)->tp_name, i); return false; }
/* fixup the last lines of a and b when the patch has no newline at eof */ static void _fix_newline(PyObject *hunk, PyObject *a, PyObject *b) { int hunksz = PyList_Size(hunk); PyObject *s = PyList_GET_ITEM(hunk, hunksz-1); char *l = PyString_AS_STRING(s); int alen = PyList_Size(a); int blen = PyList_Size(b); char c = l[0]; PyObject *hline; int sz = PyString_GET_SIZE(s); if (sz > 1 && l[sz-2] == '\r') /* tolerate CRLF in last line */ sz -= 1; hline = PyString_FromStringAndSize(l, sz-1); if (c == ' ' || c == '+') { PyObject *rline = PyString_FromStringAndSize(l + 1, sz - 2); PyList_SetItem(b, blen-1, rline); } if (c == ' ' || c == '-') { Py_INCREF(hline); PyList_SetItem(a, alen-1, hline); } PyList_SetItem(hunk, hunksz-1, hline); }
bool rpcError_Extract(PyObject *error, int *errorCode, char **errorString) { PyObject *pyErrorCode, *pyErrorString; assert(PyErr_GivenExceptionMatches(error, rpcError)); pyErrorCode = PyObject_GetAttrString(error, "errorCode"); if (errorCode && PyInt_Check(pyErrorCode)) *errorCode = (int)PyInt_AS_LONG(pyErrorCode); else { fprintf(rpcLogger, "invalid error code... deerror to -1\n"); *errorCode = -1; } pyErrorString = PyObject_GetAttrString(error, "errorString"); if (errorString && PyString_Check(pyErrorString)) { *errorString = alloc(PyString_GET_SIZE(pyErrorString) + 1); if (*errorString == NULL) return false; strcpy(*errorString, PyString_AS_STRING(pyErrorString)); } else { fprintf(rpcLogger, "invalid error string... deerror to 'unknown error'\n"); *errorString = alloc(strlen("unknown error") + 1); if (*errorString == NULL) return false; strcpy(*errorString, "unknown error"); } return true; }
static PyObject * s_unpack(PyObject *self, PyObject *inputstr) { char *start; Py_ssize_t len; PyObject *args=NULL, *result; PyStructObject *soself = (PyStructObject *)self; assert(PyStruct_Check(self)); assert(soself->s_codes != NULL); if (inputstr == NULL) goto fail; if (PyString_Check(inputstr) && PyString_GET_SIZE(inputstr) == soself->s_size) { return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); } args = PyTuple_Pack(1, inputstr); if (args == NULL) return NULL; if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) goto fail; if (soself->s_size != len) goto fail; result = s_unpack_internal(soself, start); Py_DECREF(args); return result; fail: Py_XDECREF(args); PyErr_Format(StructError, "unpack requires a string argument of length %zd", soself->s_size); return NULL; }
PyObject * PyNumber_Int(PyObject *o) { PyNumberMethods *m; const char *buffer; int buffer_len; if (o == NULL) return null_error(); if (PyInt_Check(o)) { Py_INCREF(o); return o; } if (PyString_Check(o)) return int_from_string(PyString_AS_STRING(o), PyString_GET_SIZE(o)); if (PyUnicode_Check(o)) return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o), PyUnicode_GET_SIZE(o), 10); m = o->ob_type->tp_as_number; if (m && m->nb_int) return m->nb_int(o); if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) return int_from_string((char*)buffer, buffer_len); return type_error("object can't be converted to int"); }
/* save a file from a python string */ static PyObject * save_file(PyObject *self, PyObject *args) { int status; const char *filename; PyByteArrayObject *obj; char *data; unsigned size; if (!PyArg_ParseTuple(args, "sO", &filename, &obj)) return NULL; if (!PyString_Check(obj)) return NULL; data = PyString_AS_STRING(obj); size = PyString_GET_SIZE(obj); Py_BEGIN_ALLOW_THREADS; status = _save_file(filename, size, data); Py_END_ALLOW_THREADS; if (status != 0) { PyErr_SetString(FleaError, "file save failed"); return NULL; } Py_RETURN_NONE; }
/* Modified from PyString_AsStringAndSize */ static int check_string(PyObject *obj, char **s) { long len; if (!PyString_Check(obj)) { PyErr_Format(PyExc_TypeError, "expected string object, " "%.200s found", obj->ob_type->tp_name); return -1; } *s = PyString_AS_STRING(obj); len = PyString_GET_SIZE(obj); if ((int)strlen(*s) != len) { PyErr_SetString(PyExc_TypeError, "expected string without null bytes"); return -1; } if (len > MAX_STRING_LEN) { PyErr_Format(PyExc_TypeError, "string longer than compiled limit of " "%d characters", MAX_STRING_LEN); return -1; } return 0; }
PyObject * PyNumber_Long(PyObject *o) { PyNumberMethods *m; const char *buffer; int buffer_len; if (o == NULL) return null_error(); if (PyLong_Check(o)) { Py_INCREF(o); return o; } if (PyString_Check(o)) /* need to do extra error checking that PyLong_FromString() * doesn't do. In particular long('9.5') must raise an * exception, not truncate the float. */ return long_from_string(PyString_AS_STRING(o), PyString_GET_SIZE(o)); if (PyUnicode_Check(o)) /* The above check is done in PyLong_FromUnicode(). */ return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), PyUnicode_GET_SIZE(o), 10); m = o->ob_type->tp_as_number; if (m && m->nb_long) return m->nb_long(o); if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) return long_from_string(buffer, buffer_len); return type_error("object can't be converted to long"); }
PyObject* string__format__(PyObject* self, PyObject* args) { PyObject *format_spec; PyObject *result = NULL; PyObject *tmp = NULL; /* If 2.x, convert format_spec to the same type as value */ /* This is to allow things like u''.format('') */ if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) goto done; if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) { PyErr_Format(PyExc_TypeError, "__format__ arg must be str " "or unicode, not %s", Py_TYPE(format_spec)->tp_name); goto done; } tmp = PyObject_Str(format_spec); if (tmp == NULL) goto done; format_spec = tmp; result = _PyBytes_FormatAdvanced(self, PyString_AS_STRING(format_spec), PyString_GET_SIZE(format_spec)); done: Py_XDECREF(tmp); return result; }
static PyObject* _tnetstring_pop(PyObject* self, PyObject *args) { PyObject *string, *val, *rest; char *data, *remain; size_t len; if(!PyArg_UnpackTuple(args, "pop", 1, 1, &string)) { return NULL; } if(!PyString_Check(string)) { PyErr_SetString(PyExc_TypeError, "arg must be a string"); return NULL; } Py_INCREF(string); data = PyString_AS_STRING(string); len = PyString_GET_SIZE(string); val = tns_parse(data, len, &remain); Py_DECREF(string); if(val == NULL) { return NULL; } rest = PyString_FromStringAndSize(remain, len-(remain-data)); return PyTuple_Pack(2, val, rest); }
PyObject *encodedir(PyObject *self, PyObject *args) { Py_ssize_t len, newlen; PyObject *pathobj, *newobj; char *path; if (!PyArg_ParseTuple(args, "O:encodedir", &pathobj)) return NULL; if (PyString_AsStringAndSize(pathobj, &path, &len) == -1) { PyErr_SetString(PyExc_TypeError, "expected a string"); return NULL; } newlen = len ? _encodedir(NULL, 0, path, len + 1) : 1; if (newlen == len + 1) { Py_INCREF(pathobj); return pathobj; } newobj = PyString_FromStringAndSize(NULL, newlen); if (newobj) { PyString_GET_SIZE(newobj)--; _encodedir(PyString_AS_STRING(newobj), newlen, path, len + 1); } return newobj; }
static int rw_read(SDL_RWops* context, void* ptr, int size, int maxnum) { RWHelper* helper = (RWHelper*)context->hidden.unknown.data1; PyObject* result; int retval; if(!helper->read) return -1; result = PyObject_CallFunction(helper->read, "i", size * maxnum); if(!result) return -1; if(!PyString_Check(result)) { Py_DECREF(result); return -1; } retval = PyString_GET_SIZE(result); memcpy(ptr, PyString_AsString(result), retval); retval /= size; Py_DECREF(result); return retval; }
static PyObject * dbm_setdefault(register dbmobject *dp, PyObject *args) { datum key, val; PyObject *defvalue = NULL; int tmp_size; if (!PyArg_ParseTuple(args, "s#|S:setdefault", &key.dptr, &tmp_size, &defvalue)) return NULL; key.dsize = tmp_size; check_dbmobject_open(dp); val = dbm_fetch(dp->di_dbm, key); if (val.dptr != NULL) return PyString_FromStringAndSize(val.dptr, val.dsize); if (defvalue == NULL) { defvalue = PyString_FromStringAndSize(NULL, 0); if (defvalue == NULL) return NULL; } else Py_INCREF(defvalue); val.dptr = PyString_AS_STRING(defvalue); val.dsize = PyString_GET_SIZE(defvalue); if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); return NULL; } return defvalue; }
static PyObject* cfstring_to_pystring(CFStringRef ref) { const char* s; s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8); if (s) { return PyString_FromString(s); } else { CFIndex len = CFStringGetLength(ref); Boolean ok; PyObject* result; result = PyString_FromStringAndSize(NULL, len*4); ok = CFStringGetCString(ref, PyString_AS_STRING(result), PyString_GET_SIZE(result), kCFStringEncodingUTF8); if (!ok) { Py_DECREF(result); return NULL; } else { _PyString_Resize(&result, strlen(PyString_AS_STRING(result))); } return result; } }