Skip to content

Commit

Permalink
Merge pull request #4532 from mwichmann/exception-convert
Browse files Browse the repository at this point in the history
Improve conversion to BuildError
  • Loading branch information
bdbaddog committed May 19, 2024
2 parents 37c7473 + 4b52a1f commit 04c5b6e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
old Python 2-only code block in a test.
- scons-time tests now supply a "filter" argument to tarfile.extract
to quiet a warning which was added in Python 3.13 beta 1.
- Improved the conversion of a "foreign" exception from an action
into BuildError by making sure our defaults get applied even in
corner cases. Fixes #4530.
- Restructured API docs build (Sphinx) so main module contents appear
on a given page *before* the submodule docs, not after. Also
tweaked the Util package doc build so it's structured more like the
Expand Down
3 changes: 3 additions & 0 deletions RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ FIXES
-----

- OSErrors are now no longer hidden during the execution of Actions.
- Improved the conversion of a "foreign" exception from an action
into BuildError by making sure our defaults get applied even in
corner cases. Fixes Issue #4530

IMPROVEMENTS
------------
Expand Down
8 changes: 6 additions & 2 deletions SCons/Errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ def convert_to_BuildError(status, exc_info=None):
# (for example, failure to create the directory in which the
# target file will appear).
filename = getattr(status, 'filename', None)
strerror = getattr(status, 'strerror', str(status))
errno = getattr(status, 'errno', 2)
strerror = getattr(status, 'strerror', None)
if strerror is None:
strerror = str(status)
errno = getattr(status, 'errno', None)
if errno is None:
errno = 2

buildError = BuildError(
errstr=strerror,
Expand Down
52 changes: 37 additions & 15 deletions SCons/ErrorsTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_BuildError(self):
assert e.command == "c"

try:
raise SCons.Errors.BuildError("n", "foo", 57, 3, "file",
raise SCons.Errors.BuildError("n", "foo", 57, 3, "file",
"e", "a", "c", (1,2,3))
except SCons.Errors.BuildError as e:
assert e.errstr == "foo", e.errstr
Expand Down Expand Up @@ -96,26 +96,48 @@ def test_ExplicitExit(self):
assert e.node == "node"

def test_convert_EnvironmentError_to_BuildError(self) -> None:
"""Test the convert_to_BuildError function on SConsEnvironmentError
exceptions.
"""
"""Test convert_to_BuildError on SConsEnvironmentError."""
ee = SCons.Errors.SConsEnvironmentError("test env error")
be = SCons.Errors.convert_to_BuildError(ee)
assert be.errstr == "test env error"
assert be.status == 2
assert be.exitstatus == 2
assert be.filename is None
with self.subTest():
self.assertEqual(be.errstr, "test env error")
with self.subTest():
self.assertEqual(be.status, 2)
with self.subTest():
self.assertEqual(be.exitstatus, 2)
with self.subTest():
self.assertIsNone(be.filename)

def test_convert_OSError_to_BuildError(self) -> None:
"""Test the convert_to_BuildError function on OSError
exceptions.
"""
"""Test convert_to_BuildError on OSError."""
ose = OSError(7, 'test oserror')
be = SCons.Errors.convert_to_BuildError(ose)
assert be.errstr == 'test oserror'
assert be.status == 7
assert be.exitstatus == 2
assert be.filename is None
with self.subTest():
self.assertEqual(be.errstr, 'test oserror')
with self.subTest():
self.assertEqual(be.status, 7)
with self.subTest():
self.assertEqual(be.exitstatus, 2)
with self.subTest():
self.assertIsNone(be.filename)

def test_convert_phony_OSError_to_BuildError(self) -> None:
"""Test convert_to_BuildError on OSError with defaults."""
class PhonyException(OSError):
def __init__(self, name):
OSError.__init__(self, name) # most fields will default to None
self.name = name

ose = PhonyException("test oserror")
be = SCons.Errors.convert_to_BuildError(ose)
with self.subTest():
self.assertEqual(be.errstr, 'test oserror')
with self.subTest():
self.assertEqual(be.status, 2)
with self.subTest():
self.assertEqual(be.exitstatus, 2)
with self.subTest():
self.assertIsNone(be.filename)


if __name__ == "__main__":
Expand Down

0 comments on commit 04c5b6e

Please sign in to comment.