Pillow/src/libImaging/codec_fd.c

79 lines
1.4 KiB
C
Raw Normal View History

2016-06-11 15:14:58 +03:00
#include "Python.h"
#include "Imaging.h"
2016-07-01 14:27:01 +03:00
Py_ssize_t
2016-06-11 15:14:58 +03:00
_imaging_read_pyFd(PyObject *fd, char* dest, Py_ssize_t bytes)
{
/* dest should be a buffer bytes long, returns length of read
-1 on error */
PyObject *result;
char *buffer;
Py_ssize_t length;
int bytes_result;
result = PyObject_CallMethod(fd, "read", "n", bytes);
bytes_result = PyBytes_AsStringAndSize(result, &buffer, &length);
2016-07-01 14:27:01 +03:00
if (bytes_result == -1) {
2016-06-11 15:14:58 +03:00
goto err;
}
2016-09-03 05:23:42 +03:00
2016-06-11 15:14:58 +03:00
if (length > bytes) {
goto err;
}
2016-09-03 05:23:42 +03:00
2016-07-01 14:27:01 +03:00
memcpy(dest, buffer, length);
2016-06-11 15:14:58 +03:00
Py_DECREF(result);
return length;
err:
Py_DECREF(result);
2016-07-01 14:27:01 +03:00
return -1;
2016-09-03 05:23:42 +03:00
2016-06-11 15:14:58 +03:00
}
2016-07-01 14:27:01 +03:00
Py_ssize_t
2016-06-11 15:14:58 +03:00
_imaging_write_pyFd(PyObject *fd, char* src, Py_ssize_t bytes)
{
PyObject *result;
PyObject *byteObj;
2016-09-03 05:23:42 +03:00
2016-06-11 15:14:58 +03:00
byteObj = PyBytes_FromStringAndSize(src, bytes);
result = PyObject_CallMethod(fd, "write", "O", byteObj);
Py_DECREF(byteObj);
Py_DECREF(result);
return bytes;
2016-09-03 05:23:42 +03:00
2016-06-11 15:14:58 +03:00
}
int
_imaging_seek_pyFd(PyObject *fd, Py_ssize_t offset, int whence)
{
PyObject *result;
2016-09-03 05:23:42 +03:00
2016-06-11 15:14:58 +03:00
result = PyObject_CallMethod(fd, "seek", "ni", offset, whence);
2016-07-01 14:27:01 +03:00
Py_DECREF(result);
2016-06-11 15:14:58 +03:00
return 0;
}
Py_ssize_t
_imaging_tell_pyFd(PyObject *fd)
{
PyObject *result;
Py_ssize_t location;
2016-09-03 05:23:42 +03:00
2016-06-11 15:14:58 +03:00
result = PyObject_CallMethod(fd, "tell", NULL);
2019-09-26 15:12:28 +03:00
location = PyLong_AsSsize_t(result);
2016-06-11 15:14:58 +03:00
Py_DECREF(result);
return location;
}