使用 PyChapel 从 Python 调用 Chapel 时出错

Error when calling Chapel from Python using PyChapel

我正在尝试让 Chapel 变成 return 到 Python 的整数。我想用 python call.py.

来称呼它

call.py

import os
from pych.extern import Chapel

currentloc = os.path.dirname(os.path.realpath(__file__))


@Chapel(sfile=os.path.join(currentloc + '/response.chpl'))
def flip_bit(v=int):
    return int

if __name__=="__main__":
    u = 71
    w = flip_bit(u)
    print(w)

response.chpl

export
proc flip_bit(v: int) :int {
  v = -v;
  return v;
}

这return是错误

/tmp/temp-7afvL9.chpl:2: In function 'flip_bit':
/tmp/temp-7afvL9.chpl:3: error: illegal lvalue in assignment
g++: error: /tmp/tmpvmKeSi.a: No such file or directory
Traceback (most recent call last):
  File "call.py", line 15, in <module>
    w = flip_bit(u)
  File "/home/buddha314/.virtualenvs/pychapel/local/lib/python2.7/site-packages/pych/extern.py", line 212, in wrapped_f
    raise MaterializationError(self)
pych.exceptions.MaterializationError: Failed materializing ({'anames': ['v'],
 'atypes': [<type 'int'>],
 'bfile': None,
 'dec_fp': '/home/buddha314/pychapel/tmp/response.chpl',
 'dec_hs': '7ecfac2d168f3423f7104eeb38057ac3',
 'dec_ts': 1502208246,
 'doc': None,
 'efunc': None,
 'ename': 'flip_bit',
 'lib': 'sfile-chapel-7ecfac2d168f3423f7104eeb38057ac3-1502208246.so',
 'module_dirs': [],
 'pfunc': <function flip_bit at 0x7fa4d72bd938>,
 'pname': 'flip_bit',
 'rtype': <type 'int'>,
 'sfile': '/home/buddha314/pychapel/tmp/response.chpl',
 'slang': 'chapel',
 'source': None}).

更新

根据莉迪亚的反应,我做了

export
proc flip_bit(v: int) :int {
  var w: int;
  w = -v;
  return w;
}

成功了!呜呼!!!


更新 2

根据 Brad 的评论,这也有效

export
proc flip_bit(in v: int) :int {
  return -v;
}

也许他可以对每种方法的好处发表评论。

您的问题似乎是您试图在 return 修改参数之前对其进行修改。 Chapel 对整数的默认参数意图是 const in(参见 Chapel 规范 http://chapel.cray.com/docs/latest/language/spec.html,第 13.5 节),这意味着它不能在函数并且是传递给它的值的副本。如果您将结果存储在局部变量中,而 return 则应该解决您的编译失败并为您提供所需的行为。