pymml: add MMLException, add memory management

This commit is contained in:
Merlijn Wajer 2010-09-12 15:01:42 +02:00
parent 688e3e9ef3
commit b37ad040c3
3 changed files with 25 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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