Am i missing something or what, i used as reference python's collection module but i can't figure out what i'm doing wrong.
I don't want to put python code to manage the tuple return i need it to be embedded in c code. (also should return any data type)
My module:
Python collection module (deque):
My stack module
Code:
#include "Python.h"
#include "structmember.h"
/* Type definition */
typedef struct {
PyObject_HEAD
int current_size;
int top;
PyObject **content;
} Stack;
static PyTypeObject StackType;
//create stack instance when reference from python
static PyObject *
Stack_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
Stack *stack;
stack = (Stack *)type->tp_alloc(type, 0);
if (stack == NULL)
return NULL;
Py_SIZE(stack) = 0;
stack->top = -1;
stack->current_size = 2;
stack->content = PyMem_Malloc(sizeof(PyObject) * stack->current_size);
if (stack->content != NULL) {
return (PyObject *)stack;
}
PyErr_NoMemory();
return NULL;
}
static int
Stack_tp_clear(Stack *self)
{
Py_CLEAR(self->content);
return 0;
}
//resize stack to 2 times its current size
static PyObject* Stack_resize(Stack * self){
PyObject ** resized = PyMem_Malloc(sizeof(PyObject) * self->current_size);
if(resized == NULL){
PyErr_NoMemory();
return NULL;
}
memcpy(resized, self->content, sizeof(PyObject) * self->top + 1);
PyMem_Free(self->content);
self->current_size = self->current_size * 2;
self->content = resized;
Py_RETURN_NONE;
}
//push value to stack
static PyObject* Stack_setContent(Stack * self,PyObject * item){
Py_INCREF(item);
self->top = self->top + 1;
self->content[self->top] = item;
if(self->top == self->current_size - 1){
return Stack_resize(self);
}
Py_RETURN_NONE;
}
//pop value from stack
static PyObject * Stack_get(Stack * self){
if(self->top < 0){
PyErr_SetString(PyExc_IndexError, "pop from an empty stack");
return NULL;
}
PyObject *m = self->content[self->top];
self->top = self->top - 1;
return m;
}
//clear memory garbage collector
static int
Stack_tp_traverse(Stack *self, visitproc visit, void *arg)
{
int i ;
for(i = 0;i<self->top;i++)
Py_VISIT(self->content[i]);
return 0;
}
//dealloc memory
static void
Stack_tp_dealloc(Stack *self)
{
Stack_tp_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyMethodDef Stack_methods[] = {
{"pop",(PyCFunction)Stack_get,METH_NOARGS,"Lista los metodos exportados del modulo"},
{"push",(PyCFunction)Stack_setContent,METH_VARARGS,"genera una lista de numeros aleatorios o un numero aleatorio"},
{NULL,NULL,0,NULL}
};
/* NO USE
static PyMemberDef Stack_members[] = {
{"content", T_OBJECT_EX, offsetof(Stack, content), 0, "content"},
{NULL}
};
*/
static PyTypeObject StackType = {
PyVarObject_HEAD_INIT(NULL, 0)
"foo.stack", /*tp_name*/
sizeof(Stack), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Stack_tp_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
0, /*tp_doc*/
(traverseproc)Stack_tp_traverse, /*tp_traverse*/
(inquiry)Stack_tp_clear, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
Stack_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getsets*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
Stack_tp_new, /*tp_new*/
};
static struct PyModuleDef fooModule = {
PyModuleDef_HEAD_INIT,
"foo",
"DOCS",
-1,
NULL,
NULL,
NULL,
NULL,
NULL
};
/* Module init */
PyMODINIT_FUNC
PyInit_foo(void)
{
PyObject *m;
m = PyModule_Create(&fooModule);
if (m == NULL)
return NULL;
if (PyType_Ready(&StackType) < 0)
return NULL;
Py_INCREF(&StackType);
PyModule_AddObject(m, "stack", (PyObject *)&StackType);
return m;
}
collections module:






