clarete / forbiddenfruit

Patch built-in python objects

Home Page:https://clarete.li/forbiddenfruit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redefining methods on modules causes a segfault

thedrow opened this issue · comments

Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from forbiddenfruit import curse
>>> curse(__builtins__, round, lambda x, y: y)
Segmentation fault (core dumped)

This is not very surprising but there should be a way to hack around it. At least I hope so.

P.S.
This project is great. I haven't found the time to do it myself but I wanted to do so forever. Thank you very much.

Hi, thank you so much for your report!

The problem here is that the curses function is not prepared to deal with modules. We would see exactly the same problem if you were trying to curse anything on os for example.

Probably, raising a TypeError saying that modules are not welcome would be enough, since you can still patch them without cursing them. Forbidden Fruit actually uses this technique to provide the hide_from_dir parameter in the curse function: https://github.com/clarete/forbiddenfruit/blob/master/forbiddenfruit/__init__.py#L64

What do you think?

My preference would be for the simplest API possible, so a caller doesn't need different behavior to change a module's attributes or a class's. That is, to have curse() check wether the given klass is really a module, and if so, patching it the simpler way hide_from_dir does. One-stop shopping!

That makes a lot of sense and I have the tests! :)

But how do we make sure this is done globally?