Debian Codegraph @

sure/magic.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # <sure - utility belt for automated testing in python>
3 # Copyright (C) <2012> Gabriel Falcão <[email protected]>
4 # Copyright (C) <2012> Lincoln Clarete <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from __future__ import unicode_literals
19
20 import platform
21
22 is_cpython = (
23 hasattr(platform, 'python_implementation')
24 and platform.python_implementation().lower() == 'cpython')
25
26 if is_cpython:
27
28 import ctypes
29 DictProxyType = type(object.__dict__)
30
31 Py_ssize_t = \
32 hasattr(ctypes.pythonapi, 'Py_InitModule4_64') \
33 and ctypes.c_int64 or ctypes.c_int
34
35 class PyObject(ctypes.Structure):
36 pass
37
38 PyObject._fields_ = [
39 ('ob_refcnt', Py_ssize_t),
40 ('ob_type', ctypes.POINTER(PyObject)),
41 ]
42
43 class SlotsProxy(PyObject):
44 _fields_ = [('dict', ctypes.POINTER(PyObject))]
45
46 def patchable_builtin(klass):
47 name = klass.__name__
48 target = getattr(klass, '__dict__', name)
49
50 if not isinstance(target, DictProxyType):
51 return target
52
53 proxy_dict = SlotsProxy.from_address(id(target))
54 namespace = {}
55
56 ctypes.pythonapi.PyDict_SetItem(
57 ctypes.py_object(namespace),
58 ctypes.py_object(name),
59 proxy_dict.dict,
60 )
61
62 return namespace[name]
63 else:
64 patchable_builtin = lambda *args, **kw: None