From 55b6cbfed9aedd52f221ffa6d43c56e1ebc9f9be Mon Sep 17 00:00:00 2001 From: Keerthan Jaic Date: Wed, 18 Mar 2015 00:44:14 -0400 Subject: [PATCH] make comsimulation py3 compatible --- myhdl/_Cosimulation.py | 22 ++++++++++++++-------- myhdl/_compat.py | 10 ++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/myhdl/_Cosimulation.py b/myhdl/_Cosimulation.py index 3c2835bd..1e4842a9 100644 --- a/myhdl/_Cosimulation.py +++ b/myhdl/_Cosimulation.py @@ -26,6 +26,7 @@ import os from myhdl._intbv import intbv from myhdl import _simulator, CosimulationError +from myhdl._compat import PY2, to_bytes, to_str _MAXLINE = 4096 @@ -54,6 +55,11 @@ class Cosimulation(object): self._rt, self._wt = rt, wt = os.pipe() self._rf, self._wf = rf, wf = os.pipe() + # New pipes are not inheritable by default since py 3.4 + if not PY2: + for p in rt, wt, rf, wf: + os.set_inheritable(p, True) + self._fromSignames = fromSignames = [] self._fromSizes = fromSizes = [] self._fromSigs = fromSigs = [] @@ -84,12 +90,12 @@ class Cosimulation(object): os.close(wt) os.close(rf) while 1: - s = os.read(rt, _MAXLINE) + s = to_str(os.read(rt, _MAXLINE)) if not s: raise CosimulationError(_error.SimulationEnd) e = s.split() if e[0] == "FROM": - if long(e[1]) != 0: + if int(e[1]) != 0: raise CosimulationError(_error.TimeZero, "$from_myhdl") for i in range(2, len(e)-1, 2): n = e[i] @@ -100,9 +106,9 @@ class Cosimulation(object): fromSignames.append(n) fromSigs.append(kwargs[n]) fromSizes.append(int(e[i+1])) - os.write(wf, "OK") + os.write(wf, b"OK") elif e[0] == "TO": - if long(e[1]) != 0: + if int(e[1]) != 0: raise CosimulationError(_error.TimeZero, "$to_myhdl") for i in range(2, len(e)-1, 2): n = e[i] @@ -114,11 +120,11 @@ class Cosimulation(object): toSigs.append(kwargs[n]) toSigDict[n] = kwargs[n] toSizes.append(int(e[i+1])) - os.write(wf, "OK") + os.write(wf, b"OK") elif e[0] == "START": if not toSignames: raise CosimulationError(_error.NoCommunication) - os.write(wf, "OK") + os.write(wf, b"OK") break else: raise CosimulationError("Unexpected cosim input") @@ -126,7 +132,7 @@ class Cosimulation(object): def _get(self): if not self._getMode: return - buf = os.read(self._rt, _MAXLINE) + buf = to_str(os.read(self._rt, _MAXLINE)) if not buf: raise CosimulationError(_error.SimulationEnd) e = buf.split() @@ -165,7 +171,7 @@ class Cosimulation(object): if buf[-1] == 'L': buf = buf[:-1] # strip trailing L buflist.append(buf) - os.write(self._wf, " ".join(buflist)) + os.write(self._wf, to_bytes(" ".join(buflist))) self._getMode = 1 def _waiter(self): diff --git a/myhdl/_compat.py b/myhdl/_compat.py index 2761b3ba..43312e58 100644 --- a/myhdl/_compat.py +++ b/myhdl/_compat.py @@ -3,6 +3,7 @@ import types PY2 = sys.version_info[0] == 2 +_identity = lambda x: x if not PY2: string_types = (str,) @@ -12,6 +13,12 @@ if not PY2: from io import StringIO import builtins + + def to_bytes(s): + return s.encode() + + def to_str(b): + return b.decode() else: string_types = (str, unicode) integer_types = (int, long) @@ -20,3 +27,6 @@ else: from cStringIO import StringIO import __builtin__ as builtins + + to_bytes = _identity + to_str = _identity