Skip to content

Commit 62b3867

Browse files
author
Jan Decaluwe
committed
Merge branch 'jck-py3-core'
2 parents 4b70a48 + a2fd9c9 commit 62b3867

23 files changed

+206
-364
lines changed

myhdl/_Signal.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
from copy import copy, deepcopy
3434
import operator
3535

36+
from myhdl._compat import integer_types, long
3637
from myhdl import _simulator as sim
3738
from myhdl._simulator import _signals, _siglist, _futureEvents, now
3839
from myhdl._intbv import intbv
3940
from myhdl._bin import bin
41+
4042
# from myhdl._enum import EnumItemType
4143

4244
_schedule = _futureEvents.append
@@ -136,8 +138,8 @@ def __init__(self, val=None):
136138
self._setNextVal = self._setNextBool
137139
self._printVcd = self._printVcdBit
138140
self._nrbits = 1
139-
elif isinstance(val, (int, long)):
140-
self._type = (int, long)
141+
elif isinstance(val, integer_types):
142+
self._type = integer_types
141143
self._setNextVal = self._setNextInt
142144
elif isinstance(val, intbv):
143145
self._type = intbv
@@ -191,7 +193,7 @@ def _update(self):
191193
self._val = None
192194
elif isinstance(val, intbv):
193195
self._val._val = next._val
194-
elif isinstance(val, (int, long, EnumItemType)):
196+
elif isinstance(val, (integer_types, EnumItemType)):
195197
self._val = next
196198
else:
197199
self._val = deepcopy(next)
@@ -273,14 +275,14 @@ def _setNextBool(self, val):
273275
def _setNextInt(self, val):
274276
if isinstance(val, intbv):
275277
val = val._val
276-
elif not isinstance(val, (int, long)):
278+
elif not isinstance(val, (integer_types, intbv)):
277279
raise TypeError("Expected int or intbv, got %s" % type(val))
278280
self._next = val
279281

280282
def _setNextIntbv(self, val):
281283
if isinstance(val, intbv):
282284
val = val._val
283-
elif not isinstance(val, (int, long)):
285+
elif not isinstance(val, integer_types):
284286
raise TypeError("Expected int or intbv, got %s" % type(val))
285287
self._next._val = val
286288
self._next._handleBounds()
@@ -321,11 +323,10 @@ def __hash__(self):
321323
raise TypeError("Signals are unhashable")
322324

323325

324-
def __nonzero__(self):
325-
if self._val:
326-
return 1
327-
else:
328-
return 0
326+
def __bool__(self):
327+
return bool(self._val)
328+
329+
__nonzero__ = __bool__
329330

330331
# length
331332
def __len__(self):
@@ -363,21 +364,13 @@ def __mul__(self, other):
363364
def __rmul__(self, other):
364365
return other * self._val
365366

366-
def __div__(self, other):
367+
def __truediv__(self, other):
367368
if isinstance(other, _Signal):
368369
return self._val / other._val
369370
else:
370371
return self._val / other
371-
def __rdiv__(self, other):
372-
return other / self._val
373-
374-
def __truediv__(self, other):
375-
if isinstance(other, _Signal):
376-
return operator.truediv(self._val, other._val)
377-
else:
378-
return operator.truediv(self._val, other)
379372
def __rtruediv__(self, other):
380-
return operator.truediv(other, self._val)
373+
return other / self._val
381374

382375
def __floordiv__(self, other):
383376
if isinstance(other, _Signal):
@@ -514,8 +507,10 @@ def _toVerilog(self):
514507
def _augm(self):
515508
raise TypeError("Signal object doesn't support augmented assignment")
516509

517-
__iadd__ = __isub__ = __idiv__ = __imul__ = __ipow__ = __imod__ = _augm
510+
__iadd__ = __isub__ = __imul__ = __ipow__ = __imod__ = _augm
518511
__ior__ = __iand__ = __ixor__ = __irshift__ = __ilshift__ = _augm
512+
__itruediv__ = __ifloordiv__ = _augm
513+
519514

520515
# index and slice assignment not supported
521516
def __setitem__(self, key, val):

myhdl/_Simulation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import sys
2626
import os
27+
from operator import itemgetter
2728
from warnings import warn
2829
from types import GeneratorType
2930

@@ -157,7 +158,7 @@ def run(self, duration=None, quiet=0):
157158
if t == maxTime:
158159
raise _SuspendSimulation(
159160
"Simulated %s timesteps" % duration)
160-
_futureEvents.sort()
161+
_futureEvents.sort(key=itemgetter(0))
161162
t = _simulator._time = _futureEvents[0][0]
162163
if tracing:
163164
print("#%s" % t, file=tracefile)

myhdl/_Waiter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def next(self, waiters, actives, exc):
6767
clone = _Waiter(self.generator, self.caller)
6868

6969
try:
70-
clause = self.generator.next()
70+
clause = next(self.generator)
7171
except StopIteration:
7272
if self.caller:
7373
waiters.append(self.caller)
@@ -126,7 +126,7 @@ def __init__(self, generator):
126126
self.generator = generator
127127

128128
def next(self, waiters, actives, exc):
129-
clause = self.generator.next()
129+
clause = next(self.generator)
130130
schedule((_simulator._time + clause._time, self))
131131

132132

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

141141
def next(self, waiters, actives, exc):
142-
clause = self.generator.next()
142+
clause = next(self.generator)
143143
clause.append(self)
144144

145145

@@ -154,7 +154,7 @@ def __init__(self, generator):
154154
def next(self, waiters, actives, exc):
155155
if self.hasRun:
156156
raise StopIteration
157-
clauses = self.generator.next()
157+
clauses = next(self.generator)
158158
self.hasRun = 1
159159
clone = _EdgeTupleWaiter(self.generator)
160160
for clause in clauses:
@@ -171,7 +171,7 @@ def __init__(self, generator):
171171
self.hasRun = 0
172172

173173
def next(self, waiters, actives, exc):
174-
clause = self.generator.next()
174+
clause = next(self.generator)
175175
clause._eventWaiters.append(self)
176176

177177

@@ -186,7 +186,7 @@ def __init__(self, generator):
186186
def next(self, waiters, actives, exc):
187187
if self.hasRun:
188188
raise StopIteration
189-
clauses = self.generator.next()
189+
clauses = next(self.generator)
190190
self.hasRun = 1
191191
clone = _SignalTupleWaiter(self.generator)
192192
for clause in clauses:

myhdl/_bin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
""" module with the bin function.
2121
2222
"""
23-
23+
from myhdl._compat import long
2424

2525

2626
def _int2bitstring(num):

myhdl/_compat.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
import types
3+
4+
PY2 = sys.version_info[0] == 2
5+
6+
7+
if not PY2:
8+
string_types = (str,)
9+
integer_types = (int,)
10+
long = int
11+
class_types = (type,)
12+
13+
from io import StringIO
14+
import builtins
15+
else:
16+
string_types = (str, unicode)
17+
integer_types = (int, long)
18+
long = long
19+
class_types = (type, types.ClassType)
20+
21+
from cStringIO import StringIO
22+
import __builtin__ as builtins

myhdl/_concat.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
"""
2323
from __future__ import absolute_import
2424

25+
from myhdl._compat import integer_types
2526
from myhdl._intbv import intbv
2627
from myhdl._Signal import _Signal
28+
from myhdl._compat import long
29+
30+
2731

2832
def concat(base, *args):
2933

3034
if isinstance(base, intbv):
3135
basewidth = base._nrbits
3236
val = base._val
33-
elif isinstance(base, (int, long)):
37+
elif isinstance(base, integer_types):
3438
if isinstance(base, bool):
3539
basewidth = 1
3640
else:
@@ -72,7 +76,7 @@ def concat(base, *args):
7276
if not w:
7377
raise TypeError("concat: arg on pos %d should have length" % (i+1))
7478
width += w
75-
val = val << w | v & (1L << w)-1
79+
val = val << w | v & (long(1) << w)-1
7680

7781
if basewidth:
7882
return intbv(val, _nrbits=basewidth + width)

myhdl/_delay.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919

2020
""" Module that provides the delay class."""
21+
from __future__ import absolute_import
2122

23+
from myhdl._compat import integer_types
2224

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

@@ -33,6 +35,6 @@ def __init__(self, val):
3335
val -- a natural integer representing the desired delay
3436
3537
"""
36-
if not isinstance(val, (int, long)) or val < 0:
38+
if not isinstance(val, integer_types) or val < 0:
3739
raise TypeError(_errmsg)
3840
self._time = val

myhdl/_enum.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
from __future__ import absolute_import
2424

2525

26-
from types import StringType
27-
2826
from myhdl._bin import bin
2927
from myhdl._Signal import _Signal
28+
from myhdl._compat import string_types
3029

3130
class EnumType(object):
3231
def __init__(self):
@@ -53,9 +52,9 @@ def enum(*names, **kwargs):
5352
codedict = {}
5453
i = 0
5554
for name in names:
56-
if not isinstance(name, StringType):
55+
if not isinstance(name, string_types):
5756
raise TypeError()
58-
if codedict.has_key(name):
57+
if name in codedict:
5958
raise ValueError("enum literals should be unique")
6059
if encoding == "one_hot":
6160
code = bin(1<<i, nrbits)

0 commit comments

Comments
 (0)