Skip to content

Commit 5a05f6f

Browse files
committed
FileNotFoundError and FileExistsError in more places.
1 parent ce6cb3f commit 5a05f6f

File tree

3 files changed

+5
-16
lines changed

3 files changed

+5
-16
lines changed

docs/jsonschema_role.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
from contextlib import suppress
12
from datetime import datetime
23
from importlib import metadata
34
from urllib.parse import urljoin
4-
import errno
55
import os
66
import urllib.request
77

@@ -30,11 +30,7 @@ def setup(app):
3030

3131
app.add_config_value("cache_path", "_cache", "")
3232

33-
try:
34-
os.makedirs(app.config.cache_path)
35-
except OSError as error:
36-
if error.errno != errno.EEXIST:
37-
raise
33+
os.makedirs(app.config.cache_path, exist_ok=True)
3834

3935
path = os.path.join(app.config.cache_path, "spec.html")
4036
spec = fetch_or_load(path)
@@ -61,13 +57,10 @@ def fetch_or_load(spec_path):
6157
),
6258
}
6359

64-
try:
60+
with suppress(FileNotFoundError):
6561
modified = datetime.utcfromtimestamp(os.path.getmtime(spec_path))
6662
date = modified.strftime("%a, %d %b %Y %I:%M:%S UTC")
6763
headers["If-Modified-Since"] = date
68-
except OSError as error:
69-
if error.errno != errno.ENOENT:
70-
raise
7164

7265
request = urllib.request.Request(VALIDATION_SPEC, headers=headers)
7366
response = urllib.request.urlopen(request, cafile=certifi.where())

jsonschema/cli.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from json import JSONDecodeError
66
from textwrap import dedent
77
import argparse
8-
import errno
98
import json
109
import sys
1110
import traceback
@@ -44,9 +43,7 @@ def from_arguments(cls, arguments, stdout, stderr):
4443
def load(self, path):
4544
try:
4645
file = open(path)
47-
except OSError as error:
48-
if error.errno != errno.ENOENT:
49-
raise
46+
except FileNotFoundError:
5047
self.filenotfound_error(path=path, exc_info=sys.exc_info())
5148
raise _CannotLoadFile()
5249

jsonschema/tests/test_cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pathlib import Path
55
from textwrap import dedent
66
from unittest import TestCase
7-
import errno
87
import json
98
import os
109
import subprocess
@@ -50,7 +49,7 @@ def fake_open(all_contents):
5049
def open(path):
5150
contents = all_contents.get(path)
5251
if contents is None:
53-
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), path)
52+
raise FileNotFoundError(path)
5453
return StringIO(contents)
5554
return open
5655

0 commit comments

Comments
 (0)