// Unicode versions of '_Concat' etc have different sigs. Make them the // same here... void PyWinCoreString_Concat(register PyObject **pv, register PyObject *w) { if (!w) { // hrm - string version doesn't do this, but I saw PyObject_Repr() return NULL... Py_XDECREF(*pv); *pv = NULL; return; } PyObject *tmp = PyUnicode_Concat(*pv, w); Py_DECREF(*pv); *pv = tmp; }
void pystring_concat_str(PyObject **pystr, PyObject *obj) { #if PY_MAJOR_VERSION >= 3 PyObject *repr = PyObject_Str(obj); PyObject *newstr = PyUnicode_Concat(*pystr, repr); Py_DECREF(repr); Py_DECREF(*pystr); *pystr = newstr; #else PyString_ConcatAndDel(pystr, PyObject_Str(obj)); #endif }
void pystring_concat(PyObject **pystr, const char *chars) { #if PY_MAJOR_VERSION >= 3 PyObject *str = PyUnicode_FromString(chars); PyObject *newstr = PyUnicode_Concat(*pystr, str); Py_DECREF(str); Py_DECREF(*pystr); *pystr = newstr; #else PyString_ConcatAndDel(pystr, PyString_FromString(chars)); #endif }
/* Write a series of tokens to the current stack at once. */ int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist) { int pushed = 0; PyObject *stack, *token, *left, *right, *text; Textbuffer* buffer; Py_ssize_t size; if (PyList_GET_SIZE(tokenlist) > 0) { token = PyList_GET_ITEM(tokenlist, 0); switch (PyObject_IsInstance(token, Text)) { case 0: break; case 1: { pushed = 1; buffer = self->topstack->textbuffer; if (buffer->length == 0) break; left = Textbuffer_render(buffer); if (!left) return -1; right = PyObject_GetAttrString(token, "text"); if (!right) return -1; text = PyUnicode_Concat(left, right); Py_DECREF(left); Py_DECREF(right); if (!text) return -1; if (PyObject_SetAttrString(token, "text", text)) { Py_DECREF(text); return -1; } Py_DECREF(text); if (Textbuffer_reset(buffer)) return -1; break; } case -1: return -1; } } if (!pushed) { if (Tokenizer_push_textbuffer(self)) return -1; } stack = self->topstack->stack; size = PyList_GET_SIZE(stack); if (PyList_SetSlice(stack, size, size, tokenlist)) return -1; return 0; }
// Python v3 doesn't have the Unicode equivalent of Python v2's // PyString_ConcatAndDel(). void qpycore_Unicode_ConcatAndDel(PyObject **string, PyObject *newpart) { PyObject *old = *string; if (old) { if (newpart) *string = PyUnicode_Concat(old, newpart); else *string = 0; Py_DECREF(old); } Py_XDECREF(newpart); }
static PyObject * asciistr_concat(PyObject *v, PyObject *w) { PyObject *bytes = NULL; Py_buffer view; if (PyBytes_Check(w)) { if (PyObject_GetBuffer(w, &view, PyBUF_CONTIG_RO) < 0) { return NULL; } bytes = PyBytes_FromFormat("%s%s", PyUnicode_DATA(v), (char *)view.buf); PyBuffer_Release(&view); return bytes; } return PyUnicode_Concat(v, w); }
bool concat(PyObject** val1, PyObject* val2) { if (PyUnicode_Check(*val1) && PyUnicode_Check(val2)) { PyObject* result = PyUnicode_Concat(*val1, val2); Py_DECREF(*val1); *val1 = result; return true; } if (PyBytes_Check(*val1) && PyBytes_Check(val2)) { PyBytes_Concat(val1, val2); return true; } #if PY_MAJOR_VERSION < 3 if (PyString_Check(*val1) && PyString_Check(val2)) { PyString_Concat(val1, val2); return true; } #endif return false; }
static PyObject * fortran_getattr(PyFortranObject *fp, char *name) { int i,j,k,flag; if (fp->dict != NULL) { PyObject *v = PyDict_GetItemString(fp->dict, name); if (v != NULL) { Py_INCREF(v); return v; } } for (i=0,j=1;i<fp->len && (j=strcmp(name,fp->defs[i].name));i++); if (j==0) if (fp->defs[i].rank!=-1) { /* F90 allocatable array */ if (fp->defs[i].func==NULL) return NULL; for(k=0;k<fp->defs[i].rank;++k) fp->defs[i].dims.d[k]=-1; save_def = &fp->defs[i]; (*(fp->defs[i].func))(&fp->defs[i].rank,fp->defs[i].dims.d,set_data,&flag); if (flag==2) k = fp->defs[i].rank + 1; else k = fp->defs[i].rank; if (fp->defs[i].data !=NULL) { /* array is allocated */ PyObject *v = PyArray_New(&PyArray_Type, k, fp->defs[i].dims.d, fp->defs[i].type, NULL, fp->defs[i].data, 0, NPY_FARRAY, NULL); if (v==NULL) return NULL; /* Py_INCREF(v); */ return v; } else { /* array is not allocated */ Py_INCREF(Py_None); return Py_None; } } if (strcmp(name,"__dict__")==0) { Py_INCREF(fp->dict); return fp->dict; } if (strcmp(name,"__doc__")==0) { #if PY_VERSION_HEX >= 0x03000000 PyObject *s = PyUnicode_FromString(""), *s2, *s3; for (i=0;i<fp->len;i++) { s2 = fortran_doc(fp->defs[i]); s3 = PyUnicode_Concat(s, s2); Py_DECREF(s2); Py_DECREF(s); s = s3; } #else PyObject *s = PyString_FromString(""); for (i=0;i<fp->len;i++) PyString_ConcatAndDel(&s,fortran_doc(fp->defs[i])); #endif if (PyDict_SetItemString(fp->dict, name, s)) return NULL; return s; } if ((strcmp(name,"_cpointer")==0) && (fp->len==1)) { PyObject *cobj = F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data),NULL); if (PyDict_SetItemString(fp->dict, name, cobj)) return NULL; return cobj; } #if PY_VERSION_HEX >= 0x03000000 if (1) { PyObject *str, *ret; str = PyUnicode_FromString(name); ret = PyObject_GenericGetAttr((PyObject *)fp, str); Py_DECREF(str); return ret; } #else return Py_FindMethod(fortran_methods, (PyObject *)fp, name); #endif }
inline void PyString_Concat(PyObject** lhs, PyObject* rhs) { PyObject* s = PyUnicode_Concat(*lhs, rhs); Py_DECREF(lhs); *lhs = s; }
NUITKA_MAY_BE_UNUSED static bool BINARY_OPERATION_ADD_INPLACE( PyObject **operand1, PyObject *operand2 ) { assert( operand1 ); CHECK_OBJECT( *operand1 ); CHECK_OBJECT( operand2 ); #if PYTHON_VERSION < 300 // Something similar for Python3 should exist too. if ( PyInt_CheckExact( *operand1 ) && PyInt_CheckExact( operand2 ) ) { long a, b, i; a = PyInt_AS_LONG( *operand1 ); b = PyInt_AS_LONG( operand2 ); i = a + b; // Detect overflow, in which case, a "long" object would have to be // created, which we won't handle here. TODO: Add an else for that // case. if (likely(!( (i^a) < 0 && (i^b) < 0 ) )) { PyObject *result = PyInt_FromLong( i ); Py_DECREF( *operand1 ); *operand1 = result; return true; } } #endif #if PYTHON_VERSION < 300 if ( Py_REFCNT( *operand1 ) == 1 ) { // We more or less own the operand, so we might re-use its storage and // execute stuff in-place. if ( PyString_CheckExact( *operand1 ) && !PyString_CHECK_INTERNED( *operand1 ) && PyString_CheckExact( operand2 ) ) { return STRING_ADD_INCREMENTAL( operand1, operand2 ); } else if ( PyFloat_CheckExact( *operand1 ) && PyFloat_CheckExact( operand2 ) ) { return FLOAT_ADD_INCREMENTAL( operand1, operand2 ); } } // Strings are to be treated differently. if ( PyString_CheckExact( *operand1 ) && PyString_CheckExact( operand2 ) ) { PyString_Concat( operand1, operand2 ); return !ERROR_OCCURRED(); } #else if ( Py_REFCNT( *operand1 ) == 1 ) { // We more or less own the operand, so we might re-use its storage and // execute stuff in-place. if ( PyUnicode_CheckExact( *operand1 ) && !PyUnicode_CHECK_INTERNED( *operand1 ) && PyUnicode_CheckExact( operand2 ) ) { return UNICODE_ADD_INCREMENTAL( operand1, operand2 ); } else if ( PyFloat_CheckExact( *operand1 ) && PyFloat_CheckExact( operand2 ) ) { return FLOAT_ADD_INCREMENTAL( operand1, operand2 ); } } // Strings are to be treated differently. if ( PyUnicode_CheckExact( *operand1 ) && PyUnicode_CheckExact( operand2 ) ) { PyObject *result = PyUnicode_Concat( *operand1, operand2 ); if (unlikely( result == NULL )) { return false; } Py_DECREF( *operand1 ); *operand1 = result; return true; } #endif PyObject *result = PyNumber_InPlaceAdd( *operand1, operand2 ); if (unlikely( result == NULL )) { return false; } // We got an object handed, that we have to release. Py_DECREF( *operand1 ); // That's our return value then. As we use a dedicated variable, it's // OK that way. *operand1 = result; return true; }
void ERROR_TOO_FEW_KWONLY( struct Nuitka_FunctionObject *function, PyObject **kw_vars ) { char const *function_name = Nuitka_String_AsString( function->m_name ); PyCodeObject *code_object = function->m_code_object; Py_ssize_t max_missing = 0; for( Py_ssize_t i = code_object->co_kwonlyargcount-1; i >= 0; --i ) { if ( kw_vars[ i ] == NULL ) { max_missing += 1; } } PyObject *list_str = PyUnicode_FromString( "" ); PyObject *comma_str = PyUnicode_FromString( ", " ); PyObject *and_str = PyUnicode_FromString( max_missing == 2 ? " and " : ", and " ); Py_ssize_t missing = 0; for( Py_ssize_t i = code_object->co_kwonlyargcount-1; i >= 0; --i ) { if ( kw_vars[ i ] == NULL ) { PyObject *current_str = PyTuple_GET_ITEM( code_object->co_varnames, code_object->co_argcount + i ); PyObject *current = PyObject_Repr( current_str ); if ( missing == 0 ) { PyObject *old = list_str; list_str = PyUnicode_Concat( list_str, current ); Py_DECREF( old ); } else if ( missing == 1 ) { PyObject *old = list_str; list_str = PyUnicode_Concat( and_str, list_str ); Py_DECREF( old ); old = list_str; list_str = PyUnicode_Concat( current, list_str ); Py_DECREF( old ); } else { PyObject *old = list_str; list_str = PyUnicode_Concat( comma_str, list_str ); Py_DECREF( old ); old = list_str; list_str = PyUnicode_Concat( current, list_str ); Py_DECREF( old ); } Py_DECREF( current ); missing += 1; } } Py_DECREF( comma_str ); Py_DECREF( and_str ); PyErr_Format( PyExc_TypeError, "%s() missing %zd required keyword-only argument%s: %s", function_name, max_missing, max_missing > 1 ? "s" : "", Nuitka_String_AsString( list_str ) ); Py_DECREF( list_str ); }
PyObject* PyCOMPSDict_str(PyObject *self) { COMPS_HSList *pairlist; COMPS_HSListItem *it; PyObject *ret, *tmp = NULL, *tmp2 = NULL, *tmpkey = NULL, *tmpval = NULL; ret = PyUnicode_FromString("{"); pairlist = comps_objdict_pairs(((PyCOMPS_Dict*)self)->dict); char *tmpstr; for (it = pairlist->first; it != pairlist->last; it = it->next) { tmp = ret; tmpkey = __pycomps_lang_decode(((COMPS_ObjRTreePair*)it->data)->key); if (!tmpkey) { PyErr_SetString(PyExc_TypeError, "key convert error"); goto out; } tmpstr = comps_object_tostr(((COMPS_ObjRTreePair*)it->data)->data); tmpval = __pycomps_lang_decode(tmpstr); free(tmpstr); if (!tmpval) { PyErr_SetString(PyExc_TypeError, "val convert error"); goto out; } tmp2 = PyUnicode_FromFormat("%U = '%U', ", tmpkey, tmpval); ret = PyUnicode_Concat(ret, tmp2); Py_XDECREF(tmp); Py_XDECREF(tmp2); Py_XDECREF(tmpkey); Py_XDECREF(tmpval); } tmp = ret; tmpkey = __pycomps_lang_decode(((COMPS_RTreePair*)it->data)->key); if (!tmpkey) { goto out; } tmpstr = comps_object_tostr(((COMPS_ObjRTreePair*)it->data)->data); tmpval = __pycomps_lang_decode(tmpstr); free(tmpstr); if (!tmpval) { //PyErr_SetString(PyExc_TypeError, "val convert error"); goto out; } tmp2 = PyUnicode_FromFormat("%U = '%U'", tmpkey, tmpval); ret = PyUnicode_Concat(ret, tmp2); Py_XDECREF(tmp); Py_XDECREF(tmp2); Py_XDECREF(tmpkey); Py_XDECREF(tmpval); tmp = ret; tmp2 = PyUnicode_FromString("}"); ret = PyUnicode_Concat(ret, tmp2); Py_XDECREF(tmp); Py_XDECREF(tmp2); comps_hslist_destroy(&pairlist); return ret; out: Py_XDECREF(tmp); Py_XDECREF(tmp2); Py_XDECREF(tmpkey); Py_XDECREF(tmpval); comps_hslist_destroy(&pairlist); return NULL; }