Implement SmallVector_Type, pycapsule_new
This commit is contained in:
parent
ef4c0d3be3
commit
6a861d82e6
8 changed files with 82 additions and 39 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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_
|
||||
|
||||
|
|
|
|||
23
newbinding/include/llvm_binding/extra.h
Normal file
23
newbinding/include/llvm_binding/extra.h
Normal 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
|
||||
};
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue