Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(603)

Unified Diff: client/utils/zip_package.py

Issue 2093593002: luci-py: Making __file__ usage unicode safe. (Closed) Base URL: https://github.com/luci/luci-py.git@master
Patch Set: Small fixes. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « client/utils/oauth.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/utils/zip_package.py
diff --git a/client/utils/zip_package.py b/client/utils/zip_package.py
index 49dc89054ffb7ac6940ae1cdfef27d3370fd31f0..539709b1c8106835e5ee06bd656bbc07b7dfd9fc 100755
--- a/client/utils/zip_package.py
+++ b/client/utils/zip_package.py
@@ -35,6 +35,18 @@ _extracted_files = []
_extracted_files_lock = threading.Lock()
+# Patch zipimport.zipimporter hook to accept unicode strings
+def zipimporter_unicode(archivepath):
+ if isinstance(archivepath, unicode):
+ archivepath = archivepath.encode(sys.getfilesystemencoding())
+ return zipimport.zipimporter(archivepath)
+
+
+for i, hook in enumerate(sys.path_hooks):
+ if hook is zipimport.zipimporter:
+ sys.path_hooks[i] = zipimporter_unicode
+
+
class ZipPackageError(RuntimeError):
"""Failed to create a zip package."""
@@ -238,7 +250,13 @@ def get_main_script_path():
"""
# If running from interactive console __file__ is not defined.
main = sys.modules['__main__']
- return get_module_zip_archive(main) or getattr(main, '__file__', None)
+ path = get_module_zip_archive(main)
+ if path:
+ return path
+
+ path = getattr(main, '__file__', None)
+ if path:
+ return path.decode(sys.getfilesystemencoding())
def _write_temp_data(name, data, temp_dir):
@@ -279,7 +297,8 @@ def extract_resource(package, resource, temp_dir=None):
# For regular non-zip packages just construct an absolute path.
if not is_zipped_module(package):
# Package's __file__ attribute is always an absolute path.
- path = os.path.join(os.path.dirname(package.__file__),
+ ppath = package.__file__.decode(sys.getfilesystemencoding())
+ path = os.path.join(os.path.dirname(ppath),
resource.replace('/', os.sep))
if not os.path.exists(path):
raise ValueError('No such resource in %s: %s' % (package, resource))
« no previous file with comments | « client/utils/oauth.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698