Exemple #1
0
static int
__Pyx_CyFunction_clear(__pyx_CyFunctionObject *m)
{
    Py_CLEAR(m->func_closure);
    Py_CLEAR(m->func.m_module);
    Py_CLEAR(m->func_dict);
    Py_CLEAR(m->func_name);
    Py_CLEAR(m->func_qualname);
    Py_CLEAR(m->func_doc);
    Py_CLEAR(m->func_globals);
    Py_CLEAR(m->func_code);
    Py_CLEAR(m->func_classobj);
    Py_CLEAR(m->defaults_tuple);
    Py_CLEAR(m->defaults_kwdict);
    Py_CLEAR(m->func_annotations);

    if (m->defaults) {
        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
        int i;

        for (i = 0; i < m->defaults_pyobjects; i++)
            Py_XDECREF(pydefaults[i]);

        PyObject_Free(m->defaults);
        m->defaults = NULL;
    }
Exemple #2
0
void Object_endTypeContext(JSOBJ obj, JSONTypeContext *tc)
{
  Py_XDECREF(GET_TC(tc)->newObj);

  PyObject_Free(tc->prv);
  tc->prv = NULL;
}
Exemple #3
0
/**
 * 释放连接池的资源
 **/
static void
connectPool_dealloc(PyConnectPoolObject *v)
{
    //printf("connectPool_dealloc\n");
    int n;
    PyConnectObject *conn;
    PyListObject *conns;
    //参数检查
    if(!v)
        return;
    //关闭所有的连接
    conns = (PyListObject*)(v->cons_free);
    n = conns->ob_size;
    while(--n >= 0)
    {
        conn = (PyConnectObject*)PySequence_GetItem((PyObject*)conns, n);
        mysql_close(&conn->mysql);
    }
    conns->ob_type->tp_dealloc((PyObject*)conns);
    conns = (PyListObject*)(v->cons_busy);
    n = conns->ob_size;
    while(--n >= 0)
    {
        conn = PySequence_GetItem(conns, n);
        mysql_close(&conn->mysql);
    }
    conns->ob_type->tp_dealloc(conns);
    //释放连接池的结构体
    PyObject_Free(v);
}
Exemple #4
0
static void py_variable_to_json_internal( PyObject *obj,
                                          json_writer_t *writer )
{
  if ( PyString_CheckExact( obj ) ) {
    json_writer_write_str( writer, PyString_AS_STRING( obj ) );
  }
  else if ( PyInt_CheckExact( obj ) ) {
    json_writer_write_integer( writer, PyInt_AS_LONG( obj ) );
  }
  else if ( PyFloat_CheckExact( obj ) ) {
    json_writer_write_number( writer, PyFloat_AS_DOUBLE( obj ) );
  }
  else if ( PyBool_Check( obj ) ) {
    json_writer_write_boolean( writer, ( obj == Py_True ) );
  }
  else if ( PyUnicode_CheckExact( obj ) ) {
    /* Create a new string object that is UTF-8 encoded. */
    Py_UNICODE *unicode = PyUnicode_AS_UNICODE( obj );
    Py_ssize_t size = PyUnicode_GET_SIZE( obj );
    PyObject *str_obj = PyUnicode_EncodeUTF8( unicode, size, NULL );
    py_variable_to_json_internal( str_obj, writer );
    PyObject_Free( str_obj );
  }
  else if ( PyDict_CheckExact( obj ) ) {
    py_dict_to_json( obj, writer );
  }
  else if ( PyList_CheckExact( obj ) ) {
    py_list_to_json( obj, writer );
  }
  else if ( PyTuple_CheckExact( obj ) ) {
    py_tuple_to_json( obj, writer ); 
  }
}
Exemple #5
0
// ----------------------------------------------------------------
static void ResamplerClose( PyResamplerObject *cRes )
{
	if( cRes->resample_ctx )
		audio_resample_close( cRes->resample_ctx );

	PyObject_Free( (PyObject*)cRes );
}
Exemple #6
0
	static void py_function_dealloc( PyObject * _obj )
	{
		py_function_type * py_self = (py_function_type *)_obj;

		stdex::intrusive_ptr_release( py_self->iadapter );

		PyObject_Free( _obj );
	}
Exemple #7
0
static void
overlay_dealloc (PyGameOverlay *self)
{
    if (SDL_WasInit (SDL_INIT_VIDEO) && self->cOverlay)
        SDL_FreeYUVOverlay (self->cOverlay);

    PyObject_Free ((PyObject*)self);
}
// ----------------------------------------------------------------
static void VCString_Del( PyVCStringObject *str )
{
	if( !str->iPlane && str->pBase )
		avcodec_default_decref_buffer(str->cObj->cCodec, str->pData );

	Py_DECREF( str->cObj );
	PyObject_Free( (PyObject*)str );
}
Exemple #9
0
static void
connect_dealloc(PyConnectObject *v)
{
    if(v)
    {
        mysql_close(&v->mysql);
        PyObject_Free(v);
    }
}
Exemple #10
0
Fichier : key.c Projet : dw/acid
/**
 * Destroy the key by deallocating any private memory, and decrementing the
 * refcount on any shared buffer.
 */
static void
key_dealloc(Key *self)
{
    switch((enum KeyFlags) self->flags) {
#ifdef HAVE_MEMSINK
    case KEY_SHARED:
        ms_cancel(Key_INFO(self)->source, (PyObject *)self);
        Py_DECREF(Key_INFO(self)->source);
        break;
#endif
    case KEY_COPIED:
        PyObject_Free(self->p);
        break;
    case KEY_PRIVATE:
        break;
    }
    PyObject_Free(self);
}
static void SPBObj_dealloc(SPBObject *self)
{
	/* Cleanup of self->ob_itself goes here */
	self->ob_spb.userLong = 0;
	self->ob_thiscallback = 0;
	Py_XDECREF(self->ob_completion);
	Py_XDECREF(self->ob_interrupt);
	PyObject_Free((PyObject *)self);
}
Exemple #12
0
// ----------------------------------------------------------------
static void
SoundClose( PyOSoundObject *sound )
{
  Py_BEGIN_ALLOW_THREADS
	delete sound->cObj;
  Py_END_ALLOW_THREADS

	PyObject_Free( sound );
}
Exemple #13
0
    static void
call_PyObject_Free(void *p)
{
#ifdef Py_DEBUG
    _PyObject_DebugFree(p);
#else
    PyObject_Free(p);
#endif
}
Exemple #14
0
static void
call_PyObject_Free(void *p)
{
#if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC)
	_PyObject_DebugFree(p);
#else
	PyObject_Free(p);
#endif
}
Exemple #15
0
// ----------------------------------------------------------------
static void AFrameClose( PyAFrameObject *obj )
{
	// Close avcodec first !!!
	if( obj->cData )
	{
		Py_DECREF( obj->cData );
	}

	PyObject_Free( (PyObject*)obj );
}
Exemple #16
0
// free the numpy context buffer
void Npy_releaseContext(NpyArrContext* npyarr)
{
  PRINTMARK();
  if (npyarr)
  {
    if (npyarr->shape.ptr)
    {
      PyObject_Free(npyarr->shape.ptr);
    }
    if (npyarr->dec)
    {
      npyarr->dec->npyarr = NULL;
      npyarr->dec->curdim = 0;
    }
    Py_XDECREF(npyarr->labels[0]);
    Py_XDECREF(npyarr->labels[1]);
    Py_XDECREF(npyarr->ret);
    PyObject_Free(npyarr);
  }
}
Exemple #17
0
// ----------------------------------------------------------------
static void
CodecClose( PyCodecObject *obj )
{
	// Close avicodec first !!!
	avcodec_close(obj->cCodec);
	av_free( obj->cCodec );
	if( obj->pPaddedBuf )
		av_free( obj->pPaddedBuf );

	PyObject_Free( (PyObject*)obj );
}
Exemple #18
0
void* PyObject_Realloc(void* data, size_t nsize)
{
  size_t size;
  struct _alloc_s* s = _na_find(data);
  void* ndata = PyObject_Malloc(nsize);

  _na_assert(s->ptr == data, "realloc got a pointer not returned by malloc");
  size = ((char*)s) + s->npages * PAGESIZE - (char*)data;
  memcpy(ndata, data, size<nsize ? size : nsize);
  PyObject_Free(data);
  return ndata;
}
Exemple #19
0
// ----------------------------------------------------------------
static void
FrameClose( PyVFrameObject *frame )
{
	int i= 0;
	for( i=0; i< 3; i++ )
		if( frame->cData[ i ] )
		{
			Py_DECREF( frame->cData[ i ] );
		}

	PyObject_Free( (PyObject*)frame );
}
Exemple #20
0
// ----------------------------------------------------------------
static void AnalyzerClose( PyAnalyzerObject *cRes )
{
	if( cRes->analyzer )
		delete cRes->analyzer;

	cRes->analyzer= NULL;

	if( cRes->pFreqs )
		free( cRes->pFreqs );
	if( cRes->pSamples )
		free( cRes->pSamples );
	PyObject_Free( (PyObject*)cRes );
}
Exemple #21
0
static void
cursor_dealloc(PyCursorObject *v)
{
    //首先检查缓冲池里面的空闲游标数量
    //加互斥量
    if(cursor_free_num > CURSOR_FREE_MAX)
    {
        PyObject_Free((PyObject*)v);
    }
    else
    {
        v->ob_type = cursor_free;
        cursor_free = v;
        cursor_free_num ++;
    }
}
Exemple #22
0
PyFutureFeatures *
PyFuture_FromASTObject(mod_ty mod, PyObject *filename)
{
    PyFutureFeatures *ff;

    ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
    if (ff == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    ff->ff_features = 0;
    ff->ff_lineno = -1;

    if (!future_parse(ff, mod, filename)) {
        PyObject_Free(ff);
        return NULL;
    }
    return ff;
}
Exemple #23
0
Fichier : key.c Projet : dw/acid
/**
 * Construct a KEY_SHARED Key from ...
 */
Key *
acid_make_shared_key(PyObject *source, uint8_t *p, Py_ssize_t size)
{
    if(size > KEY_MAXSIZE) {
        PyErr_SetString(PyExc_ValueError, "Key is too long .");
        return NULL;
    }

    Key *self = PyObject_Malloc(sizeof(Key) + sizeof(SharedKeyInfo));
    // TODO: relies on arch padding rules
    if(self) {
        PyObject_Init((PyObject *)self, &KeyType);
        if(ms_listen(source, (PyObject *) self)) {
            PyObject_Free(self);
            return NULL;
        }
        self->flags = KEY_SHARED;
        self->p = p;
        Key_SIZE(self) = size;
        Key_INFO(self)->source = source;
        Py_INCREF(source);
    }
    return self;
}
Exemple #24
0
void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
{
  PyObject *obj, *exc, *toDictFunc, *iter;
  TypeContext *pc;
  PRINTMARK();
  if (!_obj) {
    tc->type = JT_INVALID;
    return;
  }

  obj = (PyObject*) _obj;

  pc = (TypeContext *) tc->prv;
  if (!pc)
  {
    tc->type = JT_INVALID;
    PyErr_NoMemory();
    return;
  }
  pc->newObj = NULL;
  pc->dictObj = NULL;
  pc->itemValue = NULL;
  pc->itemName = NULL;
  pc->iterator = NULL;
  pc->attrList = NULL;
  pc->index = 0;
  pc->size = 0;
  pc->longValue = 0;

  if (PyIter_Check(obj))
  {
    PRINTMARK();
    goto ISITERABLE;
  }

  if (PyBool_Check(obj))
  {
    PRINTMARK();
    tc->type = (obj == Py_True) ? JT_TRUE : JT_FALSE;
    return;
  }
  else
  if (PyLong_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyLongToINT64;
    tc->type = JT_LONG;
    GET_TC(tc)->longValue = PyLong_AsLongLong(obj);

    exc = PyErr_Occurred();

    if (exc && PyErr_ExceptionMatches(PyExc_OverflowError))
    {
      PRINTMARK();
      goto INVALID;
    }

    return;
  }
  else
  if (PyInt_Check(obj))
  {
    PRINTMARK();
#ifdef _LP64
    pc->PyTypeToJSON = PyIntToINT64; tc->type = JT_LONG;
#else
    pc->PyTypeToJSON = PyIntToINT32; tc->type = JT_INT;
#endif
    return;
  }
  else
  if (PyString_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyStringToUTF8; tc->type = JT_UTF8;
    return;
  }
  else
  if (PyUnicode_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyUnicodeToUTF8; tc->type = JT_UTF8;
    return;
  }
  else
  if (PyFloat_Check(obj) || (type_decimal && PyObject_IsInstance(obj, type_decimal)))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyFloatToDOUBLE; tc->type = JT_DOUBLE;
    return;
  }
  else
  if (PyDateTime_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyDateTimeToINT64; tc->type = JT_LONG;
    return;
  }
  else
  if (PyDate_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyDateToINT64; tc->type = JT_LONG;
    return;
  }
  else
  if (obj == Py_None)
  {
    PRINTMARK();
    tc->type = JT_NULL;
    return;
  }

ISITERABLE:
  if (PyDict_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_OBJECT;
    SetupDictIter(obj, pc);
    Py_INCREF(obj);
    return;
  }
  else
  if (PyList_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterEnd = List_iterEnd;
    pc->iterNext = List_iterNext;
    pc->iterGetValue = List_iterGetValue;
    pc->iterGetName = List_iterGetName;
    GET_TC(tc)->index =  0;
    GET_TC(tc)->size = PyList_GET_SIZE( (PyObject *) obj);
    return;
  }
  else
  if (PyTuple_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterEnd = Tuple_iterEnd;
    pc->iterNext = Tuple_iterNext;
    pc->iterGetValue = Tuple_iterGetValue;
    pc->iterGetName = Tuple_iterGetName;
    GET_TC(tc)->index = 0;
    GET_TC(tc)->size = PyTuple_GET_SIZE( (PyObject *) obj);
    GET_TC(tc)->itemValue = NULL;

    return;
  }
  /*
  else
  if (PyAnySet_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterBegin = NULL;
    pc->iterEnd = Iter_iterEnd;
    pc->iterNext = Iter_iterNext;
    pc->iterGetValue = Iter_iterGetValue;
    pc->iterGetName = Iter_iterGetName;
    return;
  }
  */

  toDictFunc = PyObject_GetAttrString(obj, "toDict");

  if (toDictFunc)
  {
    PyObject* tuple = PyTuple_New(0);
    PyObject* toDictResult = PyObject_Call(toDictFunc, tuple, NULL);
    Py_DECREF(tuple);
    Py_DECREF(toDictFunc);

    if (toDictResult == NULL)
    {
      PyErr_Clear();
      tc->type = JT_NULL;
      return;
    }

    if (!PyDict_Check(toDictResult))
    {
      Py_DECREF(toDictResult);
      tc->type = JT_NULL;
      return;
    }

    PRINTMARK();
    tc->type = JT_OBJECT;
    SetupDictIter(toDictResult, pc);
    return;
  }

  PRINTMARK();
  PyErr_Clear();

  iter = PyObject_GetIter(obj);

  if (iter != NULL)
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterator = iter;
    pc->iterEnd = Iter_iterEnd;
    pc->iterNext = Iter_iterNext;
    pc->iterGetValue = Iter_iterGetValue;
    pc->iterGetName = Iter_iterGetName;
    return;
  }

  PRINTMARK();
  PyErr_Clear();

  PRINTMARK();
  tc->type = JT_OBJECT;
  GET_TC(tc)->attrList = PyObject_Dir(obj);
  
  if (GET_TC(tc)->attrList == NULL)
  {
    PyErr_Clear();
    goto INVALID;
  }

  GET_TC(tc)->index = 0;
  GET_TC(tc)->size = PyList_GET_SIZE(GET_TC(tc)->attrList);
  PRINTMARK();
  
  pc->iterEnd = Dir_iterEnd;
  pc->iterNext = Dir_iterNext;
  pc->iterGetValue = Dir_iterGetValue;
  pc->iterGetName = Dir_iterGetName;
  return;

INVALID:
  PRINTMARK();
  tc->type = JT_INVALID;
  PyObject_Free(tc->prv);
  tc->prv = NULL;
  return;
}
Exemple #25
0
PyObject *EscapeQueryArguments(Connection *self, PyObject *inQuery, PyObject *iterable)
{
  size_t cbOutQuery = 0;
  char *obuffer;
  char *optr;
  char *iptr;
  int heap = 0;
  int hasArg = 0;
  int appendLen;
  PyObject *retobj;
  PyObject *iterator;
  PyObject *arg;

  // Estimate output length

  cbOutQuery += PyString_GET_SIZE(inQuery);

  iterator = PyObject_GetIter(iterable);

  while ( (arg = PyIter_Next(iterator)))
  {
    // Quotes;
    cbOutQuery += 2;

    // Worst case escape and utf-8
    if (PyString_Check(arg))
      cbOutQuery += (PyString_GET_SIZE(arg) * 2);
    else
      if (PyUnicode_Check(arg))
        cbOutQuery += (PyUnicode_GET_SIZE(arg) * 6);
      else
        cbOutQuery += 64;

    Py_DECREF(arg);
  }

  Py_DECREF(iterator);

  if (cbOutQuery > (1024 * 64))
  {
    /*
    FIXME: Allocate a PyString and resize it just like the Python code does it */
    obuffer = (char *) PyObject_Malloc(cbOutQuery);
    heap = 1;
  }
  else
  {
    obuffer = (char *) alloca (cbOutQuery);
  }


  optr = obuffer;
  iptr = PyString_AS_STRING(inQuery);

  hasArg = 0;


  iterator = PyObject_GetIter(iterable);

  while (1)
  {
    switch (*iptr)
    {
    case '\0':
      goto END_PARSE;

    case '%':

      iptr ++;

      if (*iptr != 's' && *iptr != '%')
      {
        Py_DECREF(iterator);
        if (heap) PyObject_Free(obuffer);
        return PyErr_Format (PyExc_ValueError, "Found character %c expected %%", *iptr);
      }

      if (*iptr == '%')
      {
        *(optr++) = *(iptr)++;
        break;
      }

      iptr ++;

      arg = PyIter_Next(iterator);

      if (arg == NULL)
      {
        Py_DECREF(iterator);
        if (heap) PyObject_Free(obuffer);
        return PyErr_Format (PyExc_ValueError, "Unexpected end of iterator found");
      }

      appendLen = AppendEscapedArg(self, optr, obuffer + cbOutQuery, arg);
      Py_DECREF(arg);

      if (appendLen == -1)
      {
        Py_DECREF(iterator);
        if (heap) PyObject_Free(obuffer);
        return NULL;
      }

      optr += appendLen;

      break;

    default:
      *(optr++) = *(iptr)++;
      break;
    }
  }


END_PARSE:
  Py_DECREF(iterator);

  retobj = PyString_FromStringAndSize (obuffer, (optr - obuffer));

  if (heap)
  {
    PyObject_Free(obuffer);
  }

  return retobj;
}
Exemple #26
0
// ----------------------------------------------------------------
static void ACString_Del( PyACStringObject *str )
{
	av_free( str->pData );
	PyObject_Free( (PyObject*)str );
}
Exemple #27
0
static void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObjectEncoder *enc)
{
  PyObject *obj, *objRepr, *exc;
  TypeContext *pc;
  PRINTMARK();
  if (!_obj)
  {
    tc->type = JT_INVALID;
    return;
  }

  obj = (PyObject*) _obj;

  tc->prv = PyObject_Malloc(sizeof(TypeContext));
  pc = (TypeContext *) tc->prv;
  if (!pc)
  {
    tc->type = JT_INVALID;
    PyErr_NoMemory();
    return;
  }
  pc->newObj = NULL;
  pc->dictObj = NULL;
  pc->itemValue = NULL;
  pc->itemName = NULL;
  pc->iterator = NULL;
  pc->attrList = NULL;
  pc->index = 0;
  pc->size = 0;
  pc->longValue = 0;
  pc->rawJSONValue = NULL;

  if (PyIter_Check(obj))
  {
    PRINTMARK();
    goto ISITERABLE;
  }

  if (PyBool_Check(obj))
  {
    PRINTMARK();
    tc->type = (obj == Py_True) ? JT_TRUE : JT_FALSE;
    return;
  }
  else
  if (PyLong_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyLongToINT64;
    tc->type = JT_LONG;
    GET_TC(tc)->longValue = PyLong_AsLongLong(obj);

    exc = PyErr_Occurred();
    if (!exc)
    {
        return;
    }

    if (exc && PyErr_ExceptionMatches(PyExc_OverflowError))
    {
      PyErr_Clear();
      pc->PyTypeToJSON = PyLongToUINT64;
      tc->type = JT_ULONG;
      GET_TC(tc)->unsignedLongValue = PyLong_AsUnsignedLongLong(obj);

      exc = PyErr_Occurred();
      if (exc && PyErr_ExceptionMatches(PyExc_OverflowError))
      {
        PRINTMARK();
        goto INVALID;
      }
    }

    return;
  }
  else
  if (PyInt_Check(obj))
  {
    PRINTMARK();
#ifdef _LP64
    pc->PyTypeToJSON = PyIntToINT64; tc->type = JT_LONG;
#else
    pc->PyTypeToJSON = PyIntToINT32; tc->type = JT_INT;
#endif
    return;
  }
  else
  if (PyString_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyStringToUTF8; tc->type = JT_UTF8;
    return;
  }
  else
  if (PyUnicode_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyUnicodeToUTF8; tc->type = JT_UTF8;
    return;
  }
  else
  if (PyFloat_Check(obj) || (type_decimal && PyObject_IsInstance(obj, type_decimal)))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyFloatToDOUBLE; tc->type = JT_DOUBLE;
    return;
  }
  else
  if (obj == Py_None)
  {
    PRINTMARK();
    tc->type = JT_NULL;
    return;
  }

ISITERABLE:
  if (PyDict_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_OBJECT;
    SetupDictIter(obj, pc, enc);
    Py_INCREF(obj);
    return;
  }
  else
  if (PyList_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterEnd = List_iterEnd;
    pc->iterNext = List_iterNext;
    pc->iterGetValue = List_iterGetValue;
    pc->iterGetName = List_iterGetName;
    GET_TC(tc)->index =  0;
    GET_TC(tc)->size = PyList_GET_SIZE( (PyObject *) obj);
    return;
  }
  else
  if (PyTuple_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterEnd = Tuple_iterEnd;
    pc->iterNext = Tuple_iterNext;
    pc->iterGetValue = Tuple_iterGetValue;
    pc->iterGetName = Tuple_iterGetName;
    GET_TC(tc)->index = 0;
    GET_TC(tc)->size = PyTuple_GET_SIZE( (PyObject *) obj);
    GET_TC(tc)->itemValue = NULL;

    return;
  }

  if (UNLIKELY(PyObject_HasAttrString(obj, "toDict")))
  {
    PyObject* toDictFunc = PyObject_GetAttrString(obj, "toDict");
    PyObject* tuple = PyTuple_New(0);
    PyObject* toDictResult = PyObject_Call(toDictFunc, tuple, NULL);
    Py_DECREF(tuple);
    Py_DECREF(toDictFunc);

    if (toDictResult == NULL)
    {
      goto INVALID;
    }

    if (!PyDict_Check(toDictResult))
    {
      Py_DECREF(toDictResult);
      tc->type = JT_NULL;
      return;
    }

    PRINTMARK();
    tc->type = JT_OBJECT;
    SetupDictIter(toDictResult, pc, enc);
    return;
  }
  else
  if (UNLIKELY(PyObject_HasAttrString(obj, "__json__")))
  {
    PyObject* toJSONFunc = PyObject_GetAttrString(obj, "__json__");
    PyObject* tuple = PyTuple_New(0);
    PyObject* toJSONResult = PyObject_Call(toJSONFunc, tuple, NULL);
    Py_DECREF(tuple);
    Py_DECREF(toJSONFunc);

    if (toJSONResult == NULL)
    {
      goto INVALID;
    }

    if (PyErr_Occurred())
    {
      Py_DECREF(toJSONResult);
      goto INVALID;
    }

    if (!PyString_Check(toJSONResult) && !PyUnicode_Check(toJSONResult))
    {
      Py_DECREF(toJSONResult);
      PyErr_Format (PyExc_TypeError, "expected string");
      goto INVALID;
    }

    PRINTMARK();
    pc->PyTypeToJSON = PyRawJSONToUTF8;
    tc->type = JT_RAW;
    GET_TC(tc)->rawJSONValue = toJSONResult;
    return;
  }

  PRINTMARK();
  PyErr_Clear();

  objRepr = PyObject_Repr(obj);
  PyErr_Format (PyExc_TypeError, "%s is not JSON serializable", PyString_AS_STRING(objRepr));
  Py_DECREF(objRepr);

INVALID:
  PRINTMARK();
  tc->type = JT_INVALID;
  PyObject_Free(tc->prv);
  tc->prv = NULL;
  return;
}
Exemple #28
0
void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
{
  PyObject *obj, *exc, *toDictFunc;
  TypeContext *pc;
  PRINTMARK();
  if (!_obj) {
    tc->type = JT_INVALID;
    return;
  }

  obj = (PyObject*) _obj;

  tc->prv = PyObject_Malloc(sizeof(TypeContext));
  pc = (TypeContext *) tc->prv;
  if (!pc)
  {
    tc->type = JT_INVALID;
    PyErr_NoMemory();
    return;
  }
  pc->newObj = NULL;
  pc->dictObj = NULL;
  pc->itemValue = NULL;
  pc->itemName = NULL;
  pc->attrList = NULL;
  pc->index = 0;
  pc->size = 0;
  pc->longValue = 0;

  if (PyIter_Check(obj))
  {
    PRINTMARK();
    goto ISITERABLE;
  }

  if (PyBool_Check(obj))
  {
    PRINTMARK();
    tc->type = (obj == Py_True) ? JT_TRUE : JT_FALSE;
    return;
  }
  else
  if (PyLong_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyLongToINT64;
    tc->type = JT_LONG;
    GET_TC(tc)->longValue = PyLong_AsLongLong(obj);

    exc = PyErr_Occurred();

    if (exc && PyErr_ExceptionMatches(PyExc_OverflowError))
    {
#if HAS_JSON_HANDLE_BIGINTS
      PyErr_Clear();
      pc->PyTypeToJSON = PyBigIntToSTR;
      tc->type = JT_BIGINT;
      GET_TC(tc)->longValue = 0;
      return;
#endif
      PRINTMARK();
      goto INVALID;
    }

    return;
  }
  else
  if (PyInt_Check(obj))
  {
    PRINTMARK();
#ifdef _LP64
    pc->PyTypeToJSON = PyIntToINT64; tc->type = JT_LONG;
#else
    pc->PyTypeToJSON = PyIntToINT32; tc->type = JT_INT;
#endif
    return;
  }
  else
  if (PyString_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyStringToUTF8; tc->type = JT_UTF8;
    return;
  }
  else
  if (PyUnicode_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyUnicodeToUTF8; tc->type = JT_UTF8;
    return;
  }
  else
  if (PyFloat_Check(obj) || (type_decimal && PyObject_IsInstance(obj, type_decimal)))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyFloatToDOUBLE; tc->type = JT_DOUBLE;
    return;
  }
  else
  if (PyDateTime_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyDateTimeToINT64; tc->type = JT_LONG;
    return;
  }
  else
  if (PyDate_Check(obj))
  {
    PRINTMARK();
    pc->PyTypeToJSON = PyDateToINT64; tc->type = JT_LONG;
    return;
  }
  else
  if (obj == Py_None)
  {
    PRINTMARK();
    tc->type = JT_NULL;
    return;
  }

ISITERABLE:
  if (PyDict_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_OBJECT;
    pc->iterBegin = Dict_iterBegin;
    pc->iterEnd = Dict_iterEnd;
    pc->iterNext = Dict_iterNext;
    pc->iterGetValue = Dict_iterGetValue;
    pc->iterGetName = Dict_iterGetName;
    pc->dictObj = obj;
    Py_INCREF(obj);
    return;
  }
  else
  if (PyList_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterBegin = List_iterBegin;
    pc->iterEnd = List_iterEnd;
    pc->iterNext = List_iterNext;
    pc->iterGetValue = List_iterGetValue;
    pc->iterGetName = List_iterGetName;
    return;
  }
  else
  if (PyTuple_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterBegin = Tuple_iterBegin;
    pc->iterEnd = Tuple_iterEnd;
    pc->iterNext = Tuple_iterNext;
    pc->iterGetValue = Tuple_iterGetValue;
    pc->iterGetName = Tuple_iterGetName;
    return;
  }
  else
  if (PyAnySet_Check(obj))
  {
    PRINTMARK();
    tc->type = JT_ARRAY;
    pc->iterBegin = Iter_iterBegin;
    pc->iterEnd = Iter_iterEnd;
    pc->iterNext = Iter_iterNext;
    pc->iterGetValue = Iter_iterGetValue;
    pc->iterGetName = Iter_iterGetName;
    return;
  }

  toDictFunc = PyObject_GetAttrString(obj, "toDict");

  if (toDictFunc)
  {
    PyObject* tuple = PyTuple_New(0);
    PyObject* toDictResult = PyObject_Call(toDictFunc, tuple, NULL);
    Py_DECREF(tuple);
    Py_DECREF(toDictFunc);

    if (toDictResult == NULL)
    {
      PyErr_Clear();
      tc->type = JT_NULL;
      return;
    }

    if (!PyDict_Check(toDictResult))
    {
      Py_DECREF(toDictResult);
      tc->type = JT_NULL;
      return;
    }

    PRINTMARK();
    tc->type = JT_OBJECT;
    pc->iterBegin = Dict_iterBegin;
    pc->iterEnd = Dict_iterEnd;
    pc->iterNext = Dict_iterNext;
    pc->iterGetValue = Dict_iterGetValue;
    pc->iterGetName = Dict_iterGetName;
    pc->dictObj = toDictResult;
    return;
  }

  PyErr_Clear();

  PRINTMARK();
  // Falling to INVALID case as this type of object(class instance, module,
  // class, function, etc..) can't be serialized.
  PyErr_Format (PyExc_TypeError, "%s", "Object is not JSON serializable");

INVALID:
  tc->type = JT_INVALID;
  PyObject_Free(tc->prv);
  tc->prv = NULL;
  return;
}
Exemple #29
0
// ----------------------------------------------------------------
static void
CDClose( PyCDObject *cd )
{
	delete cd->cObject;
	PyObject_Free( cd );
}
static void SndCh_dealloc(SndChannelObject *self)
{
	SndDisposeChannel(self->ob_itself, 1);
	Py_XDECREF(self->ob_callback);
	PyObject_Free((PyObject *)self);
}