From: <pj...@us...> - 2008-11-27 20:56:43
|
Revision: 5648 http://jython.svn.sourceforge.net/jython/?rev=5648&view=rev Author: pjenvey Date: 2008-11-27 20:56:40 +0000 (Thu, 27 Nov 2008) Log Message: ----------- small cleanup, match CPython's __repr__ Modified Paths: -------------- trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2008-11-27 20:56:10 UTC (rev 5647) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2008-11-27 20:56:40 UTC (rev 5648) @@ -50,12 +50,12 @@ "a zipfile. ZipImportError is raised if 'archivepath' doesn't point to\n" + "a valid Zip archive."); - /** zip_searchorder defines how we search for a module in the Zip + /** zipSearchOrder defines how we search for a module in the Zip * archive */ static enum EntryType { IS_SOURCE, IS_BYTECODE, IS_PACKAGE }; - static final SearchOrderEntry[] zip_searchorder = new SearchOrderEntry[] { + static final SearchOrderEntry[] zipSearchOrder = new SearchOrderEntry[] { new SearchOrderEntry(File.separator + "__init__$py.class", EnumSet.of(EntryType.IS_PACKAGE, EntryType.IS_BYTECODE)), new SearchOrderEntry(File.separator + "__init__.py", @@ -126,9 +126,7 @@ break; } - String childFile = pathFile.getPath(); - prefix = childFile.substring(childFile.lastIndexOf(File.separator) + 1) - + File.separator + prefix; + prefix = pathFile.getName() + File.separator + prefix; pathFile = parentFile; } @@ -364,8 +362,7 @@ private ModuleInfo getModuleInfo(String fullname) { String path = makeFilename(prefix, getSubname(fullname)); - for (int i = 0; i < zip_searchorder.length; i++) { - SearchOrderEntry entry = zip_searchorder[i]; + for (SearchOrderEntry entry : zipSearchOrder) { PyObject tocEntry = files.__finditem__(path + entry.suffix); if (tocEntry == null) continue; @@ -392,8 +389,7 @@ return null; } - for (int i = 0; i < zip_searchorder.length; i++) { - SearchOrderEntry entry = zip_searchorder[i]; + for (SearchOrderEntry entry : zipSearchOrder) { String suffix = entry.suffix; String searchPath = path + suffix; @@ -606,7 +602,12 @@ @ExposedMethod(names = "__repr__") final String zipimporter_toString() { - return "<zipimporter object \"" + archive + "\">"; + String displayArchive = archive != null ? archive : "???"; + if (prefix != null && !"".equals(prefix)) { + return String.format("<zipimporter object \"%.300s%c%.150s\">", + displayArchive, File.separatorChar, prefix); + } + return String.format("<zipimporter object \"%.300s\">", displayArchive); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-12-11 00:10:27
|
Revision: 5733 http://jython.svn.sourceforge.net/jython/?rev=5733&view=rev Author: pjenvey Date: 2008-12-11 00:10:25 +0000 (Thu, 11 Dec 2008) Log Message: ----------- fail fast when path exists but isn't a normal file suggested by Ethan Glasser-Camp fixes #1199 Modified Paths: -------------- trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2008-12-10 08:23:53 UTC (rev 5732) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2008-12-11 00:10:25 UTC (rev 5733) @@ -115,8 +115,10 @@ prefix = ""; while (true) { File fullPathFile = new File(sys.getPath(pathFile.getPath())); - if (fullPathFile.isFile()) { - archive = pathFile.getPath(); + if (fullPathFile.exists()) { + if (fullPathFile.isFile()) { + archive = pathFile.getPath(); + } break; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-13 23:23:09
|
Revision: 5925 http://jython.svn.sourceforge.net/jython/?rev=5925&view=rev Author: cgroves Date: 2009-01-13 22:10:24 +0000 (Tue, 13 Jan 2009) Log Message: ----------- Allow importation from bytecode in a zip file even if the source isn't present Modified Paths: -------------- trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-01-13 02:36:27 UTC (rev 5924) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-01-13 22:10:24 UTC (rev 5925) @@ -315,7 +315,7 @@ String sourcePath = path.substring(0, path.length() - 9) + ".py"; PyObject sourceTocEntry = files.__finditem__(sourcePath); if (sourceTocEntry == null) { - return false; + return true;// If there is no source, assume the bytecode is ok } try { long bytecodeTime = dosTimeToEpoch(tocEntry.__finditem__(5).asInt(0), This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-06-06 05:45:24
|
Revision: 6456 http://jython.svn.sourceforge.net/jython/?rev=6456&view=rev Author: pjenvey Date: 2009-06-06 05:45:22 +0000 (Sat, 06 Jun 2009) Log Message: ----------- small cleanup Modified Paths: -------------- trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-06-06 05:24:44 UTC (rev 6455) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-06-06 05:45:22 UTC (rev 6456) @@ -97,6 +97,7 @@ break; } } catch (SecurityException se) { + // continue } // back up one path element @@ -127,10 +128,6 @@ return zipimporter_find_module(fullname, null); } - public PyObject find_module(String fullname, String path) { - return zipimporter_find_module(fullname, path); - } - /** * Find the module for the fully qualified name. * @@ -139,30 +136,30 @@ * @return a loader instance if this importer can load the module, None * otherwise */ + public PyObject find_module(String fullname, String path) { + return zipimporter_find_module(fullname, path); + } + @ExposedMethod(defaults = "null") final PyObject zipimporter_find_module(String fullname, String path) { return importer_find_module(fullname, path); } - public PyObject load_module(String fullname) { - return zipimporter_load_module(fullname); - } - /** * Load a module for the fully qualified name. * * @param fullname the fully qualified name of the module * @return a loaded PyModule */ + public PyObject load_module(String fullname) { + return zipimporter_load_module(fullname); + } + @ExposedMethod final PyObject zipimporter_load_module(String fullname) { return importer_load_module(fullname); } - public String get_data(String path) { - return zipimporter_get_data(path); - } - /** * Return the uncompressed data for the file at the specified path * as a String. @@ -170,6 +167,10 @@ * @param path a String path name within the archive * @return a String of data in binary mode (no CRLF) */ + public String get_data(String path) { + return zipimporter_get_data(path); + } + @ExposedMethod final String zipimporter_get_data(String path) { int len = archive.length(); @@ -186,20 +187,14 @@ byte[] data; try { data = FileUtil.readBytes(zipBundle.inputStream); - } - catch (IOException ioe) { + } catch (IOException ioe) { throw Py.IOError(ioe); - } - finally { + } finally { zipBundle.close(); } return StringUtil.fromBytes(data); } - public boolean is_package(String fullname) { - return zipimporter_is_package(fullname); - } - /** * Return a boolean signifying whether the module is a package or * not. @@ -207,25 +202,29 @@ * @param fullname the fully qualified name of the module * @return a boolean describing if the module is a package */ + public boolean is_package(String fullname) { + return zipimporter_is_package(fullname); + } + @ExposedMethod final boolean zipimporter_is_package(String fullname) { ModuleInfo moduleInfo = getModuleInfo(fullname); if (moduleInfo == ModuleInfo.NOT_FOUND) { - throw zipimport.ZipImportError("can't find module '" + fullname + "'"); + throw zipimport.ZipImportError(String.format("can't find module '%s'", fullname)); } return moduleInfo == ModuleInfo.PACKAGE; } - public PyObject get_code(String fullname) { - return zipimporter_get_code(fullname); - } - /** * Return the code object associated with the module. * * @param fullname the fully qualified name of the module * @return the module's PyCode object or None */ + public PyObject get_code(String fullname) { + return zipimporter_get_code(fullname); + } + @ExposedMethod final PyObject zipimporter_get_code(String fullname) { ModuleCodeData moduleCodeData = getModuleCode(fullname); @@ -235,10 +234,6 @@ return Py.None; } - public String get_source(String fullname) { - return zipimporter_get_source(fullname); - } - /** * Return the source code for the module as a string (using * newline characters for line endings) @@ -246,6 +241,10 @@ * @param fullname the fully qualified name of the module * @return a String of the module's source code or null */ + public String get_source(String fullname) { + return zipimporter_get_source(fullname); + } + @ExposedMethod final String zipimporter_get_source(String fullname) { ModuleInfo moduleInfo = getModuleInfo(fullname); @@ -254,14 +253,13 @@ return null; } if (moduleInfo == ModuleInfo.NOT_FOUND) { - throw zipimport.ZipImportError("can't find module '" + fullname + "'"); + throw zipimport.ZipImportError(String.format("can't find module '%s'", fullname)); } String path = makeFilename(fullname); if (moduleInfo == ModuleInfo.PACKAGE) { path += File.separator + "__init__.py"; - } - else { + } else { path += ".py"; } @@ -282,21 +280,20 @@ * @return a ZipBundle with an InputStream to the file's * uncompressed data */ + @Override public ZipBundle makeBundle(String datapath, PyObject entry) { datapath = datapath.replace(File.separatorChar, '/'); ZipFile zipArchive; try { zipArchive = new ZipFile(new File(sys.getPath(archive))); - } - catch (IOException ioe) { + } catch (IOException ioe) { throw zipimport.ZipImportError("zipimport: can not open file: " + archive); } ZipEntry dataEntry = zipArchive.getEntry(datapath); try { return new ZipBundle(zipArchive, zipArchive.getInputStream(dataEntry)); - } - catch (IOException ioe) { + } catch (IOException ioe) { Py.writeDebug("import", "zipimporter.getDataStream exception: " + ioe.toString()); throw zipimport.ZipImportError("zipimport: can not open file: " + archive); } @@ -306,12 +303,11 @@ * Determine if the byte code at path with the specified toc entry has a modification time * greater than its accompanying source code's. * - * @param path - * a String path to the byte code - * @param tocEntry - * the byte code's PyObject toc entry + * @param path a String path to the byte code + * @param tocEntry the byte code's PyObject toc entry * @return boolean whether or not the byte code is older */ + @Override protected boolean isAcceptableBytecode(String path, PyObject tocEntry) { String sourcePath = path.substring(0, path.length() - 9) + ".py"; PyObject sourceTocEntry = files.__finditem__(sourcePath); @@ -357,16 +353,15 @@ private PyObject readDirectory(String archive) { File file = new File(sys.getPath(archive)); if (!file.canRead()) { - throw zipimport.ZipImportError("can't open Zip file: '" + archive + "'"); + throw zipimport.ZipImportError(String.format("can't open Zip file: '%s'", archive)); } ZipFile zipFile; try { zipFile = new ZipFile(file); + } catch (IOException ioe) { + throw zipimport.ZipImportError(String.format("can't read Zip file: '%s'", archive)); } - catch (IOException ioe) { - throw zipimport.ZipImportError("can't read Zip file: '" + archive + "'"); - } PyObject files = new PyDictionary(); for (Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); @@ -393,14 +388,14 @@ try { zipFile.close(); - } - catch (IOException ioe) { + } catch (IOException ioe) { throw Py.IOError(ioe); } return files; } + @Override protected String getSeparator() { return File.separator; } @@ -408,12 +403,11 @@ /** * Given a full module name, return the potential file path in the archive (without extension). * - * @param prefix - * a String value - * @param name - * a String modulename value + * @param prefix a String value + * @param name a String modulename value * @return the file path String value */ + @Override protected String makeFilename(String fullname) { return prefix + getSubname(fullname).replace('.', File.separatorChar); } @@ -497,6 +491,7 @@ return d.getTime(); } + @Override public String toString() { return zipimporter_toString(); } @@ -528,11 +523,11 @@ * * Raises an IOError if a problem occurred. */ + @Override public void close() { try { zipFile.close(); - } - catch (IOException ioe) { + } catch (IOException ioe) { throw Py.IOError(ioe); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-06-07 23:14:09
|
Revision: 6465 http://jython.svn.sourceforge.net/jython/?rev=6465&view=rev Author: pjenvey Date: 2009-06-07 23:13:56 +0000 (Sun, 07 Jun 2009) Log Message: ----------- ensure the ZipFile is immediately closed when there's a problem reading it Modified Paths: -------------- trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-06-07 00:10:28 UTC (rev 6464) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-06-07 23:13:56 UTC (rev 6465) @@ -329,23 +329,9 @@ * Given a path to a Zip archive, build a dict, mapping file names * (local to the archive, using SEP as a separator) to toc entries. * - * A tocEntry is a tuple: - * - * (__file__, # value to use for __file__, available for all files - * compress, # compression kind; 0 for uncompressed - * data_size, # size of compressed data on disk - * file_size, # size of decompressed data - * file_offset, # offset of file header from start of archive (or -1 in Jython) - * time, # mod time of file (in dos format) - * date, # mod data of file (in dos format) - * crc, # crc checksum of the data - * ) - * - * Directories can be recognized by the trailing SEP in the name, - * data_size and file_offset are 0. - * * @param archive PyString path to the archive * @return a PyDictionary of tocEntrys + * @see #readZipFile(ZipFile, PyObject) */ private PyObject readDirectory(String archive) { File file = new File(sys.getPath(archive)); @@ -361,19 +347,53 @@ } PyObject files = new PyDictionary(); + try { + readZipFile(zipFile, files); + } finally { + try { + zipFile.close(); + } catch (IOException ioe) { + throw Py.IOError(ioe); + } + } + return files; + } + + /** + * Read ZipFile metadata into a dict of toc entries. + * + * A tocEntry is a tuple: + * + * (__file__, # value to use for __file__, available for all files + * compress, # compression kind; 0 for uncompressed + * data_size, # size of compressed data on disk + * file_size, # size of decompressed data + * file_offset, # offset of file header from start of archive (or -1 in Jython) + * time, # mod time of file (in dos format) + * date, # mod data of file (in dos format) + * crc, # crc checksum of the data + * ) + * + * Directories can be recognized by the trailing SEP in the name, data_size and + * file_offset are 0. + * + * @param zipFile ZipFile to read + * @param files a dict-like PyObject + */ + private void readZipFile(ZipFile zipFile, PyObject files) { for (Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); zipEntries.hasMoreElements();) { ZipEntry zipEntry = zipEntries.nextElement(); String name = zipEntry.getName().replace('/', File.separatorChar); PyObject __file__ = new PyString(archive + File.separator + name); - PyObject compress = new PyInteger(zipEntry.getMethod()); + PyObject compress = Py.newInteger(zipEntry.getMethod()); PyObject data_size = new PyLong(zipEntry.getCompressedSize()); PyObject file_size = new PyLong(zipEntry.getSize()); - // file_offset is a CPython optimization; it's used to - // seek directly to the file when reading it later. Jython - // doesn't do this nor is the offset available - PyObject file_offset = new PyInteger(-1); + // file_offset is a CPython optimization; it's used to seek directly to the + // file when reading it later. Jython doesn't do this nor is the offset + // available + PyObject file_offset = Py.newInteger(-1); PyObject time = new PyInteger(epochToDosTime(zipEntry.getTime())); PyObject date = new PyInteger(epochToDosDate(zipEntry.getTime())); PyObject crc = new PyLong(zipEntry.getCrc()); @@ -382,14 +402,6 @@ time, date, crc); files.__setitem__(new PyString(name), entry); } - - try { - zipFile.close(); - } catch (IOException ioe) { - throw Py.IOError(ioe); - } - - return files; } @Override @@ -397,13 +409,6 @@ return File.separator; } - /** - * Given a full module name, return the potential file path in the archive (without extension). - * - * @param prefix a String value - * @param name a String modulename value - * @return the file path String value - */ @Override protected String makeFilename(String fullname) { return prefix + getSubname(fullname).replace('.', File.separatorChar); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |