|
From: Charlie G. <cha...@gm...> - 2007-07-23 05:08:03
|
On 7/20/07, David Huebel <dav...@gm...> wrote: > Just out of curiousity, why is add_package needed in Jython? From a > quick glance over the Jython source, it seems to be simply a way of > declaring that a Java package exists. I'm guessing that if it were > safe to call add_package on nonexistent packages, there would be no > need to call it by hand. Jython can't just assume that a package exists because packages show up through the import statement. If it were assumed that items that can't be found in an import are Java packages, all sorts of havoc would break loose if a module that is expected to exist on the path doesn't, or if a typo is made importing a module. For example, if I tried "import BaseHttpServer" instead of actually using the std lib module BaseHTTPServer, Jython would create a java package BaseHttpServer for me. Things would get even weirder if I tried to access attributes on that nonexistent package or call dir on it. Much of this problem is ameliorated for regular Jython usage because the package scanner(http://wiki.python.org/jython/PackageScanning) picks up on all packages available on the classpath at startup and doesn't require calls to add_package. Because you're forced to use a custom classloader by Eclipse, you have to handle add_package on your own. It does seem like it would be possible to eliminate the need for add_package if we auto-created nonexistent packages if a user imports a fully qualified Java class. Since we can ask the classloader if a class really exists, we could use its existence to confirm the existence of the packages that contain it. Those packages would still be odd since they wouldn't be fully populated with things that haven't been imported, but I think that would be better than requiring add_package. Yet another thing for the list of things it would be nice to have in Jython.... Charlie |