繁体   English   中英

iPython Notebook中的myHDL手册中的基本示例的AST编译错误

[英]AST compilation errors on basic examples from myHDL manual in iPython notebook

编辑:仅当我从iPython Notebook内部运行代码时,才会发生这种情况。 从常规的.py文件可以正常工作

我刚刚开始学习myHDL,并且使用@instance或@always_comb生成器时遇到编译错误,如下所示:

TypeError:compile()预期的字符串,不包含空字节

例如:3.2信号,端口和并发:

from myhdl import Signal, delay, always, instance, now, Simulation

def ClkDriver(clk, period=20):
    lowTime = int(period/2)
    highTime = period - lowTime

    @instance
    def driveClk():
        while True:
            yield delay(lowTime)
            clk.next = 1
            yield delay(highTime)
            clk.next = 0

    return driveClk

clk = Signal(0)
clkdriver_inst = ClkDriver(clk)

给出堆栈跟踪:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-4252bd2f72e0> in <module>()
     16 
     17 clk = Signal(0)
---> 18 clkdriver_inst = ClkDriver(clk)

<ipython-input-47-4252bd2f72e0> in ClkDriver(clk, period)
      5     highTime = period - lowTime
      6 
----> 7     @instance
      8     def driveClk():
      9         while True:

C:\Python27\lib\site-packages\myhdl\_instance.pyc in instance(genFunc)
     40     if genFunc.func_code.co_argcount > 0:
     41         raise InstanceError(_error.NrOfArgs)
---> 42     return _Instantiator(genFunc)
     43 
     44 class _Instantiator(object):

C:\Python27\lib\site-packages\myhdl\_instance.pyc in __init__(self, genFunc)
     47         self.genfunc = genFunc
     48         self.gen = genFunc()
---> 49         self.waiter = _inferWaiter(self.gen)
     50 

C:\Python27\lib\site-packages\myhdl\_Waiter.pyc in _inferWaiter(gen)
    209     s = inspect.getsource(f)
    210     s = _dedent(s)
--> 211     root = ast.parse(s)
    212     root.symdict = f.f_globals.copy()
    213     root.symdict.update(f.f_locals)

C:\Python27\lib\ast.pyc in parse(source, filename, mode)
     35     Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
     36     """
---> 37     return compile(source, filename, mode, PyCF_ONLY_AST)
     38 
     39 

TypeError: compile() expected string without null bytes

关于我在做什么错的任何线索?

这看起来像是myhdl._util._dedent()中的 Unicode支持问题

这是说明问题的代码段:

在此处输入图片说明

为了快速修复,我添加了以下代码:

def _dedent(s):
    """Dedent python code string."""
    # RL convert to ascii
    s = s.encode('ascii','ignore')
    result = [t[:2] for t in generate_tokens(StringIO(s).readline)]
    # set initial indent to 0 if any
    if result[0][0] == INDENT:
        result[0] = (INDENT, '')
    return untokenize(result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM