mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-14 20:57:59 +00:00
[torch] fix exception types in custom class magic setattr/getattr (#146516)
Summary: `c10::AttributeError` is not automatically converted to Python AttributeError, it needs some special macros (e.g. `HANDLE_TH_ERRORS`). Some Python functions like `hasattr` rely on the type of the throw exception to be correct. We don't need the fully generality of those macros, so just do a targeted error type conversion here. Test Plan: added unit test Differential Revision: D69197217 Pull Request resolved: https://github.com/pytorch/pytorch/pull/146516 Approved by: https://github.com/zdevito
This commit is contained in:
parent
3a6a203b98
commit
425804db2b
2 changed files with 10 additions and 3 deletions
|
|
@ -445,6 +445,10 @@ class TestTorchbind(JitTestCase):
|
|||
|
||||
self.checkScript(fn, (1,))
|
||||
|
||||
def test_hasattr(self):
|
||||
ss = torch.classes._TorchScriptTesting._StackString(["foo", "bar"])
|
||||
self.assertFalse(hasattr(ss, "baz"))
|
||||
|
||||
def test_default_args(self):
|
||||
def fn() -> int:
|
||||
obj = torch.classes._TorchScriptTesting._DefaultArgs()
|
||||
|
|
|
|||
|
|
@ -785,7 +785,8 @@ void initJitScriptBindings(PyObject* module) {
|
|||
try {
|
||||
return toPyObject(self.attr(name));
|
||||
} catch (const ObjectAttributeError& err) {
|
||||
throw AttributeError("%s", err.what());
|
||||
pybind11::set_error(PyExc_AttributeError, err.what());
|
||||
throw py::error_already_set();
|
||||
}
|
||||
})
|
||||
.def(
|
||||
|
|
@ -806,7 +807,8 @@ void initJitScriptBindings(PyObject* module) {
|
|||
}
|
||||
return toPyObject(self.attr(name));
|
||||
} catch (const ObjectAttributeError& err) {
|
||||
throw AttributeError("%s", err.what());
|
||||
pybind11::set_error(PyExc_AttributeError, err.what());
|
||||
throw py::error_already_set();
|
||||
}
|
||||
})
|
||||
.def(
|
||||
|
|
@ -836,7 +838,8 @@ void initJitScriptBindings(PyObject* module) {
|
|||
auto ivalue = toIValue(std::move(value), type);
|
||||
self.setattr(name, ivalue);
|
||||
} catch (const ObjectAttributeError& err) {
|
||||
throw AttributeError("%s", err.what());
|
||||
pybind11::set_error(PyExc_AttributeError, err.what());
|
||||
throw py::error_already_set();
|
||||
}
|
||||
})
|
||||
.def(
|
||||
|
|
|
|||
Loading…
Reference in a new issue