Exemplo n.º 1
0
static inline int
tns_render_dict(void *val, tns_outbuf *outbuf)
{
  PyObject *key, *item;
  Py_ssize_t pos = 0;

  while(PyDict_Next(val, &pos, &key, &item)) {
      if(tns_render_value(item, outbuf) == -1) {
          return -1;
      }
      if(tns_render_value(key, outbuf) == -1) {
          return -1;
      }
  }
  return 0;
}
Exemplo n.º 2
0
static PyObject*
_tnetstring_dumps(PyObject* self, PyObject *args)
{
  PyObject *object = NULL;
  PyObject *string = NULL;
  PyObject *encoding = Py_None;
  tns_ops *ops = &_tnetstring_ops_bytes;
  tns_outbuf outbuf;

  if(!PyArg_UnpackTuple(args, "dumps", 1, 2, &object, &encoding)) {
      return NULL;
  }
  if(encoding != Py_None) {
      if(!PyString_Check(encoding)) {
          PyErr_SetString(PyExc_TypeError, "encoding must be a string");
          return NULL;
      }
      Py_INCREF(encoding);
      ops = _tnetstring_get_unicode_ops(encoding);
      if(ops == NULL) {
          Py_DECREF(encoding);
          return NULL;
      }
  }
  Py_INCREF(object);

  if(tns_outbuf_init(&outbuf) == -1) {
      goto error;
  }
  if(tns_render_value(ops, object, &outbuf) == -1) {
      goto error;
  }

  Py_DECREF(object);
  string = PyString_FromStringAndSize(NULL,tns_outbuf_size(&outbuf));
  if(string == NULL) {
      goto error;
  }

  tns_outbuf_memmove(&outbuf, PyString_AS_STRING(string));
  free(outbuf.buffer);

  if(ops != &_tnetstring_ops_bytes) {
      free(ops);
      Py_DECREF(encoding);
  }

  return string;

error:
  if(ops != &_tnetstring_ops_bytes) {
      free(ops);
      Py_DECREF(encoding);
  }
  Py_DECREF(object);
  return NULL;
}
Exemplo n.º 3
0
static inline int
tns_render_list(void *val, tns_outbuf *outbuf)
{
  PyObject *item;
  Py_ssize_t idx;

  //  Remember, all output is in reverse.
  //  So we must write the last element first.
  idx = PyList_GET_SIZE(val) - 1;
  while(idx >= 0) {
      item = PyList_GET_ITEM(val, idx);
      if(tns_render_value(item, outbuf) == -1) {
          return -1;
      }
      idx--;
  }
  return 0;
}