Search code examples
chapel

Error when calling Chapel from Python using PyChapel


I am trying to get Chapel to return an integer to Python. I'd like to call it with 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)

And response.chpl

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

This returns the error

/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}).

UPDATE

Based on Lydia's response, I did

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

And that worked! WOO-HOOO!!!!


UPDATE 2

Based on Brad's comments, this also works

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

Perhaps he can add a comment on benefits of each approach.


Solution

  • It looks like your issue is that you're trying to modify the argument before returning it. Chapel's default argument intent for integers is const in ( see the Chapel spec http://chapel.cray.com/docs/latest/language/spec.html, section 13.5 ), which means it can't be modified within the body of the function and is a copy of the value passed to it. If you store the result in a local variable and return that instead, that should solve your compilation failure and give you the behavior you desire.