summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2021-11-03 13:01:41 -0700
committerGitHub <noreply@github.com>2021-11-03 13:01:41 -0700
commit14b4fd74693e6cc68a0041349eda6ed8403d17e1 (patch)
tree92a3447642bd8d5a0bd61362d2cfcac993b7234b
parent272b4f24437d9cba1127058dc3479f9bc6d8c4b9 (diff)
parentffb5e06ec6683592f89e3fef5888a3dd74975110 (diff)
downloadjsonschema-14b4fd74693e6cc68a0041349eda6ed8403d17e1.tar.gz
Merge pull request #873 from sevein/dev/issue-871-importlib-resources
Load schemas via importlib.resources
-rw-r--r--jsonschema/_utils.py13
-rw-r--r--setup.cfg1
2 files changed, 11 insertions, 3 deletions
diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py
index d4524e8..e5051ec 100644
--- a/jsonschema/_utils.py
+++ b/jsonschema/_utils.py
@@ -2,8 +2,14 @@ from collections.abc import Mapping, MutableMapping, Sequence
from urllib.parse import urlsplit
import itertools
import json
-import pkgutil
import re
+import sys
+
+# The files() API was added in Python 3.9.
+if sys.version_info >= (3, 9): # pragma: no cover
+ import importlib.resources as resources
+else: # pragma: no cover
+ import importlib_resources as resources
class URIDict(MutableMapping):
@@ -51,8 +57,9 @@ def load_schema(name):
Load a schema from ./schemas/``name``.json and return it.
"""
- data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name))
- return json.loads(data.decode("utf-8"))
+ path = resources.files(__package__).joinpath(f"schemas/{name}.json")
+ data = path.read_text(encoding="utf-8")
+ return json.loads(data)
def format_as_index(container, indices):
diff --git a/setup.cfg b/setup.cfg
index 7dd8e38..e9e3efe 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -28,6 +28,7 @@ python_requires = >=3.7
install_requires =
attrs>=17.4.0
importlib_metadata;python_version<'3.8'
+ importlib_resources;python_version<'3.9'
pyrsistent>=0.14.0,!=0.17.0,!=0.17.1,!=0.17.2
[options.extras_require]