Implement SmallVector_Type, pycapsule_new

This commit is contained in:
Siu Kwan Lam 2013-01-23 14:50:17 -06:00
commit 6a861d82e6
8 changed files with 82 additions and 39 deletions

View file

@ -51,10 +51,12 @@ create_python_submodule(PyObject* parent, const char* name,
strcpy(fullname + len_parent + 1, name);
PyObject* submod = create_python_module(fullname, methtable);
delete [] fullname;
if (!submod)
if (!submod){
return NULL;
if( -1 == PyModule_AddObject(parent, name, submod) )
}
if (-1 == PyModule_AddObject(parent, name, submod)) {
return NULL;
}
return submod;
}
@ -70,4 +72,5 @@ int populate_submodules(PyObject* parent, SubModuleEntry* entries){
return 0;
}
return 1;
}
}

View file

@ -15,5 +15,27 @@ struct CapsuleContext {
};
static
PyObject* pycapsule_new(void* ptr,
const char* basename,
const char* classname=NULL)
{
if (!classname) {
classname = basename;
}
PyObject* cap = PyCapsule_New(ptr, basename, NULL);
if (!cap) {
PyErr_SetString(PyExc_TypeError, "Error creating new PyCapsule");
return NULL;
}
CapsuleContext* context = new CapsuleContext(classname);
if (PyCapsule_SetContext(cap, context)) {
return NULL;
}
return cap;
}
#endif //LLVMPY_CAPSULE_CONTEXT_H_

View file

@ -0,0 +1,23 @@
#include <Python.h>
#include <llvm/ADT/SmallVector.h>
static
PyObject* small_vector_from_types(PyObject* self, PyObject* args) {
using llvm::Type;
using llvm::SmallVector_Type;
SmallVector_Type* SV = new SmallVector_Type;
Py_ssize_t size = PyTuple_Size(args);
for (Py_ssize_t i = 0; i < size; ++i) {
PyObject* cap = PyTuple_GetItem(args, i);
Type* type = (Type*)PyCapsule_GetPointer(cap, "llvm::Type");
SV->push_back(type);
}
return pycapsule_new(SV, "llvm::SmallVector_Type");
}
static PyMethodDef extra_methodtable[] = {
#define method(func) { #func, (PyCFunction)func, METH_VARARGS, NULL }
method( small_vector_from_types ),
{ NULL }
#undef method
};

View file

@ -32,25 +32,7 @@ private:
void operator = (const raw_svector_ostream_helper&);
};
typedef SmallVector<Type*, 8> SmallVector_Type;
class SmallVector_Type : public SmallVector<Type*, 8> {
public:
static
SmallVector_Type* fromPySequence(PyObject* obj) {
SmallVector_Type* SV = new SmallVector_Type;
Py_ssize_t sz = PySequence_Size(obj);
for (Py_ssize_t i = 0; i < sz; ++i) {
PyObject* item = PySequence_GetItem(obj, i);
PyObject* cap = PyObject_GetAttrString(item, "_ptr");
Type* type = (Type*)PyCapsule_GetPointer(cap, "llvm::Type");
SV->push_back(type);
Py_XDECREF(cap);
Py_XDECREF(item);
}
return SV;
}
};
} // end namespace llvm