static void print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) { int err = 0, res; PyObject *cause, *context; if (seen != NULL) { /* Exception chaining */ PyObject *value_id = PyLong_FromVoidPtr(value); if (value_id == NULL || PySet_Add(seen, value_id) == -1) PyErr_Clear(); else if (PyExceptionInstance_Check(value)) { PyObject *check_id = NULL; cause = PyException_GetCause(value); context = PyException_GetContext(value); if (cause) { check_id = PyLong_FromVoidPtr(cause); if (check_id == NULL) { res = -1; } else { res = PySet_Contains(seen, check_id); Py_DECREF(check_id); } if (res == -1) PyErr_Clear(); if (res == 0) { print_exception_recursive( f, cause, seen); err |= PyFile_WriteString( cause_message, f); } } else if (context && !((PyBaseExceptionObject *)value)->suppress_context) { check_id = PyLong_FromVoidPtr(context); if (check_id == NULL) { res = -1; } else { res = PySet_Contains(seen, check_id); Py_DECREF(check_id); } if (res == -1) PyErr_Clear(); if (res == 0) { print_exception_recursive( f, context, seen); err |= PyFile_WriteString( context_message, f); } } Py_XDECREF(context); Py_XDECREF(cause); } Py_XDECREF(value_id); } print_exception(f, value); if (err != 0) PyErr_Clear(); }
static PyObject *pair_length(PyObject *self, PyObject *_noargs) { PyObject *seen; PyObject *tmp; long length = 0; if (SibNil_Check(self)) { return PyLong_FromLong(length); } seen = PySet_New(NULL); for(; SibPair_CheckExact(self); self = SibPair_CDR(self)) { tmp = PyLong_FromVoidPtr(self); if (PySet_Contains(seen, tmp)) { Py_DECREF(tmp); self = NULL; break; } else { PySet_Add(seen, tmp); Py_DECREF(tmp); length++; } } if (self && ! SibNil_Check(self)) length++; Py_DECREF(seen); return PyLong_FromLong(length); }
static int _look_for_code_object(PyObject *o, void *all_codes) { if (PyCode_Check(o) && !PySet_Contains((PyObject *)all_codes, o)) { Py_ssize_t i; PyCodeObject *co = (PyCodeObject *)o; if (emit_code_object(co) < 0) return -1; if (PySet_Add((PyObject *)all_codes, o) < 0) return -1; /* as a special case, recursively look for and add code objects found in the co_consts. The problem is that code objects are not created as GC-aware in CPython, so we need to hack like this to hope to find most of them. */ i = PyTuple_Size(co->co_consts); while (i > 0) { --i; if (_look_for_code_object(PyTuple_GET_ITEM(co->co_consts, i), all_codes) < 0) return -1; } } return 0; }
static void pwalk_fillpos(PyObject *pair, PyObject *seen, PyObject *pos) { // checked PyObject *pair_id; SibPair *sp; for (; SibPair_CheckExact(pair); pair = SibPair_CDR(pair)) { sp = (SibPair *) pair; pair_id = PyLong_FromVoidPtr(pair); if (PySet_Contains(seen, pair_id)) { Py_DECREF(pair_id); break; } else { PySet_Add(seen, pair_id); Py_DECREF(pair_id); } if (sp->position) { pos = sp->position; } else { Py_INCREF(pos); sp->position = pos; } if (SibPair_CheckExact(SibPair_CAR(sp))) { pwalk_fillpos(SibPair_CAR(sp), seen, pos); } } }
long SibPair_IsRecursive(PyObject *self) { // checked if (SibNil_Check(self)) return 0; PyObject *seen = PySet_New(NULL); PyObject *pair_id; long result = 0; for ( ; SibPair_CheckExact(self); self = SibPair_CDR(self)) { pair_id = PyLong_FromVoidPtr(self); if (PySet_Contains(seen, pair_id)) { /* seen it, therefore recursive */ Py_DECREF(pair_id); result = 1; break; } else { PySet_Add(seen, pair_id); Py_DECREF(pair_id); } } Py_DECREF(seen); return result; }
long SibPair_IsProper(PyObject *self) { // checked if (SibNil_Check(self)) return 1; PyObject *seen = PySet_New(NULL); PyObject *pair_id; long result = 0; for ( ; SibPair_CheckExact(self); self = SibPair_CDR(self)) { pair_id = PyLong_FromVoidPtr(self); if (PySet_Contains(seen, pair_id)) { /* seen it, therefore recursive */ Py_DECREF(pair_id); result = 1; break; } else { PySet_Add(seen, pair_id); Py_DECREF(pair_id); } } /* it's either recursive and thus proper, or the last item needs to have been a nil, or it's improper */ Py_DECREF(seen); return result || SibNil_Check(self); }
int WeakSet_Contains(PyObject *self,PyObject *key){ PyObject *ref = PyWeakref_NewRef((PyObject *)key,NULL); if (!ref) return -1; int r = PySet_Contains(((WeakSet *)self)->set,ref); Py_DECREF(ref); return r; }
static int t_set_seq_contains(t_set *self, PyObject *value) { int result; if (self->itemvalue.flags & V_PURE) result = PySet_Contains(self->set, value); else { value = _useValue(self, value); if (!value) return -1; result = PySet_Contains(self->set, value); Py_DECREF(value); } return result; }
static int _look_for_code_object(PyObject *o, void *all_codes) { if (PyCode_Check(o) && !PySet_Contains((PyObject *)all_codes, o)) { if (emit_code_object((PyCodeObject *)o) < 0) return -1; if (PySet_Add((PyObject *)all_codes, o) < 0) return -1; } return 0; }
static PyObject * frame_stack(PyObject *self, PyObject *args) { // frame_stack(frame, top_frames, top_codes, upper_frames, upper_codes) // returns a list of frames. PyFrameObject* frame; const PySetObject* top_frames; const PySetObject* top_codes; const PySetObject* upper_frames; const PySetObject* upper_codes; if (!PyArg_ParseTuple(args, "OOOOO", &frame, &top_frames, &top_codes, &upper_frames, &upper_codes)) { return NULL; } PyObject* frame_stack = PyList_New(0); if (frame_stack == NULL) { return NULL; } while (frame != NULL) { if (PySet_Contains(upper_frames, frame) == 1 || PySet_Contains(upper_codes, frame->f_code) == 1) { break; } if (PyList_Append(frame_stack, (PyObject*)frame)) { return NULL; } if (PySet_Contains(top_frames, frame) == 1 || PySet_Contains(top_codes, frame->f_code) == 1) { break; } frame = frame->f_back; } PyList_Reverse(frame_stack); return frame_stack; }
static PyObject * html_Tag_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { html_Tag *self = NULL; self = (html_Tag *)type->tp_alloc(type, 0); if (self == NULL) return PyErr_NoMemory(); self->bold = NULL; self->italic = NULL; self->lang = NULL; if (!PyArg_ParseTuple(args, "O|OOO", &(self->name), &(self->bold), &(self->italic), &(self->lang))) { self->ob_type->tp_free((PyObject*)self); return NULL; } if (self->bold == NULL) { self->bold = (PySet_Contains(bold_tags, self->name)) ? Py_True : Py_False; } if (self->italic == NULL) { self->italic = (PySet_Contains(italic_tags, self->name)) ? Py_True : Py_False; } if (self->lang == NULL) self->lang = Py_None; Py_INCREF(self->name); Py_INCREF(self->bold); Py_INCREF(self->italic); Py_INCREF(self->lang); return (PyObject *)self; }
//============================================================================= // STATIC : SPELLwsWarmStartImpl::shouldFilter() //============================================================================= bool SPELLwsWarmStartImpl::shouldFilter( PyObject* key, PyObject* item ) { // Do not consider modules if (PyModule_Check(item)) return true; // Do not consider callables if (PyCallable_Check(item)) return true; // If there is no filter cannot decide if (s_filterKeys == NULL) return false; if ( PYSTR(key) == DatabaseConstants::SCDB ) return true; if ( PYSTR(key) == DatabaseConstants::GDB ) return true; if ( PYSTR(key) == DatabaseConstants::PROC ) return true; // If the set contains the key, must filter it int contains = PySet_Contains( s_filterKeys, key ); return contains!=0; }
//============================================================================= // STATIC : SPELLwsWarmStartImpl::shouldFilter() //============================================================================= bool SPELLwsWarmStartImpl::shouldFilter( PyObject* key, PyObject* item ) { bool doFilter = false; // Do not consider modules if (PyModule_Check(item)) { doFilter = true; } // Do not consider callables else if (PyCallable_Check(item)) { doFilter = true; } // Do not consider functions else if (PyFunction_Check(item)) { doFilter = true; } else if ( PYREPR(key) == DatabaseConstants::SCDB ) { doFilter = true; } else if ( PYREPR(key) == DatabaseConstants::GDB ) { doFilter = true; } else if ( PYREPR(key) == DatabaseConstants::PROC ) { doFilter = true; } if ((!doFilter) && (s_filterKeys != NULL)) { // If the set contains the key, must filter it int contains = PySet_Contains( s_filterKeys, key ); doFilter = (contains!=0); } return doFilter; }
static PyObject *pfoll_iternext(PyObject *self) { SibPairFollower *s = (SibPairFollower *) self; PyObject *current; PyObject *curr_id; PyObject *result = NULL; current = s->current; if (! current) { /* then it's done */ return NULL; } if (s->seen) { /* we're set up to keep a record of what pairs we've already seen, to prevent recursion */ curr_id = PyLong_FromVoidPtr(current); if (PySet_Contains(s->seen, curr_id)) { /* if we've already seen this one, we're done */ Py_DECREF(curr_id); Py_CLEAR(s->current); return NULL; } else { /* otherwise mark it as seen, and continue */ PySet_Add(s->seen, curr_id); Py_DECREF(curr_id); } } /* get ready for the next */ if (SibPair_CheckExact(current)) { s->current = SibPair_CDR(current); Py_INCREF(s->current); } else { s->current = NULL; } /* at this point, we've stolen the old s->current ref as current, and s->current has been modified to point to something else */ if (s->just_items) { /* if we're in items mode, then we want the CAR if current is a pair, otherwise if current is non-nil we return it. */ if (SibPair_CheckExact(current)) { result = SibPair_CAR(current); Py_INCREF(result); Py_DECREF(current); } else if (SibNil_Check(current)) { result = NULL; Py_DECREF(current); } else { result = current; } } else { result = current; } return result; }
int Context_has_object(Context* cx, PyObject* val) { return PySet_Contains((PyObject*) cx->objects, val); }
static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag) { EnumPropertyItem *items= NULL; PyObject *item; int seq_len, i, totitem= 0; short def_used= 0; const char *def_cmp= NULL; seq_len= PySequence_Fast_GET_SIZE(seq_fast); if(is_enum_flag) { if(seq_len > RNA_ENUM_BITFLAG_SIZE) { PyErr_SetString(PyExc_TypeError, "EnumProperty(...): maximum " STRINGIFY(RNA_ENUM_BITFLAG_SIZE) " members for a ENUM_FLAG type property"); return NULL; } if(def && !PySet_Check(def)) { PyErr_Format(PyExc_TypeError, "EnumProperty(...): default option must be a 'set' type when ENUM_FLAG is enabled, not a '%.200s'", Py_TYPE(def)->tp_name); return NULL; } } else { if(def) { def_cmp= _PyUnicode_AsString(def); if(def_cmp==NULL) { PyErr_Format(PyExc_TypeError, "EnumProperty(...): default option must be a 'str' type when ENUM_FLAG is disabled, not a '%.200s'", Py_TYPE(def)->tp_name); return NULL; } } } /* blank value */ *defvalue= 0; for(i=0; i<seq_len; i++) { EnumPropertyItem tmp= {0, "", 0, "", ""}; item= PySequence_Fast_GET_ITEM(seq_fast, i); if(PyTuple_Check(item)==0) { PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected a sequence of tuples for the enum items"); if(items) MEM_freeN(items); return NULL; } if(!PyArg_ParseTuple(item, "sss", &tmp.identifier, &tmp.name, &tmp.description)) { PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected an identifier, name and description in the tuple"); return NULL; } if(is_enum_flag) { tmp.value= 1<<i; if(def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) { *defvalue |= tmp.value; def_used++; } } else { tmp.value= i; if(def && def_used == 0 && strcmp(def_cmp, tmp.identifier)==0) { *defvalue= tmp.value; def_used++; /* only ever 1 */ } } RNA_enum_item_add(&items, &totitem, &tmp); } RNA_enum_item_end(&items, &totitem); if(is_enum_flag) { /* strict check that all set members were used */ if(def && def_used != PySet_GET_SIZE(def)) { MEM_freeN(items); PyErr_Format(PyExc_TypeError, "EnumProperty(..., default={...}): set has %d unused member(s)", PySet_GET_SIZE(def) - def_used); return NULL; } } else { if(def && def_used == 0) { MEM_freeN(items); PyErr_Format(PyExc_TypeError, "EnumProperty(..., default=\'%s\'): not found in enum members", def); return NULL; } } return items; }
/* Method implementations */ static PyObject *searchio_tokenize(PyObject *self, PyObject *args) { /* get the set of stopwords, and the string to tokenize */ PyObject *stopwords = NULL; const char *textString = NULL; int keepStar = 0; if (!PyArg_ParseTuple(args, "Osi", &stopwords, &textString, &keepStar)) return NULL; /* loop over the characters in textString to normalize it */ size_t len = SEARCHIO_MIN(strlen(textString), SEARCHIO_TOKENIZER_BUFFER_SIZE - 1); size_t i; size_t wordsFound = 1; for (i = 0; i < len; i++) { char c = tolower(textString[i]); if (isalnum(c) || (keepStar && c == '*')) searchio_tokenizerBuffer[i] = c; else { searchio_tokenizerBuffer[i] = '\0'; wordsFound++; } } /* NUL-terminate our buffer */ searchio_tokenizerBuffer[i] = '\0'; /* create a result list */ PyObject *result = PyList_New(0); /* loop over each string, check if it's a stopword, and if not, stem it */ char *str = searchio_tokenizerBuffer; for (i = 0; i < wordsFound; i++) { size_t l = strlen(str); if (l == 0) { str += 1; continue; } /* convert to a Python string */ PyObject *pystr = PyString_FromString(str); /* is it a stop word? */ if (!PySet_Contains(stopwords, pystr)) { if (!keepStar || strchr(str, '*') == NULL) { /* stem it */ int newEnd = stem(str, 0, (int)(l - 1)); str[newEnd + 1] = '\0'; } /* add it to the list */ PyList_Append(result, PyString_FromString(str)); } Py_DECREF(pystr); str += (l + 1); } return result; }
static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag) { EnumPropertyItem *items; PyObject *item; const Py_ssize_t seq_len= PySequence_Fast_GET_SIZE(seq_fast); Py_ssize_t totbuf= 0; int i; short def_used= 0; const char *def_cmp= NULL; if(is_enum_flag) { if(seq_len > RNA_ENUM_BITFLAG_SIZE) { PyErr_SetString(PyExc_TypeError, "EnumProperty(...): maximum " STRINGIFY(RNA_ENUM_BITFLAG_SIZE) " members for a ENUM_FLAG type property"); return NULL; } if(def && !PySet_Check(def)) { PyErr_Format(PyExc_TypeError, "EnumProperty(...): default option must be a 'set' " "type when ENUM_FLAG is enabled, not a '%.200s'", Py_TYPE(def)->tp_name); return NULL; } } else { if(def) { def_cmp= _PyUnicode_AsString(def); if(def_cmp==NULL) { PyErr_Format(PyExc_TypeError, "EnumProperty(...): default option must be a 'str' " "type when ENUM_FLAG is disabled, not a '%.200s'", Py_TYPE(def)->tp_name); return NULL; } } } /* blank value */ *defvalue= 0; items= MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1"); for(i=0; i<seq_len; i++) { EnumPropertyItem tmp= {0, "", 0, "", ""}; Py_ssize_t id_str_size; Py_ssize_t name_str_size; Py_ssize_t desc_str_size; item= PySequence_Fast_GET_ITEM(seq_fast, i); if( (PyTuple_CheckExact(item)) && (PyTuple_GET_SIZE(item) == 3) && (tmp.identifier= _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) && (tmp.name= _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) && (tmp.description= _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size)) ) { if(is_enum_flag) { tmp.value= 1<<i; if(def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) { *defvalue |= tmp.value; def_used++; } } else { tmp.value= i; if(def && def_used == 0 && strcmp(def_cmp, tmp.identifier)==0) { *defvalue= tmp.value; def_used++; /* only ever 1 */ } } items[i]= tmp; /* calculate combine string length */ totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */ } else { MEM_freeN(items); PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected an tuple containing (identifier, name description)"); return NULL; } } if(is_enum_flag) { /* strict check that all set members were used */ if(def && def_used != PySet_GET_SIZE(def)) { MEM_freeN(items); PyErr_Format(PyExc_TypeError, "EnumProperty(..., default={...}): set has %d unused member(s)", PySet_GET_SIZE(def) - def_used); return NULL; } } else { if(def && def_used == 0) { MEM_freeN(items); PyErr_Format(PyExc_TypeError, "EnumProperty(..., default=\'%s\'): not found in enum members", def); return NULL; } } /* disabled duplicating strings because the array can still be freed and * the strings from it referenced, for now we can't support dynamically * created strings from python. */ #if 0 /* this would all work perfectly _but_ the python strings may be freed * immediately after use, so we need to duplicate them, ugh. * annoying because it works most of the time without this. */ { EnumPropertyItem *items_dup= MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf), "enum_items_from_py2"); EnumPropertyItem *items_ptr= items_dup; char *buf= ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1)); memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1)); for(i=0; i<seq_len; i++, items_ptr++) { buf += strswapbufcpy(buf, &items_ptr->identifier); buf += strswapbufcpy(buf, &items_ptr->name); buf += strswapbufcpy(buf, &items_ptr->description); } MEM_freeN(items); items=items_dup; } /* end string duplication */ #endif return items; }