##// END OF EJS Templates
cext: use modern buffer protocol in patches()...
Gregory Szorc -
r40027:77492c10 default
parent child Browse files
Show More
@@ -72,10 +72,10 b' static PyObject *patches(PyObject *self,'
72 {
72 {
73 PyObject *text, *bins, *result;
73 PyObject *text, *bins, *result;
74 struct mpatch_flist *patch;
74 struct mpatch_flist *patch;
75 const char *in;
75 Py_buffer buffer;
76 int r = 0;
76 int r = 0;
77 char *out;
77 char *out;
78 Py_ssize_t len, outlen, inlen;
78 Py_ssize_t len, outlen;
79
79
80 if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
80 if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
81 return NULL;
81 return NULL;
@@ -87,17 +87,19 b' static PyObject *patches(PyObject *self,'
87 return text;
87 return text;
88 }
88 }
89
89
90 if (PyObject_AsCharBuffer(text, &in, &inlen))
90 if (PyObject_GetBuffer(text, &buffer, PyBUF_CONTIG_RO)) {
91 return NULL;
91 return NULL;
92 }
92
93
93 patch = mpatch_fold(bins, cpygetitem, 0, len);
94 patch = mpatch_fold(bins, cpygetitem, 0, len);
94 if (!patch) { /* error already set or memory error */
95 if (!patch) { /* error already set or memory error */
95 if (!PyErr_Occurred())
96 if (!PyErr_Occurred())
96 PyErr_NoMemory();
97 PyErr_NoMemory();
97 return NULL;
98 result = NULL;
99 goto cleanup;
98 }
100 }
99
101
100 outlen = mpatch_calcsize(inlen, patch);
102 outlen = mpatch_calcsize(buffer.len, patch);
101 if (outlen < 0) {
103 if (outlen < 0) {
102 r = (int)outlen;
104 r = (int)outlen;
103 result = NULL;
105 result = NULL;
@@ -112,7 +114,7 b' static PyObject *patches(PyObject *self,'
112 /* clang-format off */
114 /* clang-format off */
113 {
115 {
114 Py_BEGIN_ALLOW_THREADS
116 Py_BEGIN_ALLOW_THREADS
115 r = mpatch_apply(out, in, inlen, patch);
117 r = mpatch_apply(out, buffer.buf, buffer.len, patch);
116 Py_END_ALLOW_THREADS
118 Py_END_ALLOW_THREADS
117 }
119 }
118 /* clang-format on */
120 /* clang-format on */
@@ -122,6 +124,7 b' static PyObject *patches(PyObject *self,'
122 }
124 }
123 cleanup:
125 cleanup:
124 mpatch_lfree(patch);
126 mpatch_lfree(patch);
127 PyBuffer_Release(&buffer);
125 if (!result && !PyErr_Occurred())
128 if (!result && !PyErr_Occurred())
126 setpyerr(r);
129 setpyerr(r);
127 return result;
130 return result;
General Comments 0
You need to be logged in to leave comments. Login now