示例#1
0
static PyObject *
Row_repr(PyObject* o)
{
    Row* self = (Row*)o;

    if (self->cValues == 0)
        return PyString_FromString("()");

    Object pieces = PyTuple_New(self->cValues);
    if (!pieces)
        return 0;

    for (Py_ssize_t i = 0; i < self->cValues; i++)
    {
        PyObject* piece = PyObject_Repr(self->apValues[i]);
        if (!piece)
            return 0;
        PyTuple_SET_ITEM(pieces.Get(), i, piece);
	}

    Object sep = PyString_FromString(", ");
    if (!sep)
        return 0;

    Object s = _PyString_Join(sep, pieces);
    if (!s)
        return 0;

    const char* szWrapper = (self->cValues == 1) ? "(%s, )" : "(%s)";

    Object result = PyString_FromFormat(szWrapper, PyString_AsString(s.Get()));
    return result.Detach();
}
/**
 * Concatenates the passed arguments with a dot.
 *
 * NULL arguments are ignored. The arguments are released afterwards.
 */
PyObject* _PyString_JoinWithDots(PyObject* object1,
                                 PyObject* object2,
                                 PyObject* object3) {
    PyObject *list = PyList_New(0);
    PyObject *separator = PyString_InternFromString(".");
    if (object1 != NULL) {
        PyList_Append(list, object1);
    }
    if (object2 != NULL) {
        PyList_Append(list, object2);
    }
    if (object3 != NULL) {
        PyList_Append(list, object3);
    }

    PyObject * result = _PyString_Join(separator, list);
    Py_DECREF(separator);
    Py_DECREF(list);

    return result;
}
示例#3
0
文件: _pack.c 项目: mjmaenpaa/dulwich
static PyObject *py_chunked_as_string(PyObject *py_buf)
{
	if (PyList_Check(py_buf)) {
		PyObject *sep = PyString_FromString("");
		if (sep == NULL) {
			PyErr_NoMemory();
			return NULL;
		}
		py_buf = _PyString_Join(sep, py_buf);
		Py_DECREF(sep);
		if (py_buf == NULL) {
			PyErr_NoMemory();
			return NULL;
		}
	} else if (PyString_Check(py_buf)) {
		Py_INCREF(py_buf);
	} else {
		PyErr_SetString(PyExc_TypeError,
			"src_buf is not a string or a list of chunks");
		return NULL;
	}
    return py_buf;
}
示例#4
0
static PyObject *
repr_flag(const struct FlagRepr *flags, unsigned int v)
{
	PyObject *l;

	l = PyList_New(0);
	if (l == NULL)
		return NULL;

	for (; flags->flag; flags++)
		if (v & flags->flag) {
			PyObject *f;
			f = PyString_FromString(flags->repr);
			if (f == NULL) {
				Py_DECREF(l);
				return NULL;
			}
			PyList_Append(l, f);
			Py_DECREF(f);
		}

	if (PyList_GET_SIZE(l) == 0) {
		Py_DECREF(l);
		return PyString_FromString("0");
	}
	else {
		PyObject *sep, *r;

		sep = PyString_FromString("|");
		if (sep == NULL)
			return NULL;
		r = _PyString_Join(sep, l);
		Py_DECREF(l);
		Py_DECREF(sep);
		return r;
	}
}
示例#5
0
static PyObject *
path_str(PycairoPath *p)
{
    PyObject *s, *pieces = NULL, *result = NULL;
    cairo_path_t *path = p->path;
    cairo_path_data_t *data;
    int i, ret;
    char buf[80];

    pieces = PyList_New(0);
    if (pieces == NULL)
	goto Done;

    /* loop reading elements */
    for (i=0; i < path->num_data; i += path->data[i].header.length) {
	data = &path->data[i];
	switch (data->header.type) {

	case CAIRO_PATH_MOVE_TO:
	    PyOS_snprintf(buf, sizeof(buf), "move_to %f %f",
	    		  data[1].point.x, data[1].point.y);
	    s = PyString_FromString(buf);
	    if (!s)
		goto Done;
	    ret = PyList_Append(pieces, s);
	    Py_DECREF(s);
	    if (ret < 0)
		goto Done;
	    break;

	case CAIRO_PATH_LINE_TO:
	    PyOS_snprintf(buf, sizeof(buf), "line_to %f %f",
	    		  data[1].point.x, data[1].point.y);
	    s = PyString_FromString(buf);
	    if (!s)
		goto Done;
	    ret = PyList_Append(pieces, s);
	    Py_DECREF(s);
	    if (ret < 0)
		goto Done;
	    break;

	case CAIRO_PATH_CURVE_TO:
	    PyOS_snprintf(buf, sizeof(buf), "curve_to %f %f %f %f %f %f",
	    		  data[1].point.x, data[1].point.y,
			  data[2].point.x, data[2].point.y,
			  data[3].point.x, data[3].point.y);
	    s = PyString_FromString(buf);
	    if (!s)
		goto Done;
	    ret = PyList_Append(pieces, s);
	    Py_DECREF(s);
	    if (ret < 0)
		goto Done;
	    break;

	case CAIRO_PATH_CLOSE_PATH:
	    s = PyString_FromString("close path");
	    if (!s)
		goto Done;
	    ret = PyList_Append(pieces, s);
	    Py_DECREF(s);
	    if (ret < 0)
		goto Done;
	    break;
	}
    }
    /* result = "\n".join(pieces) */
    s = PyString_FromString("\n");
    if (s == NULL)
	goto Done;
    result = _PyString_Join(s, pieces);
    Py_DECREF(s);

Done:
    Py_XDECREF(pieces);
    return result;
}
示例#6
0
static PyObject *
attr_dir_repr(PyObject *_self)
{
	attr_dir_object *self = (attr_dir_object*)_self;
	kdump_ctx *ctx = self->kdumpfile->ctx;
	kdump_attr_iter_t iter;
	kdump_status status;
	PyObject *s, *temp;
	PyObject *colon = NULL, *pieces = NULL;
	PyObject *result = NULL;
	int res;

	status = kdump_attr_ref_iter_start(ctx, &self->baseref, &iter);
	if (status != kdump_ok) {
		PyErr_Format(exception_map(status), kdump_err_str(ctx));
		return NULL;
	}

	if (!iter.key) {
		result = PyString_FromFormat("%s({})",
					     Py_TYPE(_self)->tp_name);
		goto out;
	}

	colon = PyString_FromString(": ");
	if (!colon)
		goto out;

	pieces = PyList_New(0);
	if (!pieces)
		goto out;

	while (iter.key) {
		s = PyString_FromString(iter.key);
		if (!s)
			goto out;
		temp = attr_dir_subscript(_self, s);
		if (!temp) {
			Py_DECREF(s);
			goto out;
		}
		PyString_Concat(&s, colon);
		PyString_ConcatAndDel(&s, PyObject_Repr(temp));
		Py_DECREF(temp);
		if (!s)
			goto out;

		res = PyList_Append(pieces, s);
		Py_DECREF(s);
		if (res <0)
			goto out;

		status = kdump_attr_iter_next(ctx, &iter);
		if (status != kdump_ok) {
			PyErr_Format(exception_map(status), kdump_err_str(ctx));
			goto out;
		}
	}

	s = PyString_FromFormat("%s({", Py_TYPE(_self)->tp_name);
	if (!s)
		goto out;
	temp = PyList_GET_ITEM(pieces, 0);
	PyString_ConcatAndDel(&s, temp);
	PyList_SET_ITEM(pieces, 0, s);
	if (!s)
		goto out;

	s = PyString_FromString("})");
	if (!s)
		goto out;
	temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
	PyString_ConcatAndDel(&temp, s);
	PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
	if (!temp)
		goto out;

	s = PyString_FromString(", ");
	if (!s)
		goto out;
	result = _PyString_Join(s, pieces);
	Py_DECREF(s);

 out:
	kdump_attr_iter_end(ctx, &iter);
	Py_XDECREF(pieces);
	Py_XDECREF(colon);
	return result;
}
示例#7
0
static PyObject *
tuplerepr(PyTupleObject *v)
{
	Py_ssize_t i, n;
	PyObject *s, *temp;
	PyObject *pieces, *result = NULL;

	n = v->ob_size;
	if (n == 0)
		return PyString_FromString("()");

	/* While not mutable, it is still possible to end up with a cycle in a
	   tuple through an object that stores itself within a tuple (and thus
	   infinitely asks for the repr of itself). This should only be
	   possible within a type. */
	i = Py_ReprEnter((PyObject *)v);
	if (i != 0) {
		return i > 0 ? PyString_FromString("(...)") : NULL;
	}

	pieces = PyTuple_New(n);
	if (pieces == NULL)
		return NULL;

	/* Do repr() on each element. */
	for (i = 0; i < n; ++i) {
		if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
			goto Done;
		s = PyObject_Repr(v->ob_item[i]);
		Py_LeaveRecursiveCall();
		if (s == NULL)
			goto Done;
		PyTuple_SET_ITEM(pieces, i, s);
	}

	/* Add "()" decorations to the first and last items. */
	assert(n > 0);
	s = PyString_FromString("(");
	if (s == NULL)
		goto Done;
	temp = PyTuple_GET_ITEM(pieces, 0);
	PyString_ConcatAndDel(&s, temp);
	PyTuple_SET_ITEM(pieces, 0, s);
	if (s == NULL)
		goto Done;

	s = PyString_FromString(n == 1 ? ",)" : ")");
	if (s == NULL)
		goto Done;
	temp = PyTuple_GET_ITEM(pieces, n-1);
	PyString_ConcatAndDel(&temp, s);
	PyTuple_SET_ITEM(pieces, n-1, temp);
	if (temp == NULL)
		goto Done;

	/* Paste them all together with ", " between. */
	s = PyString_FromString(", ");
	if (s == NULL)
		goto Done;
	result = _PyString_Join(s, pieces);
	Py_DECREF(s);	

Done:
	Py_DECREF(pieces);
	Py_ReprLeave((PyObject *)v);
	return result;
}