Skip to content

Commit

Permalink
Fix int/ssize_t mismatch in tests for the file shim
Browse files Browse the repository at this point in the history
Using "s#" in PyArg_ParseTuple caused test failures on big-endian
architectures. The choice between int and Py_ssize_t is governed
by the PY_SSIZE_T_CLEAN macro, but in a header file we don't control.
the macro.
Use "s*" with Py_buffer instead.

Reported by Dan Horák here: https://bugzilla.redhat.com/show_bug.cgi?id=1351971
  • Loading branch information
encukou committed Jul 5, 2016
1 parent c7e54d1 commit 4a5624c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions test/test_py3c.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,33 @@ static PyObject *file_fread(PyObject *mod, PyObject *o) {
FILE *f = (FILE*) PyCapsule_GetPointer(o, "test_py3c._file");
if (!f) return NULL;
size = fread(data, 1, 200, f);
if (size > 199) {
return PyErr_Format(PyExc_AssertionError,
"size read is too big (%zu)", size);
}
return PyStr_FromStringAndSize(data, size);
}

static PyObject *file_fwrite(PyObject *mod, PyObject *args) {
PyObject *o;
char *data;
Py_ssize_t size = 0;
Py_buffer pybuf;
size_t size_written;
FILE *f;
if (!PyArg_ParseTuple(args, "Os#", &o, &data, &size)) return NULL;
if (!PyArg_ParseTuple(args, "Os*", &o, &pybuf)) return NULL;
f = (FILE*) PyCapsule_GetPointer(o, "test_py3c._file");
if (!f) return NULL;
size = fwrite(data, 1, size, f);
if (size < 0) return PyErr_SetFromErrno(PyExc_OSError);
if (!f) {
PyBuffer_Release(&pybuf);
return NULL;
}
size_written = fwrite(pybuf.buf, 1, pybuf.len, f);
if (size_written != (size_t)pybuf.len) {
PyBuffer_Release(&pybuf);
return PyErr_Format(PyExc_AssertionError,
"size written (%zu) != size of string (%zd)",
size_written, pybuf.len);
}
fflush(f);
PyBuffer_Release(&pybuf);
Py_RETURN_NONE;
}

Expand Down

0 comments on commit 4a5624c

Please sign in to comment.