Example #1
0
    static inline PyObject* execute(Arg& x)
    {
        BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >));

        PyTypeObject* type = Derived::get_class_object(x);

        if (type == 0)
            return python::detail::none();

        PyObject* raw_result = type->tp_alloc(
            type, objects::additional_instance_size<Holder>::value);
          
        if (raw_result != 0)
        {
            python::detail::decref_guard protect(raw_result);
            
            instance_t* instance = (instance_t*)raw_result;
            
            // construct the new C++ object and install the pointer
            // in the Python object.
            Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
              
            // Note the position of the internally-stored Holder,
            // for the sake of destruction
            Py_SIZE(instance) = offsetof(instance_t, storage);

            // Release ownership of the python object
            protect.cancel();
        }
        return raw_result;
    }
Example #2
0
 template <typename U> static PyObject *c2py(U &&x) {
   PyTypeObject *p = get_type_ptr(typeid(T));
   if (p == nullptr) return NULL;
   py_type *self = (py_type *)p->tp_alloc(p, 0);
   if (self != NULL) { self->_c = new T{std::forward<U>(x)}; }
   return (PyObject *)self;
 }
Example #3
0
static PyObject* reconstruct(PyObject* self, PyObject* args)
{
    PyTypeObject* klass;
    PyObject* base;
    PyObject* state;

    if (!PyArg_ParseTuple(args, "OOO", &klass, &base, &state))
        return NULL;

    if (!PyType_Check(klass)) {
        PyErr_SetString(PyExc_TypeError, "argument 1 must be a type object");
        return NULL;
    }

    if (!PyType_Check(base)) {
        PyErr_SetString(PyExc_TypeError, "argument 2 must be a type object");
        return NULL;
    }

    if (!PyTuple_Check(state)) {
        PyErr_SetString(PyExc_TypeError, "argument 3 must be a tuple");
        return NULL;
    }

    return klass->tp_new(klass, state, NULL);
}
Example #4
0
/* PycairoFontFace_FromFontFace
 * Create a new PycairoFontFace from a cairo_font_face_t
 * font_face - a cairo_font_face_t to 'wrap' into a Python object.
 *             it is unreferenced if the PycairoFontFace creation fails
 * Return value: New reference or NULL on failure
 */
PyObject *
PycairoFontFace_FromFontFace (cairo_font_face_t *font_face)
{
    PyTypeObject *type = NULL;
    PyObject *o;

    assert (font_face != NULL);

    if (Pycairo_Check_Status (cairo_font_face_status (font_face))) {
	cairo_font_face_destroy (font_face);
	return NULL;
    }

    switch (cairo_font_face_get_type (font_face)) {
    case CAIRO_FONT_TYPE_TOY:
        type = &PycairoToyFontFace_Type;
        break;
    default:
        type = &PycairoFontFace_Type;
        break;
    }
    o = type->tp_alloc (type, 0);
    if (o == NULL)
	cairo_font_face_destroy (font_face);
    else
	((PycairoFontFace *)o)->font_face = font_face;
    return o;
}
Example #5
0
PyObject*
IcePy::createConnectionInfo(const Ice::ConnectionInfoPtr& connectionInfo)
{
    PyTypeObject* type;
    if(Ice::TCPConnectionInfoPtr::dynamicCast(connectionInfo))
    {
        type = &TCPConnectionInfoType;
    }
    else if(Ice::UDPConnectionInfoPtr::dynamicCast(connectionInfo))
    {
        type = &UDPConnectionInfoType;
    }
    else if(Ice::IPConnectionInfoPtr::dynamicCast(connectionInfo))
    {
        type = &IPConnectionInfoType;
    }
    else
    {
        type = &ConnectionInfoType;
    }

    ConnectionInfoObject* obj = reinterpret_cast<ConnectionInfoObject*>(type->tp_alloc(type, 0));
    if(!obj)
    {
        return 0;
    }
    obj->connectionInfo = new Ice::ConnectionInfoPtr(connectionInfo);

    return (PyObject*)obj;
}
PyObject* GeometryPy::copy(PyObject *args)
{
    if (!PyArg_ParseTuple(args, ""))
        return NULL;

    Part::Geometry* geom = this->getGeometryPtr();
    PyTypeObject* type = this->GetType();
    PyObject* cpy = 0;
    // let the type object decide
    if (type->tp_new)
        cpy = type->tp_new(type, this, 0);
    if (!cpy) {
        PyErr_SetString(PyExc_TypeError, "failed to create copy of geometry");
        return 0;
    }

    Part::GeometryPy* geompy = static_cast<Part::GeometryPy*>(cpy);
    // the PyMake function must have created the corresponding instance of the 'Geometry' subclass
    // so delete it now to avoid a memory leak
    if (geompy->_pcTwinPointer) {
        Part::Geometry* clone = static_cast<Part::Geometry*>(geompy->_pcTwinPointer);
        delete clone;
    }
    geompy->_pcTwinPointer = geom->copy();
    return cpy;
}
Example #7
0
/**
 * Factory function for a CArray used if only the number of items is known given.
 *
 * Note that this method does not increment the reference counter. You are responsible for
 * calling Py_INCREF(obj) in the returned obj yourself
 */
PyObject* CArray_createFromLength(const char* format, int length) {
    PyTypeObject* type = &CArray_Type;
    CArrayObj* self;
    void* items;
    size_t item_size;

    item_size = CArray_getItemSize(format);
    if (item_size <= 0) {
        return NULL;
    }

    items = calloc(item_size, length);
    if (items == NULL) {
        PyErr_SetString(PyExc_MemoryError, "out of memory");
        return NULL;
    }

    if (length <= 0) {
        PyErr_SetString(PyExc_ValueError, "length must be > 0");
        return NULL;
    }

    self = (CArrayObj*) type->tp_alloc(type, 0);
    CArray_initInstance(self, format, items, item_size, length, CArray_releaseElements);
    return (PyObject*) self;
}
Example #8
0
File: upb.c Project: chenbk85/upb
static upb_sflow_t PyUpb_Message_StartSubmessage(void *m, upb_value fval) {
  PyObject **submsg = PyUpb_Accessor_GetPtr(m, fval);
  PyTypeObject *type = Py_TYPE(m);
  if (!*submsg) *submsg = type->tp_alloc(type, 0);
  upb_stdmsg_sethas(m, fval);
  return UPB_CONTINUE_WITH(*submsg);
}
Example #9
0
File: upb.c Project: chenbk85/upb
static upb_sflow_t PyUpb_Message_StartSequence(void *m, upb_value fval) {
  PyObject **seq = PyUpb_Accessor_GetPtr(m, fval);
  PyTypeObject *type = ((PyUpb_MessageType*)Py_TYPE(m))->alt_type;
  if (!*seq) *seq = type->tp_alloc(type, 0);
  upb_stdmsg_sethas(m, fval);
  return UPB_CONTINUE_WITH(*seq);
}
Example #10
0
File: _k20.c Project: kevinarpe/kx
static PyObject *
_K_subscript(_K *self, PyObject *key)
{
	int i;
	K kobj = self->kobj;
	char *skey;
	int key_length;
	int value_index = 1;
	if (kobj->t != 5) {
		PyErr_Format(PyExc_TypeError,
			     "k object of type %d is not a dictionary", kobj->t);
		return NULL;
	}
	if (-1 == PyString_AsStringAndSize(key, &skey, &key_length)) {
		return NULL;
	}
	if (skey[key_length-1] == '.') {
		--key_length;
		++value_index;
	}
	for (i=0; i < kobj->n; ++i) {
		K e = KK(kobj)[i];
		if (0 == strncmp(skey,Ks(KK(e)[0]),key_length)) {
			PyTypeObject* type = self->ob_type;
			_K* k = (_K*)type->tp_alloc(type, 0);
			k->kobj = ci(KK(e)[value_index]);
			return (PyObject*)k;
		}
	}
	PyErr_SetObject(PyExc_KeyError, key);
	return NULL;
}
Example #11
0
PyObject *
pygi_struct_new_from_g_type (GType g_type,
                             gpointer      pointer,
                             gboolean      free_on_dealloc)
{
    PyGIStruct *self;
    PyTypeObject *type;

    type = (PyTypeObject *)pygi_type_import_by_g_type (g_type);

    if (!type)
        type = (PyTypeObject *)&PyGIStruct_Type; /* fallback */

    if (!PyType_IsSubtype (type, &PyGIStruct_Type)) {
        PyErr_SetString (PyExc_TypeError, "must be a subtype of gi.Struct");
        return NULL;
    }

    self = (PyGIStruct *) type->tp_alloc (type, 0);
    if (self == NULL) {
        return NULL;
    }

    pyg_pointer_set_ptr (self, pointer);
    ( (PyGPointer *) self)->gtype = g_type;
    self->free_on_dealloc = free_on_dealloc;

    return (PyObject *) self;
}
Example #12
0
PyObject*
IcePy::createEndpointInfo(const Ice::EndpointInfoPtr& endpointInfo)
{
    PyTypeObject* type;
    if(Ice::TCPEndpointInfoPtr::dynamicCast(endpointInfo))
    {
        type = &TCPEndpointInfoType;
    }
    else if(Ice::UDPEndpointInfoPtr::dynamicCast(endpointInfo))
    {
        type = &UDPEndpointInfoType;
    }
    else if(Ice::OpaqueEndpointInfoPtr::dynamicCast(endpointInfo))
    {
        type = &OpaqueEndpointInfoType;
    }
    else if(Ice::IPEndpointInfoPtr::dynamicCast(endpointInfo))
    {
        type = &IPEndpointInfoType;
    }
    else
    {
        type = &EndpointInfoType;
    }

    EndpointInfoObject* obj = reinterpret_cast<EndpointInfoObject*>(type->tp_alloc(type, 0));
    if(!obj)
    {
        return 0;
    }
    obj->endpointInfo = new Ice::EndpointInfoPtr(endpointInfo);

    return (PyObject*)obj;
}
Example #13
0
/*
 * Create main Fiber. There is always a main Fiber for a given (real) thread,
 * and it's parent is always NULL.
 */
static Fiber *
fiber_create_main(void)
{
    Fiber *t_main;
    PyObject *dict = PyThreadState_GetDict();
    PyTypeObject *cls = (PyTypeObject *)&FiberType;

    if (dict == NULL) {
        if (!PyErr_Occurred()) {
            PyErr_NoMemory();
        }
        return NULL;
    }

    /* create the main Fiber for this thread */
    t_main = (Fiber *)cls->tp_new(cls, NULL, NULL);
    if (!t_main) {
        return NULL;
    }
    Py_INCREF(dict);
    t_main->ts_dict = dict;
    t_main->parent = NULL;
    t_main->thread_h = stacklet_newthread();
    t_main->stacklet_h = NULL;
    t_main->initialized = True;
    t_main->is_main = True;
    return t_main;
}
Example #14
0
File: upb.c Project: chenbk85/upb
static upb_sflow_t PyUpb_Message_StartRepeatedSubmessage(
    void *a, upb_value fval) {
  (void)fval;
  PyObject **elem = upb_stdarray_append(a, sizeof(void*));
  PyTypeObject *type = ((PyUpb_MessageType*)Py_TYPE(a))->alt_type;
  if (!*elem) *elem = type->tp_alloc(type, 0);
  return UPB_CONTINUE_WITH(*elem);
}
Example #15
0
/* PycairoSurface_FromSurface
 * Create a new
 *   PycairoImageSurface,
 *   PycairoPDFSurface,
 *   PycairoPSSurface,
 *   PycairoSVGSurface,
 *   PycairoWin32Surface, or
 *   PycairoXlibSurface from a cairo_surface_t.
 * surface - a cairo_surface_t to 'wrap' into a Python object.
 *   It is unreferenced if the PycairoSurface creation fails, or if the
 *   cairo_surface_t has an error status.
 * base - the base object used to create the surface, or NULL.
 *   It is referenced to keep it alive while the cairo_surface_t is being used.
 * Return value: New reference or NULL on failure
 */
PyObject *
PycairoSurface_FromSurface (cairo_surface_t *surface, PyObject *base)
{
    PyTypeObject *type = NULL;
    PyObject *o;

    assert (surface != NULL);

    if (Pycairo_Check_Status (cairo_surface_status (surface))) {
	cairo_surface_destroy (surface);
	return NULL;
    }

    switch (cairo_surface_get_type (surface)) {
#if CAIRO_HAS_IMAGE_SURFACE
    case CAIRO_SURFACE_TYPE_IMAGE:
	type = &PycairoImageSurface_Type;
	break;
#endif
#if CAIRO_HAS_PDF_SURFACE
    case CAIRO_SURFACE_TYPE_PDF:
	type = &PycairoPDFSurface_Type;
	break;
#endif
#if CAIRO_HAS_PS_SURFACE
    case CAIRO_SURFACE_TYPE_PS:
	type = &PycairoPSSurface_Type;
	break;
#endif
#if CAIRO_HAS_SVG_SURFACE
    case CAIRO_SURFACE_TYPE_SVG:
	type = &PycairoSVGSurface_Type;
	break;
#endif
#if CAIRO_HAS_WIN32_SURFACE
    case CAIRO_SURFACE_TYPE_WIN32:
	type = &PycairoWin32Surface_Type;
	break;
#endif
#if CAIRO_HAS_XLIB_SURFACE
    case CAIRO_SURFACE_TYPE_XLIB:
	type = &PycairoXlibSurface_Type;
	break;
#endif
    default:
	type = &PycairoSurface_Type;
	break;
    }
    o = type->tp_alloc (type, 0);
    if (o == NULL) {
	cairo_surface_destroy (surface);
    } else {
	((PycairoSurface *)o)->surface = surface;
	Py_XINCREF(base);
	((PycairoSurface *)o)->base = base;
    }
    return o;
}
Example #16
0
static int
exc_traverse (zbarException *self,
              visitproc visit,
              void *arg)
{
    Py_VISIT(self->obj);
    PyTypeObject *base = (PyTypeObject*)PyExc_Exception;
    return(base->tp_traverse((PyObject*)self, visit, arg));
}
Example #17
0
inline PyObject* create_RegionMapObject(const RegionMap& d) {
  PyTypeObject* t = get_RegionMapType();
  if (t == 0)
    return 0;
  RegionMapObject* so;
  so = (RegionMapObject*)t->tp_alloc(t, 0);
  so->m_x = new RegionMap(d);
  return (PyObject*)so;
}
Example #18
0
inline PyObject* create_RegionObject(const Region& d) {
  PyTypeObject* t = get_RegionType();
  if (t == 0)
    return 0;
  RegionObject* so;
  so = (RegionObject*)t->tp_alloc(t, 0);
  ((RectObject*)so)->m_x = new Region(d);
  return (PyObject*)so;
}
Example #19
0
inline PyObject* create_RGBPixelObject(const RGBPixel& d) {
  PyTypeObject* t = get_RGBPixelType();
  if (t == 0)
    return 0;
  RGBPixelObject* so;
  so = (RGBPixelObject*)t->tp_alloc(t, 0);
  so->m_x = new RGBPixel(d);
  return (PyObject*)so;
}
Example #20
0
inline PyObject* create_FloatPointObject(const FloatPoint& d) {
  PyTypeObject* t = get_FloatPointType();
  if (t == 0)
    return 0;
  FloatPointObject* so;
  so = (FloatPointObject*)t->tp_alloc(t, 0);
  so->m_x = new FloatPoint(d);
  return (PyObject*)so;
}
Example #21
0
inline PyObject* create_DimObject(const Dim& d) {
  PyTypeObject* t = get_DimType();
  if (t == 0)
    return 0;
  DimObject* so;
  so = (DimObject*)t->tp_alloc(t, 0);
  so->m_x = new Dim(d);
  return (PyObject*)so;
}
Example #22
0
inline PyObject* create_SizeObject(const Size& d) {
  PyTypeObject* t = get_SizeType();
  if (t == 0)
    return 0;
  SizeObject* so;
  so = (SizeObject*)t->tp_alloc(t, 0);
  so->m_x = new Size(d);
  return (PyObject*)so;
}
Example #23
0
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) {
    PyTypeObject* tp = Py_TYPE(obj);
    if (likely(tp->tp_getattro))
        return tp->tp_getattro(obj, attr_name);
#if PY_MAJOR_VERSION < 3
    if (likely(tp->tp_getattr))
        return tp->tp_getattr(obj, PyString_AS_STRING(attr_name));
#endif
    return PyObject_GetAttr(obj, attr_name);
}
Example #24
0
static void __Pyx_call_next_tp_dealloc(PyObject* obj, destructor current_tp_dealloc) {
    PyTypeObject* type = Py_TYPE(obj);
    /* try to find the first parent type that has a different tp_dealloc() function */
    while (type && type->tp_dealloc != current_tp_dealloc)
        type = type->tp_base;
    while (type && type->tp_dealloc == current_tp_dealloc)
        type = type->tp_base;
    if (type)
        type->tp_dealloc(obj);
}
Example #25
0
static void __Pyx_call_next_tp_clear(PyObject* obj, inquiry current_tp_clear) {
    PyTypeObject* type = Py_TYPE(obj);
    /* try to find the first parent type that has a different tp_clear() function */
    while (type && type->tp_clear != current_tp_clear)
        type = type->tp_base;
    while (type && type->tp_clear == current_tp_clear)
        type = type->tp_base;
    if (type && type->tp_clear)
        type->tp_clear(obj);
}
Example #26
0
AerospikeKey * AerospikeKey_New(AerospikeClient * client, PyObject * args, PyObject * kwds)
{
	Py_INCREF(client);
	Py_INCREF(args);

    AerospikeKey * self = (AerospikeKey *) AerospikeKey_Type.tp_new(&AerospikeKey_Type, args, kwds);
    self->client = client;
	self->key = args;
    AerospikeKey_Type.tp_init((PyObject *) self, args, kwds);
	return self;
}
Example #27
0
static PyObject*
SortedMap_copy( SortedMap* self )
{
    PyTypeObject* type = pytype_cast( self->ob_type );
    PyObject* copy = type->tp_alloc( type, 0 );
    if( !copy )
        return 0;
    SortedMap* ccopy = reinterpret_cast<SortedMap*>( copy );
    ccopy->sortedmap = new sortedmap_t();
    *ccopy->sortedmap = *self->sortedmap;
    return copy;
}
Example #28
0
PyObject *DyND_PyWrapper_New(const T &v)
{
  PyTypeObject *type = DyND_PyWrapper_Type<T>();

  DyND_PyWrapperObject<T> *obj =
      reinterpret_cast<DyND_PyWrapperObject<T> *>(type->tp_alloc(type, 0));
  if (obj == NULL) {
    throw std::runtime_error("");
  }
  new (&obj->v) T(v);
  return reinterpret_cast<PyObject *>(obj);
}
Example #29
0
static PyObject*
SortedMap_copy( SortedMap* self )
{
    PyTypeObject* type = pytype_cast( Py_TYPE(self) );
    PyObject* copy = type->tp_alloc( type, 0 );
    if( !copy )
        return 0;
    SortedMap* ccopy = reinterpret_cast<SortedMap*>( copy );
    ccopy->m_items = new SortedMap::Items();
    *ccopy->m_items = *self->m_items;
    return copy;
}
Example #30
0
PyObject * THPStorage_(New)(THStorage *ptr)
{
  TORCH_ASSERT(ptr);
  PyTypeObject *type = (PyTypeObject *)THPStorageClass;
  PyObject *obj = type->tp_alloc(type, 0);
  if (obj) {
    ((THPStorage *)obj)->cdata = ptr;
  } else {
    THStorage_(free)(LIBRARY_STATE ptr);
  }
  return obj;
}