Author: Armin Rigo <[email protected]>
Branch:
Changeset: r485:1d518ca8dfb5
Date: 2012-06-22 18:57 +0200
http://bitbucket.org/cffi/cffi/changeset/1d518ca8dfb5/
Log: Remove code duplication.
diff --git a/c/_ffi_backend.c b/c/_ffi_backend.c
--- a/c/_ffi_backend.c
+++ b/c/_ffi_backend.c
@@ -1215,12 +1215,9 @@
return -1;
}
-static PyObject *
-cdata_subscript(CDataObject *cd, PyObject *key)
+static char *
+_cdata_get_indexed_ptr(CDataObject *cd, PyObject *key)
{
- CTypeDescrObject *ctitem = cd->c_type->ct_itemdescr;
- /* use 'mp_subscript' instead of 'sq_item' because we don't want
- negative indexes to be corrected automatically */
Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
@@ -1252,48 +1249,31 @@
cd->c_type->ct_name);
return NULL;
}
- return convert_to_object(cd->c_data + i * ctitem->ct_size, ctitem);
+ return cd->c_data + i * cd->c_type->ct_itemdescr->ct_size;
+}
+
+static PyObject *
+cdata_subscript(CDataObject *cd, PyObject *key)
+{
+ char *c = _cdata_get_indexed_ptr(cd, key);
+ CTypeDescrObject *ctitem = cd->c_type->ct_itemdescr;
+ /* use 'mp_subscript' instead of 'sq_item' because we don't want
+ negative indexes to be corrected automatically */
+ if (c == NULL)
+ return NULL;
+ return convert_to_object(c, ctitem);
}
static int
cdata_ass_sub(CDataObject *cd, PyObject *key, PyObject *v)
{
+ char *c = _cdata_get_indexed_ptr(cd, key);
CTypeDescrObject *ctitem = cd->c_type->ct_itemdescr;
/* use 'mp_ass_subscript' instead of 'sq_ass_item' because we don't want
negative indexes to be corrected automatically */
- Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
+ if (c == NULL)
return -1;
-
- if (cd->c_type->ct_flags & CT_POINTER) {
- if (CDataOwn_Check(cd) && i != 0) {
- PyErr_Format(PyExc_IndexError,
- "cdata '%s' can only be indexed by 0",
- cd->c_type->ct_name);
- return -1;
- }
- }
- else if (cd->c_type->ct_flags & CT_ARRAY) {
- if (i < 0) {
- PyErr_SetString(PyExc_IndexError,
- "negative index not supported");
- return -1;
- }
- if (i >= get_array_length(cd)) {
- PyErr_Format(PyExc_IndexError,
- "index too large for cdata '%s' (expected %zd < %zd)",
- cd->c_type->ct_name,
- i, get_array_length(cd));
- return -1;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "cdata of type '%s' does not support index assignment",
- cd->c_type->ct_name);
- return -1;
- }
- return convert_from_object(cd->c_data + i * ctitem->ct_size, ctitem, v);
+ return convert_from_object(c, ctitem, v);
}
static PyObject *
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit