Adapt to work in python2.6
This commit is contained in:
parent
435b92b4a4
commit
74982d6fef
15 changed files with 154 additions and 119 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
#include <capsulethunk.h>
|
||||||
#include <llvm_binding/capsule_context.h>
|
#include <llvm_binding/capsule_context.h>
|
||||||
|
|
||||||
static
|
static
|
||||||
|
|
@ -53,12 +54,13 @@ CapsuleContext* getContext(PyObject* self, PyObject* args) {
|
||||||
PyErr_SetString(PyExc_TypeError, "PyCapsule has no context.");
|
PyErr_SetString(PyExc_TypeError, "PyCapsule has no context.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return (CapsuleContext*)context;
|
return static_cast<CapsuleContext*>(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
PyObject* getClassName(PyObject* self, PyObject* args) {
|
PyObject* getClassName(PyObject* self, PyObject* args) {
|
||||||
CapsuleContext* context = getContext(self, args);
|
CapsuleContext* context = getContext(self, args);
|
||||||
|
//Assert(context->_magic == 0xdead);
|
||||||
if (!context) {
|
if (!context) {
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ populate_submodules(module, submodule_%(ns)s);
|
||||||
def populate_headers(println):
|
def populate_headers(println):
|
||||||
includes = [
|
includes = [
|
||||||
'cstring',
|
'cstring',
|
||||||
|
'Python.h',
|
||||||
|
'capsulethunk.h',
|
||||||
'llvm_binding/conversion.h',
|
'llvm_binding/conversion.h',
|
||||||
'llvm_binding/binding.h',
|
'llvm_binding/binding.h',
|
||||||
'llvm_binding/capsule_context.h',
|
'llvm_binding/capsule_context.h',
|
||||||
|
|
|
||||||
108
llvmpy/include/capsulethunk.h
Normal file
108
llvmpy/include/capsulethunk.h
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
/**
|
||||||
|
|
||||||
|
This is a modified version of capsulethunk.h for use in llvmpy
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef __CAPSULETHUNK_H
|
||||||
|
#define __CAPSULETHUNK_H
|
||||||
|
|
||||||
|
//#define Assert(X) do_assert(!!(X), #X, __FILE__, __LINE__)
|
||||||
|
#define Assert(X)
|
||||||
|
|
||||||
|
static
|
||||||
|
void do_assert(int cond, const char * msg, const char *file, unsigned line){
|
||||||
|
if (not cond) {
|
||||||
|
fprintf(stderr, "Assertion failed %s:%d\n%s\n", file, line, msg);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if ( (PY_VERSION_HEX < 0x02070000) \
|
||||||
|
|| ((PY_VERSION_HEX >= 0x03000000) \
|
||||||
|
&& (PY_VERSION_HEX < 0x03010000)) )
|
||||||
|
|
||||||
|
typedef void (*PyCapsule_Destructor)(PyObject *);
|
||||||
|
|
||||||
|
struct FakePyCapsule_Desc {
|
||||||
|
const char *name;
|
||||||
|
void *context;
|
||||||
|
PyCapsule_Destructor dtor;
|
||||||
|
PyObject *parent;
|
||||||
|
|
||||||
|
FakePyCapsule_Desc() : name(0), context(0), dtor(0) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
static
|
||||||
|
FakePyCapsule_Desc* get_pycobj_desc(PyObject *p){
|
||||||
|
void *desc = ((PyCObject*)p)->desc;
|
||||||
|
Assert(desc && "No desc in PyCObject");
|
||||||
|
return static_cast<FakePyCapsule_Desc*>(desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void pycobject_pycapsule_dtor(void *p, void *desc){
|
||||||
|
Assert(desc);
|
||||||
|
Assert(p);
|
||||||
|
FakePyCapsule_Desc *fpc_desc = static_cast<FakePyCapsule_Desc*>(desc);
|
||||||
|
Assert(fpc_desc->parent);
|
||||||
|
Assert(PyCObject_Check(fpc_desc->parent));
|
||||||
|
fpc_desc->dtor(static_cast<PyObject*>(fpc_desc->parent));
|
||||||
|
delete fpc_desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
PyObject* PyCapsule_New(void* ptr, const char *name, PyCapsule_Destructor dtor)
|
||||||
|
{
|
||||||
|
FakePyCapsule_Desc *desc = new FakePyCapsule_Desc;
|
||||||
|
desc->name = name;
|
||||||
|
desc->context = NULL;
|
||||||
|
desc->dtor = dtor;
|
||||||
|
PyObject *p = PyCObject_FromVoidPtrAndDesc(ptr, desc,
|
||||||
|
pycobject_pycapsule_dtor);
|
||||||
|
desc->parent = p;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
int PyCapsule_CheckExact(PyObject *p)
|
||||||
|
{
|
||||||
|
return PyCObject_Check(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void* PyCapsule_GetPointer(PyObject *p, const char *name)
|
||||||
|
{
|
||||||
|
Assert(PyCapsule_CheckExact(p));
|
||||||
|
if (strcmp(get_pycobj_desc(p)->name, name) != 0) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid PyCapsule object");
|
||||||
|
}
|
||||||
|
return PyCObject_AsVoidPtr(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void* PyCapsule_GetContext(PyObject *p)
|
||||||
|
{
|
||||||
|
Assert(p);
|
||||||
|
Assert(PyCapsule_CheckExact(p));
|
||||||
|
return get_pycobj_desc(p)->context;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
int PyCapsule_SetContext(PyObject *p, void *context)
|
||||||
|
{
|
||||||
|
Assert(PyCapsule_CheckExact(p));
|
||||||
|
get_pycobj_desc(p)->context = context;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
const char * PyCapsule_GetName(PyObject *p)
|
||||||
|
{
|
||||||
|
// Assert(PyCapsule_CheckExact(p));
|
||||||
|
return get_pycobj_desc(p)->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* #if PY_VERSION_HEX < 0x02070000 */
|
||||||
|
|
||||||
|
#endif /* __CAPSULETHUNK_H */
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include "capsulethunk.h"
|
||||||
typedef PyObject* Destructor_Fn;
|
|
||||||
|
|
||||||
struct CapsuleContext {
|
struct CapsuleContext {
|
||||||
|
//const unsigned _magic;
|
||||||
const char* className;
|
const char* className;
|
||||||
|
|
||||||
CapsuleContext(const char* cn)
|
CapsuleContext(const char* cn)
|
||||||
|
|
@ -14,6 +14,17 @@ struct CapsuleContext {
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static
|
||||||
|
void pycapsule_dtor_free_context(PyObject *pycap)
|
||||||
|
{
|
||||||
|
void * context = PyCapsule_GetContext(pycap);
|
||||||
|
Assert(context);
|
||||||
|
CapsuleContext* cc = static_cast<CapsuleContext*>(context);
|
||||||
|
//Assert(cc->_magic == 0xdead);
|
||||||
|
delete cc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static
|
static
|
||||||
PyObject* pycapsule_new(void* ptr,
|
PyObject* pycapsule_new(void* ptr,
|
||||||
|
|
@ -26,15 +37,16 @@ PyObject* pycapsule_new(void* ptr,
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
PyObject* cap = PyCapsule_New(ptr, basename, NULL);
|
PyObject* cap = PyCapsule_New(ptr, basename, pycapsule_dtor_free_context);
|
||||||
if (!cap) {
|
if (!cap) {
|
||||||
PyErr_SetString(PyExc_TypeError, "Error creating new PyCapsule");
|
PyErr_SetString(PyExc_TypeError, "Error creating new PyCapsule");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
CapsuleContext* context = new CapsuleContext(classname);
|
CapsuleContext* context = new CapsuleContext(classname);
|
||||||
if (PyCapsule_SetContext(cap, context)) {
|
if (0 != PyCapsule_SetContext(cap, context)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
//Assert(context->_magic == 0xdead);
|
||||||
return cap;
|
return cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
for fname in os.listdir(os.path.dirname(__file__)):
|
|
||||||
if ((fname.endswith('.py') or fname.endswith('.pyc')) and
|
|
||||||
not fname.startswith('__init__')):
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
base = os.path.dirname(__file__)
|
|
||||||
for fname in os.listdir(base):
|
|
||||||
print fname
|
|
||||||
is_python_script = fname.endswith('.py') or fname.endswith('.pyc')
|
|
||||||
is_init_script = fname.startswith('__init__')
|
|
||||||
is_directory = os.path.isdir(os.path.join(base, fname))
|
|
||||||
if (is_directory or is_python_script) and not is_init_script :
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,2 @@
|
||||||
import os.path, importlib
|
from src import _init
|
||||||
|
_init(__name__, __file__)
|
||||||
def _init():
|
|
||||||
base = os.path.dirname(__file__)
|
|
||||||
for fname in os.listdir(base):
|
|
||||||
print fname
|
|
||||||
is_python_script = fname.endswith('.py') or fname.endswith('.pyc')
|
|
||||||
is_init_script = fname.startswith('__init__')
|
|
||||||
is_directory = os.path.isdir(os.path.join(base, fname))
|
|
||||||
if (is_directory or is_python_script) and not is_init_script :
|
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
|
||||||
importlib.import_module('.' + modname, __name__)
|
|
||||||
|
|
||||||
_init()
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import os.path, importlib
|
import os.path
|
||||||
|
|
||||||
def _init():
|
def _init(root=__name__, file=__file__):
|
||||||
base = os.path.dirname(__file__)
|
base = os.path.dirname(file)
|
||||||
for fname in os.listdir(base):
|
for fname in os.listdir(base):
|
||||||
print fname
|
print fname
|
||||||
is_python_script = fname.endswith('.py') or fname.endswith('.pyc')
|
is_python_script = fname.endswith('.py') or fname.endswith('.pyc')
|
||||||
|
|
@ -9,7 +9,8 @@ def _init():
|
||||||
is_directory = os.path.isdir(os.path.join(base, fname))
|
is_directory = os.path.isdir(os.path.join(base, fname))
|
||||||
if (is_directory or is_python_script) and not is_init_script :
|
if (is_directory or is_python_script) and not is_init_script :
|
||||||
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
modname = os.path.basename(fname).rsplit('.', 1)[0]
|
||||||
importlib.import_module('.' + modname, __name__)
|
#importlib.import_module('.' + modname, __name__)
|
||||||
|
__import__('.'.join([root, modname]))
|
||||||
|
|
||||||
_init()
|
_init()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue