Skip to content
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

Improve conversion to BuildError #4532

Merged
merged 3 commits into from
May 19, 2024
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
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
Loading