static PyObject * Noddy_name(Noddy* self) { static PyObject *format = NULL; PyObject *args, *result; if (format == NULL) { format = PyString_FromString("%s %s"); if (format == NULL) return NULL; } if (self->first == NULL) { PyErr_SetString(PyExc_AttributeError, "first"); return NULL; } if (self->last == NULL) { PyErr_SetString(PyExc_AttributeError, "last"); return NULL; } args = Py_BuildValue("OO", self->first, self->last); if (args == NULL) return NULL; result = PyString_Format(format, args); Py_DECREF(args); return result; }
static PyObject * DecimalResultProcessor_process(DecimalResultProcessor *self, PyObject *value) { PyObject *str, *result, *args; if (value == Py_None) Py_RETURN_NONE; /* Decimal does not accept float values directly */ /* SQLite can also give us an integer here (see [ticket:2432]) */ /* XXX: starting with Python 3.1, we could use Decimal.from_float(f), but the result wouldn't be the same */ args = PyTuple_Pack(1, value); if (args == NULL) return NULL; #if PY_MAJOR_VERSION >= 3 str = PyUnicode_Format(self->format, args); #else str = PyString_Format(self->format, args); #endif Py_DECREF(args); if (str == NULL) return NULL; result = PyObject_CallFunctionObjArgs(self->type, str, NULL); Py_DECREF(str); return result; }
// Python function PyObject* NameObject::PyName(NameObject *self, PyObject *unused) { PyObject *args = NULL; PyObject *format = NULL; PyObject *result = NULL; format = PyString_FromString("%s %s"); if (format == NULL) goto fail; if (self->firstname == NULL) { PyErr_SetString(PyExc_AttributeError, "firstname"); goto fail; } if (self->lastname == NULL) { PyErr_SetString(PyExc_AttributeError, "lastname"); goto fail; } args = Py_BuildValue("OO", self->firstname, self->lastname); if (args == NULL) goto fail; result = PyString_Format(format, args); fail: Py_XDECREF(format); Py_XDECREF(args); return result; }
static PyObject * DecimalResultProcessor_process(DecimalResultProcessor *self, PyObject *value) { PyObject *str, *result, *args; if (value == Py_None) Py_RETURN_NONE; if (PyFloat_CheckExact(value)) { /* Decimal does not accept float values directly */ args = PyTuple_Pack(1, value); if (args == NULL) return NULL; str = PyString_Format(self->format, args); if (str == NULL) return NULL; result = PyObject_CallFunctionObjArgs(self->type, str, NULL); Py_DECREF(str); return result; } else { return PyObject_CallFunctionObjArgs(self->type, value, NULL); } }
static PyObject * string_mod(PyObject *v, PyObject *w) { if (!PyString_Check(v)) { Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } return PyString_Format(v, w); }
static PyObject* Snmp_oid2string(PyObject *resultvalue) { PyObject *dot, *tmp, *tmp2, *list; int i; if ((list = PyTuple_New(PyTuple_Size(resultvalue))) == NULL) return NULL; for (i = 0; i < PyTuple_Size(resultvalue); i++) { if ((tmp = PyTuple_GetItem(resultvalue, i)) == NULL) { Py_DECREF(list); return NULL; } if ((tmp2 = PyObject_Str(tmp)) == NULL) { Py_DECREF(list); return NULL; } PyTuple_SetItem(list, i, tmp2); if (PyErr_Occurred()) { Py_DECREF(tmp2); Py_DECREF(list); return NULL; } } Py_DECREF(resultvalue); resultvalue = list; if ((dot = PyString_FromString(".")) == NULL) return NULL; if ((tmp = PyObject_CallMethod(dot, "join", "(O)", resultvalue)) == NULL) { Py_DECREF(dot); return NULL; } Py_DECREF(resultvalue); Py_DECREF(dot); resultvalue = tmp; if ((tmp = PyTuple_Pack(1, resultvalue)) == NULL) return NULL; Py_DECREF(resultvalue); resultvalue = tmp; if ((tmp2 = PyString_FromString(".%s")) == NULL) return NULL; if ((tmp = PyString_Format(tmp2, resultvalue)) == NULL) { Py_DECREF(tmp2); return NULL; } Py_DECREF(tmp2); Py_DECREF(resultvalue); return tmp; }
static PyObject * DiscoDBIter_format(DiscoDBIter *self, PyObject *format, int N) { PyObject *iterator = PyObject_GetIter((PyObject *)self), *string = PyString_FromString(""), *item = NULL; int i; if (iterator == NULL) goto Done; if (string == NULL) goto Done; for (i = 0; i < N + 1; i++) { item = PyIter_Next(iterator); if (item == NULL) { if (PyErr_Occurred()) goto Done; else break; } if (i > 0) PyString_ConcatAndDel(&string, PyString_FromString(", ")); if (i < N) PyString_ConcatAndDel(&string, PyString_Format(format, item)); else PyString_ConcatAndDel(&string, PyString_FromString("...")); if (string == NULL) goto Done; Py_CLEAR(item); } Done: Py_CLEAR(iterator); Py_CLEAR(item); if (PyErr_Occurred()) { Py_CLEAR(string); return NULL; } return string; }
void set_iter_type_error(PyObject* obj) { // Set an exception when we expected a LinkedListIter and got something // else PyObject* args; PyObject* fmt; PyObject* err_str; args = Py_BuildValue("(O)", obj); fmt = PyString_FromString("Expected LinkedListIter, got %r"); err_str = PyString_Format(fmt, args); PyErr_SetObject(PyExc_TypeError, err_str); Py_DECREF(fmt); Py_DECREF(err_str); Py_DECREF(args); }
// keep this simple so that print(x) and str(x) are concise if x is NA static PyObject * NA_repr(SlopNAObject *self) { PyObject* type_repr = PyObject_Repr(self->exc_type); PyObject* repr = PyTuple_Pack(1, type_repr); PyObject* fmt = PyString_FromString("<NA %s>"); PyObject* ret = PyString_Format(fmt, repr); Py_DECREF(fmt); Py_DECREF(repr); Py_DECREF(type_repr); return ret; }
static PyObject *t_tzinfo_repr(t_tzinfo *self) { PyObject *format = PyString_FromString("<ICUtzinfo: %s>"); PyObject *str = PyObject_Str((PyObject *) self->tz); #if PY_VERSION_HEX < 0x02040000 PyObject *args = Py_BuildValue("(O)", str); #else PyObject *args = PyTuple_Pack(1, str); #endif PyObject *repr = PyString_Format(format, args); Py_DECREF(args); Py_DECREF(str); Py_DECREF(format); return repr; }
static PyObject *t_floatingtz_repr(t_floatingtz *self) { t_tzinfo *tzinfo = self->tzinfo ? self->tzinfo : _default; PyObject *format = PyString_FromString("<FloatingTZ: %s>"); PyObject *str = PyObject_Str((PyObject *) tzinfo->tz); #if PY_VERSION_HEX < 0x02040000 PyObject *args = Py_BuildValue("(O)", str); #else PyObject *args = PyTuple_Pack(1, str); #endif PyObject *repr = PyString_Format(format, args); Py_DECREF(args); Py_DECREF(str); Py_DECREF(format); return repr; }
static PyObject *t_set_repr(t_set *self) { PyObject *str = PyObject_Str(self->set); if (str) { PyObject *format = PyString_FromString("<%s: %s>"); PyTypeObject *type = self->itemvalue.persistentvalue.ob_type; PyObject *name = PyObject_GetAttrString((PyObject *) type, "__name__"); PyObject *args = PyTuple_Pack(2, name, str); Py_DECREF(str); Py_DECREF(name); str = PyString_Format(format, args); Py_DECREF(format); Py_DECREF(args); } return str; }
static PyObject *t_link_repr(t_link *self) { PyObject *value = self->value ? PyObject_Repr(self->value) : NULL; if (!value && PyErr_Occurred()) return NULL; if (value) { PyObject *format = PyString_FromString("<link: %s>"); PyObject *repr = PyString_Format(format, value); Py_DECREF(format); Py_XDECREF(value); return repr; } return PyString_FromString("<link: (null)>"); }
static PyObject *KernelParm_Str(svms_KernelParmObject *self) { // Make the arguments for the formatting operation. KERNEL_PARM *k = self->kparm; PyObject *args = Py_BuildValue ("siiddds", self->ob_type->tp_name, k->kernel_type, k->poly_degree, k->rbf_gamma, k->coef_lin, k->coef_const, k->custom); if (args == NULL) return NULL; // Make the format for the formatting operation. PyObject *format = PyString_FromString ("<%s kernel_type=%d poly_degree=%d rbf_gamma=%g coef_lin=%g coef_const=%g custom=%s>"); if (format==NULL) { Py_DECREF(args); return NULL; } // Make the formatted string. PyObject *result = PyString_Format(format, args); Py_DECREF(args); Py_DECREF(format); return result; }
static PyObject * NA_detailed_repr(SlopNAObject *self) { PyObject* type_repr = PyObject_Repr(self->exc_type); PyObject* value_repr = PyObject_Repr(self->exc_value); PyObject* cur_locals = PyEval_GetLocals(); PyObject* locals_repr = PyObject_Repr(cur_locals); PyObject* repr = PyTuple_Pack(4, type_repr, value_repr, self->exc_traceback_str, locals_repr); PyObject* fmt = PyString_FromString("{NA %s %s\n %s\nLocals: %s\n}"); PyObject* ret = PyString_Format(fmt, repr); Py_DECREF(fmt); Py_DECREF(repr); Py_DECREF(locals_repr); Py_DECREF(value_repr); Py_DECREF(type_repr); return ret; }
PyObject * PyNumber_Remainder(PyObject *v, PyObject *w) { if (PyString_Check(v)) return PyString_Format(v, w); else if (PyUnicode_Check(v)) return PyUnicode_Format(v, w); BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder); if (v->ob_type->tp_as_number != NULL) { PyObject *x = NULL; PyObject * (*f)(PyObject *, PyObject *); if (PyNumber_Coerce(&v, &w) != 0) return NULL; if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL) x = (*f)(v, w); Py_DECREF(v); Py_DECREF(w); if (f != NULL) return x; } return type_error("bad operand type(s) for %"); }