aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py2/libImaging/codec_fd.c
blob: 7bd4dadf8682c804d78186559c93052c460211d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Python.h"
#include "Imaging.h"
#include "../py3.h"


Py_ssize_t
_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);
    if (bytes_result == -1) {
        goto err;
    }

    if (length > bytes) {
        goto err;
    }

    memcpy(dest, buffer, length);

    Py_DECREF(result);
    return length;

 err:
    Py_DECREF(result);
    return -1;

}

Py_ssize_t
_imaging_write_pyFd(PyObject *fd, char* src, Py_ssize_t bytes)
{

    PyObject *result;
    PyObject *byteObj;

    byteObj = PyBytes_FromStringAndSize(src, bytes);
    result = PyObject_CallMethod(fd, "write", "O", byteObj);

    Py_DECREF(byteObj);
    Py_DECREF(result);

    return bytes;

}

int
_imaging_seek_pyFd(PyObject *fd, Py_ssize_t offset, int whence)
{
    PyObject *result;

    result = PyObject_CallMethod(fd, "seek", "ni", offset, whence);

    Py_DECREF(result);
    return 0;

}

Py_ssize_t
_imaging_tell_pyFd(PyObject *fd)
{
    PyObject *result;
    Py_ssize_t location;

    result = PyObject_CallMethod(fd, "tell", NULL);
    location = PyInt_AsSsize_t(result);

    Py_DECREF(result);
    return location;
}