| 1 |
from pypy.interpreter.error import OperationError |
| 2 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 3 |
from pypy.module.cpyext.api import CANNOT_FAIL, cpython_api, CONST_STRING |
| 4 |
from pypy.module.cpyext.pyobject import PyObject, from_ref |
| 5 |
|
| 6 |
@cpython_api([CONST_STRING], PyObject, error=CANNOT_FAIL, result_borrowed=True) |
| 7 |
def PySys_GetObject(space, name): |
| 8 |
"""Return the object name from the sys module or NULL if it does |
| 9 |
not exist, without setting an exception.""" |
| 10 |
name = rffi.charp2str(name) |
| 11 |
w_dict = space.sys.getdict(space) |
| 12 |
w_obj = space.finditem_str(w_dict, name) |
| 13 |
return w_obj |
| 14 |
|
| 15 |
@cpython_api([CONST_STRING, PyObject], rffi.INT_real, error=-1) |
| 16 |
def PySys_SetObject(space, name, pyobj): |
| 17 |
"""Set name in the sys module to v unless v is NULL, in which |
| 18 |
case name is deleted from the sys module. Returns 0 on success, -1 |
| 19 |
on error.""" |
| 20 |
|
| 21 |
w_str = space.newbytes(rffi.charp2str(name)) |
| 22 |
w_name = space.call_method(w_str, 'decode', space.newtext("utf-8")) |
| 23 |
w_dict = space.sys.getdict(space) |
| 24 |
if pyobj: |
| 25 |
w_obj = from_ref(space, pyobj) |
| 26 |
space.setitem(w_dict, w_name, w_obj) |
| 27 |
else: |
| 28 |
try: |
| 29 |
|
| 30 |
space.delitem(w_dict, w_name) |
| 31 |
except: |
| 32 |
pass |
| 33 |
return 0 |