From dcc7dca7497f332bdee8346a2758d95f991bbbd8 Mon Sep 17 00:00:00 2001 From: alxf Date: Sun, 22 May 2016 11:43:10 +0200 Subject: [PATCH 1/2] Fix returned pointer with caca types. --- python/caca/canvas.py | 6 ++++++ python/caca/display.py | 8 ++++++++ python/caca/dither.py | 5 +++++ python/caca/font.py | 6 +++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/caca/canvas.py b/python/caca/canvas.py index 046a1c3..9426480 100644 --- a/python/caca/canvas.py +++ b/python/caca/canvas.py @@ -21,6 +21,11 @@ from caca import _lib, utf8_to_utf32, utf32_to_utf8 from caca import _PYTHON3, _str_to_bytes, _bytes_to_str from caca.font import _Font + +class _CanvasStruct(ctypes.Structure): + pass + + class _Canvas(object): """ Model for Canvas objects. """ @@ -61,6 +66,7 @@ class Canvas(_Canvas): pointer -- pointer to libcaca canvas """ _lib.caca_create_canvas.argtypes = [ctypes.c_int, ctypes.c_int] + _lib.caca_create_canvas.restype = ctypes.POINTER(_CanvasStruct) if pointer is None: try: diff --git a/python/caca/display.py b/python/caca/display.py index 7cf4d71..5d9a8d9 100644 --- a/python/caca/display.py +++ b/python/caca/display.py @@ -19,6 +19,10 @@ import ctypes from caca import _lib, _PYTHON3, _str_to_bytes from caca.canvas import _Canvas, Canvas +class _DisplayStruct(ctypes.Structure): + pass + + class _Display(object): """ Model for Display objects. """ @@ -57,11 +61,15 @@ class Display(_Display): if driver is None: _lib.caca_create_display.argtypes = [_Canvas] + _lib.caca_create_display.restype = ctypes.POINTER(_DisplayStruct) self._dp = _lib.caca_create_display(cv) else: _lib.caca_create_display_with_driver.argtypes = [ _Canvas, ctypes.c_char_p ] + _lib.caca_create_display_with_driver.restype = ctypes.POINTER( + _DisplayStruct + ) if _PYTHON3 and isinstance(driver, str): driver = _str_to_bytes(driver) diff --git a/python/caca/dither.py b/python/caca/dither.py index fde2cb0..8a0e60f 100644 --- a/python/caca/dither.py +++ b/python/caca/dither.py @@ -19,6 +19,10 @@ import ctypes from caca import _lib from caca.canvas import _Canvas + +class _DitherStruct(ctypes.Structure): + pass + class _Dither(object): """ Model for Dither object. """ @@ -66,6 +70,7 @@ class Dither(_Dither): ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ] + _lib.caca_create_dither.restype = ctypes.POINTER(_DitherStruct) self._dither = _lib.caca_create_dither(bpp, width, height, pitch, rmask, gmask, bmask, amask) diff --git a/python/caca/font.py b/python/caca/font.py index 7792e4a..fc66f8a 100644 --- a/python/caca/font.py +++ b/python/caca/font.py @@ -19,6 +19,10 @@ import errno from caca import _lib, _PYTHON3, _str_to_bytes + +class _FontStruct(ctypes.Structure): + pass + class _Font(object): """ Model for Font object. """ @@ -62,7 +66,7 @@ class Font(_Font): else: raise FontError("Unsupported method") - _lib.caca_load_font.restype = ctypes.c_int + _lib.caca_load_font.restype = ctypes.POINTER(_FontStruct) if _PYTHON3: font = _str_to_bytes(font) From f5a052e996585f7fefb3a9324c8c2e74114b5cdf Mon Sep 17 00:00:00 2001 From: alxf Date: Sun, 22 May 2016 14:40:21 +0200 Subject: [PATCH 2/2] Fix free for caca types. --- python/caca/canvas.py | 5 ++--- python/caca/display.py | 5 ++++- python/caca/dither.py | 4 ++-- python/caca/font.py | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/python/caca/canvas.py b/python/caca/canvas.py index 9426480..d69baa6 100644 --- a/python/caca/canvas.py +++ b/python/caca/canvas.py @@ -25,13 +25,12 @@ from caca.font import _Font class _CanvasStruct(ctypes.Structure): pass - class _Canvas(object): """ Model for Canvas objects. """ def __init__(self): - self._cv = 0 + self._cv = None def from_param(self): """ Required by ctypes module to call object as parameter of @@ -43,7 +42,7 @@ class _Canvas(object): return "" % (self.get_width(), self.get_height()) def __del__(self): - if self._cv > 0 and _lib is not None: + if self._cv and _lib is not None: self._free() def _free(self): diff --git a/python/caca/display.py b/python/caca/display.py index 5d9a8d9..25a4873 100644 --- a/python/caca/display.py +++ b/python/caca/display.py @@ -19,6 +19,7 @@ import ctypes from caca import _lib, _PYTHON3, _str_to_bytes from caca.canvas import _Canvas, Canvas + class _DisplayStruct(ctypes.Structure): pass @@ -36,7 +37,7 @@ class _Display(object): return "" def __del__(self): - if self._dp > 0 and _lib is not None: + if self._dp and _lib is not None: self._free() def _free(self): @@ -205,9 +206,11 @@ class Display(_Display): return _lib.caca_get_mouse_y(self) + class DisplayError(Exception): pass + class Event(ctypes.Structure): """ Object to store libcaca event. """ diff --git a/python/caca/dither.py b/python/caca/dither.py index 8a0e60f..d423289 100644 --- a/python/caca/dither.py +++ b/python/caca/dither.py @@ -27,7 +27,7 @@ class _Dither(object): """ Model for Dither object. """ def __init__(self): - self._dither = 0 + self._dither = None def from_param(self): """ Required by ctypes module to call object as parameter of @@ -36,7 +36,7 @@ class _Dither(object): return self._dither def __del__(self): - if self._dither > 0: + if self._dither: self._free() def __str__(self): diff --git a/python/caca/font.py b/python/caca/font.py index fc66f8a..a04cc8e 100644 --- a/python/caca/font.py +++ b/python/caca/font.py @@ -27,7 +27,7 @@ class _Font(object): """ Model for Font object. """ def __init__(self): - self._font = 0 + self._font = None def from_param(self): """ Required by ctypes module to call object as parameter of @@ -37,7 +37,7 @@ class _Font(object): def __del__(self): if hasattr(self, "_font"): - if self._font > 0: + if self._font: self._free() def __str__(self):