Skip to content

Commit bd684a0

Browse files
committed
Another attempt to ensure __repr__ works on both python2 and 3, including for unicode units from CDS
1 parent 50448b3 commit bd684a0

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

‎astropy/units/core.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ..extern.six.moves import zip
1212

1313
import inspect
14-
import sys
14+
import numbers
1515
import textwrap
1616
import warnings
1717
import numpy as np
@@ -490,7 +490,11 @@ def __unicode__(self):
490490
__str__ = __unicode__
491491

492492
def __repr__(self):
493-
return 'Unit({0!r})'.format(unit_format.Generic().to_string(self))
493+
string = unit_format.Generic().to_string(self)
494+
if six.PY2:
495+
string = string.encode('unicode_escape')
496+
497+
return 'Unit("{0}")'.format(string)
494498

495499
def _get_physical_type_id(self):
496500
"""
@@ -655,7 +659,11 @@ def __rmul__(self, m):
655659
from .quantity import Quantity
656660
return m * Quantity(1, self)
657661

662+
<<<<<<< HEAD
658663
if six.PY3:
664+
=======
665+
if six.PY3: # pragma: no cover
666+
>>>>>>> Another attempt to ensure __repr__ works on both python2 and 3, including for unicode units from CDS
659667
def __hash__(self):
660668
# Since this class defines __eq__, it will become unhashable
661669
# on Python 3.x, so we need to define our own hash.
@@ -1699,9 +1707,14 @@ def __call__(self, s, represents=None, format=None, namespace=None,
16991707
format = 'generic'
17001708

17011709
f = unit_format.get_format(format)
1710+
<<<<<<< HEAD
17021711
if six.PY3:
17031712
if isinstance(s, bytes):
17041713
s = s.decode('ascii')
1714+
=======
1715+
if six.PY3 and isinstance(s, bytes):
1716+
s = s.decode('ascii')
1717+
>>>>>>> Another attempt to ensure __repr__ works on both python2 and 3, including for unicode units from CDS
17051718

17061719
try:
17071720
return f.parse(s)

0 commit comments

Comments
 (0)