Windows: How to avoid: “ZipFile instance has no attribute '__exit__''” with module zipfile? --- use version 1.1.0 (includes Jython 2.7)

Asked by george munteanu

***************** workaround/alternative *************
after 2015-10-02 this new feature is available:

unzip(sourceFile, targetFolder)

unzips the content to targetFolder while preserving a zipped folder structure
 (implemented using the Java builtin support for zip-files)

... also unzips jar files

------------------------------------------------------------------------------------------------------------

[1]
The structure is: .bat file triggers a .py file
The bat file reads: start /i /b /wait C:\Sikuli\Downloads\runIDE.cmd -r "C:\automat\end2end.sikuli"
The code inside .py is :
import zipfile
with zipfile.ZipFile('test.jar', 'r') as z:
    z.extractall(r"C:\testfolder")

This code produces:
AttributeError ( ZipFile instance has no attribute '__exit__' )

[2]
note: The code from "test.py" works when run from python idle. I am running python v2.7.10
note: Other methods (that i tried) from zip module do not return any issues.

[3]
Full stack trace:
Exception in thread "main" java.lang.IllegalStateException: No match found
        at java.util.regex.Matcher.group(Matcher.java:485)
        at org.sikuli.scriptrunner.JythonScriptRunner.findErrorSourceWalkTrace(JythonScriptRunner.ja
va:326)
        at org.sikuli.scriptrunner.JythonScriptRunner.findErrorSource(JythonScriptRunner.java:290)
        at org.sikuli.scriptrunner.JythonScriptRunner.runPython(JythonScriptRunner.java:200)
        at org.sikuli.scriptrunner.JythonScriptRunner.runScript(JythonScriptRunner.java:162)
        at org.sikuli.basics.SikuliScript.main(SikuliScript.java:181)
        at org.sikuli.ide.SikuliIDE.main(SikuliIDE.java:212)

@ edit :
hmm. I believed I found the cause. when running "import sys print sys.version" using the structure mentioned above I get: 2.5.4rc1 (2.5:723492dbab02, Feb 8 2013, 10:13:55) . In python default idle I get: 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]

Therefore, please tell me, how can I get sikuli to recognize 2.7.10 ?

Take care

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

SikuliX will never be able to "recognize" any Python interpreter (C-Python).
SikuliX Python scripting internally uses the Jython interpreter (Java based), to run the scripts.

Apparently you do not use version 1.1.0, which come with the Jython version 2.7, which at least is more compatible in most aspects to Python 2.7.

Nevertheless:
AttributeError ( ZipFile instance has no attribute '__exit__' )
simply means, that you cannot use with here.

try:
z = zipfile.ZipFile('test.jar', 'r')
z.extractall(r"C:\testfolder")

Revision history for this message
george munteanu (georgetemenos) said :
#2

Hi,

I have tried:
z = zipfile.ZipFile('test.jar', 'r')
z.extractall(r"C:\testfolder")
This returns: ( ZipFile instance has no attribute '__extractall__' )

That's I am running now sikuli x 1.1.0.
But if I use: extractall, the system returns:
line: module ( function ) statement
1059: zipfile ( _extract_member ) TypeError: character mapping must return integer, None or unicode

Solution: I have altered: zipfile.py inside sikulix.jar.
At line 1057, 1058, 1059 I have written:
 table = string.maketrans(illegal, '_' * len(illegal))
 table = table.decode("latin-1")
 arcname = str(arcname.translate(table))

Maybe this will help others.
Take care

Revision history for this message
RaiMan (raimund-hocke) said :
#3

@George @comment #2

Your hack in the Jython module zipfile might have helped in your situation with the jar file: I could reproduce the problem on Windows 10 with the SikuliX jars and your solution worked in these cases.

But after unpacking such a critical jar with 7Zip and re-zipping it with 7Zip, the problem was gone and the unmodified zipfile module did the job.
But now with your modification, it did not work anymore with this re-zipped jar.

Conclusion:
- whether or not you get the decode problem with Jython 2.7 depends on the way, the jar was created.
- in cases, where you get the problem, you might either try your hack or use a utility run from inside the script

... or use the new feature
unzip(sourceFile, targetFolder)
available with tomorrows build.
A zip(sourceFolder, targetFile) will follow the next days.
(it is implemented using the Java builtin support for zip-files)