comparison src_classic/helper-fileobj.i @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 %{
2 //-------------------------------------
3 // fz_output for Python file objects
4 //-------------------------------------
5 static void
6 JM_bytesio_write(fz_context *ctx, void *opaque, const void *data, size_t len)
7 { // bio.write(bytes object)
8 PyObject *bio = opaque, *b, *name, *rc;
9 fz_try(ctx){
10 b = PyBytes_FromStringAndSize((const char *) data, (Py_ssize_t) len);
11 name = PyUnicode_FromString("write");
12 PyObject_CallMethodObjArgs(bio, name, b, NULL);
13 rc = PyErr_Occurred();
14 if (rc) {
15 RAISEPY(ctx, "could not write to Py file obj", rc);
16 }
17 }
18 fz_always(ctx) {
19 Py_XDECREF(b);
20 Py_XDECREF(name);
21 Py_XDECREF(rc);
22 PyErr_Clear();
23 }
24 fz_catch(ctx) {
25 fz_rethrow(ctx);
26 }
27 }
28
29 static void
30 JM_bytesio_truncate(fz_context *ctx, void *opaque)
31 { // bio.truncate(bio.tell()) !!!
32 PyObject *bio = opaque, *trunc = NULL, *tell = NULL, *rctell= NULL, *rc = NULL;
33 fz_try(ctx) {
34 trunc = PyUnicode_FromString("truncate");
35 tell = PyUnicode_FromString("tell");
36 rctell = PyObject_CallMethodObjArgs(bio, tell, NULL);
37 PyObject_CallMethodObjArgs(bio, trunc, rctell, NULL);
38 rc = PyErr_Occurred();
39 if (rc) {
40 RAISEPY(ctx, "could not truncate Py file obj", rc);
41 }
42 }
43 fz_always(ctx) {
44 Py_XDECREF(tell);
45 Py_XDECREF(trunc);
46 Py_XDECREF(rc);
47 Py_XDECREF(rctell);
48 PyErr_Clear();
49 }
50 fz_catch(ctx) {
51 fz_rethrow(ctx);
52 }
53 }
54
55 static int64_t
56 JM_bytesio_tell(fz_context *ctx, void *opaque)
57 { // returns bio.tell() -> int
58 PyObject *bio = opaque, *rc = NULL, *name = NULL;
59 int64_t pos = 0;
60 fz_try(ctx) {
61 name = PyUnicode_FromString("tell");
62 rc = PyObject_CallMethodObjArgs(bio, name, NULL);
63 if (!rc) {
64 RAISEPY(ctx, "could not tell Py file obj", PyErr_Occurred());
65 }
66 pos = (int64_t) PyLong_AsUnsignedLongLong(rc);
67 }
68 fz_always(ctx) {
69 Py_XDECREF(name);
70 Py_XDECREF(rc);
71 PyErr_Clear();
72 }
73 fz_catch(ctx) {
74 fz_rethrow(ctx);
75 }
76 return pos;
77 }
78
79
80 static void
81 JM_bytesio_seek(fz_context *ctx, void *opaque, int64_t off, int whence)
82 { // bio.seek(off, whence=0)
83 PyObject *bio = opaque, *rc = NULL, *name = NULL, *pos = NULL;
84 fz_try(ctx) {
85 name = PyUnicode_FromString("seek");
86 pos = PyLong_FromUnsignedLongLong((unsigned long long) off);
87 PyObject_CallMethodObjArgs(bio, name, pos, whence, NULL);
88 rc = PyErr_Occurred();
89 if (rc) {
90 RAISEPY(ctx, "could not seek Py file obj", rc);
91 }
92 }
93 fz_always(ctx) {
94 Py_XDECREF(rc);
95 Py_XDECREF(name);
96 Py_XDECREF(pos);
97 PyErr_Clear();
98 }
99 fz_catch(ctx) {
100 fz_rethrow(ctx);
101 }
102 }
103
104 fz_output *
105 JM_new_output_fileptr(fz_context *ctx, PyObject *bio)
106 {
107 fz_output *out = fz_new_output(ctx, 0, bio, JM_bytesio_write, NULL, NULL);
108 out->seek = JM_bytesio_seek;
109 out->tell = JM_bytesio_tell;
110 out->truncate = JM_bytesio_truncate;
111 return out;
112 }
113 %}