Wrap some long lines

Top/Bottom factor out some common expressions
Add a XXX comment about widing offset.
This commit is contained in:
Neal Norwitz 2006-06-11 05:45:25 +00:00
parent b4fcf8d787
commit 3c5431e132
1 changed files with 10 additions and 15 deletions

View File

@ -218,7 +218,8 @@ static int
get_wrapped_long(PyObject *v, long *p) get_wrapped_long(PyObject *v, long *p)
{ {
if (get_long(v, p) < 0) { if (get_long(v, p) < 0) {
if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) { if (PyLong_Check(v) &&
PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyObject *wrapped; PyObject *wrapped;
long x; long x;
PyErr_Clear(); PyErr_Clear();
@ -1396,24 +1397,18 @@ s_unpack_internal(PyStructObject *soself, char *startfrom) {
const char *res = startfrom + code->offset; const char *res = startfrom + code->offset;
if (e->format == 's') { if (e->format == 's') {
v = PyString_FromStringAndSize(res, code->size); v = PyString_FromStringAndSize(res, code->size);
if (v == NULL)
goto fail;
PyTuple_SET_ITEM(result, i++, v);
} else if (e->format == 'p') { } else if (e->format == 'p') {
Py_ssize_t n = *(unsigned char*)res; Py_ssize_t n = *(unsigned char*)res;
if (n >= code->size) if (n >= code->size)
n = code->size - 1; n = code->size - 1;
v = PyString_FromStringAndSize(res + 1, n); v = PyString_FromStringAndSize(res + 1, n);
if (v == NULL)
goto fail;
PyTuple_SET_ITEM(result, i++, v);
} else { } else {
v = e->unpack(res, e); v = e->unpack(res, e);
}
if (v == NULL) if (v == NULL)
goto fail; goto fail;
PyTuple_SET_ITEM(result, i++, v); PyTuple_SET_ITEM(result, i++, v);
} }
}
return result; return result;
fail: fail:
@ -1438,7 +1433,8 @@ s_unpack(PyObject *self, PyObject *inputstr)
if (inputstr == NULL || !PyString_Check(inputstr) || if (inputstr == NULL || !PyString_Check(inputstr) ||
PyString_GET_SIZE(inputstr) != soself->s_size) { PyString_GET_SIZE(inputstr) != soself->s_size) {
PyErr_Format(StructError, PyErr_Format(StructError,
"unpack requires a string argument of length %zd", soself->s_size); "unpack requires a string argument of length %zd",
soself->s_size);
return NULL; return NULL;
} }
return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
@ -1504,17 +1500,18 @@ static int
s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
{ {
formatcode *code; formatcode *code;
/* XXX(nnorwitz): why does i need to be a local? can we use
the offset parameter or do we need the wider width? */
Py_ssize_t i; Py_ssize_t i;
memset(buf, '\0', soself->s_size); memset(buf, '\0', soself->s_size);
i = offset; i = offset;
for (code = soself->s_codes; code->fmtdef != NULL; code++) { for (code = soself->s_codes; code->fmtdef != NULL; code++) {
Py_ssize_t n; Py_ssize_t n;
PyObject *v; PyObject *v = PyTuple_GET_ITEM(args, i++);
const formatdef *e = code->fmtdef; const formatdef *e = code->fmtdef;
char *res = buf + code->offset; char *res = buf + code->offset;
if (e->format == 's') { if (e->format == 's') {
v = PyTuple_GET_ITEM(args, i++);
if (!PyString_Check(v)) { if (!PyString_Check(v)) {
PyErr_SetString(StructError, PyErr_SetString(StructError,
"argument for 's' must be a string"); "argument for 's' must be a string");
@ -1526,7 +1523,6 @@ s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
if (n > 0) if (n > 0)
memcpy(res, PyString_AS_STRING(v), n); memcpy(res, PyString_AS_STRING(v), n);
} else if (e->format == 'p') { } else if (e->format == 'p') {
v = PyTuple_GET_ITEM(args, i++);
if (!PyString_Check(v)) { if (!PyString_Check(v)) {
PyErr_SetString(StructError, PyErr_SetString(StructError,
"argument for 'p' must be a string"); "argument for 'p' must be a string");
@ -1541,7 +1537,6 @@ s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
n = 255; n = 255;
*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
} else { } else {
v = PyTuple_GET_ITEM(args, i++);
if (e->pack(res, v, e) < 0) { if (e->pack(res, v, e) < 0) {
if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(StructError, PyErr_SetString(StructError,