diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 961dc5c7af..a860318b71 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1598,11 +1598,11 @@ def _format_time(timespan, precision=3): # See bug: https://bugs.launchpad.net/ipython/+bug/348466 # Try to prevent crashes by being more secure than it needs to # E.g. eclipse is able to print a ยต, but has no sys.stdout.encoding set. - units = [u"s", u"ms",u'us',"ns"] # the safe value - if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: + units = ["s", "ms", "us", "ns"] # the safe value + if hasattr(sys.stdout, "encoding") and sys.stdout.encoding: try: - u'\u03bc'.encode(sys.stdout.encoding) - units = [u"s", u"ms",u'\u03bcs',"ns"] + "\u03bc".encode(sys.stdout.encoding) + units = ["s", "ms", "\u03bcs", "ns"] except: pass scaling = [1, 1e3, 1e6, 1e9] diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index c2cad41542..c757b9cf62 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -529,12 +529,14 @@ def test_time_local_ns(): assert ip.user_ns["myvar"] == 1 del ip.user_ns["myvar"] + def test_time_microseconds_display(): """Ensure ASCII is used when necessary""" - with mock.patch('sys.stdout', io.TextIOWrapper(StringIO(), encoding='utf-8')): - assert execution._format_time(0.000001) == '1 \u03bcs' - with mock.patch('sys.stdout', io.TextIOWrapper(StringIO(), encoding='ascii')): - assert execution._format_time(0.000001) == '1 us' + with mock.patch("sys.stdout", io.TextIOWrapper(StringIO(), encoding="utf-8")): + assert execution._format_time(0.000001) == "1 \u03bcs" + with mock.patch("sys.stdout", io.TextIOWrapper(StringIO(), encoding="ascii")): + assert execution._format_time(0.000001) == "1 us" + # Test %%capture magic. Added to test issue #13926 def test_capture():