Ejemplo n.º 1
0
def applevel(code, noconvert=False):
    code = '\n'.join(['    ' + line for line in code.split('\n') if line])
    code = 'def anonymous():\n' + code
    py_code = ffi.gc(lib.Py_CompileString(code, 'exec', lib.Py_file_input),
                     lib.Py_DECREF)
    lib.Py_INCREF(py_code)
    py_elem = lib.PyObject_GetAttrString(py_code, 'co_consts')
    lib.Py_INCREF(py_elem)
    py_zero = ffi.gc(lib.PyInt_FromLong(0), lib.Py_DECREF)
    py_item = lib.PyObject_GetItem(py_elem, py_zero)
    py_locals = ffi.gc(lib.PyDict_New(), lib.Py_DECREF)
    py_globals = ffi.gc(lib.PyDict_New(), lib.Py_DECREF)
    py_bltns = lib.PyEval_GetBuiltins()
    lib.PyDict_SetItemString(py_globals, '__builtins__', py_bltns)
    py_res = lib.PyEval_EvalCode(py_item, py_globals, py_locals)
    return MetabiosisWrapper(py_res, noconvert=noconvert)
Ejemplo n.º 2
0
def convert_unknown(obj):
    try:
        aw = _applevel_by_obj.get(obj)
    except TypeError:
        aw = _applevel_by_unhashable_obj.get(obj)
    if aw is None:
        aw = ApplevelWrapped()._cpyobj
        try:
            _applevel_by_obj[obj] = aw
        except TypeError:
            _applevel_by_unhashable_obj[obj] = aw
        _obj_by_applevel[aw] = obj
    lib.Py_INCREF(aw)
    return aw
Ejemplo n.º 3
0
def convert_bool(obj):
    py_obj = lib.Py_True if obj else lib.Py_False
    lib.Py_INCREF(py_obj)
    return ffi.gc(py_obj, lib.Py_DECREF)
Ejemplo n.º 4
0
def convert_None(obj):
    lib.Py_INCREF(lib.Py_None)
    return ffi.gc(lib.Py_None, lib.Py_DECREF)
Ejemplo n.º 5
0
    def get_type(self):
        typeobject = ffi.cast("PyObject*", self._cpyobj.ob_type)

        lib.Py_INCREF(typeobject)

        return MetabiosisWrapper(ffi.gc(typeobject, lib.Py_DECREF))