예제 #1
0
def PyModule_GetDict(space, w_mod):
    if PyModule_Check(space, w_mod):
        assert isinstance(w_mod, Module)
        w_dict = w_mod.getdict(space)
        return borrow_from(w_mod, w_dict)
    else:
        PyErr_BadInternalCall(space)
예제 #2
0
def _Py_InitPyPyModule(space, name, methods, doc, w_self, apiver):
    """
    Create a new module object based on a name and table of functions, returning
    the new module object. If doc is non-NULL, it will be used to define the
    docstring for the module. If self is non-NULL, it will passed to the
    functions of the module as their (otherwise NULL) first parameter. (This was
    added as an experimental feature, and there are no known uses in the current
    version of Python.) For apiver, the only value which should be passed is
    defined by the constant PYTHON_API_VERSION.

    Note that the name parameter is actually ignored, and the module name is
    taken from the package_context attribute of the cpyext.State in the space
    cache.  CPython includes some extra checking here to make sure the module
    being initialized lines up with what's expected, but we don't.
    """
    from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
    modname = rffi.charp2str(name)
    state = space.fromcache(State)
    f_name, f_path = state.package_context
    w_mod = PyImport_AddModule(space, f_name)

    dict_w = {'__file__': space.wrap(f_path)}
    convert_method_defs(space, dict_w, methods, None, w_self, modname)
    for key, w_value in dict_w.items():
        space.setattr(w_mod, space.wrap(key), w_value)
    if doc:
        space.setattr(w_mod, space.wrap("__doc__"),
                      space.wrap(rffi.charp2str(doc)))
    return borrow_from(None, w_mod)
예제 #3
0
파일: eval.py 프로젝트: Qointum/pypy
def PyEval_GetGlobals(space):
    """Return a dictionary of the global variables in the current execution
    frame, or NULL if no frame is currently executing."""
    caller = space.getexecutioncontext().gettopframe_nohidden()
    if caller is None:
        return None
    return borrow_from(None, caller.w_globals)
예제 #4
0
def _Py_InitPyPyModule(space, name, methods, doc, w_self, apiver):
    """
    Create a new module object based on a name and table of functions, returning
    the new module object. If doc is non-NULL, it will be used to define the
    docstring for the module. If self is non-NULL, it will passed to the
    functions of the module as their (otherwise NULL) first parameter. (This was
    added as an experimental feature, and there are no known uses in the current
    version of Python.) For apiver, the only value which should be passed is
    defined by the constant PYTHON_API_VERSION.

    Note that the name parameter is actually ignored, and the module name is
    taken from the package_context attribute of the cpyext.State in the space
    cache.  CPython includes some extra checking here to make sure the module
    being initialized lines up with what's expected, but we don't.
    """
    from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
    modname = rffi.charp2str(name)
    state = space.fromcache(State)
    f_name, f_path = state.package_context
    if f_name is not None:
        modname = f_name
    w_mod = PyImport_AddModule(space, modname)
    state.package_context = None, None

    if f_path is not None:
        dict_w = {'__file__': space.wrap(f_path)}
    else:
        dict_w = {}
    convert_method_defs(space, dict_w, methods, None, w_self, modname)
    for key, w_value in dict_w.items():
        space.setattr(w_mod, space.wrap(key), w_value)
    if doc:
        space.setattr(w_mod, space.wrap("__doc__"),
                      space.wrap(rffi.charp2str(doc)))
    return borrow_from(None, w_mod)
예제 #5
0
def PySys_GetObject(space, name):
    """Return the object name from the sys module or NULL if it does
    not exist, without setting an exception."""
    name = rffi.charp2str(name)
    w_dict = space.sys.getdict(space)
    w_obj = space.finditem_str(w_dict, name)
    return borrow_from(None, w_obj)
예제 #6
0
def PyModule_GetDict(space, w_mod):
    if PyModule_Check(space, w_mod):
        assert isinstance(w_mod, Module)
        w_dict = w_mod.getdict(space)
        return borrow_from(w_mod, w_dict)
    else:
        PyErr_BadInternalCall(space)
예제 #7
0
def PyEval_GetGlobals(space):
    """Return a dictionary of the global variables in the current execution
    frame, or NULL if no frame is currently executing."""
    caller = space.getexecutioncontext().gettopframe_nohidden()
    if caller is None:
        return None
    return borrow_from(None, caller.w_globals)
예제 #8
0
파일: sysmodule.py 프로젝트: Debug-Orz/Sypy
def PySys_GetObject(space, name):
    """Return the object name from the sys module or NULL if it does
    not exist, without setting an exception."""
    name = rffi.charp2str(name)
    w_dict = space.sys.getdict(space)
    w_obj = space.finditem_str(w_dict, name)
    return borrow_from(None, w_obj)
예제 #9
0
파일: test_borrow.py 프로젝트: Qointum/pypy
 def test_borrowing(self, space, api):
     w_int = space.wrap(1)
     w_tuple = space.newtuple([w_int])
     api.Py_IncRef(w_tuple)
     one_pyo = borrow_from(w_tuple, w_int).get_ref(space)
     api.Py_DecRef(w_tuple)
     state = space.fromcache(RefcountState)
     state.print_refcounts()
     py.test.raises(AssertionError, api.Py_DecRef, one_pyo)
예제 #10
0
파일: dictobject.py 프로젝트: pypyjs/pypy
def PyDict_GetItemString(space, w_dict, key):
    """This is the same as PyDict_GetItem(), but key is specified as a
    char*, rather than a PyObject*."""
    try:
        w_res = space.finditem_str(w_dict, rffi.charp2str(key))
    except:
        w_res = None
    if w_res is None:
        return None
    return borrow_from(w_dict, w_res)
예제 #11
0
def PyDict_GetItemString(space, w_dict, key):
    """This is the same as PyDict_GetItem(), but key is specified as a
    char*, rather than a PyObject*."""
    try:
        w_res = space.finditem_str(w_dict, rffi.charp2str(key))
    except:
        w_res = None
    if w_res is None:
        return None
    return borrow_from(w_dict, w_res)
예제 #12
0
파일: sequence.py 프로젝트: charred/pypy
def PySequence_Fast_GET_ITEM(space, w_obj, index):
    """Return the ith element of o, assuming that o was returned by
    PySequence_Fast(), o is not NULL, and that i is within bounds.
    """
    if isinstance(w_obj, listobject.W_ListObject):
        w_res = w_obj.getitem(index)
    else:
        assert isinstance(w_obj, tupleobject.W_TupleObject)
        w_res = w_obj.wrappeditems[index]
    return borrow_from(w_obj, w_res)
예제 #13
0
def PySequence_Fast_GET_ITEM(space, w_obj, index):
    """Return the ith element of o, assuming that o was returned by
    PySequence_Fast(), o is not NULL, and that i is within bounds.
    """
    if isinstance(w_obj, listobject.W_ListObject):
        w_res = w_obj.getitem(index)
    else:
        assert isinstance(w_obj, tupleobject.W_TupleObject)
        w_res = w_obj.wrappeditems[index]
    return borrow_from(w_obj, w_res)
예제 #14
0
def _PyType_Lookup(space, type, w_name):
    """Internal API to look for a name through the MRO.
    This returns a borrowed reference, and doesn't set an exception!"""
    w_type = from_ref(space, rffi.cast(PyObject, type))
    assert isinstance(w_type, W_TypeObject)

    if not space.isinstance_w(w_name, space.w_str):
        return None
    name = space.str_w(w_name)
    w_obj = w_type.lookup(name)
    return borrow_from(w_type, w_obj)
예제 #15
0
파일: listobject.py 프로젝트: charred/pypy
def PyList_GetItem(space, w_list, index):
    """Return the object at position pos in the list pointed to by p.  The
    position must be positive, indexing from the end of the list is not
    supported.  If pos is out of bounds, return NULL and set an
    IndexError exception."""
    if not isinstance(w_list, W_ListObject):
        PyErr_BadInternalCall(space)
    wrappeditems = w_list.getitems()
    if index < 0 or index >= len(wrappeditems):
        raise OperationError(space.w_IndexError, space.wrap(
            "list index out of range"))
    return borrow_from(w_list, wrappeditems[index])
예제 #16
0
def PyList_GetItem(space, w_list, index):
    """Return the object at position pos in the list pointed to by p.  The
    position must be positive, indexing from the end of the list is not
    supported.  If pos is out of bounds, return NULL and set an
    IndexError exception."""
    if not isinstance(w_list, W_ListObject):
        PyErr_BadInternalCall(space)
    wrappeditems = w_list.getitems()
    if index < 0 or index >= len(wrappeditems):
        raise OperationError(space.w_IndexError, space.wrap(
            "list index out of range"))
    return borrow_from(w_list, wrappeditems[index])
예제 #17
0
def PyEval_GetBuiltins(space):
    """Return a dictionary of the builtins in the current execution
    frame, or the interpreter of the thread state if no frame is
    currently executing."""
    caller = space.getexecutioncontext().gettopframe_nohidden()
    if caller is not None:
        w_globals = caller.w_globals
        w_builtins = space.getitem(w_globals, space.wrap('__builtins__'))
        if not space.isinstance_w(w_builtins, space.w_dict):
            w_builtins = w_builtins.getdict(space)
    else:
        w_builtins = space.builtin.getdict(space)
    return borrow_from(None, w_builtins)
예제 #18
0
파일: eval.py 프로젝트: Qointum/pypy
def PyEval_GetBuiltins(space):
    """Return a dictionary of the builtins in the current execution
    frame, or the interpreter of the thread state if no frame is
    currently executing."""
    caller = space.getexecutioncontext().gettopframe_nohidden()
    if caller is not None:
        w_globals = caller.w_globals
        w_builtins = space.getitem(w_globals, space.wrap('__builtins__'))
        if not space.isinstance_w(w_builtins, space.w_dict):
            w_builtins = w_builtins.getdict(space)
    else:
        w_builtins = space.builtin.getdict(space)
    return borrow_from(None, w_builtins)
예제 #19
0
파일: import_.py 프로젝트: Debug-Orz/Sypy
def PyImport_AddModule(space, name):
    """Return the module object corresponding to a module name.  The name
    argument may be of the form package.module. First check the modules
    dictionary if there's one there, and if not, create a new one and insert
    it in the modules dictionary. Return NULL with an exception set on
    failure.

    This function does not load or import the module; if the module wasn't
    already loaded, you will get an empty module object. Use
    PyImport_ImportModule() or one of its variants to import a module.
    Package structures implied by a dotted name for name are not created if
    not already present."""
    from pypy.module.imp.importing import check_sys_modules_w
    modulename = rffi.charp2str(name)
    w_mod = check_sys_modules_w(space, modulename)
    if not w_mod or space.is_w(w_mod, space.w_None):
        w_mod = Module(space, space.wrap(modulename))
    return borrow_from(None, w_mod)
예제 #20
0
def PyImport_AddModule(space, name):
    """Return the module object corresponding to a module name.  The name
    argument may be of the form package.module. First check the modules
    dictionary if there's one there, and if not, create a new one and insert
    it in the modules dictionary. Return NULL with an exception set on
    failure.

    This function does not load or import the module; if the module wasn't
    already loaded, you will get an empty module object. Use
    PyImport_ImportModule() or one of its variants to import a module.
    Package structures implied by a dotted name for name are not created if
    not already present."""
    from pypy.module.imp.importing import check_sys_modules_w
    modulename = rffi.charp2str(name)
    w_mod = check_sys_modules_w(space, modulename)
    if not w_mod or space.is_w(w_mod, space.w_None):
        w_mod = Module(space, space.wrap(modulename))
    return borrow_from(None, w_mod)
예제 #21
0
def PyMethod_Class(space, w_method):
    """Return the class object from which the method meth was created; if this was
    created from an instance, it will be the class of the instance."""
    assert isinstance(w_method, Method)
    return borrow_from(w_method, w_method.w_class)
예제 #22
0
def PyFunction_GetCode(space, w_func):
    """Return the code object associated with the function object op."""
    func = space.interp_w(Function, w_func)
    w_code = space.wrap(func.code)
    return borrow_from(w_func, w_code)
예제 #23
0
def PyMethod_Function(space, w_method):
    """Return the function object associated with the method meth."""
    assert isinstance(w_method, Method)
    return borrow_from(w_method, w_method.w_function)
예제 #24
0
def PyFunction_GetCode(space, w_func):
    """Return the code object associated with the function object op."""
    func = space.interp_w(Function, w_func)
    w_code = space.wrap(func.code)
    return borrow_from(w_func, w_code)
예제 #25
0
def PyMethod_Class(space, w_method):
    """Return the class object from which the method meth was created; if this was
    created from an instance, it will be the class of the instance."""
    assert isinstance(w_method, Method)
    return borrow_from(w_method, w_method.w_class)
예제 #26
0
파일: tupleobject.py 프로젝트: njues/Sypy
def PyTuple_GetItem(space, w_t, pos):
    if not PyTuple_Check(space, w_t):
        PyErr_BadInternalCall(space)
    w_obj = space.getitem(w_t, space.wrap(pos))
    return borrow_from(w_t, w_obj)
예제 #27
0
파일: pyfile.py 프로젝트: Qointum/pypy
def PyFile_Name(space, w_p):
    """Return the name of the file specified by p as a string object."""
    return borrow_from(w_p, space.getattr(w_p, space.wrap("name")))
예제 #28
0
파일: pypyintf.py 프로젝트: sota/pypy-old
def PyPy_Borrow(space, w_parentobj, w_obj):
    """Returns a borrowed reference to 'obj', borrowing from the 'parentobj'.
    """
    return borrow_from(w_parentobj, w_obj)
예제 #29
0
def PyMethod_Self(space, w_method):
    """Return the instance associated with the method meth if it is bound,
    otherwise return NULL."""
    assert isinstance(w_method, Method)
    return borrow_from(w_method, w_method.w_instance)
예제 #30
0
파일: dictobject.py 프로젝트: pypyjs/pypy
def PyDict_GetItem(space, w_dict, w_key):
    try:
        w_res = space.getitem(w_dict, w_key)
    except:
        return None
    return borrow_from(w_dict, w_res)
예제 #31
0
def PyErr_Occurred(space):
    state = space.fromcache(State)
    if state.operror is None:
        return None
    return borrow_from(None, state.operror.w_type)
예제 #32
0
def PyTuple_GetItem(space, w_t, pos):
    if not PyTuple_Check(space, w_t):
        PyErr_BadInternalCall(space)
    w_obj = space.getitem(w_t, space.wrap(pos))
    return borrow_from(w_t, w_obj)
예제 #33
0
def PyImport_GetModuleDict(space):
    """Return the dictionary used for the module administration (a.k.a.
    sys.modules).  Note that this is a per-interpreter variable."""
    w_modulesDict = space.sys.get('modules')
    return borrow_from(None, w_modulesDict)
예제 #34
0
def PyWeakref_GET_OBJECT(space, w_ref):
    """Similar to PyWeakref_GetObject(), but implemented as a macro that does no
    error checking.
    """
    return borrow_from(w_ref, space.call_function(w_ref))
예제 #35
0
def PyDict_GetItem(space, w_dict, w_key):
    try:
        w_res = space.getitem(w_dict, w_key)
    except:
        return None
    return borrow_from(w_dict, w_res)
예제 #36
0
파일: pyerrors.py 프로젝트: Qointum/pypy
def PyErr_Occurred(space):
    state = space.fromcache(State)
    if state.operror is None:
        return None
    return borrow_from(None, state.operror.w_type)
예제 #37
0
def PyMethod_Function(space, w_method):
    """Return the function object associated with the method meth."""
    assert isinstance(w_method, Method)
    return borrow_from(w_method, w_method.w_function)
예제 #38
0
파일: pyfile.py 프로젝트: Debug-Orz/Sypy
def PyFile_Name(space, w_p):
    """Return the name of the file specified by p as a string object."""
    return borrow_from(w_p, space.getattr(w_p, space.wrap("name")))
예제 #39
0
def PyMethod_Self(space, w_method):
    """Return the instance associated with the method meth if it is bound,
    otherwise return NULL."""
    assert isinstance(w_method, Method)
    return borrow_from(w_method, w_method.w_instance)
예제 #40
0
파일: import_.py 프로젝트: Debug-Orz/Sypy
def PyImport_GetModuleDict(space):
    """Return the dictionary used for the module administration (a.k.a.
    sys.modules).  Note that this is a per-interpreter variable."""
    w_modulesDict = space.sys.get('modules')
    return borrow_from(None, w_modulesDict)
예제 #41
0
파일: weakrefobject.py 프로젝트: ieure/pypy
def PyWeakref_GetObject(space, w_ref):
    """Return the referenced object from a weak reference.  If the referent is
    no longer live, returns None. This function returns a borrowed reference.
    """
    return borrow_from(w_ref, space.call_function(w_ref))