summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2015-06-01 08:49:26 +0200
committerArmin Rigo <arigo@tunes.org>2015-06-01 08:49:26 +0200
commitd78afb31fbb9c242cb3ccd8d40572c071bd7b3ae (patch)
treebd8aa21119206169cac50c1a06e40f0571e224e2
parent533b6e7c4aa1c878615e11f3733c62f9a62e7e88 (diff)
downloadcffi-d78afb31fbb9c242cb3ccd8d40572c071bd7b3ae.tar.gz
Issue #203: add keyword arguments to ffi.string(), ffi.buffer(),
ffi.getwinerror()
-rw-r--r--c/_cffi_backend.c22
-rw-r--r--c/ffi_obj.c6
-rw-r--r--c/misc_win32.h10
-rw-r--r--testing/cffi1/test_ffi_obj.py12
4 files changed, 33 insertions, 17 deletions
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
index faad567..f56e4e2 100644
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5158,12 +5158,14 @@ static PyObject *b_getcname(PyObject *self, PyObject *args)
return PyText_FromStringAndSize(s, namelen + replacelen);
}
-static PyObject *b_string(PyObject *self, PyObject *args)
+static PyObject *b_string(PyObject *self, PyObject *args, PyObject *kwds)
{
CDataObject *cd;
Py_ssize_t maxlen = -1;
- if (!PyArg_ParseTuple(args, "O!|n:string",
- &CData_Type, &cd, &maxlen))
+ static char *keywords[] = {"cdata", "maxlen", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|n:string", keywords,
+ &CData_Type, &cd, &maxlen))
return NULL;
if (cd->c_type->ct_itemdescr != NULL &&
@@ -5246,12 +5248,14 @@ static PyObject *b_string(PyObject *self, PyObject *args)
return NULL;
}
-static PyObject *b_buffer(PyObject *self, PyObject *args)
+static PyObject *b_buffer(PyObject *self, PyObject *args, PyObject *kwds)
{
CDataObject *cd;
Py_ssize_t size = -1;
- if (!PyArg_ParseTuple(args, "O!|n:buffer",
- &CData_Type, &cd, &size))
+ static char *keywords[] = {"cdata", "size", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|n:buffer", keywords,
+ &CData_Type, &cd, &size))
return NULL;
if (cd->c_type->ct_flags & CT_POINTER) {
@@ -5796,15 +5800,15 @@ static PyMethodDef FFIBackendMethods[] = {
{"typeoffsetof", b_typeoffsetof, METH_VARARGS},
{"rawaddressof", b_rawaddressof, METH_VARARGS},
{"getcname", b_getcname, METH_VARARGS},
- {"string", b_string, METH_VARARGS},
- {"buffer", b_buffer, METH_VARARGS},
+ {"string", (PyCFunction)b_string, METH_VARARGS | METH_KEYWORDS},
+ {"buffer", (PyCFunction)b_buffer, METH_VARARGS | METH_KEYWORDS},
{"get_errno", b_get_errno, METH_NOARGS},
{"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},
#ifdef MS_WIN32
- {"getwinerror", b_getwinerror, METH_VARARGS},
+ {"getwinerror", (PyCFunction)b_getwinerror, METH_VARARGS | METH_KEYWORDS},
#endif
{"_get_types", b__get_types, METH_NOARGS},
{"_testfunc", b__testfunc, METH_VARARGS},
diff --git a/c/ffi_obj.c b/c/ffi_obj.c
index 4c107ed..134e303 100644
--- a/c/ffi_obj.c
+++ b/c/ffi_obj.c
@@ -774,7 +774,7 @@ static PyObject *ffi_int_const(FFIObject *self, PyObject *args, PyObject *kwds)
static PyMethodDef ffi_methods[] = {
{"addressof", (PyCFunction)ffi_addressof, METH_VARARGS, ffi_addressof_doc},
{"alignof", (PyCFunction)ffi_alignof, METH_O, ffi_alignof_doc},
- {"buffer", (PyCFunction)ffi_buffer, METH_VARARGS, ffi_buffer_doc},
+ {"buffer", (PyCFunction)ffi_buffer, METH_VKW, ffi_buffer_doc},
{"callback", (PyCFunction)ffi_callback, METH_VKW, ffi_callback_doc},
{"cast", (PyCFunction)ffi_cast, METH_VARARGS, ffi_cast_doc},
{"dlclose", (PyCFunction)ffi_dlclose, METH_VARARGS, ffi_dlclose_doc},
@@ -784,14 +784,14 @@ static PyMethodDef ffi_methods[] = {
{"gc", (PyCFunction)ffi_gc, METH_VKW, ffi_gc_doc},
{"getctype", (PyCFunction)ffi_getctype, METH_VKW, ffi_getctype_doc},
#ifdef MS_WIN32
- {"getwinerror",(PyCFunction)ffi_getwinerror,METH_VARARGS, ffi_getwinerror_doc},
+ {"getwinerror",(PyCFunction)ffi_getwinerror,METH_VKW, ffi_getwinerror_doc},
#endif
{"integer_const",(PyCFunction)ffi_int_const,METH_VKW, ffi_int_const_doc},
{"new", (PyCFunction)ffi_new, METH_VKW, ffi_new_doc},
{"new_handle", (PyCFunction)ffi_new_handle, METH_O, ffi_new_handle_doc},
{"offsetof", (PyCFunction)ffi_offsetof, METH_VARARGS, ffi_offsetof_doc},
{"sizeof", (PyCFunction)ffi_sizeof, METH_O, ffi_sizeof_doc},
- {"string", (PyCFunction)ffi_string, METH_VARARGS, ffi_string_doc},
+ {"string", (PyCFunction)ffi_string, METH_VKW, ffi_string_doc},
{"typeof", (PyCFunction)ffi_typeof, METH_O, ffi_typeof_doc},
{NULL}
};
diff --git a/c/misc_win32.h b/c/misc_win32.h
index 5678e21..05bfabc 100644
--- a/c/misc_win32.h
+++ b/c/misc_win32.h
@@ -82,14 +82,15 @@ static void restore_errno_only(void)
}
#if PY_MAJOR_VERSION >= 3
-static PyObject *b_getwinerror(PyObject *self, PyObject *args)
+static PyObject *b_getwinerror(PyObject *self, PyObject *args, PyObject *kwds)
{
int err = -1;
int len;
WCHAR *s_buf = NULL; /* Free via LocalFree */
PyObject *v, *message;
+ static char *keywords[] = {"code", NULL};
- if (!PyArg_ParseTuple(args, "|i", &err))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", keywords, &err))
return NULL;
if (err == -1) {
@@ -129,7 +130,7 @@ static PyObject *b_getwinerror(PyObject *self, PyObject *args)
return v;
}
#else
-static PyObject *b_getwinerror(PyObject *self, PyObject *args)
+static PyObject *b_getwinerror(PyObject *self, PyObject *args, PyObject *kwds)
{
int err = -1;
int len;
@@ -137,8 +138,9 @@ static PyObject *b_getwinerror(PyObject *self, PyObject *args)
char *s_buf = NULL; /* Free via LocalFree */
char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
PyObject *v;
+ static char *keywords[] = {"code", NULL};
- if (!PyArg_ParseTuple(args, "|i", &err))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", keywords, &err))
return NULL;
if (err == -1) {
diff --git a/testing/cffi1/test_ffi_obj.py b/testing/cffi1/test_ffi_obj.py
index 2def7e6..3c5d828 100644
--- a/testing/cffi1/test_ffi_obj.py
+++ b/testing/cffi1/test_ffi_obj.py
@@ -1,4 +1,4 @@
-import py
+import py, sys
import _cffi_backend as _cffi1_backend
@@ -65,6 +65,7 @@ def test_ffi_string():
ffi = _cffi1_backend.FFI()
p = ffi.new("char[]", init=b"foobar\x00baz")
assert ffi.string(p) == b"foobar"
+ assert ffi.string(cdata=p, maxlen=3) == b"foo"
def test_ffi_errno():
# xxx not really checking errno, just checking that we can read/write it
@@ -162,6 +163,7 @@ def test_ffi_buffer():
ffi = _cffi1_backend.FFI()
a = ffi.new("signed char[]", [5, 6, 7])
assert ffi.buffer(a)[:] == b'\x05\x06\x07'
+ assert ffi.buffer(cdata=a, size=2)[:] == b'\x05\x06'
def test_ffi_from_buffer():
import array
@@ -178,3 +180,11 @@ def test_ffi_types():
ffi = _cffi1_backend.FFI()
assert isinstance(ffi.cast("int", 42), CData)
assert isinstance(ffi.typeof("int"), CType)
+
+def test_ffi_getwinerror():
+ if sys.platform != "win32":
+ py.test.skip("for windows")
+ ffi = _cffi1_backend.FFI()
+ n = (1 << 29) + 42
+ code, message = ffi.getwinerror(code=n)
+ assert code == n