Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 6bab024

Browse files
Introduce compat.py
1 parent 5aa888d commit 6bab024

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

coreapi_cli/auth.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1+
from coreapi_cli.compat import urlparse
12
from requests.auth import AuthBase
23

3-
try:
4-
# Python 2
5-
import urlparse
6-
7-
except ImportError:
8-
# Python 3
9-
import urllib.parse as urlparse
10-
114

125
class DomainCredentials(AuthBase):
136
allow_cookies = False

coreapi_cli/compat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
3+
try:
4+
# Python 3
5+
JSONDecodeError = json.decoder.JSONDecodeError
6+
except AttributeError:
7+
# Python 2
8+
JSONDecodeError = ValueError
9+
10+
11+
try:
12+
# Python 2
13+
import urlparse # noqa
14+
except ImportError:
15+
# Python 3
16+
import urllib.parse as urlparse # noqa

coreapi_cli/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from coreapi_cli import __version__ as client_version
33
from coreapi_cli import codec_plugins
44
from coreapi_cli.auth import DomainCredentials
5+
from coreapi_cli.compat import JSONDecodeError
56
from coreapi_cli.display import display
67
from coreapi_cli.debug import DebugSession
78
from coreapi_cli.history import History, dump_history, load_history
@@ -47,7 +48,7 @@ def coerce_key_types(doc, keys):
4748
if isinstance(active, coreapi.Array):
4849
try:
4950
key = int(key)
50-
except:
51+
except ValueError:
5152
pass
5253

5354
# Descend through the document, so we can correctly identify
@@ -269,7 +270,7 @@ def parse_params(ctx, param, tokens):
269270

270271
try:
271272
pair = (field, json.loads(value))
272-
except:
273+
except JSONDecodeError:
273274
if value.startswith('{') or value.startswith('['):
274275
# Guard against malformed composite objects being treated as strings.
275276
raise click.BadParameter('Unclear if parameter "%s" should be interperted as a string or data. Use --data or --string instead.' % field)
@@ -289,7 +290,7 @@ def parse_json(ctx, param, tokens):
289290

290291
try:
291292
pair = (field, json.loads(value))
292-
except:
293+
except JSONDecodeError:
293294
raise click.BadParameter('Could not parse value for data argument "%s"' % field)
294295
ret.append(pair)
295296

0 commit comments

Comments
 (0)