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 (!Bytes_Check (result)) { Py_DECREF (result); return -1; } retval = Bytes_GET_SIZE (result); memcpy (ptr, Bytes_AsString (result), retval); retval /= size; Py_DECREF (result); return retval; }
static int rw_read_th (SDL_RWops* context, void* ptr, int size, int maxnum) { RWHelper* helper = (RWHelper*) context->hidden.unknown.data1; PyObject* result; int retval; PyThreadState* oldstate; if (!helper->read) return -1; PyEval_AcquireLock (); oldstate = PyThreadState_Swap (helper->thread); result = PyObject_CallFunction (helper->read, "i", size * maxnum); if (!result) { PyErr_Print(); retval = -1; goto end; } if (!Bytes_Check (result)) { Py_DECREF (result); PyErr_Print(); retval = -1; goto end; } retval = Bytes_GET_SIZE (result); memcpy (ptr, Bytes_AsString (result), retval); retval /= size; Py_DECREF (result); end: PyThreadState_Swap (oldstate); PyEval_ReleaseLock (); return retval; }
static int _pyobj_read_threaded (SDL_RWops *ops, void* ptr, int size, int num) { _RWWrapper *wrapper = (_RWWrapper *) ops->hidden.unknown.data1; PyObject *result; int retval; PyThreadState* oldstate; if (!wrapper->read) return -1; PyEval_AcquireThread (wrapper->thread); oldstate = PyThreadState_Swap (wrapper->thread); result = PyObject_CallFunction (wrapper->read, "i", size * num); if (!result) { PyErr_Print (); retval = -1; goto end; } if (!Bytes_Check (result)) { Py_DECREF (result); PyErr_Print (); retval = -1; goto end; } retval = Bytes_GET_SIZE (result); memcpy (ptr, Bytes_AS_STRING (result), (size_t) retval); retval /= size; Py_DECREF (result); end: PyThreadState_Swap (oldstate); PyEval_ReleaseThread (wrapper->thread); return retval; }
static PyObject * psyco_quote_ident(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *ident = NULL, *obj = NULL, *result = NULL; connectionObject *conn; char *quoted = NULL; static char *kwlist[] = {"ident", "scope", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO", kwlist, &ident, &obj)) { return NULL; } if (PyObject_TypeCheck(obj, &cursorType)) { conn = ((cursorObject*)obj)->conn; } else if (PyObject_TypeCheck(obj, &connectionType)) { conn = (connectionObject*)obj; } else { PyErr_SetString(PyExc_TypeError, "argument 2 must be a connection or a cursor"); return NULL; } Py_INCREF(ident); /* for ensure_bytes */ if (!(ident = psycopg_ensure_bytes(ident))) { goto exit; } if (!(quoted = psycopg_escape_identifier(conn, Bytes_AS_STRING(ident), Bytes_GET_SIZE(ident)))) { goto exit; } result = conn_text_from_chars(conn, quoted); exit: PQfreemem(quoted); Py_XDECREF(ident); return result; }
static int _pyobj_read (SDL_RWops *ops, void* ptr, int size, int num) { _RWWrapper *wrapper = (_RWWrapper *) ops->hidden.unknown.data1; PyObject *result; int retval; if (!wrapper->read) return -1; result = PyObject_CallFunction (wrapper->read, "i", size * num); if (!result) return -1; if (!Bytes_Check (result)) { Py_DECREF (result); return -1; } retval = Bytes_GET_SIZE (result); memcpy (ptr, Bytes_AS_STRING (result), (size_t) retval); retval /= size; Py_DECREF (result); return retval; }
static PyObject* font_render(PyObject* self, PyObject* args) { TTF_Font* font = PyFont_AsFont (self); int aa; PyObject* text, *final; PyObject* fg_rgba_obj, *bg_rgba_obj = NULL; Uint8 rgba[] = {0, 0, 0, 0}; SDL_Surface* surf; SDL_Color foreg, backg; int just_return; if (!PyArg_ParseTuple(args, "OiO|O", &text, &aa, &fg_rgba_obj, &bg_rgba_obj)) { return NULL; } if (!RGBAFromColorObj(fg_rgba_obj, rgba)) { return RAISE(PyExc_TypeError, "Invalid foreground RGBA argument"); } foreg.r = rgba[0]; foreg.g = rgba[1]; foreg.b = rgba[2]; foreg.unused = 0; if (bg_rgba_obj != NULL) { if (!RGBAFromColorObj(bg_rgba_obj, rgba)) { bg_rgba_obj = NULL; backg.r = 0; backg.g = 0; backg.b = 0; backg.unused = 0; } else { backg.r = rgba[0]; backg.g = rgba[1]; backg.b = rgba[2]; backg.unused = 0; } } else { backg.r = 0; backg.g = 0; backg.b = 0; backg.unused = 0; } just_return = PyObject_Not(text); if (just_return) { int height = TTF_FontHeight(font); if (just_return == -1 || !(PyUnicode_Check(text) || Bytes_Check(text) || text == Py_None)) { PyErr_Clear(); return RAISE_TEXT_TYPE_ERROR(); } surf = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, height, 32, 0xff<<16, 0xff<<8, 0xff, 0); if (surf == NULL) { return RAISE(PyExc_SDLError, SDL_GetError()); } if (bg_rgba_obj != NULL) { Uint32 c = SDL_MapRGB(surf->format, backg.r, backg.g, backg.b); SDL_FillRect(surf, NULL, c); } else { SDL_SetColorKey(surf, SDL_SRCCOLORKEY, 0); } } else if (PyUnicode_Check(text)) { PyObject *bytes = PyUnicode_AsEncodedString(text, "utf-8", "replace"); const char *astring = NULL; if (!bytes) { return NULL; } astring = Bytes_AsString(bytes); if (strlen(astring) != Bytes_GET_SIZE(bytes)) { Py_DECREF(bytes); return RAISE(PyExc_ValueError, "A null character was found in the text"); } if (utf_8_needs_UCS_4(astring)) { Py_DECREF(bytes); return RAISE(PyExc_UnicodeError, "A Unicode character above '\\uFFFF' was found;" " not supported"); } if (aa) { if (bg_rgba_obj == NULL) { surf = TTF_RenderUTF8_Blended(font, astring, foreg); } else { surf = TTF_RenderUTF8_Shaded(font, astring, foreg, backg); } } else { surf = TTF_RenderUTF8_Solid(font, astring, foreg); } Py_DECREF(bytes); } else if (Bytes_Check(text)) { const char *astring = Bytes_AsString(text); if (strlen(astring) != Bytes_GET_SIZE(text)) { return RAISE(PyExc_ValueError, "A null character was found in the text"); } if (aa) { if (bg_rgba_obj == NULL) { surf = TTF_RenderText_Blended(font, astring, foreg); } else { surf = TTF_RenderText_Shaded(font, astring, foreg, backg); } } else { surf = TTF_RenderText_Solid(font, astring, foreg); } } else { return RAISE_TEXT_TYPE_ERROR(); } if (surf == NULL) { return RAISE(PyExc_SDLError, TTF_GetError()); } if (!aa && (bg_rgba_obj != NULL) && !just_return) { /* turn off transparancy */ SDL_SetColorKey(surf, 0, 0); surf->format->palette->colors[0].r = backg.r; surf->format->palette->colors[0].g = backg.g; surf->format->palette->colors[0].b = backg.b; } final = PySurface_New(surf);
static int _view_kind(PyObject *obj, void *view_kind_vptr) { unsigned long ch; _pc_view_kind_t *view_kind_ptr = (_pc_view_kind_t *)view_kind_vptr; if (PyUnicode_Check(obj)) { if (PyUnicode_GET_SIZE(obj) != 1) { PyErr_SetString(PyExc_TypeError, "expected a length 1 string for argument 3"); return 0; } ch = *PyUnicode_AS_UNICODE(obj); } else if (Bytes_Check(obj)) { if (Bytes_GET_SIZE(obj) != 1) { PyErr_SetString(PyExc_TypeError, "expected a length 1 string for argument 3"); return 0; } ch = *Bytes_AS_STRING(obj); } else { PyErr_Format(PyExc_TypeError, "expected a length one string for argument 3: got '%s'", Py_TYPE(obj)->tp_name); return 0; } switch (ch) { case 'R': case 'r': *view_kind_ptr = VIEWKIND_RED; break; case 'G': case 'g': *view_kind_ptr = VIEWKIND_GREEN; break; case 'B': case 'b': *view_kind_ptr = VIEWKIND_BLUE; break; case 'A': case 'a': *view_kind_ptr = VIEWKIND_ALPHA; break; case 'C': case 'c': *view_kind_ptr = VIEWKIND_COLORKEY; break; case 'P': case 'p': *view_kind_ptr = VIEWKIND_RGB; break; default: PyErr_Format(PyExc_TypeError, "unrecognized view kind '%c' for argument 3", (int)ch); return 0; } return 1; }
static int CPyStreamWrapper_Read (CPyStreamWrapper *wrapper, void *buf, pguint32 offset, pguint32 count, pguint32 *read_) { PyObject *result; pguint32 off, _read = 0; int retval = 1; char *tmp; char *ptr = (char*) buf; if (!wrapper->read || !wrapper->seek || !read_) return 0; if (offset != 0) { /* Move to the correct offset. */ result = PyObject_CallFunction (wrapper->seek, "li", offset, SEEK_SET); if (!result) { PyErr_Print(); retval = 0; goto end; } Py_DECREF (result); } if (count == 0) { /* Just a seek was wanted */ goto end; } result = PyObject_CallFunction (wrapper->read, "l", count); if (!result) { PyErr_Print (); retval = 0; goto end; } if (!Bytes_Check (result)) { Py_DECREF (result); PyErr_Print (); retval = 0; goto end; } _read = (pguint32) Bytes_GET_SIZE (result); tmp = Bytes_AS_STRING (result); off = 0; while ((_read - off) > SIZE_MAX) { memcpy (ptr + off, tmp + off, SIZE_MAX); off += SIZE_MAX; } memcpy (ptr + off, tmp + off, (size_t) (_read - off)); Py_DECREF (result); end: *read_ = _read; return retval; }
PyObject * Bytes_Format(PyObject *format, PyObject *args) { char *fmt, *res; Py_ssize_t arglen, argidx; Py_ssize_t reslen, rescnt, fmtcnt; int args_owned = 0; PyObject *result; PyObject *dict = NULL; if (format == NULL || !Bytes_Check(format) || args == NULL) { PyErr_BadInternalCall(); return NULL; } fmt = Bytes_AS_STRING(format); fmtcnt = Bytes_GET_SIZE(format); reslen = rescnt = fmtcnt + 100; result = Bytes_FromStringAndSize((char *)NULL, reslen); if (result == NULL) return NULL; res = Bytes_AsString(result); if (PyTuple_Check(args)) { arglen = PyTuple_GET_SIZE(args); argidx = 0; } else { arglen = -1; argidx = -2; } if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && !PyObject_TypeCheck(args, &Bytes_Type)) dict = args; while (--fmtcnt >= 0) { if (*fmt != '%') { if (--rescnt < 0) { rescnt = fmtcnt + 100; reslen += rescnt; if (_Bytes_Resize(&result, reslen)) return NULL; res = Bytes_AS_STRING(result) + reslen - rescnt; --rescnt; } *res++ = *fmt++; } else { /* Got a format specifier */ Py_ssize_t width = -1; int c = '\0'; PyObject *v = NULL; PyObject *temp = NULL; char *pbuf; Py_ssize_t len; fmt++; if (*fmt == '(') { char *keystart; Py_ssize_t keylen; PyObject *key; int pcount = 1; if (dict == NULL) { PyErr_SetString(PyExc_TypeError, "format requires a mapping"); goto error; } ++fmt; --fmtcnt; keystart = fmt; /* Skip over balanced parentheses */ while (pcount > 0 && --fmtcnt >= 0) { if (*fmt == ')') --pcount; else if (*fmt == '(') ++pcount; fmt++; } keylen = fmt - keystart - 1; if (fmtcnt < 0 || pcount > 0) { PyErr_SetString(PyExc_ValueError, "incomplete format key"); goto error; } key = Text_FromUTF8AndSize(keystart, keylen); if (key == NULL) goto error; if (args_owned) { Py_DECREF(args); args_owned = 0; } args = PyObject_GetItem(dict, key); Py_DECREF(key); if (args == NULL) { goto error; } args_owned = 1; arglen = -1; argidx = -2; } while (--fmtcnt >= 0) { c = *fmt++; break; } if (fmtcnt < 0) { PyErr_SetString(PyExc_ValueError, "incomplete format"); goto error; } if (c != '%') { v = getnextarg(args, arglen, &argidx); if (v == NULL) goto error; } switch (c) { case '%': pbuf = "%"; len = 1; break; case 's': /* only bytes! */ if (!Bytes_CheckExact(v)) { PyErr_Format(PyExc_ValueError, "only bytes values expected, got %s", Py_TYPE(v)->tp_name); goto error; } temp = v; Py_INCREF(v); pbuf = Bytes_AS_STRING(temp); len = Bytes_GET_SIZE(temp); break; default: PyErr_Format(PyExc_ValueError, "unsupported format character '%c' (0x%x) " "at index " FORMAT_CODE_PY_SSIZE_T, c, c, (Py_ssize_t)(fmt - 1 - Bytes_AsString(format))); goto error; } if (width < len) width = len; if (rescnt < width) { reslen -= rescnt; rescnt = width + fmtcnt + 100; reslen += rescnt; if (reslen < 0) { Py_DECREF(result); Py_XDECREF(temp); return PyErr_NoMemory(); } if (_Bytes_Resize(&result, reslen)) { Py_XDECREF(temp); return NULL; } res = Bytes_AS_STRING(result) + reslen - rescnt; } Py_MEMCPY(res, pbuf, len); res += len; rescnt -= len; while (--width >= len) { --rescnt; *res++ = ' '; } if (dict && (argidx < arglen) && c != '%') { PyErr_SetString(PyExc_TypeError, "not all arguments converted during string formatting"); Py_XDECREF(temp); goto error; } Py_XDECREF(temp); } /* '%' */ } /* until end */ if (argidx < arglen && !dict) { PyErr_SetString(PyExc_TypeError, "not all arguments converted during string formatting"); goto error; } if (args_owned) { Py_DECREF(args); } if (_Bytes_Resize(&result, reslen - rescnt)) return NULL; return result; error: Py_DECREF(result); if (args_owned) { Py_DECREF(args); } return NULL; }