hack2 / pypyembed /

import os
import traceback, sys
sys.path.insert(0, os.path.join(os.path.dirname(so_path), 'platformer'))
    import ctypes
    from platformer import ExternalCompilationInfo
    import numpypy

    RTLD_NOW = 2
    mode = ctypes.RTLD_GLOBAL | RTLD_NOW
    python = ctypes.CDLL(None)

    def declare(name, argtypes=(), restype=None, canraise=True, errcond=None):
        func = getattr(python, name)
        func.argtypes = argtypes
        func.restype = restype

        def wrapper(*args):
            res = func(*args)
            if canraise and ((errcond is None and not res) or errcond == res):
                raise Exception("Call exploded")
            return res
        return wrapper

    def declare_extra(name, argtypes=(), restype=None):
        func = getattr(extra, name)
        func.argtypes = argtypes
        func.restype = restype
        return func

    eci = ExternalCompilationInfo(
        separate_module_files=(os.path.join(__dir__, 'emb.c'),),
    soname = eci.compile_shared_lib().libraries[0]
    extra = ctypes.CDLL(soname, mode)

    PY_OBJECT = ctypes.c_void_p # for now as we don't need any fields
    Py_Initialize = declare('Py_Initialize', canraise=False)
    Py_Finalize = declare('Py_Finalize', canraise=False)
    PyRun_SimpleString = declare('PyRun_SimpleString', [ctypes.c_char_p], None)
    PyString_FromString = declare('PyString_FromString', [ctypes.c_char_p], PY_OBJECT)
    PyString_AsString = declare('PyString_AsString', [PY_OBJECT], ctypes.c_char_p)
    PyImport_Import = declare('PyImport_Import', [PY_OBJECT], PY_OBJECT)
    PyObject_Repr = declare('PyObject_Repr', [PY_OBJECT], PY_OBJECT)
    PyObject_Str = declare('PyObject_Str', [PY_OBJECT], PY_OBJECT)
    Py_DECREF = declare_extra('_Py_DECREF', [PY_OBJECT], None)
    Py_INCREF = declare_extra('_Py_INCREF', [PY_OBJECT], None)
    PyErr_Print = declare('PyErr_Print', [], None, canraise=False)
    PyErr_Clear = declare('PyErr_Clear', [], None, canraise=False)
    PyDict_New = declare('PyDict_New', [], PY_OBJECT)
    PyObject_GetAttrString = declare('PyObject_GetAttrString',
                                     [PY_OBJECT, ctypes.c_char_p], PY_OBJECT)
    PyObject_Call = declare('PyObject_Call', [PY_OBJECT, PY_OBJECT, PY_OBJECT],
    PyTuple_New = declare('PyTuple_New', [ctypes.c_long], PY_OBJECT)
    PyTuple_SetItem = declare('PyTuple_SetItem', [PY_OBJECT, ctypes.c_long,
                                                  PY_OBJECT], ctypes.c_long,
    PyTuple_GetItem = declare('PyTuple_GetItem', [PY_OBJECT, ctypes.c_long],
    PyTuple_Check = declare_extra('_PyTuple_Check', [PY_OBJECT],
    PyTuple_Size = declare('PyTuple_Size', [PY_OBJECT], ctypes.c_long)
    Py_CompileStringFlags = declare('Py_CompileStringFlags',
         [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, PY_OBJECT], PY_OBJECT)
    PyObject_GetItem = declare('PyObject_GetItem', [PY_OBJECT, PY_OBJECT],
    PyInt_AsLong = declare('PyInt_AsLong', [PY_OBJECT], ctypes.c_long)
    PyInt_Check = declare_extra('_PyInt_Check', [PY_OBJECT], ctypes.c_bool)
    PyInt_FromLong = declare('PyInt_FromLong', [ctypes.c_long], PY_OBJECT)
    PyFloat_Check = declare_extra('_PyFloat_Check', [PY_OBJECT], ctypes.c_bool)
    PyFloat_AsDouble = declare('PyFloat_AsDouble', [PY_OBJECT], ctypes.c_double)
    PyFloat_FromDouble = declare('PyFloat_FromDouble', [ctypes.c_double],
    _Py_get_file_input = declare_extra('_Py_get_file_input', [],
    Py_file_input = _Py_get_file_input()
    PyEval_EvalCode = declare('PyEval_EvalCode', [PY_OBJECT] * 3, PY_OBJECT)
    PyDict_SetItemString = declare('PyDict_SetItemString',
                                   [PY_OBJECT, ctypes.c_char_p, PY_OBJECT],
                                   ctypes.c_int, errcond=-1)
    PyEval_GetBuiltins = declare('PyEval_GetBuiltins', [], PY_OBJECT)
    PyString_Check = declare_extra('_PyString_Check', [PY_OBJECT], ctypes.c_bool)
    Py_GetNone = declare_extra('_Py_GetNone', [], PY_OBJECT)
    PyBool_FromLong = declare('PyBool_FromLong', [ctypes.c_long], PY_OBJECT)

    global_arrays = []

    def wrap(x):
        if isinstance(x, int):
            return PyInt_FromLong(x)
        elif isinstance(x, str):
            return PyString_FromString(x)
        elif isinstance(x, float):
            return PyFloat_FromDouble(x)
        elif isinstance(x, numpypy.ndarray):
            data = x.__array_interface__['data'][0]
            # XXX implement refcounts
            item = PyInt_FromLong(data)
            res = PyTuple_New(3)
            PyTuple_SetItem(res, 0, item)
            PyTuple_SetItem(res, 1, PyString_FromString(str(x.dtype)))
            shape = PyTuple_New(len(x.shape))
            for i in range(len(x.shape)):
                PyTuple_SetItem(shape, i, PyInt_FromLong(x.shape[i]))
            PyTuple_SetItem(res, 2, shape)
            return res
            raise NotImplementedError(x)

    def unwrap(py_x):
        if PyInt_Check(py_x):
            res = PyInt_AsLong(py_x)
        elif PyString_Check(py_x):
            res = PyString_AsString(py_x)
        elif PyFloat_Check(py_x):
            res = PyFloat_AsDouble(py_x)
        elif PyTuple_Check(py_x):
            buf = PyInt_AsLong(PyTuple_GetItem(py_x, 0))
            dtype = PyString_AsString(PyTuple_GetItem(py_x, 1))
            py_shape = PyTuple_GetItem(py_x, 2)
            shape = [PyInt_AsLong(PyTuple_GetItem(py_shape, i)) for i in range(PyTuple_Size(py_shape))]
            return numpypy.ndarray(shape, dtype, buffer=buf)
            raise NotImplementedError("unknown type")
        return res

    def cross_call(func):
        def wrapped(*args):
                return wrap(func(*[unwrap(arg) for arg in args]))
            except Exception, e:
                print e
                return Py_GetNone()
        return wrapped

except Exception, e:
    print e
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.