Skip to content

Commit 8d6f613

Browse files
cclausssdispater
authored andcommitted
_compat.py Use feature detection instead of version detection (#139)
* _compat.py Use feature detection instead of version detection https://docs.python.org/3/howto/pyporting.html#use-feature-detection-instead-of-version-detection On `encodings`, given the `return` statement, all other _falsey_ values are just as dangerous as `None`. * Treat FileNotFoundError as a builtin * Treat FileNotFoundError as a builtin * FileNotFoundError = FileNotFoundError * except _compat.FileNotFoundError:
1 parent 89980fe commit 8d6f613

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

pendulum/_compat.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44

55
PY2 = sys.version_info[0] == 2
66
PY3K = sys.version_info[0] >= 3
7-
PY33 = sys.version_info >= (3, 3)
87
PY36 = sys.version_info >= (3, 6)
98

109

11-
if PY2:
12-
long = long
13-
unicode = unicode
14-
basestring = basestring
15-
else:
10+
try: # Python 2
11+
long
12+
unicode
13+
basestring
14+
except NameError: # Python 3
1615
long = int
1716
unicode = str
1817
basestring = str
1918

20-
if PY33:
19+
try: # Python >= 3.3
2120
FileNotFoundError = FileNotFoundError
22-
else:
23-
FileNotFoundError = IOError # cf PEP-3151
21+
except NameError: # Python < 3.3
22+
FileNotFoundError = IOError # cf PEP-3151
2423

2524
def decode(string, encodings=None):
2625
if not PY2 and not isinstance(string, bytes):
@@ -29,8 +28,7 @@ def decode(string, encodings=None):
2928
if PY2 and isinstance(string, unicode):
3029
return string
3130

32-
if encodings is None:
33-
encodings = ['utf-8', 'latin1', 'ascii']
31+
encodings = encodings or ['utf-8', 'latin1', 'ascii']
3432

3533
for encoding in encodings:
3634
try:
@@ -48,8 +46,7 @@ def encode(string, encodings=None):
4846
if PY2 and isinstance(string, str):
4947
return string
5048

51-
if encodings is None:
52-
encodings = ['utf-8', 'latin1', 'ascii']
49+
encodings = encodings or ['utf-8', 'latin1', 'ascii']
5350

5451
for encoding in encodings:
5552
try:

0 commit comments

Comments
 (0)