-
Notifications
You must be signed in to change notification settings - Fork 0
/
calldules.py
90 lines (79 loc) · 3.17 KB
/
calldules.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
# -*- coding: utf-8 -*-
# The MIT License (MIT)
#
# Copyright (c) 2013 Matthew Iversen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import ctypes
# Create a function prototype for a 3-arg function
ternaryfunc = ctypes.CFUNCTYPE(ctypes.py_object, ctypes.py_object,
ctypes.py_object, ctypes.c_void_p)
# Define a new python type that's callable, via a ternaryfunc
class PyTypeObject(ctypes.Structure):
_fields_ = (
("ob_refcnt", ctypes.c_ssize_t),
("ob_type", ctypes.c_void_p),
("ob_size", ctypes.c_ssize_t),
("tp_name", ctypes.c_char_p),
("tp_basicsize", ctypes.c_ssize_t),
("tp_itemsize", ctypes.c_ssize_t),
("tp_dealloc", ctypes.c_void_p),
("tp_print", ctypes.c_void_p),
("tp_getattr", ctypes.c_void_p),
("tp_setattr", ctypes.c_void_p),
("tp_reserved", ctypes.c_void_p),
("tp_repr", ctypes.c_void_p),
("tp_as_number", ctypes.c_void_p),
("tp_as_sequence", ctypes.c_void_p),
("tp_as_wrapping", ctypes.c_void_p),
("tp_hash", ctypes.c_void_p),
("tp_call", ternaryfunc),
("tp_str", ctypes.c_void_p),
)
# And define a python object whose type is the one above
class PyObject(ctypes.Structure):
_fields_ = (
("ob_refcnt", ctypes.c_ssize_t),
("ob_type", ctypes.POINTER(PyTypeObject)),
)
@ternaryfunc
def _module_call(obj, args, kwargs):
# kwargs are 'special' in ctypes.
if kwargs:
kwargs = ctypes.cast(kwargs, ctypes.py_object).value
else:
kwargs = {}
name = obj.__name__
guesses = [name.title(), name]
if name.endswith('s'):
name = name.rstrip('s')
guesses += [name.title(), name]
guesses = [p for p in guesses if hasattr(obj, p)]
if len(guesses) == 0:
return
prop = getattr(obj, guesses[0])
# HERE GOES!
return prop(*args, **kwargs)
# Load a module object into our new PyObject
ctypes_mod = PyObject.from_address(id(ctypes))
# Grab its type's contents
ctypes_mod_ob_type = ctypes_mod.ob_type.contents
# Assign its callable slot to _module_call()
ctypes_mod_ob_type.tp_call = _module_call
# HERE BE MODRAGONS, BEWARE ALL YE WHO IMPORT.