From: Paul D. F. <pdf...@ku...> - 2007-11-03 12:49:11
|
I'm trying to have Jython import modules directly from a fine-grained versioned repository of source code and content, similar in some ways to IBM/OTI's ENVY/Developer for Smalltalk and Java (but more generalized). A sample screenshot of an (unreleased yet) Jython version of the code I'm working on is here: http://sourceforge.net/project/screenshots.php?group_id=6010 For some reason I thought Jython 2.2 supported import hooks. http://fwierzbicki.blogspot.com/2007/08/jython-22-released-woohoo.html "PEP 302 implementation" http://www.python.org/dev/peps/pep-0302/ "New Import Hooks" But when I try this: import sys print sys.version import ihooks I get: 2.2 Exception in thread "AWT-EventQueue-0" Traceback (innermost last): File "/home/pdf/workspace/pointrel20071003/browser.py", line 120, in runPressed File "<string>", line 3, in ? ImportError: no module named ihooks I tried copying over a CPython2.4 ihooks.py file, but it has CPython specific stuff and does not load. When I removed some of the CPython specific stuff, it loaded, but when I ran it with this example: http://mail.python.org/pipermail/python-list/2001-May/086186.html the code installed OK,, but on doing a new import, I then got CPython functions missing (weirdly prefixed os.path references). Exception in thread "AWT-EventQueue-0" Traceback (innermost last): File "/home/pdf/workspace/pointrel20071003/browser.py", line 120, in runPressed File "<string>", line 2, in ? File "/home/pdf/workspace/pointrel20071003/ihooks_from_cpython.py", line 408, in import_module File "/home/pdf/workspace/pointrel20071003/ihooks_from_cpython.py", line 461, in load_tail ImportError: No module named org.python.modules.os.path But when I do: import os print os.path I get: <module 'javapath' from '/home/pdf/workspace/PROJECT/jython2.2/Lib/javapath.py'> Rather than go further, I decided to stop and ask. I don't know much about import hooks, so perhaps I'm just missing something obvious before I reinvent the wheel. The only direct reference I found in recent jython-dev emails is ihooks in the "List of missing modules" from a post on 2007-01-18. On the jython-users list on 2007-08-30, "Separated .class and .py directories", dperez links to an import hook, but it was written in Java. """ The patch is here (fully documented): http://sourceforge.net/tracker/index.php?func=detail&aid=1784696&group_id=12867&atid=312867 """ The file ihooks.py did not seem to be on SourceForge in the libraries, http://jython.svn.sourceforge.net/viewvc/jython/trunk/jython/Lib/ so I was not sure if I should copy a CPython version over. (I still feel it would be nice if all the supported lib modules were there and not copied from CPython during builds, to avoid such uncertainty.) Anyway, does Jython2.2 just support import hooks in general but not the ihooks module? Can I do an import hook written in Jython somehow just not using the ihooks module? Or do I need to make a Java class like dperez did? If I want to use the ihooks module, would I need to continue hacking away until I make a module that works? In general Jython2.2 is a great advance and I appreciate all the hard work that went into it. Just wondering how to proceed. --Paul Fernhout |
From: Paul D. F. <pdf...@ku...> - 2007-11-04 23:45:48
|
To answer my own question, under Jython2.2 the following code works to set an import hook, so all you really need is the "imp" module and the "__builtin__" module, not "ihooks": """ import imp # resources: # http://wiki.python.org/moin/ImpModule # http://docs.python.org/lib/module-imp.html #http://groups.google.com/group/comp.lang.python/browse_thread/thread/fdafee093d2e1d09/3efc3ceb5849adf7?hl=en&lnk=st&q=python+%22imp+module%22#3efc3ceb5849adf7 import __builtin__ oldimport = __builtin__.__import__ def mycode(name, globals=None, locals=None, fromlist=None): print "MY IMPORT!", name return oldimport(name, globals, locals, fromlist) __builtin__.__import__ = mycode import java print "done" """ Output: """" run pressed === run code === MY IMPORT! java done === done === run pressed === run code === MY IMPORT! imp MY IMPORT! __builtin__ MY IMPORT! java MY IMPORT! java done === done === run pressed === run code === MY IMPORT! imp MY IMPORT! imp MY IMPORT! __builtin__ MY IMPORT! __builtin__ MY IMPORT! java MY IMPORT! java MY IMPORT! java done === done === run pressed === run code === MY IMPORT! imp MY IMPORT! imp MY IMPORT! imp MY IMPORT! __builtin__ MY IMPORT! __builtin__ MY IMPORT! __builtin__ MY IMPORT! java MY IMPORT! java MY IMPORT! java MY IMPORT! java done === done === """ Every time it is run (from within a host GUI) it adds the same new "mycode" importer to the chain, which ends up calling itself recursively -- something I need to fix (probably by just doing it once!). And I don't quite understand the increasing numbers of imports of imp and __builtin__ each time I eval the code -- once I can understand, but why more, I don't know yet. Might have something to do with the rest of my evaluation environment? Well, unimportant for now, but a curiosity. :-) I don't actually use the "imp" module yet, but presume I will have to at some point to manage compiled code and really make my new import hook do stuff properly? So it looks like the general functionality for overriding what __import__ does is supported in Jython2.2 (maybe even earlier?) but the ihooks module itself is still missing? In my case, fortunately the general "__builtin__" functionality and "imp" functionality is hopefully really all I need for now (I think). If I understand it correctly now, ihooks just lets multiple modifiers of the import mechanism coordinate their hooks in a cooperative fashion, or as the comment there says: "Consistent use of this [ihooks] module will make it possible to change the different mechanisms involved in loading modules independently." Hope this might help someone else understand the basics if they want to do something similar. The comp.lang.python thread referenced above was what pointed me in this direction. --Paul Fernhout Paul D. Fernhout wrote: > Anyway, does Jython2.2 just support import hooks in general but not the > ihooks module? > > Can I do an import hook written in Jython somehow just not using the ihooks > module? Or do I need to make a Java class like dperez did? > > If I want to use the ihooks module, would I need to continue hacking away > until I make a module that works? |
From: Paul D. F. <pdf...@ku...> - 2007-11-05 00:15:46
|
Silly me, the mycode hook is recursively calling down the chain for the imports before the latest hook is set too. So it does import them just once, but reports on it for every previous time the routine was eval'd. So that is why they are reported one less time than the number of java imports (which is done after the latest hook is added to the chain). --Paul Fernhout Paul D. Fernhout wrote: > Every time it is run (from within a host GUI) it adds the same new "mycode" > importer to the chain, which ends up calling itself recursively -- something > I need to fix (probably by just doing it once!). And I don't quite > understand the increasing numbers of imports of imp and __builtin__ each > time I eval the code -- once I can understand, but why more, I don't know > yet. Might have something to do with the rest of my evaluation environment? > Well, unimportant for now, but a curiosity. :-) |