@@ -33,6 +33,12 @@ static inline PyObject* PyObject_CallOneArg(PyObject* func, PyObject* arg) {
33
33
#define Py_IsNone (x ) ((x) == Py_None)
34
34
#endif
35
35
36
+ #define HANDLE_NULL_VALUE (value ) \
37
+ if ((value) == NULL ) { \
38
+ PyErr_Clear (); \
39
+ return false ; \
40
+ }
41
+
36
42
static inline bool PyObject_Equal (PyObject* a, PyObject* b) {
37
43
if (a == b) {
38
44
return true ;
@@ -84,6 +90,7 @@ bool TypeMatchGuard::check(PyObject* value) {
84
90
bool IdMatchGuard::check (PyObject* value) { return value == expected_; }
85
91
86
92
bool ValueMatchGuard::check (PyObject* value) {
93
+ HANDLE_NULL_VALUE (value);
87
94
return PyObject_Equal (value, expected_value_);
88
95
}
89
96
@@ -107,6 +114,7 @@ bool DtypeMatchGuard::check(PyObject* value) {
107
114
}
108
115
109
116
bool ShapeMatchGuard::check (PyObject* value) {
117
+ HANDLE_NULL_VALUE (value);
110
118
auto tensor = GetTensorFromPyObject (value);
111
119
if (!tensor) {
112
120
return false ;
@@ -194,10 +202,12 @@ bool WeakRefMatchGuard::check(PyObject* value) {
194
202
}
195
203
196
204
PyObject* ConstantExprNode::eval (FrameProxy* frame) { return value_ptr_; }
197
- std::string ConstantExprNode::stringify () { return py::str (value_ptr_); }
205
+ std::string ConstantExprNode::stringify (int indent) {
206
+ return py::str (value_ptr_);
207
+ }
198
208
199
209
PyObject* ExternVarExprNode::eval (FrameProxy* frame) { return value_ptr_; }
200
- std::string ExternVarExprNode::stringify () { return var_name_; }
210
+ std::string ExternVarExprNode::stringify (int indent ) { return var_name_; }
201
211
202
212
PyObject* LocalVarExprNode::eval (FrameProxy* frame) {
203
213
#if PY_3_13_PLUS
@@ -208,7 +218,7 @@ PyObject* LocalVarExprNode::eval(FrameProxy* frame) {
208
218
return PyDict_GetItemString (frame->f_locals , var_name_.c_str ());
209
219
#endif
210
220
}
211
- std::string LocalVarExprNode::stringify () {
221
+ std::string LocalVarExprNode::stringify (int indent ) {
212
222
return " locals[" + var_name_ + " ]" ;
213
223
}
214
224
@@ -219,15 +229,15 @@ PyObject* GlobalVarExprNode::eval(FrameProxy* frame) {
219
229
return PyDict_GetItemString (frame->f_globals , var_name_.c_str ());
220
230
#endif
221
231
}
222
- std::string GlobalVarExprNode::stringify () {
232
+ std::string GlobalVarExprNode::stringify (int indent ) {
223
233
return " globals[" + var_name_ + " ]" ;
224
234
}
225
235
226
236
PyObject* AttributeExprNode::eval (FrameProxy* frame) {
227
237
PyObject* var = var_expr_->eval (frame);
228
238
return PyObject_GetAttrString (var, attr_name_.c_str ());
229
239
}
230
- std::string AttributeExprNode::stringify () {
240
+ std::string AttributeExprNode::stringify (int indent ) {
231
241
std::stringstream ss;
232
242
ss << var_expr_->stringify () << " ." << attr_name_;
233
243
return ss.str ();
@@ -238,7 +248,7 @@ PyObject* ItemExprNode::eval(FrameProxy* frame) {
238
248
PyObject* key = key_expr_->eval (frame);
239
249
return PyObject_GetItem (var, key);
240
250
}
241
- std::string ItemExprNode::stringify () {
251
+ std::string ItemExprNode::stringify (int indent ) {
242
252
std::stringstream ss;
243
253
ss << var_expr_->stringify () << " [" << key_expr_->stringify () << " ]" ;
244
254
return ss.str ();
@@ -261,10 +271,19 @@ std::optional<int> GuardNode::lookup(FrameProxy* frame) {
261
271
}
262
272
return std::nullopt;
263
273
}
264
- std::string GuardNode::stringify () {
274
+ std::string GuardNode::stringify (int indent ) {
265
275
std::stringstream ss;
266
- ss << guard->get_guard_name ();
276
+ // TODO(zrr1999): support multiple exprs
277
+ auto expr = exprs.back ();
278
+ ss << std::string (indent, ' ' ) << guard->get_guard_name ();
267
279
ss << " (" << exprs.back ()->stringify () << " )" ;
280
+ if (!next_guard_nodes.empty ()) {
281
+ ss << " |" << std::endl;
282
+ for (auto & next_guard_node : next_guard_nodes) {
283
+ ss << std::string (indent + 2 , ' ' );
284
+ ss << next_guard_node->stringify (indent + 2 ) << std::endl;
285
+ }
286
+ }
268
287
return ss.str ();
269
288
}
270
289
@@ -295,7 +314,7 @@ std::string GuardTree::stringify() {
295
314
std::stringstream ss;
296
315
for (size_t i = 0 ; i < guard_nodes_.size (); ++i) {
297
316
if (i > 0 ) {
298
- ss << " and " ;
317
+ ss << std::endl << " and " << std::endl ;
299
318
}
300
319
ss << guard_nodes_[i]->stringify ();
301
320
}
0 commit comments