Skip to content

Add BaseException.__reduce__ #3665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,8 +1703,6 @@ def test_inconsistent_converters_state(self):
class ExceptionPicklingTestCase(unittest.TestCase):
"""Tests for issue #13760: ConfigParser exceptions are not picklable."""

# TODO: RUSTPYTHON Exception.__reduce__ missing.
@unittest.expectedFailure
def test_error(self):
import pickle
e1 = configparser.Error('value')
Expand All @@ -1714,8 +1712,6 @@ def test_error(self):
self.assertEqual(e1.message, e2.message)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_nosectionerror(self):
import pickle
e1 = configparser.NoSectionError('section')
Expand All @@ -1727,8 +1723,6 @@ def test_nosectionerror(self):
self.assertEqual(e1.section, e2.section)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_nooptionerror(self):
import pickle
e1 = configparser.NoOptionError('option', 'section')
Expand All @@ -1741,8 +1735,6 @@ def test_nooptionerror(self):
self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_duplicatesectionerror(self):
import pickle
e1 = configparser.DuplicateSectionError('section', 'source', 123)
Expand All @@ -1756,8 +1748,6 @@ def test_duplicatesectionerror(self):
self.assertEqual(e1.lineno, e2.lineno)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_duplicateoptionerror(self):
import pickle
e1 = configparser.DuplicateOptionError('section', 'option', 'source',
Expand All @@ -1773,8 +1763,6 @@ def test_duplicateoptionerror(self):
self.assertEqual(e1.lineno, e2.lineno)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_interpolationerror(self):
import pickle
e1 = configparser.InterpolationError('option', 'section', 'msg')
Expand All @@ -1787,8 +1775,6 @@ def test_interpolationerror(self):
self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_interpolationmissingoptionerror(self):
import pickle
e1 = configparser.InterpolationMissingOptionError('option', 'section',
Expand All @@ -1803,8 +1789,6 @@ def test_interpolationmissingoptionerror(self):
self.assertEqual(e1.reference, e2.reference)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_interpolationsyntaxerror(self):
import pickle
e1 = configparser.InterpolationSyntaxError('option', 'section', 'msg')
Expand All @@ -1817,8 +1801,6 @@ def test_interpolationsyntaxerror(self):
self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_interpolationdeptherror(self):
import pickle
e1 = configparser.InterpolationDepthError('option', 'section',
Expand All @@ -1832,8 +1814,6 @@ def test_interpolationdeptherror(self):
self.assertEqual(e1.option, e2.option)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_parsingerror(self):
import pickle
e1 = configparser.ParsingError('source')
Expand Down Expand Up @@ -1861,8 +1841,6 @@ def test_parsingerror(self):
self.assertEqual(e1.errors, e2.errors)
self.assertEqual(repr(e1), repr(e2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_missingsectionheadererror(self):
import pickle
e1 = configparser.MissingSectionHeaderError('filename', 123, 'line')
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1923,8 +1923,6 @@ def test_non_str_argument(self):
exc = ImportError(arg)
self.assertEqual(str(arg), str(exc))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_copy_pickle(self):
for kwargs in (dict(),
dict(name='somename'),
Expand Down
2 changes: 0 additions & 2 deletions Lib/unittest/test/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,6 @@ def test_discover_with_modules_that_fail_to_import(self):
with self.assertRaises(ImportError):
test.test_this_does_not_exist()

# TODO: RUSTPYTHON ImportError.__reduce__ missing
@unittest.expectedFailure
def test_discover_with_init_modules_that_fail_to_import(self):
vfs = {abspath('/foo'): ['my_package'],
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
Expand Down
9 changes: 9 additions & 0 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,15 @@ impl PyBaseException {
let cls = zelf.class();
format!("{}({})", cls.name(), repr_args.iter().format(", "))
}

#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it doesn't require type checking, this may be:

Suggested change
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
fn reduce(zelf: PyObjectRef, vm: &VirtualMachine) -> PyTupleRef {

Copy link
Contributor Author

@fanninpm fanninpm Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work in practice because rustc will complain:

no method named `args` found for struct `core::PyObjectRef` in the current scope
method not found in `core::PyObjectRef`

if let Some(dict) = zelf.as_object().dict().filter(|x| !x.is_empty()) {
return vm.new_tuple((zelf.class().clone(), zelf.args(), dict));
} else {
return vm.new_tuple((zelf.class().clone(), zelf.args()));
}
}
}

impl ExceptionZoo {
Expand Down