@@ -364,7 +364,6 @@ typedef struct
364364 FT2Font *x;
365365 PyObject *fname;
366366 PyObject *py_file;
367- int close_file;
368367 FT_StreamRec stream;
369368 Py_ssize_t shape[2 ];
370369 Py_ssize_t strides[2 ];
@@ -402,16 +401,16 @@ static unsigned long read_from_file_callback(FT_Stream stream,
402401
403402static void close_file_callback (FT_Stream stream)
404403{
405- PyObject *py_file = (( PyFT2Font *)stream->descriptor .pointer )-> py_file ;
404+ PyFT2Font *self = (PyFT2Font *)stream->descriptor .pointer ;
406405 PyObject *close_result = NULL ;
407- if (!(close_result = PyObject_CallMethod (py_file, " close" , " " ))) {
406+ if (!(close_result = PyObject_CallMethod (self-> py_file , " close" , " " ))) {
408407 goto exit;
409408 }
410409exit:
411410 Py_XDECREF (close_result);
412- Py_DECREF ( py_file);
411+ Py_CLEAR (self-> py_file );
413412 if (PyErr_Occurred ()) {
414- PyErr_WriteUnraisable (py_file );
413+ PyErr_WriteUnraisable ((PyObject*)self );
415414 }
416415}
417416
@@ -421,6 +420,7 @@ static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Arg
421420 PyObject *data = NULL ;
422421
423422 int result = 0 ;
423+ bool close_file = false ;
424424
425425 memset ((void *)open_args, 0 , sizeof (FT_Open_Args));
426426
@@ -429,7 +429,7 @@ static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Arg
429429 || !(self->py_file = PyObject_CallFunction (open, " Os" , py_file_arg, " rb" ))) {
430430 goto exit;
431431 }
432- self-> close_file = 1 ;
432+ close_file = true ;
433433 } else if (!PyObject_HasAttrString (py_file_arg, " read" )
434434 || !(data = PyObject_CallMethod (py_file_arg, " read" , " i" , 0 ))
435435 || !PyBytes_Check (data)) {
@@ -446,7 +446,7 @@ static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Arg
446446 self->stream .pos = 0 ;
447447 self->stream .descriptor .pointer = self;
448448 self->stream .read = &read_from_file_callback;
449- self->stream .close = &close_file_callback;
449+ self->stream .close = close_file ? &close_file_callback : NULL ;
450450 open_args->flags = FT_OPEN_STREAM;
451451 open_args->stream = &self->stream ;
452452
@@ -468,7 +468,6 @@ static PyObject *PyFT2Font_new(PyTypeObject *type, PyObject *args, PyObject *kwd
468468 self->x = NULL ;
469469 self->fname = NULL ;
470470 self->py_file = NULL ;
471- self->close_file = 0 ;
472471 memset (&self->stream , 0 , sizeof (FT_StreamRec));
473472 return (PyObject *)self;
474473}
@@ -500,12 +499,6 @@ const char *PyFT2Font_init__doc__ =
500499 " underline_thickness vertical thickness of the underline\n "
501500 " postscript_name PostScript name of the font\n " ;
502501
503- static void PyFT2Font_fail (PyFT2Font *self)
504- {
505- Py_XDECREF (self->py_file );
506- self->py_file = NULL ;
507- }
508-
509502static int PyFT2Font_init (PyFT2Font *self, PyObject *args, PyObject *kwds)
510503{
511504 PyObject *fname;
@@ -525,7 +518,8 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
525518 }
526519
527520 CALL_CPP_FULL (
528- " FT2Font" , (self->x = new FT2Font (open_args, hinting_factor)), PyFT2Font_fail (self), -1 );
521+ " FT2Font" , (self->x = new FT2Font (open_args, hinting_factor)),
522+ Py_CLEAR (self->py_file ), -1 );
529523
530524 CALL_CPP_INIT (" FT2Font->set_kerning_factor" , (self->x ->set_kerning_factor (kerning_factor)));
531525
0 commit comments