Commit b09eab07 authored by René Fritze's avatar René Fritze Committed by probot-auto-merge[bot]

[tools] fix formatsrc for non jupyter case

parent 794094b5
Pipeline #68664 failed with stages
in 26 minutes and 38 seconds
......@@ -18,7 +18,7 @@ def format_source(obj):
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter
return highlight(self.source, PythonLexer(), Terminal256Formatter())
return highlight(source, PythonLexer(), Terminal256Formatter())
except ImportError:
return source
......@@ -26,6 +26,7 @@ def format_source(obj):
def print_source(obj):
source = format_source(obj)
if is_jupyter():
from IPython.display import display, Code
display(source)
else:
print(source)
......@@ -34,6 +35,7 @@ def print_source(obj):
def source_repr(obj):
source = format_source(obj)
if is_jupyter():
from IPython.display import display, Code
display(source)
return ''
else:
......
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
import inspect
import operator
from math import sin, pi, exp, factorial
import numpy as np
......@@ -10,6 +11,7 @@ import itertools
from hypothesis import given
from pymor.core.logger import getLogger
from pymor.tools.formatsrc import print_source, source_repr
from pymor.tools.io import SafeTemporaryFileName
from pymortests.base import runmodule
from pymortests.fixtures.grid import hy_rect_or_tria_grid
......@@ -18,8 +20,7 @@ from pymor.discretizers.builtin.quadratures import GaussQuadratures
from pymor.tools.deprecated import Deprecated
from pymor.tools.floatcmp import float_cmp, float_cmp_all, compare_with_tolerance, almost_less
from pymor.vectorarrays.numpy import NumpyVectorSpace
from pymor.tools import timing
from pymor.tools import timing, formatsrc
logger = getLogger('pymortests.tools')
......@@ -183,5 +184,21 @@ def testDeprecated():
assert "DeprecationWarning" in str(w[-1].message)
def test_formatsrc():
obj = formatsrc.format_source
formatsrc.format_source(obj)
print_source(obj)
source_repr(obj)
def test_formatsrc_nopygments(monkeypatch):
try:
from pygments import highlight
monkeypatch.delattr('pygments.highlight')
except ImportError:
pass
test_formatsrc()
if __name__ == "__main__":
runmodule(filename=__file__)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment