Skip to content

Commit b7ba314

Browse files
committed
Fix for Python 3.5
1 parent 0200cdd commit b7ba314

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

src/face.c

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static int _py_file_to_open_args(
130130
py_file_def *stream_info = NULL;
131131
long file_size;
132132
void *new_memory;
133+
PyObject *read_string = NULL;
133134

134135
int result = -1;
135136

@@ -169,40 +170,57 @@ static int _py_file_to_open_args(
169170

170171
open_args->flags = FT_OPEN_STREAM;
171172
open_args->stream = &face->stream;
172-
} else {
173-
if (PyObject_HasAttrString(py_file_arg, "read") &&
174-
(data = PyObject_CallMethod(py_file_arg, "read", ""))) {
175-
if (PyBytes_AsStringAndSize(data, &data_ptr, &data_len)) {
176-
goto exit;
177-
}
178-
179-
if (face->mem) {
180-
free(face->mem);
181-
}
182-
face->mem = PyMem_Malloc(face->mem_size + data_len);
183-
if (face->mem == NULL) {
184-
goto exit;
185-
}
186-
new_memory = face->mem + face->mem_size;
187-
face->mem_size += data_len;
188-
189-
memcpy(new_memory, data_ptr, data_len);
190-
open_args->flags = FT_OPEN_MEMORY;
191-
open_args->memory_base = new_memory;
192-
open_args->memory_size = data_len;
193-
open_args->stream = NULL;
194-
} else {
195-
PyErr_SetString(
196-
PyExc_TypeError,
197-
"First argument must be a path or file object reading bytes");
173+
174+
result = 0;
175+
goto exit;
176+
}
177+
178+
PyErr_Clear();
179+
180+
read_string = PyUnicode_FromString("read");
181+
if (read_string == NULL) {
182+
goto exit;
183+
}
184+
185+
if (PyObject_HasAttrString(py_file_arg, "read")) {
186+
data = PyObject_CallMethodObjArgs(py_file_arg, read_string, NULL);
187+
if (data == NULL) {
198188
goto exit;
199189
}
200-
}
201190

202-
result = 0;
191+
if (PyBytes_AsStringAndSize(data, &data_ptr, &data_len)) {
192+
goto exit;
193+
}
194+
195+
if (face->mem) {
196+
free(face->mem);
197+
}
198+
face->mem = PyMem_Malloc(face->mem_size + data_len);
199+
if (face->mem == NULL) {
200+
goto exit;
201+
}
202+
new_memory = face->mem + face->mem_size;
203+
face->mem_size += data_len;
204+
205+
memcpy(new_memory, data_ptr, data_len);
206+
open_args->flags = FT_OPEN_MEMORY;
207+
open_args->memory_base = new_memory;
208+
open_args->memory_size = data_len;
209+
open_args->stream = NULL;
210+
211+
result = 0;
212+
goto exit;
213+
}
203214

204215
exit:
205216

217+
if (result && !PyErr_Occurred()) {
218+
PyErr_SetString(
219+
PyExc_TypeError,
220+
"First argument must be a path or file object reading bytes");
221+
}
222+
223+
Py_XDECREF(read_string);
206224
Py_XDECREF(py_file);
207225
Py_XDECREF(data);
208226

0 commit comments

Comments
 (0)