Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkey patch / decorate extension types #7

Open
sdvillal opened this issue Jul 29, 2015 · 1 comment
Open

Monkey patch / decorate extension types #7

sdvillal opened this issue Jul 29, 2015 · 1 comment

Comments

@sdvillal
Copy link
Owner

Sometimes it can be useful to add a what method to an extension type. I would recommend in these cases to be clean and create a whatable function/class that delegates the work to the function in C-land.

However, it might be useful to allow monkey-patching extension types. Some info here and here.

forbiddenfruit is a small tool to nastily monkey-patch any extension type (including builtins). It is released in pypi GPL licensed, although whatami can use it under the MIT license. I have been playing with it and works great, so it can be a neat addition together with the ability to specify custom what functions to the whatable decorator (in any case introspection won't work with these kind of objects). These few lines in whatable made the trick:

if inspect.isclass(obj):
    try:
        obj.what = whatablefunc
    except:
        from forbiddenfruit import curse
        curse(obj, 'what', whatablefunc)
        print('WARNING, patched builtin/extension type %s' % type(obj))
       # This will anyway fail for extension types 
       # (can we make forbiddenfruit work with instances?)

Also add a like like this to optional dependencies in setup.py:

           'ubermonkey': ('forbiddenfruit',),

Note: the comments on the forbiddenfruit issue tracker are an interesting read.

@sdvillal
Copy link
Owner Author

It does not seem to be a good idea. Testing at 2b29f5a showed very unstable interpreter and random crashes after enacting forbiddenfruit. Will assess alternative ways (e.g. dynamically create mixins inheriting from extension types and adding the what method).

@sdvillal sdvillal changed the title Use forbiddenfruit to monkey patch extension types Monkey patch / decorate extension types Aug 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant