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

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: 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..3a35286d2deeed1b36f168ee98e689a718d16aed 100755
--- a/client/utils/zip_package.py
+++ b/client/utils/zip_package.py
@@ -34,6 +34,16 @@ EXCLUDE_LIST = (
_extracted_files = []
_extracted_files_lock = threading.Lock()
M-A Ruel 2016/06/23 13:09:54 add another empty line above.
mithro 2016/06/24 03:22:42 Done.
+# 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
M-A Ruel 2016/06/23 13:09:54 sadness
mithro 2016/06/24 03:22:42 Yeah :( It works properly in Python 3...
+
class ZipPackageError(RuntimeError):
"""Failed to create a zip package."""
@@ -238,7 +248,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 +295,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