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)
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)
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)
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)
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)
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)
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)
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)
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)
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])
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)
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)
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)
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)
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)
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)
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")))
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)
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)
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)
def PyErr_Occurred(space): state = space.fromcache(State) if state.operror is None: return None return borrow_from(None, state.operror.w_type)
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)
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))
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))