1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
https://github.com/mahmoud/boltons/commit/270e974
From: Mahmoud Hashemi <mahmoud@hatnote.com>
Date: Sun, 10 Oct 2021 23:26:24 -0700
Subject: [PATCH] address ecoutils import issue, fixes #294
--- a/boltons/ecoutils.py
+++ b/boltons/ecoutils.py
@@ -354,38 +354,53 @@ def get_profile(**kwargs):
return ret
-_real_safe_repr = pprint._safe_repr
-
-
-def _fake_json_dumps(val, indent=2):
- # never do this. this is a hack for Python 2.4. Python 2.5 added
- # the json module for a reason.
- def _fake_safe_repr(*a, **kw):
- res, is_read, is_rec = _real_safe_repr(*a, **kw)
- if res == 'None':
- res = 'null'
- if res == 'True':
- res = 'true'
- if res == 'False':
- res = 'false'
- if not (res.startswith("'") or res.startswith("u'")):
- res = res
- else:
- if res.startswith('u'):
- res = res[1:]
+try:
+ import json
+
+ def dumps(val, indent):
+ if indent:
+ return json.dumps(val, sort_keys=True, indent=indent)
+ return json.dumps(val, sort_keys=True)
+
+except ImportError:
+ _real_safe_repr = pprint._safe_repr
+
+ def _fake_json_dumps(val, indent=2):
+ # never do this. this is a hack for Python 2.4. Python 2.5 added
+ # the json module for a reason.
+ def _fake_safe_repr(*a, **kw):
+ res, is_read, is_rec = _real_safe_repr(*a, **kw)
+ if res == 'None':
+ res = 'null'
+ if res == 'True':
+ res = 'true'
+ if res == 'False':
+ res = 'false'
+ if not (res.startswith("'") or res.startswith("u'")):
+ res = res
+ else:
+ if res.startswith('u'):
+ res = res[1:]
- contents = res[1:-1]
- contents = contents.replace('"', '').replace(r'\"', '')
- res = '"' + contents + '"'
- return res, is_read, is_rec
+ contents = res[1:-1]
+ contents = contents.replace('"', '').replace(r'\"', '')
+ res = '"' + contents + '"'
+ return res, is_read, is_rec
- pprint._safe_repr = _fake_safe_repr
- try:
- ret = pprint.pformat(val, indent=indent)
- finally:
- pprint._safe_repr = _real_safe_repr
+ pprint._safe_repr = _fake_safe_repr
+ try:
+ ret = pprint.pformat(val, indent=indent)
+ finally:
+ pprint._safe_repr = _real_safe_repr
+
+ return ret
+
+ def dumps(val, indent):
+ ret = _fake_json_dumps(val, indent=indent)
+ if not indent:
+ ret = re.sub(r'\n\s*', ' ', ret)
+ return ret
- return ret
def get_profile_json(indent=False):
@@ -393,20 +408,6 @@ def get_profile_json(indent=False):
indent = 2
else:
indent = 0
- try:
- import json
-
- def dumps(val, indent):
- if indent:
- return json.dumps(val, sort_keys=True, indent=indent)
- return json.dumps(val, sort_keys=True)
-
- except ImportError:
- def dumps(val, indent):
- ret = _fake_json_dumps(val, indent=indent)
- if not indent:
- ret = re.sub(r'\n\s*', ' ', ret)
- return ret
data_dict = get_profile()
return dumps(data_dict, indent)
|