From 1a99dd3025da72ef70b870722b36466b81a840f4 Mon Sep 17 00:00:00 2001 From: sinoroc Date: Sat, 26 Sep 2020 15:12:28 +0200 Subject: [PATCH] Use pkgutil.get_data for zip-safe resource loading When running from a Zip file (`zipapp`, `*.pyz`), loading resources from package data can not be done by directly reading files relative to `__file__`, since there is no valid path on the file system for zipped files. Using `pkgutil.get_data` ensures that loading files from a Zip archive is handled accordingly. Resolves: python-poetry/poetry#2965 --- poetry/core/_vendor/jsonschema/_utils.py | 12 ++++++------ vendors/patches/jsonschema.patch | 23 ++++++----------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/poetry/core/_vendor/jsonschema/_utils.py b/poetry/core/_vendor/jsonschema/_utils.py index 117eec24b..dcbe2476f 100644 --- a/poetry/core/_vendor/jsonschema/_utils.py +++ b/poetry/core/_vendor/jsonschema/_utils.py @@ -1,6 +1,6 @@ import itertools import json -import os +import pkgutil import re from jsonschema.compat import MutableMapping, str_types, urlsplit @@ -50,12 +50,12 @@ def load_schema(name): """ Load a schema from ./schemas/``name``.json and return it. """ - with open( - os.path.join(os.path.dirname(__file__), "schemas", "{0}.json".format(name)) - ) as f: - data = f.read() - return json.loads(data) + data = pkgutil.get_data( + "poetry.core", + "_vendor/jsonschema/schemas/{0}.json".format(name), + ) + return json.loads(data.decode("utf-8")) def indent(string, times=1): diff --git a/vendors/patches/jsonschema.patch b/vendors/patches/jsonschema.patch index 423e641d3..7698bd3d1 100644 --- a/vendors/patches/jsonschema.patch +++ b/vendors/patches/jsonschema.patch @@ -17,26 +17,15 @@ diff --git a/poetry/core/_vendor/jsonschema/_utils.py b/poetry/core/_vendor/json index 8fb8593..368474a 100644 --- a/poetry/core/_vendor/jsonschema/_utils.py +++ b/poetry/core/_vendor/jsonschema/_utils.py -@@ -1,6 +1,6 @@ - import itertools - import json --import pkgutil -+import os - import re - - from jsonschema.compat import MutableMapping, str_types, urlsplit -@@ -50,9 +50,12 @@ def load_schema(name): - """ +@@ -51,7 +51,10 @@ def load_schema(name): Load a schema from ./schemas/``name``.json and return it. """ -+ with open( -+ os.path.join(os.path.dirname(__file__), "schemas", "{0}.json".format(name)) -+ ) as f: -+ data = f.read() - data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name)) -- return json.loads(data.decode("utf-8")) -+ return json.loads(data) ++ data = pkgutil.get_data( ++ "poetry.core", ++ "_vendor/jsonschema/schemas/{0}.json".format(name), ++ ) + return json.loads(data.decode("utf-8")) - def indent(string, times=1):