Skip to content

Commit

Permalink
%time foo() output is often copied into code comments to explain
Browse files Browse the repository at this point in the history
performance improvements. The `\xb5` Latin Extended micro sign and the
`\u03bc` Greek small letter mu have different codes but often look
identical.

Output mu to align with:

* [The International System of Units (SI) brochure](
  https://www.bipm.org/documents/20126/41483022/SI-Brochure-9-EN.pdf
  ), such as Table 7 SI prefixes
* NFKC normalized [Python code](https://peps.python.org/pep-3131/
  ) and [domain names](https://unicode.org/reports/tr36/). For example:
```sh
python -c 'print("""class C:
    \xb5=1
print(hex(ord(dir(C)[-1])))""")' | tee /dev/fd/2 | python -
```
```python
class C:
    µ=1
print(hex(ord(dir(C)[-1])))
```
`0x3bc`
* Section 2.5 Duplicated Characters of [Unicode Technical Report 25](
  https://www.unicode.org/reports/tr25/)
> ...U+03BC μ is the preferred character in a Unicode context.
* Ruff confusable mapping [updates](
  https://github.com/astral-sh/ruff/pull/4430/files
  ),currently in the "preview" stage

Add a unit test for UTF-8 display and
https://bugs.launchpad.net/ipython/+bug/348466 ASCII fallback.
  • Loading branch information
covracer committed May 1, 2024
1 parent f9982db commit 08e0d22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions IPython/core/magics/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
units = [u"s", u"ms",u'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"]
u'\u03bc'.encode(sys.stdout.encoding)
units = [u"s", u"ms",u'\u03bcs',"ns"]
except:
pass
scaling = [1, 1e3, 1e6, 1e9]
Expand Down
6 changes: 6 additions & 0 deletions IPython/core/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ 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'

# Test %%capture magic. Added to test issue #13926
def test_capture():
Expand Down

0 comments on commit 08e0d22

Please sign in to comment.