Skip to content

polyfills

Functions:

  • disable_poly

    Internal function to disable builtin methods override.

  • enable_poly

    Internal function to enable builtin methods override.

BinaryFunc_p module-attribute

FILE_p module-attribute

FILE_p = POINTER(PyFile)

Inquiry_p module-attribute

Inquiry_p = CFUNCTYPE(c_int, PyObject_p)

LenFunc_p module-attribute

NotImplementedRet module-attribute

NotImplementedRet = get_not_implemented()

ObjObjProc_p module-attribute

PyObject_p module-attribute

PyObject_p = py_object

PyTypeObject_as_types_dict module-attribute

PyTypeObject_as_types_dict: dict[str, type[Structure]] = {
    "tp_as_async": PyAsyncMethods,
    "tp_as_number": PyNumberMethods,
    "tp_as_sequence": PySequenceMethods,
    "tp_as_mapping": PyMappingMethods,
}

Py_ssize_t module-attribute

Py_ssize_t = c_int64 if sizeof(c_void_p) == 8 else c_int32

SSizeArgFunc_p module-attribute

SSizeObjArgProc_p module-attribute

TernaryFunc_p module-attribute

UnaryFunc_p module-attribute

UnaryFunc_p = CFUNCTYPE(py_object, PyObject_p)

as_number module-attribute

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"),
    ],
)

builtin_methods module-attribute

builtin_methods = {
    (ob_type, dunder): (copy_func(getattr(ob_type, dunder)))
    for dunder in dunder_methods
    for ob_type in ob_types
    if dunder in dir(ob_type)
}

dunder_methods module-attribute

dunder_methods = [
    "__add__",
    "__iadd__",
    "__sub__",
    "__isub__",
    "__mul__",
    "__imul__",
    "__floordiv__",
    "__ifloordiv__",
    "__pow__",
    "__exp__",
    "__log__",
    "__sqrt__",
    "__neg__",
    "__pos__",
    "__invert__",
    "__int__",
    "__float__",
    "__abs__",
    "__mod__",
    "__and__",
    "__or__",
    "__xor__",
]

ob_types module-attribute

ob_types = (float,)

override_dict module-attribute

override_dict = {}

substitutions module-attribute

substitutions = {'builtins': _builtins, 'math': _math}

tp_as_dict module-attribute

tp_as_dict = dict[tuple[Any, Any], Any]()

tp_as_name module-attribute

tp_as_name = as_number[0]

tp_func_dict module-attribute

tp_func_dict = dict[tuple[Any, Any], Any]()

PyAsyncMethods

Bases: Structure

PyFile

Bases: Structure

PyMappingMethods

Bases: Structure

PyNumberMethods

Bases: Structure

PyObject

Bases: Structure

Methods:

decref

decref() -> None
Source code
61
62
def decref(self) -> None:
    self.ob_refcnt -= 1

incref

incref() -> None
Source code
58
59
def incref(self) -> None:
    self.ob_refcnt += 1

PySequenceMethods

Bases: Structure

PyTypeObject

Bases: Structure

disable_poly

disable_poly() -> None

Internal function to disable builtin methods override.

Source code
375
376
377
378
379
380
381
382
383
384
385
386
387
def disable_poly() -> None:
    """
    Internal function to disable builtin methods override.
    """
    global _to_patch

    for k, v in substitutions.items():
        eval(k).__dict__.update(**{k: v[0] for k, v in v.items()})

    for (obtype, dunder), dunfunc in builtin_methods.items():
        _curse(obtype, dunder, dunfunc)

    _to_patch = True

enable_poly

enable_poly() -> None

Internal function to enable builtin methods override.

Source code
360
361
362
363
364
365
366
367
368
369
370
371
372
def enable_poly() -> None:
    """
    Internal function to enable builtin methods override.
    """
    global _to_patch

    for k, v in substitutions.items():
        eval(k).__dict__.update(**{k: v[1] for k, v in v.items()})

    for obtype, dunder in builtin_methods:
        _curse(obtype, dunder, getattr(ExprVar, dunder))

    _to_patch = False

get_not_implemented

get_not_implemented() -> Any
Source code
84
85
86
87
88
89
90
def get_not_implemented() -> Any:
    namespace = dict[Any, Any]()
    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]