Skip to content

Commit ef36046

Browse files
davidriazatifacebook-github-bot
authored andcommitted
Better error message for using Python builtin_function_or_method (#22935)
Summary: * better error in `toSugaredValue` * removes a bunch of periods on error messages, `ErrorReport` already adds a `:` at the end of the message](https://our.intern.facebook.com/intern/diff/16291079/) Pull Request resolved: #22935 Pulled By: driazati Differential Revision: D16291079 fbshipit-source-id: 478724fc7d1ae79093f4ede18553ffeafa2c7964
1 parent 25b6999 commit ef36046

File tree

10 files changed

+26
-28
lines changed

10 files changed

+26
-28
lines changed

test/test_jit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10782,7 +10782,7 @@ def test_wrong_type():
1078210782

1078310783
def test_unsupported_builtin_error(self):
1078410784
with self.assertRaisesRegex(RuntimeError,
10785-
"calling a python builtin_function_or_method which is currently not supported"):
10785+
"Python builtin <built-in function hypot> is currently"):
1078610786
@torch.jit.script
1078710787
def test_unsupported(a):
1078810788
return math.hypot(a, 2.0)

torch/csrc/jit/attributes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct AttributeError : public std::exception {
123123
std::stringstream ss;
124124
if (!defined) {
125125
ss << "required keyword attribute '" << name.toUnqualString()
126-
<< "' is undefined.";
126+
<< "' is undefined";
127127
} else {
128128
ss << "required keyword attribute '" << name.toUnqualString()
129129
<< "' has the wrong type";

torch/csrc/jit/import_source.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct ConstantTableValue : public SugaredValue {
9393
if (offset < 0 || size_t(offset) >= constants_.size()) {
9494
throw ErrorReport(loc) << "constant index " << offset
9595
<< " is out of bounds (constant table has "
96-
<< constants_.size() << " entries).";
96+
<< constants_.size() << " entries)";
9797
}
9898
Value* value = m.graph()->insertConstant(constants_[offset], nullptr, loc);
9999
return std::make_shared<SimpleValue>(value);
@@ -176,7 +176,7 @@ struct SourceImporter {
176176
throw ErrorReport(p_.lexer().cur().range)
177177
<< "Attempting to load a script generated from a newer version of PyTorch. Maximum supported TorchScript version is "
178178
<< CURRENT_OP_VERSION_SET
179-
<< " but the script being loaded is version " << version_ << ".";
179+
<< " but the script being loaded is version " << version_;
180180
}
181181
}
182182

torch/csrc/jit/passes/python_print.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ struct PythonPrintPass {
650650
throw script::ErrorReport(stmt.node()->sourceRange())
651651
<< "loop cannot be printed as python "
652652
<< "because it has gone through an optimization "
653-
<< "that combined while and for loops. File a bug.";
653+
<< "that combined while and for loops. File a bug";
654654
}
655655

656656
bool emit_as_for_loop = loop_type == LoopView::For;
@@ -786,7 +786,7 @@ struct PythonPrintPass {
786786
if (enforce_importable_ && node->inputs().size() != 1) {
787787
throw script::ErrorReport(node->sourceRange())
788788
<< "Exportable methods must have a single return value. "
789-
<< "Normal use of ScriptMethods should enforce this.";
789+
<< "Normal use of ScriptMethods should enforce this";
790790
}
791791
if (node->inputs().size() > 0) {
792792
indent();

torch/csrc/jit/passes/shape_analysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class ShapePropagator {
170170
// prevented it
171171
std::stringstream ss;
172172
ss << "unable to create representative value for: " << type_->str()
173-
<< ". File a bug report.";
173+
<< ". File a bug report";
174174
throw std::runtime_error(ss.str());
175175
}
176176

torch/csrc/jit/script/compiler.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ struct to_ir {
13621362
auto body = stmt.body();
13631363
if (stmt.itrs().size() != 1) {
13641364
throw ErrorReport(stmt)
1365-
<< "List of iterables is not supported currently.";
1365+
<< "List of iterables is not supported currently";
13661366
}
13671367
// Emit loop information for builtinFunction values like range(), zip(),
13681368
// enumerate() or SimpleValue like List, Tensor, Dict, etc.
@@ -1464,19 +1464,19 @@ struct to_ir {
14641464
num_starred++;
14651465
} else {
14661466
throw ErrorReport(assignee) << "lhs of assignment must be a variable, "
1467-
<< "subscript, or starred expression.";
1467+
<< "subscript, or starred expression";
14681468
}
14691469
}
14701470

14711471
if (num_starred > 1) {
14721472
throw ErrorReport(r)
1473-
<< "Only one starred expression is allowed on the lhs.";
1473+
<< "Only one starred expression is allowed on the lhs";
14741474
}
14751475

14761476
if (num_starred > 0 && num_normal_assign == 0) {
14771477
throw ErrorReport(r) << "A Starred expression may only appear on the "
14781478
<< "lhs within the presence of another non-starred"
1479-
<< " expression.";
1479+
<< " expression";
14801480
}
14811481

14821482
return num_starred;
@@ -1523,7 +1523,7 @@ struct to_ir {
15231523
default:
15241524
throw ErrorReport(stmt.lhs())
15251525
<< "unexpected expression on "
1526-
<< "left-hand side of augmented assignment.";
1526+
<< "left-hand side of augmented assignment";
15271527
}
15281528
}
15291529

@@ -1665,7 +1665,7 @@ struct to_ir {
16651665
throw ErrorReport(subscriptExprs)
16661666
<< "Sliced expression not yet supported for"
16671667
<< " subscripted list augmented assignment. "
1668-
<< "File a bug if you want this.";
1668+
<< "File a bug if you want this";
16691669
}
16701670
const auto idxValue = emitExpr(subscriptExprs[0]);
16711671

@@ -1735,7 +1735,7 @@ struct to_ir {
17351735
throw ErrorReport(subscript)
17361736
<< "Sliced expression not yet supported for"
17371737
<< " subscripted list assignment. "
1738-
<< "File a bug if you want this.";
1738+
<< "File a bug if you want this";
17391739
}
17401740

17411741
std::vector<NamedValue> args;
@@ -1798,7 +1798,7 @@ struct to_ir {
17981798
auto var = Starred(assignee).expr();
17991799
if (var.kind() != TK_VAR) {
18001800
throw ErrorReport(var)
1801-
<< "Cannot pack a tuple into a non-variable.";
1801+
<< "Cannot pack a tuple into a non-variable";
18021802
}
18031803
size_t n_matched = outputs.size() - n_binders;
18041804
ArrayRef<std::shared_ptr<SugaredValue>> outputs_ref = outputs;
@@ -1850,7 +1850,7 @@ struct to_ir {
18501850
break;
18511851
default:
18521852
throw ErrorReport(stmt.lhs())
1853-
<< "unexpected expression on left-hand side of assignment.";
1853+
<< "unexpected expression on left-hand side of assignment";
18541854
}
18551855
}
18561856

@@ -2443,7 +2443,7 @@ struct to_ir {
24432443
}
24442444
case TK_STARRED: {
24452445
throw ErrorReport(tree)
2446-
<< "Unexpected starred expansion. File a bug report.";
2446+
<< "Unexpected starred expansion. File a bug report";
24472447
}
24482448
case TK_CONST: {
24492449
return emitConst(Const(tree));
@@ -2751,7 +2751,7 @@ struct to_ir {
27512751
if (!sliceable->type()->isSubtypeOf(TensorType::get())) {
27522752
throw ErrorReport(loc)
27532753
<< "Unsupported operation: attempted to use multidimensional "
2754-
<< "indexing on a non-tensor type.";
2754+
<< "indexing on a non-tensor type";
27552755
}
27562756

27572757
std::vector<Value*> tensor_indices;

torch/csrc/jit/script/final_returns.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct ReturnInfo {
1313

1414
void checkNoReturn(const TreeRef& ref) {
1515
if (ref->kind() == TK_RETURN) {
16-
throw ErrorReport(ref) << "return is not allowed from a loop.";
16+
throw ErrorReport(ref) << "return is not allowed from a loop";
1717
}
1818
// do not search into first-class functions
1919
if (ref->kind() == TK_DEF) {
@@ -56,7 +56,7 @@ ReturnInfo makeReturnsFinal(
5656
if (!rest_final.returns_) {
5757
throw ErrorReport(if_stmt)
5858
<< "This if statement performs an early return, but the block of code that follows it does not return."
59-
<< " Early returns are only allowed when the block following them also returns.";
59+
<< " Early returns are only allowed when the block following them also returns";
6060
}
6161
changed.emplace_back(
6262
if_stmt.withNewBranches(true_final.stmts_, rest_final.stmts_));
@@ -77,7 +77,7 @@ ReturnInfo makeReturnsFinal(
7777
}
7878
throw ErrorReport(if_stmt)
7979
<< "This if statement contains some paths that return and some paths that do not. "
80-
<< "If statements must either entirely return or never return.";
80+
<< "If statements must either entirely return or never return";
8181
} break;
8282
case TK_WHILE:
8383
case TK_FOR:

torch/csrc/jit/script/function_schema_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ struct SchemaParser {
194194
return c10::impl::toList(fmap(vs, [](IValue v) { return v.toBool(); }));
195195
default:
196196
throw ErrorReport(range)
197-
<< "lists are only supported for float or int types.";
197+
<< "lists are only supported for float or int types";
198198
}
199199
}
200200
IValue parseConstantList(TypeKind kind) {

torch/csrc/jit/script/python_sugared_value.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,8 @@ std::shared_ptr<SugaredValue> toSugaredValue(
487487

488488
if (py::isinstance<py::function>(obj)) {
489489
if (typeString(obj) == "builtin_function_or_method") {
490-
throw ErrorReport(loc)
491-
<< "You are calling a python builtin_function_or_method "
492-
<< "which is currently not supported in Torchscript."
493-
<< "Please open a feature request to add it.";
490+
throw ErrorReport(loc) << "Python builtin " << py::str(obj)
491+
<< " is currently not supported in Torchscript";
494492
}
495493
}
496494

torch/csrc/jit/script/sugared_value.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void SimpleValue::setAttr(
207207
if (insertPoint->owningBlock() != topLevelBlock) {
208208
throw ErrorReport(loc)
209209
<< "First assignment cannot be in a control-flow block. "
210-
<< "Initialize the field at the top level first.";
210+
<< "Initialize the field at the top level first";
211211
}
212212
} else {
213213
throw ErrorReport(loc)
@@ -407,7 +407,7 @@ std::shared_ptr<SugaredValue> ClassValue::call(
407407
auto self = g.insertNode(g.createObject(type_))->output();
408408
if (!type_->getMethod("__init__")) {
409409
throw ErrorReport(loc)
410-
<< "Class " << type_->basename() << " does not have an __init__ function defined.";
410+
<< "Class " << type_->basename() << " does not have an __init__ function defined";
411411
}
412412

413413
// Call the init function

0 commit comments

Comments
 (0)