Skip to content

Commit

Permalink
decode stdout and stderr of CallledProcessError in non-delegating branch
Browse files Browse the repository at this point in the history
  • Loading branch information
xflr6 committed Dec 9, 2021
1 parent b194dc2 commit ded35d9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
28 changes: 14 additions & 14 deletions examples/graphviz-escapes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"data": {
"text/plain": [
"('0.19', (2, 49, 3))"
"('0.19.1.dev0', (2, 49, 3))"
]
},
"execution_count": 1,
Expand Down Expand Up @@ -75,7 +75,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d84834c0>"
"<graphviz.graphs.Digraph at 0x2a9c5b2bd60>"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -125,7 +125,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d84839d0>"
"<graphviz.graphs.Digraph at 0x2a9c5b2b6d0>"
]
},
"execution_count": 4,
Expand All @@ -146,10 +146,10 @@
"name": "stderr",
"output_type": "stream",
"text": [
"Error: <stdin>: syntax error in line 4 scanning a quoted string (missing endquote? longer than 16384?)\n",
"String starting:\"\"]\n",
"}\n",
"\n"
"Error: <stdin>: syntax error in line 4 scanning a quoted string (missing endquote? longer than 16384?)\r\n",
"String starting:\"\"]\r\n",
"}\r\n",
"\r\n"
]
},
{
Expand All @@ -165,7 +165,7 @@
],
"source": [
"try:\n",
" make_graph(node_label='\\\\').pipe(format='svg', encoding='utf-8');\n",
" make_graph(node_label='\\\\').pipe(format='svg', encoding='ascii');\n",
"except graphviz.CalledProcessError as e:\n",
" assert 'syntax error' in e.stderr"
]
Expand Down Expand Up @@ -208,7 +208,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d8483b80>"
"<graphviz.graphs.Digraph at 0x2a9c5b2b1c0>"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -258,7 +258,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d8483340>"
"<graphviz.graphs.Digraph at 0x2a9c5b2be80>"
]
},
"execution_count": 7,
Expand Down Expand Up @@ -315,7 +315,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d8483730>"
"<graphviz.graphs.Digraph at 0x2a9c5b2b9d0>"
]
},
"execution_count": 8,
Expand Down Expand Up @@ -366,7 +366,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d84d0340>"
"<graphviz.graphs.Digraph at 0x2a9c5b784f0>"
]
},
"execution_count": 9,
Expand Down Expand Up @@ -419,7 +419,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d8483c10>"
"<graphviz.graphs.Digraph at 0x2a9c5b2bdc0>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -471,7 +471,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x211d8482f80>"
"<graphviz.graphs.Digraph at 0x2a9c5acbd90>"
]
},
"execution_count": 11,
Expand Down
6 changes: 3 additions & 3 deletions graphviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

from .backend import (DOT_BINARY, UNFLATTEN_BINARY,
render, pipe, pipe_string, pipe_lines, pipe_lines_string,
unflatten, version, view,
ExecutableNotFound, CalledProcessError)
unflatten, version, view)
from .exceptions import (RequiredArgumentError, FileExistsError,
UnknownSuffixWarning, FormatSuffixMismatchWarning)
UnknownSuffixWarning, FormatSuffixMismatchWarning,
ExecutableNotFound, CalledProcessError)
from .graphs import Graph, Digraph
from .jupyter_integration import SUPPORTED_JUPYTER_FORMATS
from .parameters import ENGINES, FORMATS, RENDERERS, FORMATTERS
Expand Down
5 changes: 4 additions & 1 deletion graphviz/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Commonly used exception classes."""

from .backend.execute import ExecutableNotFound, CalledProcessError

__all__ = ['RequiredArgumentError', 'FileExistsError',
'UnknownSuffixWarning', 'FormatSuffixMismatchWarning']
'UnknownSuffixWarning', 'FormatSuffixMismatchWarning',
'ExecutableNotFound', 'CalledProcessError']


class RequiredArgumentError(TypeError):
Expand Down
14 changes: 12 additions & 2 deletions graphviz/piping.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from . import _tools
from . import backend
from . import exceptions
from . import base
from . import encoding

Expand Down Expand Up @@ -136,6 +137,15 @@ def _pipe_future(self, format: typing.Optional[str] = None, *,
if codecs.lookup(encoding) is codecs.lookup(self.encoding):
# common case: both stdin and stdout need the same encoding
return self._pipe_lines_string(*args, encoding=encoding, **kwargs)
raw = self._pipe_lines(*args, input_encoding=self.encoding, **kwargs)
return raw.decode(encoding)
try:
raw = self._pipe_lines(*args, input_encoding=self.encoding, **kwargs)
except exceptions.CalledProcessError as e:
*args, output, stderr = e.args
if output is not None:
output = output.decode(self.encoding)
if stderr is not None:
stderr = stderr.decode(self.encoding)
raise e.__class__(*args, output=output, stderr=stderr)
else:
return raw.decode(encoding)
return self._pipe_lines(*args, input_encoding=self.encoding, **kwargs)
21 changes: 21 additions & 0 deletions tests/test_all_classes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import locale
import pathlib
import re
import subprocess

import pytest

Expand Down Expand Up @@ -227,6 +228,26 @@ def test_pipe_lines_mocked(mocker, mock_pipe_lines, dot, format_='svg'):
assert list(data) == expected_lines


@pytest.mark.exe
def test_pipe_lines_called_process_error(cls, encoding='ascii',
input_encoding='utf-8'):
kwargs = {'encoding': input_encoding}
if cls.__name__ == 'Source':
dot = cls('graph { spam -- \\ }', **kwargs)
else:
dot = cls(**kwargs)
dot.edge('spam', '\\')

assert encoding != input_encoding

with pytest.raises(subprocess.CalledProcessError,
match=r'syntax error') as info:
dot.pipe(format='svg', encoding=encoding)

assert isinstance(info.value.stderr, str)
assert 'syntax error' in info.value.stderr


def test_repr_mimebundle_image_svg_xml_mocked(mocker, dot):
mock_pipe = mocker.patch.object(dot, 'pipe', autospec=True)

Expand Down

0 comments on commit ded35d9

Please sign in to comment.