-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpywrap.py
96 lines (73 loc) · 2.34 KB
/
pywrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import ctypes
def _wrap(functype, name, library, restype, params, errcheck=None):
prototype = functype(restype, *(param.type for param in params))
paramflags = tuple(param.paramflags for param in params)
wrapper = prototype((name, library), paramflags)
if errcheck:
wrapper.errcheck = errcheck
return wrapper
def wrap_winapi(name, library, restype, params, errcheck=None):
return _wrap(ctypes.WINFUNCTYPE, name, library, restype, params, errcheck=errcheck)
def wrap_cdecl(name, library, restype, params, errcheck=None):
return _wrap(ctypes.CFUNCTYPE, name, library, restype, params, errcheck=errcheck)
class Parameter(object):
def __init__(self, name, type_, default=None, out=False):
self._name = name
self._type = type_
self._out = out
self._default = default
@property
def flag(self):
if self._out:
return 2
else:
return 1
@property
def type(self):
return self._type
@property
def paramflags(self):
paramflags = (self.flag, self._name, self._default)
if self._default is None:
return paramflags[:-1]
else:
return paramflags
class Errcheck(object):
@staticmethod
def expect_true(result, func, args):
if not result:
raise ctypes.WinError()
return result
@staticmethod
def expect_null(result, func, args):
if result:
raise ctypes.WinError()
return result
@staticmethod
def expect_not_null(result, func, args):
if not result:
raise ctypes.WinError()
return result
@staticmethod
def expect_value(value):
def errcheck(result, func, args):
if result != value:
raise ctypes.WinError()
return result
return errcheck
@staticmethod
def expect_lasterror(value):
def errcheck(result, func, args):
if ctypes.get_last_error() != value:
raise ctypes.WinError()
return result
return errcheck
@staticmethod
def expect_no_error(result, func, args):
if ctypes.get_last_error():
raise ctypes.WinError()
return result
@staticmethod
def print_all(result, func, args):
print result, func, args
return result