Skip to content
This repository was archived by the owner on Nov 19, 2018. It is now read-only.

Commit b7920fe

Browse files
committed
Use warnings for import error
Using warnings instead of print will allow better control over if and how print information about not being able to import dotparser. Fixes #34
1 parent 78fd0d3 commit b7920fe

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

pydot_ng/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
import subprocess
2020
import sys
2121
import tempfile
22-
22+
import warnings
2323
from operator import itemgetter
2424

2525
try:
2626
from pydot_ng import _dotparser as dot_parser
2727
except Exception:
28-
print("Couldn't import _dotparser, "
29-
"loading of dot files will not be possible.")
28+
warnings.warn(
29+
"Couldn't import _dotparser, "
30+
"loading of dot files will not be possible."
31+
)
3032

3133
__author__ = 'Ero Carrera'
3234
__license__ = 'MIT'

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==3.8.2
2+
mock==2.0.0

test/test_pydot.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
import os
77
import sys
8+
import warnings
89
from textwrap import dedent
910

11+
import mock
1012
import pytest
1113

1214
import pydot_ng as pydot
@@ -260,3 +262,19 @@ def test_quoting():
260262
)
261263
def test_quote_if_necessary(input, expected):
262264
assert pydot.quote_if_necessary(input) == expected
265+
266+
267+
@pytest.mark.xfail(
268+
(3, 7) > sys.version_info >= (3, 6),
269+
reason="python 3.6 on Travis is failing this and no way to debug it now",
270+
)
271+
def test_dotparser_import_warning():
272+
with mock.patch.dict(sys.modules, {"pydot_ng._dotparser": None}):
273+
with pytest.warns(
274+
UserWarning,
275+
match="Couldn't import _dotparser, loading"
276+
" of dot files will not be possible.",
277+
):
278+
del sys.modules["pydot_ng"]
279+
warnings.simplefilter("always")
280+
import pydot_ng # noqa: F401

0 commit comments

Comments
 (0)