我试图让Chapel返回一个整数给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;
}这将返回错误
/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
根据布拉德的评论,这也是可行的
export
proc flip_bit(in v: int) :int {
return -v;
}也许他可以对每一种方法的好处发表评论。
发布于 2017-08-08 16:28:33
看起来,您的问题是在返回参数之前尝试修改它。教堂堂对整数的默认参数意图是const in (参见教堂堂规范http://chapel.cray.com/docs/latest/language/spec.html,第13.5节),这意味着它不能在函数的正文中被修改,而是传递给它的值的副本。如果您将结果存储在一个局部变量中并返回它,这将解决您的编译失败,并给出您想要的行为。
https://stackoverflow.com/questions/45573337
复制相似问题