[pypy-commit] cffi cffi-1.0: ffi.errno

arigo noreply at buildbot.pypy.org
Sat Apr 18 12:47:47 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1751:a6bf54f3fe6e
Date: 2015-04-18 12:48 +0200
http://bitbucket.org/cffi/cffi/changeset/a6bf54f3fe6e/

Log:	ffi.errno

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5139,12 +5139,16 @@
     return PyInt_FromLong(err);
 }
 
-static PyObject *b_set_errno(PyObject *self, PyObject *args)
-{
-    int i;
-    if (!PyArg_ParseTuple(args, "i:set_errno", &i))
+static PyObject *b_set_errno(PyObject *self, PyObject *arg)
+{
+    long ival = PyInt_AsLong(arg);
+    if (ival == -1 && PyErr_Occurred())
         return NULL;
-    errno = i;
+    else if (ival < INT_MIN || ival > INT_MAX) {
+        PyErr_SetString(PyExc_OverflowError, "errno value too large");
+        return NULL;
+    }
+    errno = (int)ival;
     save_errno_only();
     errno = 0;
     Py_INCREF(Py_None);
@@ -5626,7 +5630,7 @@
     {"string", b_string, METH_VARARGS},
     {"buffer", b_buffer, METH_VARARGS},
     {"get_errno", b_get_errno, METH_NOARGS},
-    {"set_errno", b_set_errno, METH_VARARGS},
+    {"set_errno", b_set_errno, METH_O},
     {"newp_handle", b_newp_handle, METH_VARARGS},
     {"from_handle", b_from_handle, METH_O},
     {"from_buffer", b_from_buffer, METH_VARARGS},
diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -499,6 +499,24 @@
 }
 #endif
 
+PyDoc_STRVAR(ffi_errno_doc, "the value of 'errno' from/to the C calls");
+
+static PyObject *ffi_get_errno(PyObject *self, void *closure)
+{
+    /* xxx maybe think about how to make the saved errno local
+       to an ffi instance */
+    return b_get_errno(NULL, NULL);
+}
+
+static int ffi_set_errno(PyObject *self, PyObject *newval, void *closure)
+{
+    PyObject *x = b_set_errno(NULL, newval);
+    if (x == NULL)
+        return -1;
+    Py_DECREF(x);
+    return 0;
+}
+
 static PyMethodDef ffi_methods[] = {
 #if 0
     {"addressof",     (PyCFunction)ffi_addressof, METH_VARARGS},
@@ -520,6 +538,11 @@
     {NULL}
 };
 
+static PyGetSetDef ffi_getsets[] = {
+    {"errno",  ffi_get_errno,  ffi_set_errno,  ffi_errno_doc},
+    {NULL}
+};
+
 static PyTypeObject FFI_Type = {
     PyVarObject_HEAD_INIT(NULL, 0)
     "cffi.FFI",
@@ -551,7 +574,7 @@
     0,                                          /* tp_iternext */
     ffi_methods,                                /* tp_methods */
     0,                                          /* tp_members */
-    0,                                          /* tp_getset */
+    ffi_getsets,                                /* tp_getset */
     0,                                          /* tp_base */
     0,                                          /* tp_dict */
     0,                                          /* tp_descr_get */
diff --git a/new/test_ffi_obj.py b/new/test_ffi_obj.py
--- a/new/test_ffi_obj.py
+++ b/new/test_ffi_obj.py
@@ -59,3 +59,9 @@
     ffi = _cffi1_backend.FFI()
     p = ffi.new("char[]", "foobar\x00baz")
     assert ffi.string(p) == "foobar"
+
+def test_ffi_errno():
+    # xxx not really checking errno, just checking that we can read/write it
+    ffi = _cffi1_backend.FFI()
+    ffi.errno = 42
+    assert ffi.errno == 42


More information about the pypy-commit mailing list