Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import * statement failed to work on Jython 2.7.2 #309

Open
Keita2 opened this issue Feb 21, 2024 · 19 comments
Open

Import * statement failed to work on Jython 2.7.2 #309

Keita2 opened this issue Feb 21, 2024 · 19 comments

Comments

@Keita2
Copy link

Keita2 commented Feb 21, 2024

I encountered the issue as below when executing Jython script.

**************************************************
Traceback (most recent call last):
  File "<string>", line 48, in loadProperties
NameError: global name 'Properties' is not defined
**************************************************
WASX7017E: Exception received while running file "deploy.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (most recent call last):
  File "<string>", line 108, in <module>
  File "<string>", line 75, in loadProperties
SystemExit: -1

This error happened after Jython 2.7 upgraded to 2.7.2 , so didn't happen with same script on Jython 2.7.
By modifying import * to specify class , this issue was resolved.

Before:
from java.util import *
from java.util import *
from java.io import *

After :
from java.util import Properties
from java.io import FileInputStream
from java.lang import System

Question
Import * seems to fail to work on Jython V2.7.2 . Is this issue due to bug on Jython 2.7.2 ?

@yogi1967
Copy link

What is the JDK version for both tests? What happens if you revert JDK to earlier version?

@Keita2
Copy link
Author

Keita2 commented Feb 21, 2024

The JDK version is as below.
Jython V2.7 and JDK 8.0.7.16 <-- OK
Jython V2.7.2 and JDK 8.0.8.0 <--NG

@Keita2
Copy link
Author

Keita2 commented Feb 21, 2024

I will check if the issue can be recreated with Jython V2.7.2 and other JDK level.

@yogi1967
Copy link

Ah ok. I saw a similar issue for JDK21. So I doubt it's the same issue...

@Keita2
Copy link
Author

Keita2 commented Feb 22, 2024

I confirmed the issue is recreated with older version.
So, this issue is assumed due to Jython upgrade.
Jython V2.7.2 and JDK 8.0.7.20 <--NG
Jython V2.7.2 and JDK 8.0.6.31 <--NG

Let me add information about JDK. I use IBM Java .

@robertpatrick
Copy link

@jeff5 assuming this worked in 2.7 and before, this seems like a bug.

@jeff5
Copy link
Member

jeff5 commented Feb 25, 2024

import just goes on giving (#105, #304). However, I'm not able to reproduce this (on Windows) with:

Jython 2.7.2 (v2.7.2:925a3cc3b49d, Mar 21 2020, 10:03:58)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_321
Type "help", "copyright", "credits" or "license" for more information.
>>> globals().keys()
['__builtins__', '__doc__', '__name__', '__package__']
>>> from java.util import *
>>> from java.io import *
>>> from java.lang import *
>>> len(globals().keys())
300
>>> Properties
<type 'java.util.Properties'>
>>> System
<type 'java.lang.System'>
>>> FileInputStream
<type 'java.io.FileInputStream'>

It's the same in the development tip locally.

@Keita2
Copy link
Author

Keita2 commented Feb 28, 2024

@jeff5 On Mac, this can be recreated as follows.
But I am not able to recreate on Windows.
Could you try to recreate on Unix environment ?

Mac

% java -jar jython.jar
Jython 2.7.3 (tags/v2.7.3:5f29801fe, Sep 10 2022, 18:52:49)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_333
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.util import *
>>> prop = Properties()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Properties' is not defined
>>> from java.util import Properties
>>> prop = Properties()
>>> 

Windows

C:\jython2.7.3>java -jar jython.jar
Jython 2.7.3 (tags/v2.7.3:5f29801fe, Sep 10 2022, 18:52:49)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_371
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.util import *
>>> prop = Properties()
>>>

@Keita2
Copy link
Author

Keita2 commented Feb 29, 2024

@jeff5 I also recreated on Windows.
On standalone release, this can be recreated.
Could you analyze why different behavior can be found ?

Stanalone

C:\temp>java -jar jython-standalone-2.7.2.jar
Jython 2.7.2 (v2.7.2:925a3cc3b49d, Mar 21 2020, 10:03:58)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_371
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.util import *
>>> prop = Properties()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Properties' is not defined
>>> from java.util import Properties
>>> prop = Properties()
>>> 

Installer

C:\jython2.7.2>java -jar jython.jar
Jython 2.7.2 (v2.7.2:925a3cc3b49d, Mar 21 2020, 10:03:58)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_371
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.util import *
>>> prop = Properties()
>>> 

@Keita2
Copy link
Author

Keita2 commented Mar 21, 2024

Hello are there any updates ?

@ohumbel
Copy link

ohumbel commented Mar 21, 2024

@jeff5 I also recreated on Windows. On standalone release, this can be recreated. Could you analyze why different behavior can be found ?

Standalone mode was deliberately designed to not support java imports of the form from a.package import *: Package scanning is disabled in standalone mode.

The reason for this are application containers like WildFly, Payara, Websphere and other more complex java applications. Each of those has its own layout and handling of the class path and class loaders. It would be impossible for jython to completely scan the whole class path for packages. And it would considerably slow down the startup process.

Hope to have helped to spot the difference.

@Stewori
Copy link
Contributor

Stewori commented Mar 21, 2024

@ohumbel Thank you for the clarification. I was somewhat surprised by this limitation; looking at the Package Java API, indeed no method there can list the classes in a given package. Apparently, class loaders do not provide this functionality and workarounds are expensive.
In any case, Jython 2.x is not the place for revisiting established behavior and for Jython 3 there might not even be a standalone. So, we could perhaps look into improving the error message, i.e. ideally Jython would tell the user something like "Wildcard imports are not supported in the standalone build.". And it should be mentioned prominently on the user guide (I can't remember to have ever heard of this limitation; maybe I overlooked it).

@Keita2
Copy link
Author

Keita2 commented Apr 5, 2024

Is it correct that error message will be improved and user guide will be also updated ?

@Stewori
Copy link
Contributor

Stewori commented Apr 5, 2024

No promise from my side, but I have loose plans to clean up the user guide and then I would include this bit. Fixing the error is rather low priority and everyone is busy :) However I think this issue will stay open until it is resolved eventually. If you want to make a PR, that would be very welcome. @jeff5: It would be good if you could confirm the resolution (i.e. via improving the error message)(, or suggest an alternative).

@jeff5
Copy link
Member

jeff5 commented Apr 6, 2024

I second Stefan's thanks @ohumbel, that's a really useful insight. And "standalone" was what I was missing earlier.

The way that Python packages differ from Java's concept is a continual source of surprises: superficially similar, then not quite the same thing. In some ways, Java's concept is more robust: it will look for what you explicitly request, and in as many remote corners as it has class loaders for, but it won't enumerate what is available.

Java packages are somewhat like Python namepace packages, in having multiple loci, but those are confined to the file system.

It is interesting to see what you can get from a star import:

PS 274b1-trial> java -jar kit\jython-standalone.jar
Jython 2.7.4b1 (tags/v2.7.4b1:228fe9ef9, Apr 1 2024, 20:04:13)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java11.0.22
Type "help", "copyright", "credits" or "license" for more information.
>>> globals().keys()
['__builtins__', '__doc__', '__name__', '__package__']
>>> from java.util import *
>>> globals().keys()
['Set', '__name__', '__builtins__', '__doc__', 'List', 'Collections', 'Map', '__package__', 'WeakHashMap']

I assume this is because some other code already imported those classes explicitly to Python's idea of java.util.

Star imports are discouraged in Python, and I wonder if they should not simply be an error when the package is a Java one. (Not in 2.7, probably.)

@Keita2
Copy link
Author

Keita2 commented Apr 8, 2024

@Stewori
Thank you for reply.
@jeff5
I am afraid, but so would you have a plan to fix the code so that not error but other kind of message is issued when the package is a Java one ?

@jeff5
Copy link
Member

jeff5 commented Apr 9, 2024

... so would you have a plan to fix the code so that not error but other kind of message is issued when the package is a Java one ?

Are you suggesting a warning in this case that an import * may not have imported everything in the Java package?

@Keita2
Copy link
Author

Keita2 commented Apr 9, 2024

Yes, I would like to make the request.

@yogi1967
Copy link

Very odd.. Import * Works for me on 2.7.2 (Java21) on Mac, but NOT on Windows....?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants