コード例 #1
0
ファイル: timemodule.c プロジェクト: pogigroo/py3k-__format__
static PyObject *
structtime_totuple(PyObject *t)
{
    PyObject *x = NULL;
    unsigned int i;
    PyObject *v = PyTuple_New(9);
    if (v == NULL)
        return NULL;

    for (i=0; i<9; i++) {
        x = PyStructSequence_GET_ITEM(t, i);
        Py_INCREF(x);
        PyTuple_SET_ITEM(v, i, x);
    }

    if (PyErr_Occurred()) {
        Py_XDECREF(v);
        return NULL;
    }

    return v;
}
コード例 #2
0
static PyObject *structseq_repr(PyObject *iobj) {

/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100

  PyStructSequence *obj = (PyStructSequence *)iobj;
  PyTypeObject *typ = Py_TYPE(obj);
  bool removelast = false;
  Py_ssize_t len, i;
  char buf[REPR_BUFFER_SIZE];
  char *endofbuf, *pbuf = buf;
  /* pointer to end of writeable buffer; safes space for "...)\0" */
  endofbuf = &buf[REPR_BUFFER_SIZE - 5];

  /* "typename(", limited to  TYPE_MAXSIZE */
  len =
      strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE : strlen(typ->tp_name);
  strncpy(pbuf, typ->tp_name, len);
  pbuf += len;
  *pbuf++ = '(';

  for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
    PyObject *val, *repr;
    char *crepr;

    val = PyStructSequence_GET_ITEM(obj, i);
    repr = PyObject_Repr(val);
    if (repr == NULL)
      return NULL;
    crepr = PyUnicode_AsUTF8(repr);
    if (crepr == NULL) {
      Py_DECREF(repr);
      return NULL;
    }

    /* + 3: keep space for ", " */
    len = strlen(crepr) + 2;
    if ((pbuf + len) <= endofbuf) {
      strcpy(pbuf, crepr);
      pbuf += strlen(crepr);
      *pbuf++ = ',';
      *pbuf++ = ' ';
      removelast = 1;
      Py_DECREF(repr);
    } else {
      strcpy(pbuf, "...");
      pbuf += 3;
      removelast = 0;
      Py_DECREF(repr);
      break;
    }
  }
  if (removelast) {
    /* overwrite last ", " */
    pbuf -= 2;
  }
  *pbuf++ = ')';
  *pbuf = '\0';

  return PyUnicode_FromString(buf);
}
コード例 #3
0
ファイル: structseq.c プロジェクト: Naddiseo/cpython
PyObject*
PyStructSequence_GetItem(PyObject* op, Py_ssize_t i)
{
    return PyStructSequence_GET_ITEM(op, i);
}
コード例 #4
0
ファイル: structseq.c プロジェクト: Naddiseo/cpython
static PyObject *
structseq_repr(PyStructSequence *obj)
{
    /* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100

    PyTypeObject *typ = Py_TYPE(obj);
    int i, removelast = 0;
    Py_ssize_t len;
    char buf[REPR_BUFFER_SIZE];
    char *endofbuf, *pbuf = buf;

    /* pointer to end of writeable buffer; safes space for "...)\0" */
    endofbuf= &buf[REPR_BUFFER_SIZE-5];

    /* "typename(", limited to  TYPE_MAXSIZE */
    len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
                            strlen(typ->tp_name);
    strncpy(pbuf, typ->tp_name, len);
    pbuf += len;
    *pbuf++ = '(';

    for (i=0; i < VISIBLE_SIZE(obj); i++) {
        PyObject *val, *repr;
        char *cname, *crepr;

        cname = typ->tp_members[i].name;
        if (cname == NULL) {
            PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %d name is NULL"
                         " for type %.500s", i, typ->tp_name);
            return NULL;
        }
        val = PyStructSequence_GET_ITEM(obj, i);
        repr = PyObject_Repr(val);
        if (repr == NULL)
            return NULL;
        crepr = _PyUnicode_AsString(repr);
        if (crepr == NULL) {
            Py_DECREF(repr);
            return NULL;
        }

        /* + 3: keep space for "=" and ", " */
        len = strlen(cname) + strlen(crepr) + 3;
        if ((pbuf+len) <= endofbuf) {
            strcpy(pbuf, cname);
            pbuf += strlen(cname);
            *pbuf++ = '=';
            strcpy(pbuf, crepr);
            pbuf += strlen(crepr);
            *pbuf++ = ',';
            *pbuf++ = ' ';
            removelast = 1;
            Py_DECREF(repr);
        }
        else {
            strcpy(pbuf, "...");
            pbuf += 3;
            removelast = 0;
            Py_DECREF(repr);
            break;
        }
    }
    if (removelast) {
        /* overwrite last ", " */
        pbuf-=2;
    }
    *pbuf++ = ')';
    *pbuf = '\0';

    return PyUnicode_FromString(buf);
}