A follow up on the last message, reading the
class file in a loop eventually reads it completely,
so it appears to be a bug with SyspathJavaLoader.
This would be better code to read from the archive:
byte[] buffer = new byte[size];
int nread=0;
while (nread<size) {
nread+=fis.read(buffer,nread,size-nread);
}
fis.close();
I haven't been keeping track of the latest jython code, so I
don't know whether SyspathJavaLoader has been improved since the
2.1 release. Perhaps you could check and submit a patch.
Fabio Zadrozny wrote:
> Hi, this is not really a syntax error...
>
> actually, this import below is just an example (that I think should
> work), were I really have the problem is something much more dynamic,
> where I do the import of the module dynamically and then, do a dir in
> the found module / class to get the attributes, and then get the
> attribute to discover info on it, such as if it is a class or a method,
> etc.
>
> So, I call something like Find('junit.framework.TestCase') -- see the
> code below -- and it should return the class definition, but what
> happens is the traceback (just below), because I can import the package
> junit.framework, but I cannot do getattr(mod, 'TestCase') in it (which
> is weird, because if I do a dir in it, it appears there...). Also, the
> same code works without any problems in python, and works if I do add it
> to the classpath before, but not if I add it only to the sys.path (and
> that's the only way I see to do it if I want to use jython for it,
> otherwise, I would have to do a custom classloader myself...
> --------------------------
> Traceback (innermost last):
> File
> "D:\dev_programs\eclipse_3\eclipse\workspace\org.python.pydev\PySrc\jyimportsTipper.py",
> line 317, in ?
> File
> "D:\dev_programs\eclipse_3\eclipse\workspace\org.python.pydev\PySrc\jyimportsTipper.py",
> line 43, in Find
> AttributeError: java package 'junit.framework' has no attribute 'TestCase'
> ---------------------------
> def _imp(name):
> try:
> return __import__(name)
> except:
> if '.' in name:
> sub = name[0:name.rfind('.')]
> return _imp(sub)
> else:
> s = 'Unable to import module: %s - sys.path: %s' %
> (str(name), sys.path)
> raise RuntimeError(s)
>
> def Find( name ):
> mod = _imp(name)
> components = name.split('.')
>
> for comp in components[1:]:
> mod = getattr(mod, comp)
> return mod
>
>
> Thanks,
>
> Fabio
>
>
> Jeff Emanuel wrote:
>
>> Your import syntax is incorrect. import
>> similar but different from Java import. If you
>> want TestCase available without the full package
>> name, then import it like this:
>>
>> from junit.framework import TestCase
>> tc=TestCase()
>>
>> If you want to use TestCase with full qualification,
>> then you need only import junit:
>>
>> import junit
>> tc=junit.framework.TestCase()
>>
>> You can go halfway, too:
>>
>> from junit import framework
>> tc=framework.TestCase()
>>
>>
>>
>> Fabio Zadrozny wrote:
>>
>>> Hi, I'm having some problems with jython not finding my classes if
>>> they are not defined in the classpath, but just later added to the
>>> sys.path. In my case, I do not have the information before the run. To
>>> give an example of what happens:
>>>
>>> import sys
>>> sys.path.insert(1, r"C:\bin\eclipse310\plugins
>>> \org.junit_3.8.1\junit.jar" )
>>>
>>> import junit.framework
>>> print dir(junit.framework) #shows the TestCase class here
>>>
>>> import junit.framework.TestCase
>>>
>>> raises the error:
>>> Traceback (innermost last):
>>> File "<console>", line 1, in ?
>>> ImportError: No module named TestCase
>>>
>>> if I set the classpath to point to the jar above before the run, it
>>> works. The problem seems to be the classloader jython is using, as it
>>> seems it does not process new entries to the sys.path (only seems to
>>> process those that are added to the classpath at the start of the
>>> program).
>>>
>>> (I did this tests with jython 2.1, so, if it is already fixed, please
>>> let me know)
>>>
>>> So, is this correct? Are there any plans to have it fixed?
>>>
>>> And the really strange part (for me)... why does the 'dir' above bring
>>> me the correct things, and when I actually go for it, I get an error?
>>>
>>> Thanks,
>>>
>>> Fabio
>>>
>>
>>
>
>
|