static PyObject* condition_notify(ConditionObject *self, int num) { PyObject *res, *waiters; PyObject *iter, *item; DEBUG("self:%p", self); res = call_method((PyObject*)self, "_is_owned"); if (res == NULL) { return NULL; } if (PyObject_Not(res)) { Py_DECREF(res); PyErr_SetString(PyExc_RuntimeError, "cannot release un-acquired lock"); return NULL; } Py_DECREF(res); waiters = PyList_GetSlice(self->waiters, 0, num); if (waiters == NULL) { return NULL; } if (PyObject_Not(waiters)) { Py_RETURN_NONE; } iter = PyObject_GetIter(waiters); if (PyErr_Occurred()) { return NULL; } while ((item = PyIter_Next(iter))) { res = semaphore_release((SemaphoreObject*)item); Py_XDECREF(res); if (res == NULL) { Py_DECREF(item); goto err; } if (remove_from_list((PyListObject*)self->waiters, item) == -1) { Py_DECREF(item); goto err; } Py_DECREF(item); /* DEBUG("self->waiters len:%d", PyList_Size(self->waiters)); */ } Py_DECREF(waiters); Py_DECREF(iter); Py_RETURN_NONE; err: Py_DECREF(waiters); Py_DECREF(iter); return NULL; }
/* Compare two range objects. Return 1 for equal, 0 for not equal and -1 on error. The algorithm is roughly the C equivalent of if r0 is r1: return True if len(r0) != len(r1): return False if not len(r0): return True if r0.start != r1.start: return False if len(r0) == 1: return True return r0.step == r1.step */ static int range_equals(rangeobject *r0, rangeobject *r1) { int cmp_result; PyObject *one; if (r0 == r1) return 1; cmp_result = PyObject_RichCompareBool(r0->length, r1->length, Py_EQ); /* Return False or error to the caller. */ if (cmp_result != 1) return cmp_result; cmp_result = PyObject_Not(r0->length); /* Return True or error to the caller. */ if (cmp_result != 0) return cmp_result; cmp_result = PyObject_RichCompareBool(r0->start, r1->start, Py_EQ); /* Return False or error to the caller. */ if (cmp_result != 1) return cmp_result; one = PyLong_FromLong(1); if (!one) return -1; cmp_result = PyObject_RichCompareBool(r0->length, one, Py_EQ); Py_DECREF(one); /* Return True or error to the caller. */ if (cmp_result != 0) return cmp_result; return PyObject_RichCompareBool(r0->step, r1->step, Py_EQ); }
/* Compare two range objects. Return 1 for equal, 0 for not equal and -1 on error. The algorithm is roughly the C equivalent of if r0 is r1: return True if len(r0) != len(r1): return False if not len(r0): return True if r0.start != r1.start: return False if len(r0) == 1: return True return r0.step == r1.step */ static int range_equals(rangeobject *r0, rangeobject *r1) { int cmp_result; if (r0 == r1) return 1; cmp_result = PyObject_RichCompareBool(r0->length, r1->length, Py_EQ); /* Return False or error to the caller. */ if (cmp_result != 1) return cmp_result; cmp_result = PyObject_Not(r0->length); /* Return True or error to the caller. */ if (cmp_result != 0) return cmp_result; cmp_result = PyObject_RichCompareBool(r0->start, r1->start, Py_EQ); /* Return False or error to the caller. */ if (cmp_result != 1) return cmp_result; cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_One, Py_EQ); /* Return True or error to the caller. */ if (cmp_result != 0) return cmp_result; return PyObject_RichCompareBool(r0->step, r1->step, Py_EQ); }
static PyObject * pickle_slotnames(PyTypeObject *cls) { PyObject *slotnames; slotnames = PyDict_GetItem(cls->tp_dict, py___slotnames__); if (slotnames) { int n = PyObject_Not(slotnames); if (n < 0) return NULL; if (n) slotnames = Py_None; Py_INCREF(slotnames); return slotnames; } slotnames = PyObject_CallFunctionObjArgs(copy_reg_slotnames, (PyObject*)cls, NULL); if (slotnames && !(slotnames == Py_None || PyList_Check(slotnames))) { PyErr_SetString(PyExc_TypeError, "copy_reg._slotnames didn't return a list or None"); Py_DECREF(slotnames); return NULL; } return slotnames; }
/* Hash function for range objects. Rough C equivalent of if not len(r): return hash((len(r), None, None)) if len(r) == 1: return hash((len(r), r.start, None)) return hash((len(r), r.start, r.step)) */ static Py_hash_t range_hash(rangeobject *r) { PyObject *t; Py_hash_t result = -1; int cmp_result; t = PyTuple_New(3); if (!t) return -1; Py_INCREF(r->length); PyTuple_SET_ITEM(t, 0, r->length); cmp_result = PyObject_Not(r->length); if (cmp_result == -1) goto end; if (cmp_result == 1) { Py_INCREF(Py_None); Py_INCREF(Py_None); PyTuple_SET_ITEM(t, 1, Py_None); PyTuple_SET_ITEM(t, 2, Py_None); } else { PyObject *one; Py_INCREF(r->start); PyTuple_SET_ITEM(t, 1, r->start); one = PyLong_FromLong(1); if (!one) goto end; cmp_result = PyObject_RichCompareBool(r->length, one, Py_EQ); Py_DECREF(one); if (cmp_result == -1) goto end; if (cmp_result == 1) { Py_INCREF(Py_None); PyTuple_SET_ITEM(t, 2, Py_None); } else { Py_INCREF(r->step); PyTuple_SET_ITEM(t, 2, r->step); } } result = PyObject_Hash(t); end: Py_DECREF(t); return result; }
static int macerror_callback(LIBSSH2_SESSION *session, const char *packet, int packet_len, void **abstract) { PyObject *callback = ((SSH2_SessionObj *) *abstract)->cb_macerror; PyObject *rv; PyGILState_STATE gstate = PyGILState_Ensure(); int ret = -1; #if PY_MAJOR_VERSION < 3 rv = PyObject_CallFunction(callback, "s#", packet, packet_len); #else rv = PyObject_CallFunction(callback, "y#", packet, packet_len); #endif if (rv == NULL || (ret = PyObject_Not(rv)) == -1) PyErr_WriteUnraisable(callback); Py_XDECREF(rv); PyGILState_Release(gstate); return ret; }
static PyObject* downcast(PyObject* self, PyObject* args) { PyObject *obj, *cls; if (!PyArg_ParseTuple(args, "OO", &obj, &cls)) { return NULL; } if ((PyObject *) Py_TYPE(obj) == cls) { Py_INCREF(obj); return obj; } PyObject* apiModule = GetAPIModule(); auto_pyobject fromTy = PyObject_GetAttrString(obj, "_llvm_type_"); auto_pyobject toTy = PyObject_GetAttrString(cls, "_llvm_type_"); std::ostringstream oss; auto_pyobject fromTyStr = PyObject_Str(*fromTy); auto_pyobject toTyStr = PyObject_Str(*toTy); const char * fromCS = PyString_AsString(*fromTyStr); const char * toCS = PyString_AsString(*toTyStr); oss << "downcast_"; NormalizeString(oss, fromCS); oss << "_to_"; NormalizeString(oss, toCS); std::string fname = oss.str(); auto_pyobject caster = PyObject_GetAttrString(GetDowncastModule(), fname.c_str()); if (!caster) { std::ostringstream oss; oss << "Downcast from " << fromCS << " to " << toCS; std::string errmsg = oss.str(); PyErr_SetString(PyExc_TypeError, errmsg.c_str()); return NULL; } auto_pyobject oldObj = Unwrap(obj); auto_pyobject newObj = PyObject_CallFunctionObjArgs(*caster, *oldObj, NULL); bool used_to_own = HasOwnership(*oldObj); PyObject *result = Wrap(*newObj, !used_to_own); int status = PyObject_Not(result); switch(status) { case 0: return result; case 1: default: PyErr_SetString(PyExc_ValueError, "Downcast failed"); Py_XDECREF(result); return NULL; } }
static PyObject* ConditionObject_wait(ConditionObject *self, PyObject *args, PyObject *kwargs) { PyObject *timeout = NULL; PyObject *res, *result, *saved_state = NULL; SemaphoreObject *waiter; long seconds = 0; static char *keywords[] = {"timeout", NULL}; DEBUG("self:%p", self); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:wait", keywords, &timeout)) { return NULL; } if (timeout == NULL) { seconds = 0; } else if (timeout == Py_None) { seconds = 0; } else if (PyLong_Check(timeout)) { seconds = PyLong_AsLong(timeout); if (seconds < 0) { PyErr_SetString(PyExc_ValueError, "timeout value out of range"); return NULL; } } else { PyErr_SetString(PyExc_TypeError, "an integer is required"); return NULL; } res = call_method((PyObject*)self, "_is_owned"); if (res == NULL) { return NULL; } if (PyObject_Not(res)) { Py_DECREF(res); PyErr_SetString(PyExc_RuntimeError, "cannot release un-acquired lock"); return NULL; } Py_DECREF(res); waiter = (SemaphoreObject*)PyObject_CallFunctionObjArgs((PyObject*)&SemaphoreObjectType, NULL); if (waiter == NULL) { return NULL; } res = semaphore_acquire(waiter, Py_True, 0); Py_XDECREF(res); if (res == NULL) { return NULL; } if (PyList_Append(self->waiters, (PyObject*)waiter) == -1) { return NULL; } saved_state = call_method((PyObject*)self, "_release_save"); if (saved_state == NULL) { Py_DECREF(waiter); return NULL; } res = semaphore_acquire(waiter, Py_True, seconds); result = call_method_args1((PyObject*)self, "_acquire_restore", saved_state); Py_DECREF(saved_state); if (result == NULL) { return NULL; } Py_DECREF(waiter); return res; }
PyObject* semaphore_acquire(SemaphoreObject *self, PyObject *blocking, long timeout) { PyObject *res = NULL; PyObject *current = NULL; PyObject *timer = NULL; LoopObject *loop = NULL; DEBUG("self:%p timeout:%ld counter:%d", self, timeout, self->counter); if (self->counter > 0) { self->counter--; DEBUG("counter decr self:%p", self); Py_RETURN_TRUE; } else if(PyObject_Not(blocking)) { Py_RETURN_FALSE; } loop = get_event_loop(); if (loop == NULL) { return NULL; } current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } timer = loop_set_timeout(loop, timeout, NULL); if (timer == NULL) { return NULL; } if (PySet_Add(self->waiters, current) == -1) { ((TimerObject*)timer)->cancelled = 1; return NULL; } while (self->counter <= 0) { res = loop_switch(loop); Py_XDECREF(res); if (res == NULL) { if (PyErr_ExceptionMatches(TimeoutException)) { RDEBUG("catch TimeoutException self:%p", self); if (fetch_timer() == timer) { PyErr_Clear(); res = Py_False; goto fin; } } goto fin; } } self->counter--; res = Py_True; fin: ((TimerObject*)timer)->cancelled = 1; if (PySet_Discard(self->waiters, current) == -1) { res = NULL; } Py_DECREF(timer); Py_XINCREF(res); return res; }
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);