Debian Codegraph @

pypy/module/cpyext/test/test_sysmodule.py

raw symbols SCIP
1 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
2 from pypy.module.cpyext.test.test_api import BaseApiTest
3 from rpython.rtyper.lltypesystem import rffi
4
5 class AppTestSysModule(AppTestCpythonExtensionBase):
6 def test_sysmodule(self):
7 module = self.import_extension('foo', [
8 ("get", "METH_VARARGS",
9 """
10 char *name = _PyUnicode_AsString(PyTuple_GetItem(args, 0));
11 PyObject *retval = PySys_GetObject(name);
12 return PyBool_FromLong(retval != NULL);
13 """)])
14 assert module.get("excepthook")
15 assert not module.get("spam_spam_spam")
16
17 def test_writestdout(self):
18 module = self.import_extension('foo', [
19 ("writestdout", "METH_NOARGS",
20 """
21 PySys_WriteStdout("format: %d\\n", 42);
22 Py_RETURN_NONE;
23 """)])
24 import sys, io
25 prev = sys.stdout
26 sys.stdout = io.StringIO()
27 try:
28 module.writestdout()
29 assert sys.stdout.getvalue() == "format: 42\n"
30 finally:
31 sys.stdout = prev
32
33 def test_sysgetset(self):
34 module = self.import_extension('foo', [
35 ("setobject", "METH_VARARGS",
36 """
37 const char *name;
38 Py_ssize_t size;
39 PyObject *value = NULL;
40 if (!PyArg_ParseTuple(args, "z#|O", &name, &size, &value)) {
41 return NULL;
42 }
43 if (value == Py_None) {
44 value = NULL;
45 }
46 int ret = PySys_SetObject(name, value);
47 if (ret < 0)
48 return NULL;
49 return PyLong_FromLong(ret);
50 """)], PY_SSIZE_T_CLEAN=True)
51 import sys
52 module.setobject("newattr", 1)
53 assert sys.newattr == 1
54 module.setobject("newattr")
55 assert not hasattr(sys, 'newattr')
56 # Cpython lets you call this, even if the attribute does not exist
57 module.setobject("newattr")
58 with raises(UnicodeDecodeError):
59 module.setobject(b'\xff', 1)