nedbat/byterun

CPython used for imported modules rather than byterun

andychu opened this issue · 5 comments

Thanks for this great project -- I'm learning a lot from it. I'm using byterun as a test of a bigger project, and I found and fixed a bug:

oils-for-unix/oils@62cd492

There are some irrelevant diffs there, but if you search for BUG FIX you will see this part. The core issue is that the MAKE_FUNCTION bytecode is not called if you do __import__. byterun uses the host __import__, which results in a native function, not a pyobj.Function. I also changed __repr__ of Function to make this more apparent.

Also of interest might be the speed tests at the end. I made two files: speed_main.py and speed.py. You will see that under byterun, using a function in library is much faster than using one in the same module, because it gets executed with CPython.

+ if isinstance(func, types.FunctionType):
+            defaults = func.func_defaults or ()
+            byterun_func = Function(
+                    func.func_name, func.func_code, func.func_globals,
+                    defaults, func.func_closure, self)
+        else:
+            byterun_func = func

If you are interested in a pull request, let me know. However I am highly confused about how you run byterun/__main__.py directly from the git repo with its relative imports? (and this is after 13 years of programming in Python, and being somewhat responsible for __main__.py beiing added in Python 2.5 or 2.6).

I guess you must either install it on the system or use some kind of wrapper? I think it is related to this:

http://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py

I had to write a shell script wrapper than changed to the directory and used python -m byterun.__main__. Somehow changing PYTHONPATH didn't work.

I also just ran into this when trying to run byterun with itself. byterun.main is only imported once, then cached, so run_fn is only called the first time.

it doesn't sound too hard to have a Module class, and replace import with a call to execfile or something similar.. is there a reason this hasn't been implemented yet?

I would guess that if you want it implemented you should send a patch yourself.

sure, but before I spend lots of time on such a thing, it would be nice to know if there is a particular reason for it not being implemented yet..

Hi @srepmub and @andychu - thanks for the bug report! No, there’s not a good reason why this isn’t implemented yet, and a patch is welcome.

OK as far as I know the code I linked/quoted above fixes it. (I stopped working with this code some months ago and didn't get a response, so I didn't submit a patch.)