diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index f3688f4..bf33ea3 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1592,17 +1592,17 @@ def _format_time(timespan, precision=3): break return " ".join(time) - - # Unfortunately the unicode 'micro' symbol can cause problems in - # certain terminals. + + # Unfortunately characters outside of range(128) can cause problems in + # certain terminals. # 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 save 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'\xb5'.encode(sys.stdout.encoding) - units = [u"s", u"ms",u'\xb5s',"ns"] + "μ".encode(sys.stdout.encoding) + units = ["s", "ms", "μs", "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 da998fa..c757b9c 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -530,6 +530,14 @@ def test_time_local_ns(): 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" + + # Test %%capture magic. Added to test issue #13926 def test_capture(): ip = get_ipython()