Skip to content

Commit

Permalink
Add standard error to exception message
Browse files Browse the repository at this point in the history
The rationale is that when castxml has an error it raises a RuntimeError to the user with the message.
However, the message only contains the stdout from castxml - this makes the RuntimeError contain no information about what actually went wrong.
This change allows the user to add error handling to their applications based on what went wrong with castxml (such as changing their include directories, etc)
  • Loading branch information
allisonChilton committed Apr 10, 2021
1 parent aa5920c commit c1ea281
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pygccxml/parser/source_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def create_xml_file(self, source_file, destination=None):
process = subprocess.Popen(
args=command_line,
shell=True,
stdout=subprocess.PIPE)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

try:
results = []
Expand All @@ -242,9 +243,12 @@ def create_xml_file(self, source_file, destination=None):
for line in process.stdout.readlines():
if line.strip():
results.append(line.rstrip())
for line in process.stderr.readlines():
if line.strip():
results.append(line.rstrip())

exit_status = process.returncode
msg = os.linesep.join([str(s) for s in results])
msg = os.linesep.join([str(s.decode()) for s in results])
if self.__config.ignore_gccxml_output:
if not os.path.isfile(xml_file):
raise RuntimeError(
Expand Down
12 changes: 12 additions & 0 deletions unittests/source_reader_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# See http://www.boost.org/LICENSE_1_0.txt

import unittest
import os

from . import parser_test_case

Expand Down Expand Up @@ -31,6 +32,17 @@ def test_compound_argument_type(self):
self.assertTrue(do_smth, "unable to find do_smth")
do_smth.function_type()

def test_stderr_present_and_readable(self):
with open(os.path.join('unittests', 'data', self.header), 'r') as f:
source_str = f.read()

err_str = "add some stuff that should not compile"
source_str += err_str
with self.assertRaises(RuntimeError) as e_context:
decls = parser.parse_string(source_str, self.config)

self.assertIn(err_str, e_context.exception.args[0])


def create_suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit c1ea281

Please sign in to comment.