Skip to content

Commit 23d36d9

Browse files
committed
Inline convert_open_args.
1 parent 505cbed commit 23d36d9

1 file changed

Lines changed: 32 additions & 51 deletions

File tree

src/ft2font_wrapper.cpp

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -414,51 +414,6 @@ static void close_file_callback(FT_Stream stream)
414414
}
415415
}
416416

417-
static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Args *open_args)
418-
{
419-
PyObject *open = NULL;
420-
PyObject *data = NULL;
421-
422-
int result = 0;
423-
bool close_file = false;
424-
425-
memset((void *)open_args, 0, sizeof(FT_Open_Args));
426-
427-
if (PyBytes_Check(py_file_arg) || PyUnicode_Check(py_file_arg)) {
428-
if (!(open = PyDict_GetItemString(PyEval_GetBuiltins(), "open")) // Borrowed reference.
429-
|| !(self->py_file = PyObject_CallFunction(open, "Os", py_file_arg, "rb"))) {
430-
goto exit;
431-
}
432-
close_file = true;
433-
} else if (!PyObject_HasAttrString(py_file_arg, "read")
434-
|| !(data = PyObject_CallMethod(py_file_arg, "read", "i", 0))
435-
|| !PyBytes_Check(data)) {
436-
PyErr_SetString(PyExc_TypeError,
437-
"First argument must be a path or binary-mode file object");
438-
goto exit;
439-
} else {
440-
self->py_file = py_file_arg;
441-
Py_INCREF(py_file_arg);
442-
}
443-
444-
self->stream.base = NULL;
445-
self->stream.size = 0x7fffffff; // Unknown size.
446-
self->stream.pos = 0;
447-
self->stream.descriptor.pointer = self;
448-
self->stream.read = &read_from_file_callback;
449-
self->stream.close = close_file ? &close_file_callback : NULL;
450-
open_args->flags = FT_OPEN_STREAM;
451-
open_args->stream = &self->stream;
452-
453-
result = 1;
454-
455-
exit:
456-
457-
Py_XDECREF(data);
458-
459-
return result;
460-
}
461-
462417
static PyTypeObject PyFT2FontType;
463418

464419
static PyObject *PyFT2Font_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@@ -501,20 +456,43 @@ const char *PyFT2Font_init__doc__ =
501456

502457
static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
503458
{
504-
PyObject *fname;
459+
PyObject *filename = NULL, *open = NULL, *data = NULL;
505460
FT_Open_Args open_args;
506461
long hinting_factor = 8;
507462
int kerning_factor = 0;
508463
const char *names[] = { "filename", "hinting_factor", "_kerning_factor", NULL };
509464

510465
if (!PyArg_ParseTupleAndKeywords(
511-
args, kwds, "O|l$i:FT2Font", (char **)names, &fname,
466+
args, kwds, "O|l$i:FT2Font", (char **)names, &filename,
512467
&hinting_factor, &kerning_factor)) {
513468
return -1;
514469
}
515470

516-
if (!convert_open_args(self, fname, &open_args)) {
517-
return -1;
471+
self->stream.base = NULL;
472+
self->stream.size = 0x7fffffff; // Unknown size.
473+
self->stream.pos = 0;
474+
self->stream.descriptor.pointer = self;
475+
self->stream.read = &read_from_file_callback;
476+
memset((void *)&open_args, 0, sizeof(FT_Open_Args));
477+
open_args.flags = FT_OPEN_STREAM;
478+
open_args.stream = &self->stream;
479+
480+
if (PyBytes_Check(filename) || PyUnicode_Check(filename)) {
481+
if (!(open = PyDict_GetItemString(PyEval_GetBuiltins(), "open")) // Borrowed reference.
482+
|| !(self->py_file = PyObject_CallFunction(open, "Os", filename, "rb"))) {
483+
goto exit;
484+
}
485+
self->stream.close = &close_file_callback;
486+
} else if (!PyObject_HasAttrString(filename, "read")
487+
|| !(data = PyObject_CallMethod(filename, "read", "i", 0))
488+
|| !PyBytes_Check(data)) {
489+
PyErr_SetString(PyExc_TypeError,
490+
"First argument must be a path or binary-mode file object");
491+
goto exit;
492+
} else {
493+
self->py_file = filename;
494+
self->stream.close = NULL;
495+
Py_INCREF(filename);
518496
}
519497

520498
CALL_CPP_FULL(
@@ -523,8 +501,11 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
523501

524502
CALL_CPP_INIT("FT2Font->set_kerning_factor", (self->x->set_kerning_factor(kerning_factor)));
525503

526-
Py_INCREF(fname);
527-
self->fname = fname;
504+
Py_INCREF(filename);
505+
self->fname = filename;
506+
507+
exit:
508+
Py_XDECREF(data);
528509

529510
return 0;
530511
}

0 commit comments

Comments
 (0)