Skip to content

This is a terrible module made to do terrible things

Notifications You must be signed in to change notification settings

chilaxan/py_future

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

I wrote this python module to experiment with how CPython handles internal types. It allows for the live replacement of internal methods with python ones. Several examples are included, including jsdict and iterint.

JavaScript dictionaries

Makes python dictionary values settable via attributes

Example Usage
from py_future import jsdict

x = {}

x.a = 'whatever'

print(x) # {'a':'whatever'}
print(x.a) # whatever

del x.a

print(x) # {}
Implementation
# py_future/jsdict.py

from . import _utils as utils

@utils.edit(dict, 'tp_getattro')
@utils.nullwrap
def dict_getattro(self, key):
    try:
        return dict.__getattribute__(self, key)
    except AttributeError as attr_err:
        err = attr_err
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            pass
    raise err

@utils.edit(dict, 'tp_setattro')
@utils.nullwrap
def dict_setattro(self, key, val) -> 'c_int':
    if val == utils.Null:
        del self[key]
        return 0
    dict.__setitem__(self, key, val)
    return 0

Iterable Integers (PEP 276)

Makes integers iterable

Example Usage
from py_future import iterint

for i in 10:
  print(i)

# prints 0 to 9
Implementation
# py_future/iterint.py

from . import _utils as utils

@utils.edit(int, 'tp_iter')
@utils.nullwrap
def int_iter(self):
    yield from range(self)

About

This is a terrible module made to do terrible things

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages