Skip to content

polyfills

Functions:

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 = {
    "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 = {}

tp_as_name module-attribute

tp_as_name = as_number[0]

tp_func_dict module-attribute

tp_func_dict = {}

PyAsyncMethods

Bases: Structure

PyFile

Bases: Structure

PyMappingMethods

Bases: Structure

PyNumberMethods

Bases: Structure

PyObject

Bases: Structure

Methods:

decref

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

incref

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

PySequenceMethods

Bases: Structure

PyTypeObject

Bases: Structure

curse

curse(klass: Any, attr: Any, func: Any) -> None
Source code
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
def curse(klass: Any, attr: Any, func: Any) -> None:
    assert callable(func)

    @wraps(func)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        try:
            if _to_patch:
                return func(*map(_try_cast(klass), args), **kwargs)
            return func(*args, **kwargs)
        except NotImplementedError:
            return NotImplementedRet

    tp_as_name, impl_method = override_dict[attr]

    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:
            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]

        for fname, ftype in struct_ty._fields_:  # type: ignore[misc]
            if fname == impl_method:
                cfunc_t = ftype

        cfunc = cfunc_t(wrapper)
        tp_func_dict[(klass, attr)] = cfunc

        setattr(tp_as, impl_method, cfunc)
    else:
        for fname, ftype in PyTypeObject._fields_:  # type: ignore[misc]
            if fname == impl_method:
                cfunc_t = ftype

        if (klass, attr) not in tp_as_dict:
            tp_as_dict[(klass, attr)] = ctypes.cast(getattr(tyobj, impl_method), cfunc_t)

        cfunc = cfunc_t(wrapper)
        tp_func_dict[(klass, attr)] = cfunc
        setattr(tyobj, impl_method, cfunc)

disable_poly

disable_poly() -> None
Source code
385
386
387
388
389
390
391
392
393
394
def disable_poly() -> None:
    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
Source code
373
374
375
376
377
378
379
380
381
382
def enable_poly() -> None:
    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.keys():
        curse(obtype, dunder, getattr(ExprVar, dunder))

    _to_patch = False

get_not_implemented

get_not_implemented() -> Any
Source code
85
86
87
88
89
90
91
92
93
94
95
96
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]

reverse

reverse(klass: Any, attr: Any) -> None
Source code
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
def reverse(klass: Any, attr: Any) -> None:
    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_:  # type: ignore[misc]
                if fname == impl_method:
                    cfunc_t = ftype

            setattr(tp_as, impl_method,
                    ctypes.cast(ctypes.c_void_p(None), cfunc_t))
        else:
            if (klass, attr) not in tp_as_dict:
                return

            cfunc = tp_as_dict[(klass, attr)]
            setattr(tyobj, impl_method, cfunc)