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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
# forbiddenfruit - Patch built-in python objects
#
# Copyright (c) 2013-2020 Lincoln de Sousa <lincoln@clarete.li>
#
# This program is dual licensed under GPLv3 and MIT.
#
# GPLv3
# -----
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# MIT
# ---
# 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 gc
import sys
from types import FunctionType
import ctypes
import inspect
from functools import wraps
from collections import defaultdict
from contextlib import contextmanager
try:
import __builtin__
except ImportError:
# Python 3 support
import builtins as __builtin__
__version__ = '0.1.4'
__all__ = 'curse', 'curses', 'reverse'
Py_ssize_t = ctypes.c_int64 if ctypes.sizeof(ctypes.c_void_p) == 8 else ctypes.c_int32
# dictionary holding references to the allocated function resolution
# arrays to type objects
tp_as_dict = {}
# container to cfunc callbacks
tp_func_dict = {}
class PyObject(ctypes.Structure):
def incref(self):
self.ob_refcnt += 1
def decref(self):
self.ob_refcnt -= 1
class PyFile(ctypes.Structure):
pass
PyObject_p = ctypes.py_object
Inquiry_p = ctypes.CFUNCTYPE(ctypes.c_int, PyObject_p)
# return type is void* to allow ctypes to convert python integers to
# plain PyObject*
UnaryFunc_p = ctypes.CFUNCTYPE(ctypes.py_object, PyObject_p)
BinaryFunc_p = ctypes.CFUNCTYPE(ctypes.py_object, PyObject_p, PyObject_p)
TernaryFunc_p = ctypes.CFUNCTYPE(ctypes.py_object, PyObject_p, PyObject_p, PyObject_p)
LenFunc_p = ctypes.CFUNCTYPE(Py_ssize_t, PyObject_p)
SSizeArgFunc_p = ctypes.CFUNCTYPE(ctypes.py_object, PyObject_p, Py_ssize_t)
SSizeObjArgProc_p = ctypes.CFUNCTYPE(ctypes.c_int, PyObject_p, Py_ssize_t, PyObject_p)
ObjObjProc_p = ctypes.CFUNCTYPE(ctypes.c_int, PyObject_p, PyObject_p)
FILE_p = ctypes.POINTER(PyFile)
def get_not_implemented():
namespace = {}
name = "_Py_NotImplmented"
not_implemented = ctypes.cast(
ctypes.pythonapi._Py_NotImplementedStruct, ctypes.py_object)
ctypes.pythonapi.PyDict_SetItem(
ctypes.py_object(namespace),
ctypes.py_object(name),
not_implemented
)
return namespace[name]
# address of the _Py_NotImplementedStruct singleton
NotImplementedRet = get_not_implemented()
class PyNumberMethods(ctypes.Structure):
_fields_ = [
('nb_add', BinaryFunc_p),
('nb_subtract', BinaryFunc_p),
('nb_multiply', BinaryFunc_p),
('nb_remainder', BinaryFunc_p),
('nb_divmod', BinaryFunc_p),
('nb_power', BinaryFunc_p),
('nb_negative', UnaryFunc_p),
('nb_positive', UnaryFunc_p),
('nb_absolute', UnaryFunc_p),
('nb_bool', Inquiry_p),
('nb_invert', UnaryFunc_p),
('nb_lshift', BinaryFunc_p),
('nb_rshift', BinaryFunc_p),
('nb_and', BinaryFunc_p),
('nb_xor', BinaryFunc_p),
('nb_or', BinaryFunc_p),
('nb_int', UnaryFunc_p),
('nb_reserved', ctypes.c_void_p),
('nb_float', UnaryFunc_p),
('nb_inplace_add', BinaryFunc_p),
('nb_inplace_subtract', BinaryFunc_p),
('nb_inplace_multiply', BinaryFunc_p),
('nb_inplace_remainder', BinaryFunc_p),
('nb_inplace_power', TernaryFunc_p),
('nb_inplace_lshift', BinaryFunc_p),
('nb_inplace_rshift', BinaryFunc_p),
('nb_inplace_and', BinaryFunc_p),
('nb_inplace_xor', BinaryFunc_p),
('nb_inplace_or', BinaryFunc_p),
('nb_floor_divide', BinaryFunc_p),
('nb_true_divide', BinaryFunc_p),
('nb_inplace_floor_divide', BinaryFunc_p),
('nb_inplace_true_divide', BinaryFunc_p),
('nb_index', BinaryFunc_p),
('nb_matrix_multiply', BinaryFunc_p),
('nb_inplace_matrix_multiply', BinaryFunc_p),
]
class PySequenceMethods(ctypes.Structure):
_fields_ = [
('sq_length', LenFunc_p),
('sq_concat', BinaryFunc_p),
('sq_repeat', SSizeArgFunc_p),
('sq_item', SSizeArgFunc_p),
('was_sq_slice', ctypes.c_void_p),
('sq_ass_item', SSizeObjArgProc_p),
('was_sq_ass_slice', ctypes.c_void_p),
('sq_contains', ObjObjProc_p),
('sq_inplace_concat', BinaryFunc_p),
('sq_inplace_repeat', SSizeArgFunc_p),
]
class PyMappingMethods(ctypes.Structure):
pass
class PyTypeObject(ctypes.Structure):
pass
class PyAsyncMethods(ctypes.Structure):
pass
PyObject._fields_ = [
('ob_refcnt', Py_ssize_t),
('ob_type', ctypes.POINTER(PyTypeObject)),
]
PyTypeObject._fields_ = [
# varhead
('ob_base', PyObject),
('ob_size', Py_ssize_t),
# declaration
('tp_name', ctypes.c_char_p),
('tp_basicsize', Py_ssize_t),
('tp_itemsize', Py_ssize_t),
('tp_dealloc', ctypes.CFUNCTYPE(None, PyObject_p)),
('printfunc', ctypes.CFUNCTYPE(ctypes.c_int, PyObject_p, FILE_p, ctypes.c_int)),
('getattrfunc', ctypes.CFUNCTYPE(PyObject_p, PyObject_p, ctypes.c_char_p)),
('setattrfunc', ctypes.CFUNCTYPE(ctypes.c_int, PyObject_p, ctypes.c_char_p, PyObject_p)),
('tp_as_async', ctypes.CFUNCTYPE(PyAsyncMethods)),
('tp_repr', ctypes.CFUNCTYPE(PyObject_p, PyObject_p)),
('tp_as_number', ctypes.POINTER(PyNumberMethods)),
('tp_as_sequence', ctypes.POINTER(PySequenceMethods)),
('tp_as_mapping', ctypes.POINTER(PyMappingMethods)),
('tp_hash', ctypes.CFUNCTYPE(ctypes.c_int64, PyObject_p)),
('tp_call', ctypes.CFUNCTYPE(PyObject_p, PyObject_p, PyObject_p, PyObject_p)),
('tp_str', ctypes.CFUNCTYPE(PyObject_p, PyObject_p)),
# ...
]
# redundant dict of pointee types, because ctypes doesn't allow us
# to extract the pointee type from the pointer
PyTypeObject_as_types_dict = {
'tp_as_async': PyAsyncMethods,
'tp_as_number': PyNumberMethods,
'tp_as_sequence': PySequenceMethods,
'tp_as_mapping': PyMappingMethods,
}
def patchable_builtin(klass):
refs = gc.get_referents(klass.__dict__)
assert len(refs) == 1
return refs[0]
@wraps(__builtin__.dir)
def __filtered_dir__(obj=None):
name = hasattr(obj, '__name__') and obj.__name__ or obj.__class__.__name__
if obj is None:
# Return names from the local scope of the calling frame,
# taking into account indirection added by __filtered_dir__
calling_frame = inspect.currentframe().f_back
return sorted(calling_frame.f_locals.keys())
return sorted(set(__dir__(obj)).difference(__hidden_elements__[name]))
# Switching to the custom dir impl declared above
__hidden_elements__ = defaultdict(list)
__dir__ = dir
__builtin__.dir = __filtered_dir__
# build override infomation for dunder methods
as_number = ('tp_as_number', [
("add", "nb_add"),
("sub", "nb_subtract"),
("mul", "nb_multiply"),
("mod", "nb_remainder"),
("pow", "nb_power"),
("neg", "nb_negative"),
("pos", "nb_positive"),
("abs", "nb_absolute"),
("bool", "nb_bool"),
("inv", "nb_invert"),
("invert", "nb_invert"),
("lshift", "nb_lshift"),
("rshift", "nb_rshift"),
("and", "nb_and"),
("xor", "nb_xor"),
("or", "nb_or"),
("int", "nb_int"),
("float", "nb_float"),
("iadd", "nb_inplace_add"),
("isub", "nb_inplace_subtract"),
("imul", "nb_inplace_multiply"),
("imod", "nb_inplace_remainder"),
("ipow", "nb_inplace_power"),
("ilshift", "nb_inplace_lshift"),
("irshift", "nb_inplace_rshift"),
("iadd", "nb_inplace_and"),
("ixor", "nb_inplace_xor"),
("ior", "nb_inplace_or"),
("floordiv", "nb_floor_divide"),
("div", "nb_true_divide"),
("ifloordiv", "nb_inplace_floor_divide"),
("idiv", "nb_inplace_true_divide"),
("index", "nb_index"),
("matmul", "nb_matrix_multiply"),
("imatmul", "nb_inplace_matrix_multiply"),
])
as_sequence = ("tp_as_sequence", [
("len", "sq_length"),
("concat", "sq_concat"),
("repeat", "sq_repeat"),
("getitem", "sq_item"),
("setitem", "sq_ass_item"),
("contains", "sq_contains"),
("iconcat", "sq_inplace_concat"),
("irepeat", "sq_inplace_repeat"),
])
as_async = ("tp_as_async", [
("await", "am_await"),
("aiter", "am_aiter"),
("anext", "am_anext"),
])
override_dict = {}
for override in [as_number, as_sequence, as_async]:
tp_as_name = override[0]
for dunder, impl_method in override[1]:
override_dict["__{}__".format(dunder)] = (tp_as_name, impl_method)
# divmod isn't a dunder, still make it overridable
override_dict['divmod()'] = ('tp_as_number', "nb_divmod")
override_dict['__str__'] = ('tp_str', "tp_str")
def _is_dunder(func_name):
return func_name.startswith("__") and func_name.endswith("__")
def _curse_special(klass, attr, func):
"""
Curse one of the "dunder" methods, i.e. methods beginning with __ which have a
precial resolution code path
"""
assert callable(func)
@wraps(func)
def wrapper(*args, **kwargs):
"""
This wrapper returns the address of the resulting object as a
python integer which is then converted to a pointer by ctypes
"""
try:
return func(*args, **kwargs)
except NotImplementedError:
return NotImplementedRet
tp_as_name, impl_method = override_dict[attr]
# get the pointer to the correct tp_as_* structure
# or create it if it doesn't exist
tyobj = PyTypeObject.from_address(id(klass))
if tp_as_name in PyTypeObject_as_types_dict:
struct_ty = PyTypeObject_as_types_dict[tp_as_name]
tp_as_ptr = getattr(tyobj, tp_as_name)
if not tp_as_ptr:
# allocate new array
tp_as_obj = struct_ty()
tp_as_dict[(klass, attr)] = tp_as_obj
tp_as_new_ptr = ctypes.cast(ctypes.addressof(tp_as_obj),
ctypes.POINTER(struct_ty))
setattr(tyobj, tp_as_name, tp_as_new_ptr)
tp_as = tp_as_ptr[0]
# find the C function type
for fname, ftype in struct_ty._fields_:
if fname == impl_method:
cfunc_t = ftype
cfunc = cfunc_t(wrapper)
tp_func_dict[(klass, attr)] = cfunc
setattr(tp_as, impl_method, cfunc)
else:
# find the C function type
for fname, ftype in PyTypeObject._fields_:
if fname == impl_method:
cfunc_t = ftype
if not (klass, attr) in tp_as_dict:
tp_as_dict[(klass, attr)] = ctypes.cast(getattr(tyobj, impl_method), cfunc_t)
# override function call
cfunc = cfunc_t(wrapper)
tp_func_dict[(klass, attr)] = cfunc
setattr(tyobj, impl_method, cfunc)
def _revert_special(klass, attr):
tp_as_name, impl_method = override_dict[attr]
tyobj = PyTypeObject.from_address(id(klass))
tp_as_ptr = getattr(tyobj, tp_as_name)
if tp_as_ptr:
if tp_as_name in PyTypeObject_as_types_dict:
tp_as = tp_as_ptr[0]
struct_ty = PyTypeObject_as_types_dict[tp_as_name]
for fname, ftype in struct_ty._fields_:
if fname == impl_method:
cfunc_t = ftype
setattr(tp_as, impl_method,
ctypes.cast(ctypes.c_void_p(None), cfunc_t))
else:
if not (klass, attr) in tp_as_dict:
# we didn't save this pointer
# most likely never cursed
return
cfunc = tp_as_dict[(klass, attr)]
setattr(tyobj, impl_method, cfunc)
def curse(klass, attr, value, hide_from_dir=False):
"""Curse a built-in `klass` with `attr` set to `value`
This function monkey-patches the built-in python object `attr` adding a new
attribute to it. You can add any kind of argument to the `class`.
It's possible to attach methods as class methods, just do the following:
>>> def myclassmethod(cls):
... return cls(1.5)
>>> curse(float, "myclassmethod", classmethod(myclassmethod))
>>> float.myclassmethod()
1.5
Methods will be automatically bound, so don't forget to add a self
parameter to them, like this:
>>> def hello(self):
... return self * 2
>>> curse(str, "hello", hello)
>>> "yo".hello()
"yoyo"
"""
if _is_dunder(attr):
if sys.version_info < (3, 3):
raise NotImplementedError(
"Dunder overloading is only supported on Python >= 3.3")
_curse_special(klass, attr, value)
return
dikt = patchable_builtin(klass)
old_value = dikt.get(attr, None)
old_name = '_c_%s' % attr # do not use .format here, it breaks py2.{5,6}
# Patch the thing
dikt[attr] = value
if old_value:
hide_from_dir = False # It was already in dir
dikt[old_name] = old_value
try:
dikt[attr].__name__ = old_value.__name__
except (AttributeError, TypeError): # py2.5 will raise `TypeError`
pass
try:
dikt[attr].__qualname__ = old_value.__qualname__
except AttributeError:
pass
ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass))
if hide_from_dir:
__hidden_elements__[klass.__name__].append(attr)
def reverse(klass, attr):
"""Reverse a curse in a built-in object
This function removes *new* attributes. It's actually possible to remove
any kind of attribute from any built-in class, but just DON'T DO IT :)
Good:
>>> curse(str, "blah", "bleh")
>>> assert "blah" in dir(str)
>>> reverse(str, "blah")
>>> assert "blah" not in dir(str)
Bad:
>>> reverse(str, "strip")
>>> " blah ".strip()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'strip'
"""
if _is_dunder(attr):
_revert_special(klass, attr)
return
dikt = patchable_builtin(klass)
del dikt[attr]
ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass))
def curses(klass, name):
"""Decorator to add decorated method named `name` the class `klass`
So you can use it like this:
>>> @curses(dict, 'banner')
... def dict_banner(self):
... l = len(self)
... print('This dict has {0} element{1}'.format(
... l, l is 1 and '' or 's')
>>> {'a': 1, 'b': 2}.banner()
'This dict has 2 elements'
"""
def wrapper(func):
curse(klass, name, func)
return func
return wrapper
@contextmanager
def cursed(obj, attr, val, hide_from_dir=False):
curse(obj, attr, val, hide_from_dir)
try:
yield
finally:
reverse(obj, attr)
|