@@ -364,7 +364,6 @@ typedef struct
364
364
FT2Font *x;
365
365
PyObject *fname;
366
366
PyObject *py_file;
367
- int close_file;
368
367
FT_StreamRec stream;
369
368
Py_ssize_t shape[2 ];
370
369
Py_ssize_t strides[2 ];
@@ -402,16 +401,16 @@ static unsigned long read_from_file_callback(FT_Stream stream,
402
401
403
402
static void close_file_callback (FT_Stream stream)
404
403
{
405
- PyObject *py_file = (( PyFT2Font *)stream->descriptor .pointer )-> py_file ;
404
+ PyFT2Font *self = (PyFT2Font *)stream->descriptor .pointer ;
406
405
PyObject *close_result = NULL ;
407
- if (!(close_result = PyObject_CallMethod (py_file, " close" , " " ))) {
406
+ if (!(close_result = PyObject_CallMethod (self-> py_file , " close" , " " ))) {
408
407
goto exit ;
409
408
}
410
409
exit :
411
410
Py_XDECREF (close_result);
412
- Py_DECREF ( py_file);
411
+ Py_CLEAR (self-> py_file );
413
412
if (PyErr_Occurred ()) {
414
- PyErr_WriteUnraisable (py_file );
413
+ PyErr_WriteUnraisable ((PyObject*)self );
415
414
}
416
415
}
417
416
@@ -421,6 +420,7 @@ static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Arg
421
420
PyObject *data = NULL ;
422
421
423
422
int result = 0 ;
423
+ bool close_file = false ;
424
424
425
425
memset ((void *)open_args, 0 , sizeof (FT_Open_Args));
426
426
@@ -429,7 +429,7 @@ static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Arg
429
429
|| !(self->py_file = PyObject_CallFunction (open , " Os" , py_file_arg, " rb" ))) {
430
430
goto exit ;
431
431
}
432
- self-> close_file = 1 ;
432
+ close_file = true ;
433
433
} else if (!PyObject_HasAttrString (py_file_arg, " read" )
434
434
|| !(data = PyObject_CallMethod (py_file_arg, " read" , " i" , 0 ))
435
435
|| !PyBytes_Check (data)) {
@@ -446,7 +446,7 @@ static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Arg
446
446
self->stream .pos = 0 ;
447
447
self->stream .descriptor .pointer = self;
448
448
self->stream .read = &read_from_file_callback;
449
- self->stream .close = &close_file_callback;
449
+ self->stream .close = close_file ? &close_file_callback : NULL ;
450
450
open_args->flags = FT_OPEN_STREAM;
451
451
open_args->stream = &self->stream ;
452
452
@@ -468,7 +468,6 @@ static PyObject *PyFT2Font_new(PyTypeObject *type, PyObject *args, PyObject *kwd
468
468
self->x = NULL ;
469
469
self->fname = NULL ;
470
470
self->py_file = NULL ;
471
- self->close_file = 0 ;
472
471
memset (&self->stream , 0 , sizeof (FT_StreamRec));
473
472
return (PyObject *)self;
474
473
}
@@ -500,12 +499,6 @@ const char *PyFT2Font_init__doc__ =
500
499
" underline_thickness vertical thickness of the underline\n "
501
500
" postscript_name PostScript name of the font\n " ;
502
501
503
- static void PyFT2Font_fail (PyFT2Font *self)
504
- {
505
- Py_XDECREF (self->py_file );
506
- self->py_file = NULL ;
507
- }
508
-
509
502
static int PyFT2Font_init (PyFT2Font *self, PyObject *args, PyObject *kwds)
510
503
{
511
504
PyObject *fname;
@@ -525,7 +518,8 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
525
518
}
526
519
527
520
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 );
529
523
530
524
CALL_CPP_INIT (" FT2Font->set_kerning_factor" , (self->x ->set_kerning_factor (kerning_factor)));
531
525
0 commit comments