From b37ad040c358304c3bc6b259758821adf0c70900 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Sun, 12 Sep 2010 15:01:42 +0200 Subject: [PATCH] pymml: add MMLException, add memory management --- Projects/libmml/pymml/mml.py | 14 ++++++++++---- Projects/libmml/pymml/mmlcolor.py | 20 ++++++++++---------- Projects/libmml/pymml/mmltypes.py | 5 +++++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Projects/libmml/pymml/mml.py b/Projects/libmml/pymml/mml.py index 95b58d1..04cbf77 100755 --- a/Projects/libmml/pymml/mml.py +++ b/Projects/libmml/pymml/mml.py @@ -4,6 +4,7 @@ from ctypes import * from mmlmouse import Mouse from mmlcolor import Color from time import sleep +from mmltypes import PINTEGER class MMLCoreException(Exception): def __init__(self, err): @@ -25,6 +26,9 @@ class MMLCore(object): self.dll.get_last_error.restype = c_char_p self.dll.get_last_error.argtypes = None + self.dll.free_ptr.restype = c_bool + self.dll.free_ptr.argtypes = [c_void_p] + if self.dll.init() != 0: del self.dll raise MMLCoreException("Could not initialize the DLL") @@ -35,6 +39,10 @@ class MMLCore(object): del t return s + def free(self, ptr): + _ptr = cast(ptr, c_void_p) + self.dll.free_ptr(_ptr) + def __del__(self): del self.dll @@ -50,14 +58,12 @@ if __name__ == '__main__': c = Color(DLL, client) - ret = c.find((0, 0, 100000, 10000), 0) - ret = c.find((0, 0, 100, 100), 0) print ret - ret = c.findAll((0, 0, 100, 100), 0) + ret = c.findAll((0, 0, 100, 100), 0, tol=100) print ret - + m = Mouse(DLL, client) diff --git a/Projects/libmml/pymml/mmlcolor.py b/Projects/libmml/pymml/mmlcolor.py index 567accb..d620c68 100644 --- a/Projects/libmml/pymml/mmlcolor.py +++ b/Projects/libmml/pymml/mmlcolor.py @@ -2,6 +2,7 @@ from ctypes import * from mmltypes import isiterable from mmltypes import POINT, PPOINT, PINTEGER from mmltypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR +from mmltypes import MMLException """ The Color Class @@ -10,9 +11,9 @@ The Color Class This class does the color finding. """ -class ColorException(Exception): +class ColorException(MMLException): def __init__(self, err): - Exception.__init__(self, err) + MMLException.__init__(self, err) # FIXME: Complete... @@ -48,7 +49,6 @@ class Color(object): if ret is RESULT_OK: return (x, y) elif ret is RESULT_ERROR: - print self._mc raise ColorException(self._mc.get_last_error()) return None @@ -59,7 +59,6 @@ class Color(object): returned are all the matching points """ ptr, _len = PPOINT(), c_int(42) - print type(_len) if tol is 0: self._mc.dll.find_colors(self._cli, byref(ptr), byref(_len), color, *box) @@ -67,12 +66,13 @@ class Color(object): self._mc.dll.find_colors_tolerance(self._cli, byref(ptr), byref(_len), color, tol, *box) -# print 'Length:', _len -# for x in range(_len.value): -# print ptr[x].x -# print ptr - # FIXME return python list? - return '' + # Construct list + l = [(ptr[x].x, ptr[x].y) for x in range(_len.value)] + + # Free PPOINT + self._mc.free(ptr) + + return l def _initialiseDLLFuncs(self): self._mc.dll.find_color.restype = c_int diff --git a/Projects/libmml/pymml/mmltypes.py b/Projects/libmml/pymml/mmltypes.py index 2ce924d..90d05dc 100644 --- a/Projects/libmml/pymml/mmltypes.py +++ b/Projects/libmml/pymml/mmltypes.py @@ -10,6 +10,11 @@ class POINT(Structure): _fields_ = [('x', c_int), ('y', c_int)] +class MMLException(Exception): + def __init__(self, err): + Exception.__init__(self, err) + + #class PascalArray(object): # """ # PascalArray is a class that allows one to easily use a Pascal-style