Port to Python 3.
[catacomb-python] / catacomb / __init__.py
index 5483cca..0e5c31c 100644 (file)
@@ -55,7 +55,8 @@ if _dlflags >= 0:
       else: pass # can't do this.
   _sys.setdlopenflags(_dlflags)
 
-import _base
+if _sys.version_info >= (3,): from . import _base
+else: import _base
 
 if _odlflags >= 0:
   _sys.setdlopenflags(_odlflags)
@@ -78,14 +79,23 @@ def default_lostexchook(why, ty, val, tb):
 lostexchook = default_lostexchook
 
 ## Text/binary conversions.
-def _bin(s): return s
+if _sys.version_info >= (3,):
+  def _bin(s): return s.encode('iso8859-1')
+else:
+  def _bin(s): return s
 
 ## Iterating over dictionaries.
-def _iteritems(dict): return dict.iteritems()
-def _itervalues(dict): return dict.itervalues()
+if _sys.version_info >= (3,):
+  def _iteritems(dict): return dict.items()
+  def _itervalues(dict): return dict.values()
+else:
+  def _iteritems(dict): return dict.iteritems()
+  def _itervalues(dict): return dict.itervalues()
 
 ## The built-in bignum type.
-_long = long
+try: long
+except NameError: _long = int
+else: _long = long
 
 ## How to fix a name back into the right identifier.  Alas, the rules are not
 ## consistent.
@@ -177,14 +187,32 @@ def _pp_dict(pp, items):
   _pp_commas(pp, p, items)
 
 ###--------------------------------------------------------------------------
+### Mappings.
+
+if _sys.version_info >= (3,):
+  class _tmp:
+    def __str__(me): return '%s(%r)' % (type(me).__name__, list(me))
+    __repr__ = __str__
+    def _repr_pretty_(me, pp, cyclep):
+      ind = _pp_bgroup_tyname(pp, me, '([')
+      _pp_commas(pp, pp.pretty, me)
+      pp.end_group(ind, '])')
+  _augment(_base._KeyView, _tmp)
+  _augment(_base._ValueView, _tmp)
+  _augment(_base._ItemView, _tmp)
+
+###--------------------------------------------------------------------------
 ### Bytestrings.
 
 class _tmp:
   def fromhex(x):
     return ByteString(_unhexify(x))
   fromhex = staticmethod(fromhex)
-  def hex(me): return _hexify(me)
-  __hex__ = hex
+  if _sys.version_info >= (3,):
+    def hex(me): return _hexify(me).decode()
+  else:
+    def hex(me): return _hexify(me)
+    __hex__ = hex
   def __repr__(me):
     return 'bytes(%r)' % me.hex()
 _augment(ByteString, _tmp)
@@ -388,8 +416,9 @@ class BaseRat (object):
   def __rtruediv__(me, you):
     n, d = _split_rat(you)
     return type(me)(me._d*n, me._n*d)
-  __div__ = __truediv__
-  __rdiv__ = __rtruediv__
+  if _sys.version_info < (3,):
+    __div__ = __truediv__
+    __rdiv__ = __rtruediv__
   def _order(me, you, op):
     n, d = _split_rat(you)
     return op(me._n*d, n*me._d)
@@ -425,8 +454,9 @@ class _tmp:
   def __rtruediv__(me, you):
     if isinstance(you, float): return you/_long(me)
     else: return IntRat(you, me)
-  __div__ = __truediv__
-  __rdiv__ = __rtruediv__
+  if _sys.version_info < (3,):
+    __div__ = __truediv__
+    __rdiv__ = __rtruediv__
   _repr_pretty_ = _pp_str
 _augment(MP, _tmp)
 
@@ -439,8 +469,9 @@ class _tmp:
   def quadsolve(x, y): return x.reduce().quadsolve(y)
   def __truediv__(me, you): return GFRat(me, you)
   def __rtruediv__(me, you): return GFRat(you, me)
-  __div__ = __truediv__
-  __rdiv__ = __rtruediv__
+  if _sys.version_info < (3,):
+    __div__ = __truediv__
+    __rdiv__ = __rtruediv__
   _repr_pretty_ = _pp_str
 _augment(GF, _tmp)