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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include "loads.h"
#include <Python.h>
#include <library/cpp/json/fast_sax/parser.h>
#include <util/generic/algorithm.h>
#include <util/generic/stack.h>
#include <util/generic/vector.h>
#include <util/generic/ylimits.h>
#include <util/string/ascii.h>
using namespace NJson;
namespace {
enum EKind {
Undefined,
Array,
Dict,
Value,
Key,
};
static inline TStringBuf ToStr(EKind kind) noexcept {
switch (kind) {
case Undefined:
return TStringBuf("Undefined");
case Array:
return TStringBuf("Array");
case Dict:
return TStringBuf("Dict");
case Value:
return TStringBuf("Value");
case Key:
return TStringBuf("Key");
}
Y_UNREACHABLE();
}
struct TUnref {
static inline void Destroy(PyObject* o) noexcept {
Py_XDECREF(o);
}
};
using TObjectPtr = TAutoPtr<PyObject, TUnref>;
static inline TObjectPtr BuildBool(bool val) noexcept {
if (val) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
// Translate python exceptions from object-creating functions into c++ exceptions
// Such errors are reported by returning nullptr
// When a python error is set and C++ exception is caught by Cython wrapper,
// Python exception is propagated, while C++ exception is discarded.
PyObject* CheckNewObject(PyObject* obj) {
Y_ENSURE(obj != nullptr, "got python exception");
return obj;
}
void CheckRetcode(int retcode) {
Y_ENSURE(retcode == 0, "got python exception");
}
static inline TObjectPtr BuildSmall(long val) {
#if PY_VERSION_HEX >= 0x03000000
return CheckNewObject(PyLong_FromLong(val));
#else
return CheckNewObject(PyInt_FromLong(val));
#endif
}
PyObject* CreatePyString(TStringBuf str, bool intern, bool mayUnicode) {
#if PY_VERSION_HEX >= 0x03000000
Y_UNUSED(mayUnicode);
PyObject* pyStr = PyUnicode_FromStringAndSize(str.data(), str.size());
if (intern) {
PyUnicode_InternInPlace(&pyStr);
}
#else
const bool needUnicode = mayUnicode && !AllOf(str, IsAscii);
PyObject* pyStr = needUnicode ? PyUnicode_FromStringAndSize(str.data(), str.size())
: PyString_FromStringAndSize(str.data(), str.size());
if (intern && !needUnicode) {
PyString_InternInPlace(&pyStr);
}
#endif
return pyStr;
}
struct TVal {
EKind Kind = Undefined;
TObjectPtr Val;
inline TVal() noexcept
: Kind(Undefined)
{
}
inline TVal(EKind kind, TObjectPtr val) noexcept
: Kind(kind)
, Val(val)
{
}
};
static inline TObjectPtr NoneRef() noexcept {
Py_RETURN_NONE;
}
struct TContext: public TJsonCallbacks {
const bool InternKeys;
const bool InternVals;
const bool MayUnicode;
TStack<TVal, TVector<TVal>> S;
inline TContext(bool internKeys, bool internVals, bool mayUnicode)
: TJsonCallbacks(true)
, InternKeys(internKeys)
, InternVals(internVals)
, MayUnicode(mayUnicode)
{
S.emplace();
}
inline bool Consume(TObjectPtr o) {
auto& t = S.top();
if (t.Kind == Array) {
CheckRetcode(PyList_Append(t.Val.Get(), o.Get()));
} else if (t.Kind == Key) {
auto key = S.top().Val;
S.pop();
CheckRetcode(PyDict_SetItem(S.top().Val.Get(), key.Get(), o.Get()));
} else {
t = TVal(Value, o);
}
return true;
}
inline TObjectPtr Pop(EKind expect) {
auto res = S.top();
S.pop();
if (res.Kind != expect) {
ythrow yexception() << "unexpected kind(expect " << ToStr(expect) << ", got " << ToStr(res.Kind) << ")";
}
return res.Val;
}
inline void Push(EKind kind, TObjectPtr object) {
S.push(TVal(kind, object));
}
virtual bool OnNull() {
return Consume(NoneRef());
}
virtual bool OnBoolean(bool v) {
return Consume(BuildBool(v));
}
virtual bool OnInteger(long long v) {
if (v >= (long long)Min<long>()) {
return Consume(BuildSmall((long)v));
}
return Consume(CheckNewObject(PyLong_FromLongLong(v)));
}
virtual bool OnUInteger(unsigned long long v) {
if (v <= (unsigned long long)Max<long>()) {
return Consume(BuildSmall((long)v));
}
return Consume(CheckNewObject(PyLong_FromUnsignedLongLong(v)));
}
virtual bool OnDouble(double v) {
return Consume(CheckNewObject(PyFloat_FromDouble(v)));
}
virtual bool OnString(const TStringBuf& v) {
return Consume(CheckNewObject(CreatePyString(v, InternVals, MayUnicode)));
}
virtual bool OnOpenMap() {
Push(Dict, CheckNewObject(PyDict_New()));
return true;
}
virtual bool OnCloseMap() {
return Consume(Pop(Dict));
}
virtual bool OnMapKey(const TStringBuf& k) {
Push(Key, CheckNewObject(CreatePyString(k, InternKeys, MayUnicode)));
return true;
}
virtual bool OnOpenArray() {
Push(Array, CheckNewObject(PyList_New(0)));
return true;
}
virtual bool OnCloseArray() {
return Consume(Pop(Array));
}
};
}
PyObject* LoadJsonFromString(const char* data, size_t len, bool internKeys, bool internVals, bool mayUnicode) {
TContext ctx(internKeys, internVals, mayUnicode);
if (!len) {
ythrow yexception() << "parse error: zero length input string";
}
if (!NJson::ReadJsonFast(TStringBuf(data, len), &ctx)) {
ythrow yexception() << "parse error";
}
auto& s = ctx.S;
if (!s || s.top().Kind != Value) {
ythrow yexception() << "shit happen";
}
return s.top().Val.Release();
}
|