From: leon j. b. <lj...@ne...> - 2002-02-06 13:09:17
|
hi, i need to make all the classes in a specific package available to Jython script code without the code having to qualify the class names. this is what i currently do to do the import: /** * @see ScriptingEngine#importClass(String, Object) * @since 1.1.0 */ public void importClass(String name, Object namespace) throws ScriptingException { namespace = namespace == null ? module.__dict__ : namespace; if (!(namespace instanceof PyStringMap)) { throw new ScriptingException("Namespace is not a valid Python namespace"); } try { __builtin__.__import__(name, module.__dict__, (PyStringMap)namespace); } catch (PyException e) { throw new ScriptingException(e, "Could not import \"" + name + "\""); } } and this gets invoked after the namespace for the script has been created. this is how scripts are compiled: /** * @see ScriptingEngine#compile(String) * @since 1.1.0 */ public Object compile(String code) throws ScriptingException { try { PyCode cobj = Py.compile_flags( code, STRING_FILE_NAME, TYPE_EXEC, null); return cobj; } catch (PyException e) { throw new ScriptingException(e, "Could not compile Python code"); } } and this is how they are run: /** * @see ScriptingEngine#run(Object, Object) * @since 1.1.0 */ public Object run(Object obj, Object namespace) throws ScriptingException { if (!(obj instanceof PyCode)) { throw new ScriptingException("Object is not compiled Python code"); } if ((namespace != null) && (!(namespace instanceof PyStringMap))) { throw new ScriptingException("Namespace is not a valid Python namespace"); } try { namespace = namespace == null ? module.__dict__ : namespace; return Py.runCode((PyCode)obj, (PyStringMap)namespace, module.__dict__); } catch (PyException e) { throw new ScriptingException(e, "Could not run Python code"); } } the namespace passed to importClass() is the same as the namespace the run() method takes. but i still get NameErrors in the actual scripts. what is the programmatic (in Java) equivalent of "from package import *"? thanks, leon. -- lj...@ne... :: +27.82.7890445 >> don't play in the kiln. |