static PyObject *convert(T p[n])
   {
      array_wrap *aw = PyObject_New(array_wrap, &array_wrapType);
      aw->p = p;

      return PyObject_Init((PyObject *)aw, &array_wrapType);
   }
Exemplo n.º 2
0
/* Create a thunk without checking if `func` is a strict type.
   return: A new reference. */
static PyObject *
_thunk_new_no_check(PyTypeObject *cls,
                    PyObject *func,
                    PyObject *args,
                    PyObject *kwargs)
{
    thunk *self;

    if (!(self = (thunk*) cls->tp_alloc(cls, 0))) {
        return NULL;
    }

    Py_INCREF(func);
    self->th_func = func;

    Py_INCREF(args);
    self->th_args = args;

    Py_XINCREF(kwargs);
    self->th_kwargs = kwargs;

    self->th_normal = NULL;

    PyObject_Init((PyObject*) self, cls);
    return (PyObject*) self;
}
Exemplo n.º 3
0
static PyObject *
pcap_PcapObject_iter(PyObject *p)
{
  pcap_PcapObject     *self = (pcap_PcapObject *)p;
  pcap_PcapIterObject *it   = NULL;
  PyObject            *pIt;

  attempt {
    if ('r' != self->mode) {
      PyErr_Format(PyExc_IOError, "can not read from writable stream");
      break;
    }

    it = PyObject_New(pcap_PcapIterObject, &pcap_PcapIterType);
    if (! it) break;

    pIt = PyObject_Init((PyObject *)it, &pcap_PcapIterType);
    if (pIt != (PyObject *)it) break;

    it->pcap = p;
    Py_INCREF(it->pcap);
  }

  recover {
    if (it) {
      Py_CLEAR(it->pcap);
      Py_CLEAR(it);
    }
    return NULL;
  }

  return (PyObject *)it;
}
Exemplo n.º 4
0
extern "C" PyObject* _PyObject_New(PyTypeObject* cls) {
    assert(cls->tp_itemsize == 0);

    auto rtn = (PyObject*)gc_alloc(cls->tp_basicsize, gc::GCKind::PYTHON);
    // no memset for this function

    PyObject_Init(rtn, cls);
    return rtn;
}
Exemplo n.º 5
0
extern "C" PyObject* PyType_GenericAlloc(PyTypeObject* cls, Py_ssize_t nitems) {
    RELEASE_ASSERT(nitems == 0, "unimplemented");
    RELEASE_ASSERT(cls->tp_itemsize == 0, "unimplemented");

    auto rtn = (PyObject*)gc_alloc(cls->tp_basicsize, gc::GCKind::PYTHON);
    memset(rtn, 0, cls->tp_basicsize);

    PyObject_Init(rtn, cls);
    return rtn;
}
static PyObject *PySilcUser_New(SilcClientEntry user)
{
    PySilcUser *pyuser = (PySilcUser *)PyObject_New(PySilcUser, &PySilcUser_Type);
    if (!pyuser)
        return NULL;
    
    pyuser->silcobj = user;             // TODO: maybe we need to do a clone?
    pyuser->silcobj->context = pyuser;  // TODO: self ref should be weak ref?
    PyObject_Init((PyObject *)pyuser, &PySilcUser_Type);
    return (PyObject *)pyuser;
}
Exemplo n.º 7
0
PyObject*
PyThunkUnaryPipeline_FromFunction(UnaryPipelineFunction function, PyObject *left) {
    PyThunkOperation_UnaryPipeline *op = PyObject_MALLOC(sizeof(PyThunkOperation_UnaryPipeline));
    if (op == NULL)
        return PyErr_NoMemory();
    PyObject_Init((PyObject*)op, &PyThunkUnaryPipeline_Type);
    op->function = function;
    Py_XINCREF(left);
    op->left = left;
    return (PyObject*)op;
}
Exemplo n.º 8
0
PyObject*
PyThunk_FromOperation(PyObject *operation, ssize_t cardinality, int cardinality_type, int type) {
	register PyThunkObject *thunk;

    thunk = (PyThunkObject *)PyObject_MALLOC(sizeof(PyThunkObject));
    if (thunk == NULL)
        return PyErr_NoMemory();
    PyObject_Init((PyObject*)thunk, &PyThunk_Type);
    PyThunk_FromOperation_Inplace(thunk, operation, cardinality, cardinality_type, type);
    return (PyObject*)thunk;
}
Exemplo n.º 9
0
/*static*/
PyObject * PythonScript::QObjectToPython(QObject * o)
{
    pyQObject * obj;
    obj = PyObject_New(pyQObject, &pyQObjectType);

    if (!obj) return NULL;

    obj = (pyQObject*)PyObject_Init((PyObject*)obj, &pyQObjectType);
    obj->_TWcontext = ENCAPSULATE_C_POINTER(o);
    return (PyObject*)obj;
}
Exemplo n.º 10
0
/*static*/
PyObject* PythonScript::getAttribute(PyObject * o, PyObject * attr_name)
{
    QObject * obj;
    QMetaMethod method;
    QString propName;
    QVariant result;
    pyQObjectMethodObject * pyMethod;

    // Get the QObject* we operate on
    if (!PyObject_TypeCheck(o, &pyQObjectType)) {
        PyErr_SetString(PyExc_TypeError, qPrintable(tr("getattr: not a valid TW object")));
        return NULL;
    }
    if (!IS_ENCAPSULATED_C_POINTER(((pyQObject*)o)->_TWcontext)) {
        PyErr_SetString(PyExc_TypeError, qPrintable(tr("getattr: not a valid TW object")));
        return NULL;
    }
    obj = (QObject*)GET_ENCAPSULATED_C_POINTER((PyObject*)(((pyQObject*)o)->_TWcontext));

    if (!asQString(attr_name, propName)) {
        PyErr_SetString(PyExc_TypeError, qPrintable(tr("getattr: invalid property name")));
        return NULL;
    }

    if (propName.length() > 1 && propName.endsWith(QChar('_')))
        propName.chop(1);

    switch (doGetProperty(obj, propName, result)) {
    case Property_DoesNotExist:
        PyErr_Format(PyExc_AttributeError, qPrintable(tr("getattr: object doesn't have property/method %s")), qPrintable(propName));
        return NULL;
    case Property_NotReadable:
        PyErr_Format(PyExc_AttributeError, qPrintable(tr("getattr: property %s is not readable")), qPrintable(propName));
        return NULL;
    case Property_Method:
        pyMethod = PyObject_New(pyQObjectMethodObject, &pyQObjectMethodType);
        pyMethod = (pyQObjectMethodObject*)PyObject_Init((PyObject*)pyMethod, &pyQObjectMethodType);
        Py_INCREF(pyMethod);
        pyMethod->_TWcontext = ENCAPSULATE_C_POINTER(obj);
        Py_XINCREF(attr_name);
        pyMethod->_methodName = (PyObject*)attr_name;
        return (PyObject*)pyMethod;
    case Property_OK:
        return PythonScript::VariantToPython(result);
    default:
        break;
    }
    // we should never reach this point
    return NULL;
}
Exemplo n.º 11
0
static PyObject *
_thunk_new_normal(PyTypeObject *cls, PyObject *normal)
{
    thunk *self;

    if (!(self = (thunk*) cls->tp_alloc(cls, 0))) {
        return NULL;
    }
    self->th_func = NULL;
    self->th_args = NULL;
    self->th_kwargs = NULL;
    self->th_normal = normal;
    Py_INCREF(normal);

    PyObject_Init((PyObject*) self, cls);
    return (PyObject*) self;
}
Exemplo n.º 12
0
PyObject*
PyThunk_Copy(PyThunkObject *original) {
    register PyThunkObject *thunk;

    thunk = (PyThunkObject *)PyObject_MALLOC(sizeof(PyThunkObject));
    if (thunk == NULL)
        return PyErr_NoMemory();
    PyObject_Init((PyObject*)thunk, &PyThunk_Type);
    thunk->storage = original->storage;
    thunk->evaluated = original->evaluated;
    thunk->operation = original->operation;
    thunk->cardinality = original->cardinality;
    thunk->type = original->type;
    thunk->options = original->options;
    thunk->blockmask = original->blockmask;
    return (PyObject*)thunk;
}
Exemplo n.º 13
0
PyObject *
possibilitiesToPyObject(HyPossibilities possibilities, PyObject* sack)
{
    _PossibilitiesObject *p;

    p = PyObject_New(_PossibilitiesObject, &possibilities_Type);
    if (!p)
	return NULL;

    if (!PyObject_Init((PyObject *)p, &possibilities_Type)) {
	Py_DECREF(p);
	return NULL;
    }
    p->possibilities = possibilities;
    p->sack = sack;
    Py_XINCREF(p->sack);
    return (PyObject *)p;
}
Exemplo n.º 14
0
Arquivo: key.c Projeto: dw/acid
/**
 * Construct a KEY_PRIVATE Key from `p[0..size]` and return it.
 */
Key *
acid_make_private_key(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) + size);
    if(self) {
        PyObject_Init((PyObject *)self, &KeyType);
        Key_SIZE(self) = size;
        self->flags = KEY_PRIVATE;
        self->p = ((uint8_t *) self) + sizeof(Key) + KEY_PREFIX_SLACK;
        if(p) {
            memcpy(self->p + KEY_PREFIX_SLACK, p, size);
        }
    }
    return self;
}
Exemplo n.º 15
0
/* This is the `tp_iter()' method of `EMList' object. */
static PyObject *em_list_iter(em_list_t *self)
{
    em_list_index_hdr_t *index = self->index->address;

    em_list_iter_t *iter;

    if((iter = PyObject_New(em_list_iter_t, &em_list_iter_type)) != NULL)
    {
        PyObject_Init((PyObject *)iter, &em_list_iter_type);
        Py_INCREF(self);

        iter->em_list = self;
        iter->pos = 0;
        iter->maxpos = index->used;
    }
    else
        PyErr_SetString(PyExc_RuntimeError, "Failed to initialize iterator");

    return (PyObject *)iter;
}
Exemplo n.º 16
0
PyObject*
PyThunk_FromArray(PyObject *unused, PyObject *input) {
    register PyThunkObject *thunk;
    (void) unused;
    input = PyArray_FromAny(input, NULL, 0, 0, NPY_ARRAY_ENSURECOPY, NULL);
    if (input == NULL || !PyArray_CheckExact(input)) {
        PyErr_SetString(PyExc_TypeError, "Expected a NumPy array as parameter.");
        return NULL;
    }
    thunk = (PyThunkObject *)PyObject_MALLOC(sizeof(PyThunkObject));
    if (thunk == NULL)
        return PyErr_NoMemory();
    PyObject_Init((PyObject*)thunk, &PyThunk_Type);
    thunk->storage = (PyArrayObject*) input;
    thunk->evaluated = true;
    thunk->operation = NULL;
    thunk->cardinality =  PyArray_SIZE(thunk->storage);
    thunk->type = PyArray_TYPE(thunk->storage);
    thunk->options = THUNK_CARDINALITY_EXACT;
    thunk->blockmask = NULL;
    return (PyObject*)thunk;
}
Exemplo n.º 17
0
Arquivo: key.c Projeto: 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;
}
Exemplo n.º 18
0
static PyObject *
ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUSED(kwds)) {
    /* Keywords are ignored for now */

    PyObject *function, *pyname = NULL;
    int nin, nout, i;
    PyUFunc_PyFuncData *fdata;
    PyUFuncObject *self;
    char *fname, *str;
    Py_ssize_t fname_len = -1;
    int offset[2];

    if (!PyArg_ParseTuple(args, "Oii", &function, &nin, &nout)) {
        return NULL;
    }
    if (!PyCallable_Check(function)) {
        PyErr_SetString(PyExc_TypeError, "function must be callable");
        return NULL;
    }
    if (nin + nout > NPY_MAXARGS) {
        PyErr_Format(PyExc_ValueError,
                     "Cannot construct a ufunc with more than %d operands "
                     "(requested number were: inputs = %d and outputs = %d)",
                     NPY_MAXARGS, nin, nout);
        return NULL;
    }
    self = PyArray_malloc(sizeof(PyUFuncObject));
    if (self == NULL) {
        return NULL;
    }
    PyObject_Init((PyObject *)self, &PyUFunc_Type);

    self->userloops = NULL;
    self->nin = nin;
    self->nout = nout;
    self->nargs = nin + nout;
    self->identity = PyUFunc_None;
    self->functions = pyfunc_functions;
    self->ntypes = 1;

    /* generalized ufunc */
    self->core_enabled = 0;
    self->core_num_dim_ix = 0;
    self->core_num_dims = NULL;
    self->core_dim_ixs = NULL;
    self->core_offsets = NULL;
    self->core_signature = NULL;
    self->op_flags = PyArray_malloc(sizeof(npy_uint32)*self->nargs);
    if (self->op_flags == NULL) {
        return PyErr_NoMemory();
    }
    memset(self->op_flags, 0, sizeof(npy_uint32)*self->nargs);
    self->iter_flags = 0;

    self->type_resolver = &object_ufunc_type_resolver;
    self->legacy_inner_loop_selector = &object_ufunc_loop_selector;

    pyname = PyObject_GetAttrString(function, "__name__");
    if (pyname) {
        (void) PyString_AsStringAndSize(pyname, &fname, &fname_len);
    }
    if (PyErr_Occurred()) {
        fname = "?";
        fname_len = 1;
        PyErr_Clear();
    }

    /*
     * self->ptr holds a pointer for enough memory for
     * self->data[0] (fdata)
     * self->data
     * self->name
     * self->types
     *
     * To be safest, all of these need their memory aligned on void * pointers
     * Therefore, we may need to allocate extra space.
     */
    offset[0] = sizeof(PyUFunc_PyFuncData);
    i = (sizeof(PyUFunc_PyFuncData) % sizeof(void *));
    if (i) {
        offset[0] += (sizeof(void *) - i);
    }
    offset[1] = self->nargs;
    i = (self->nargs % sizeof(void *));
    if (i) {
        offset[1] += (sizeof(void *)-i);
    }
    self->ptr = PyArray_malloc(offset[0] + offset[1] + sizeof(void *) +
                            (fname_len + 14));
    if (self->ptr == NULL) {
        Py_XDECREF(pyname);
        return PyErr_NoMemory();
    }
    Py_INCREF(function);
    self->obj = function;
    fdata = (PyUFunc_PyFuncData *)(self->ptr);
    fdata->nin = nin;
    fdata->nout = nout;
    fdata->callable = function;

    self->data = (void **)(((char *)self->ptr) + offset[0]);
    self->data[0] = (void *)fdata;
    self->types = (char *)self->data + sizeof(void *);
    for (i = 0; i < self->nargs; i++) {
        self->types[i] = NPY_OBJECT;
    }
    str = self->types + offset[1];
    memcpy(str, fname, fname_len);
    memcpy(str+fname_len, " (vectorized)", 14);
    self->name = str;

    Py_XDECREF(pyname);

    /* Do a better job someday */
    self->doc = "dynamic ufunc based on a python function";

    return (PyObject *)self;
}
Exemplo n.º 19
0
Arquivo: fffpy.c Projeto: FNNDSC/nipy
fffpy_multi_iterator* fffpy_multi_iterator_new(int narr, int axis, ...)
{
  fffpy_multi_iterator* thisone; 
  va_list va;
  fff_vector** vector; 
  PyArrayMultiIterObject *multi;
  PyObject *current, *arr;
  int i, err=0;

  /* Create new instance */ 
  thisone = (fffpy_multi_iterator*)malloc(sizeof(fffpy_multi_iterator));
  multi = PyArray_malloc(sizeof(PyArrayMultiIterObject));
  vector = (fff_vector**)malloc(narr*sizeof(fff_vector*)); 

  /* Initialize the PyArrayMultiIterObject instance from the variadic arguments */ 
  PyObject_Init((PyObject *)multi, &PyArrayMultiIter_Type);
  
  for (i=0; i<narr; i++) 
    multi->iters[i] = NULL;
  multi->numiter = narr;
  multi->index = 0;
  
  va_start(va, axis);
  for (i=0; i<narr; i++) {
    current = va_arg(va, PyObject *);
    arr = PyArray_FROM_O(current);
    if (arr==NULL) {
      err=1; break;
    }
    else {
      multi->iters[i] = (PyArrayIterObject *)PyArray_IterAllButAxis(arr, &axis);
      Py_DECREF(arr);
    }
  }
  
  va_end(va);

  /* Test */
  if (!err && _PyArray_BroadcastAllButAxis(multi, axis) < 0) 
    err=1; 
  if (err) {
    FFF_ERROR("Cannot create broadcast object", ENOMEM); 
    free(thisone); 
    free(vector); 
    Py_DECREF(multi);
    return NULL;
  }
  
  /* Initialize the multi iterator */  
  PyArray_MultiIter_RESET(multi);

  /* Create the fff vectors (views or copies) */ 
  for(i=0; i<narr; i++) 
    vector[i] = _fff_vector_new_from_PyArrayIter((const PyArrayIterObject*)multi->iters[i], axis); 
  
  /* Instantiate fiels */ 
  thisone->narr = narr; 
  thisone->axis = axis; 
  thisone->vector = vector;
  thisone->multi = multi; 
  thisone->index = thisone->multi->index; 
  thisone->size = thisone->multi->size;   

  return thisone; 
}
Exemplo n.º 20
0
static PyObject *
pyshark_iter(PyObject *self, PyObject *args)
{
    gsize i;
    char *filename;
    PyObject *keylistobj;
    const char *dfilter;
    char *decode_as = NULL;
    pyshark_Iter *p;

    gint ret;

    /* NB: Automatic sanity checks for 1st, 3rd, and optional 4th argument */
    if(!PyArg_ParseTuple(args, "sOs|s", &filename, &keylistobj, &dfilter, &decode_as)) {
        return NULL;
    }

    /* NB: Explicit sanity checks needed for the second argument */
    if(!PyList_Check(keylistobj)) {
        PyErr_SetString(PyExc_TypeError, "Second argument must be a list of wireshark fieldnames");
        return NULL;
    }

    for(i = 0; i < PyList_Size(keylistobj); i++) {
        PyObject *fieldnameobj = PyList_GetItem(keylistobj, i);

        if (!PyString_Check(fieldnameobj)) {
            PyErr_SetString(PyExc_TypeError, "All items in second argument list must be strings");
            return NULL;
        }
    }

    /* NB: See PyObject.ob_type in http://docs.python.org/c-api/typeobj.html
       for more info
     */
    pyshark_IterType.ob_type = &PyType_Type; //pyshark_Iter;

    /* NB: look at bottom of http://docs.python.org/c-api/type.html */
    ret = PyType_Ready(&pyshark_IterType);
    if(ret) {
        return NULL;
    }

    /* Create our iterator object */
    p = PyObject_New(pyshark_Iter, &pyshark_IterType);
    if(!p) {
        return NULL;
    }

    /* Initialize all our data structures in the iterator object to 0. This makes it easier
       to implement deallocation logic for both expected and unexpected cases.
     */
    p->clean = FALSE;
    p->decode_as = NULL;
    p->stdata = NULL;
    p->nwpykeylist = NULL;
    p->wpykeyhash = NULL;
    p->asel = FALSE;
    p->show_empty_fields = FALSE;

    if(!PyObject_Init((PyObject *)p, &pyshark_IterType)) {
        Py_DECREF(p);
        return NULL;
    }

    p->stdata = stdata_new();
    if(!p->stdata) {
        Py_DECREF(p);
        return NULL;
    }

    p->wpykeyhash = g_hash_table_new(g_str_hash, g_str_equal);

    p->nwpykeylist = g_ptr_array_new();

    /*
      Iterate through the Python List and add to either fieldnames OR wfieldnames
     depending on presence of a '*' in the string
    */
    for(i = 0; i < PyList_Size(keylistobj); i++) {
        /* NB: we know these are not NULL because of our sanity checks above */
        PyObject *keyobj = PyList_GetItem(keylistobj, i);

        /* Check for wildcard entries, e.g. "*", "ip.*", "eth.*", etc. */
        const gchar *key = PyString_AsString(keyobj);
        gchar *ptr = g_strstr_len(key, strnlen(key, 100), "*");

        if(ptr) {
            /* We have a fieldname with a wildcard in it
             *
             * Use pointer arithmetic to figure out the length
             * TODO: better way to do this, maybe?
             */
            gsize prefix_len = (gsize)ptr - (gsize)key;

            g_ptr_array_add(p->stdata->wfieldnames, g_strndup(key, prefix_len));
        }
        else {
            /*
             * Non-wildcard entry.
             */
            g_ptr_array_add(p->stdata->fieldnames, PyString_AsString(keyobj));

            /* On the python-module side of things, keep a list of python objects,
               one for each non-wildcard fieldname to be processed by sharktools.
               NB: the index between entries in p->{stdata->fieldnames,nwpykeylist}
               MUST be the same.
            */
            g_ptr_array_add(p->nwpykeylist, keyobj);

            /* The above array_add() call doesn't deep copy the fieldname,
               let's increment the refcount, and decrement it when we cleanup.
               NB: also used for our copy of the key in p->nwpykeylist
            */
            Py_INCREF(keyobj);
        }
    }

    /* If there is a decode_as string set, add it */
    if(decode_as) {
        dprintf("decode as string added: %s\n", decode_as);
        ret = sharktools_add_decode_as(decode_as);
        if(ret == FALSE) {
            dprintf("%s\n", sharktools_errmsg);
            PyErr_SetString(PySharkError, sharktools_errmsg);
            Py_DECREF(p);
            return NULL;
        }
        /* NB: Add to object state; we'll need to remove it later */
        p->decode_as = strndup(decode_as, strlen(decode_as));
    }

    /*
     * Create and initialize sharktools' state
     */
    ret = sharktools_iter_init(p->stdata, filename, strdup(dfilter));
    if(ret < 0) {
        dprintf("%s\n", sharktools_errmsg);
        PyErr_SetString(PySharkError, sharktools_errmsg);
        Py_DECREF(p);
        return NULL;
    }

    /* NB: We are dirty */
    p->clean = FALSE;

    return (PyObject *)p;
}