Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core python3 support #30

Merged
merged 31 commits into from Mar 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
154d0ec
fix isinstance long, int checks for python3
jck Feb 1, 2015
43b0653
Add compatibility layer for dealing with long
jck Feb 1, 2015
9e756bf
add builtins to _compat
jck Feb 1, 2015
9887ad3
add string_types to _compat
jck Feb 1, 2015
1c3a05d
fix builtins import
jck Feb 1, 2015
a8def54
fix typo in _compat
jck Feb 1, 2015
d9e7105
fix string_types in _compat
jck Feb 1, 2015
475b3e7
remove unused import, var
jck Feb 1, 2015
ebeb57e
move StringIO to compat
jck Feb 1, 2015
cb4785a
move classtype to compat
jck Feb 2, 2015
185d0dd
fix __builtin__ references in _analyze
jck Feb 2, 2015
64be570
fix long compat in _bin.py
jck Feb 2, 2015
5c8a291
Use sys.maxsize instead of maxint in core tests
jck Feb 2, 2015
8ec439d
fix dict.has_key usage for python3
jck Feb 2, 2015
1c7fe1f
Fix iterator next() methods for python3
jck Feb 2, 2015
0009d7b
Remove deprecated _unparse module
jck Feb 2, 2015
1530585
fix long references in _signal
jck Feb 2, 2015
64e477c
make test_intbv use operator rather than exec
jck Feb 2, 2015
aec0ee6
import compat long to test_Signal
jck Feb 2, 2015
0671c4f
make test_Signal use operator rather than exec
jck Feb 2, 2015
adcd90e
correctly sort _futureevents
jck Feb 2, 2015
e6bf9e0
make list range before assigning in test_sim
jck Feb 2, 2015
a213fa4
test_concat import reduce, long for python3
jck Feb 2, 2015
79704da
more long->integer_types fixes
jck Feb 2, 2015
d0dc39d
fix usage of x in integer_types,..
jck Feb 6, 2015
969c122
use py3 compatible method of merging dicts
jck Feb 6, 2015
8137e6c
replace backqote with repr everywhere excpt conversion test
jck Mar 11, 2015
f2274bd
replace __nonzero__ with __bool__
jck Mar 9, 2015
b2948f4
use future division in signal, intbv
jck Mar 9, 2015
b3416d4
fix intbv.signed behavior when min is None
jck Mar 11, 2015
a2fd9c9
test_Signal: use set to check equality of lists
jck Mar 9, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions myhdl/_Signal.py
Expand Up @@ -33,10 +33,12 @@
from copy import copy, deepcopy
import operator

from myhdl._compat import integer_types, long
from myhdl import _simulator as sim
from myhdl._simulator import _signals, _siglist, _futureEvents, now
from myhdl._intbv import intbv
from myhdl._bin import bin

# from myhdl._enum import EnumItemType

_schedule = _futureEvents.append
Expand Down Expand Up @@ -136,8 +138,8 @@ def __init__(self, val=None):
self._setNextVal = self._setNextBool
self._printVcd = self._printVcdBit
self._nrbits = 1
elif isinstance(val, (int, long)):
self._type = (int, long)
elif isinstance(val, integer_types):
self._type = integer_types
self._setNextVal = self._setNextInt
elif isinstance(val, intbv):
self._type = intbv
Expand Down Expand Up @@ -191,7 +193,7 @@ def _update(self):
self._val = None
elif isinstance(val, intbv):
self._val._val = next._val
elif isinstance(val, (int, long, EnumItemType)):
elif isinstance(val, (integer_types, EnumItemType)):
self._val = next
else:
self._val = deepcopy(next)
Expand Down Expand Up @@ -273,14 +275,14 @@ def _setNextBool(self, val):
def _setNextInt(self, val):
if isinstance(val, intbv):
val = val._val
elif not isinstance(val, (int, long)):
elif not isinstance(val, (integer_types, intbv)):
raise TypeError("Expected int or intbv, got %s" % type(val))
self._next = val

def _setNextIntbv(self, val):
if isinstance(val, intbv):
val = val._val
elif not isinstance(val, (int, long)):
elif not isinstance(val, integer_types):
raise TypeError("Expected int or intbv, got %s" % type(val))
self._next._val = val
self._next._handleBounds()
Expand Down Expand Up @@ -321,11 +323,10 @@ def __hash__(self):
raise TypeError("Signals are unhashable")


def __nonzero__(self):
if self._val:
return 1
else:
return 0
def __bool__(self):
return bool(self._val)

__nonzero__ = __bool__

# length
def __len__(self):
Expand Down Expand Up @@ -363,21 +364,13 @@ def __mul__(self, other):
def __rmul__(self, other):
return other * self._val

def __div__(self, other):
def __truediv__(self, other):
if isinstance(other, _Signal):
return self._val / other._val
else:
return self._val / other
def __rdiv__(self, other):
return other / self._val

def __truediv__(self, other):
if isinstance(other, _Signal):
return operator.truediv(self._val, other._val)
else:
return operator.truediv(self._val, other)
def __rtruediv__(self, other):
return operator.truediv(other, self._val)
return other / self._val

def __floordiv__(self, other):
if isinstance(other, _Signal):
Expand Down Expand Up @@ -514,8 +507,10 @@ def _toVerilog(self):
def _augm(self):
raise TypeError("Signal object doesn't support augmented assignment")

__iadd__ = __isub__ = __idiv__ = __imul__ = __ipow__ = __imod__ = _augm
__iadd__ = __isub__ = __imul__ = __ipow__ = __imod__ = _augm
__ior__ = __iand__ = __ixor__ = __irshift__ = __ilshift__ = _augm
__itruediv__ = __ifloordiv__ = _augm


# index and slice assignment not supported
def __setitem__(self, key, val):
Expand Down
3 changes: 2 additions & 1 deletion myhdl/_Simulation.py
Expand Up @@ -24,6 +24,7 @@

import sys
import os
from operator import itemgetter
from warnings import warn
from types import GeneratorType

Expand Down Expand Up @@ -157,7 +158,7 @@ def run(self, duration=None, quiet=0):
if t == maxTime:
raise _SuspendSimulation(
"Simulated %s timesteps" % duration)
_futureEvents.sort()
_futureEvents.sort(key=itemgetter(0))
t = _simulator._time = _futureEvents[0][0]
if tracing:
print("#%s" % t, file=tracefile)
Expand Down
12 changes: 6 additions & 6 deletions myhdl/_Waiter.py
Expand Up @@ -67,7 +67,7 @@ def next(self, waiters, actives, exc):
clone = _Waiter(self.generator, self.caller)

try:
clause = self.generator.next()
clause = next(self.generator)
except StopIteration:
if self.caller:
waiters.append(self.caller)
Expand Down Expand Up @@ -126,7 +126,7 @@ def __init__(self, generator):
self.generator = generator

def next(self, waiters, actives, exc):
clause = self.generator.next()
clause = next(self.generator)
schedule((_simulator._time + clause._time, self))


Expand All @@ -139,7 +139,7 @@ def __init__(self, generator):
self.hasRun = 0

def next(self, waiters, actives, exc):
clause = self.generator.next()
clause = next(self.generator)
clause.append(self)


Expand All @@ -154,7 +154,7 @@ def __init__(self, generator):
def next(self, waiters, actives, exc):
if self.hasRun:
raise StopIteration
clauses = self.generator.next()
clauses = next(self.generator)
self.hasRun = 1
clone = _EdgeTupleWaiter(self.generator)
for clause in clauses:
Expand All @@ -171,7 +171,7 @@ def __init__(self, generator):
self.hasRun = 0

def next(self, waiters, actives, exc):
clause = self.generator.next()
clause = next(self.generator)
clause._eventWaiters.append(self)


Expand All @@ -186,7 +186,7 @@ def __init__(self, generator):
def next(self, waiters, actives, exc):
if self.hasRun:
raise StopIteration
clauses = self.generator.next()
clauses = next(self.generator)
self.hasRun = 1
clone = _SignalTupleWaiter(self.generator)
for clause in clauses:
Expand Down
2 changes: 1 addition & 1 deletion myhdl/_bin.py
Expand Up @@ -20,7 +20,7 @@
""" module with the bin function.

"""

from myhdl._compat import long


def _int2bitstring(num):
Expand Down
22 changes: 22 additions & 0 deletions myhdl/_compat.py
@@ -0,0 +1,22 @@
import sys
import types

PY2 = sys.version_info[0] == 2


if not PY2:
string_types = (str,)
integer_types = (int,)
long = int
class_types = (type,)

from io import StringIO
import builtins
else:
string_types = (str, unicode)
integer_types = (int, long)
long = long
class_types = (type, types.ClassType)

from cStringIO import StringIO
import __builtin__ as builtins
8 changes: 6 additions & 2 deletions myhdl/_concat.py
Expand Up @@ -22,15 +22,19 @@
"""
from __future__ import absolute_import

from myhdl._compat import integer_types
from myhdl._intbv import intbv
from myhdl._Signal import _Signal
from myhdl._compat import long



def concat(base, *args):

if isinstance(base, intbv):
basewidth = base._nrbits
val = base._val
elif isinstance(base, (int, long)):
elif isinstance(base, integer_types):
if isinstance(base, bool):
basewidth = 1
else:
Expand Down Expand Up @@ -72,7 +76,7 @@ def concat(base, *args):
if not w:
raise TypeError("concat: arg on pos %d should have length" % (i+1))
width += w
val = val << w | v & (1L << w)-1
val = val << w | v & (long(1) << w)-1

if basewidth:
return intbv(val, _nrbits=basewidth + width)
Expand Down
4 changes: 3 additions & 1 deletion myhdl/_delay.py
Expand Up @@ -18,7 +18,9 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

""" Module that provides the delay class."""
from __future__ import absolute_import

from myhdl._compat import integer_types

_errmsg = "arg of delay constructor should be a natural integeer"

Expand All @@ -33,6 +35,6 @@ def __init__(self, val):
val -- a natural integer representing the desired delay

"""
if not isinstance(val, (int, long)) or val < 0:
if not isinstance(val, integer_types) or val < 0:
raise TypeError(_errmsg)
self._time = val
7 changes: 3 additions & 4 deletions myhdl/_enum.py
Expand Up @@ -23,10 +23,9 @@
from __future__ import absolute_import


from types import StringType

from myhdl._bin import bin
from myhdl._Signal import _Signal
from myhdl._compat import string_types

class EnumType(object):
def __init__(self):
Expand All @@ -53,9 +52,9 @@ def enum(*names, **kwargs):
codedict = {}
i = 0
for name in names:
if not isinstance(name, StringType):
if not isinstance(name, string_types):
raise TypeError()
if codedict.has_key(name):
if name in codedict:
raise ValueError("enum literals should be unique")
if encoding == "one_hot":
code = bin(1<<i, nrbits)
Expand Down