24874, "lydia-duncan", "[Bug]: Restore ability to export functions with arrays of C-style strings to Python", "2024-04-17T17:11:41Z"
Summary of Problem
Description:
Prior to the move from c_string
to c_ptrConst(c_char)
, we were able to export functions that took arguments of type [ ] c_string
. Unfortunately, we did not have a test of this in our testing system, so missed that the move to c_ptrConst(c_char)
broke this functionality. We should (re-)enable array arguments with this element type.
Is this a blocking issue with no known work-arounds?
There are no work-arounds that allow you to modify the string array in both Python and Chapel, but it's not essential to what I'm working on.
If you want to return an array of strings from Chapel and send that as an argument to another exported function, I think you can use opaque arrays, but given that it tries to use an external array by default for this type you'd have to explicitly specify the opaque array type in some way.
Steps to Reproduce
Source Code:
Chapel file (foo.chpl):
use CTypes;
export proc takesArray(in x: [] c_ptrConst(c_char)) {
for str in x {
// Note: this assumes x will have initial contents
writeln(string.createCopyingBuffer(str));
}
}
export proc returnsArray(): [0..3] c_ptrConst(c_char) {
var x: [0..3] c_ptrConst(c_char) = ["guess", "this", "could", "work?"];
return x;
}
Python file (use_foo.py):
import foo
foo.chpl_setup()
x = foo.returnsArray()
foo.takesArray(x)
y = [b"maybe", b"this", b"also", b"works", b"too?"]
foo.takesArray(y)
foo.chpl_cleanup()
Compile command:
chpl --library-python foo.chpl
Execution command:
(note: need to make sure the location of the generated Python library is in your $PYTHONPATH
)
python3 use_foo.py
Associated Future Test(s):
test/interop/python/cptrConstCharArrays.chpl
#TBD
Configuration Information
- Output of
chpl --version
: chpl version 2.1.0 pre-release - Output of
$CHPL_HOME/util/printchplenv --anonymize
: (should be almost any settings as long asCHPL_LIB_PIC=pic
, though solving the problem for multilocale will probably involve additional steps)
CHPL_TARGET_PLATFORM: darwin
CHPL_TARGET_COMPILER: clang
CHPL_TARGET_ARCH: x86_64
CHPL_TARGET_CPU: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: qthreads
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_ATOMICS: cstdlib
CHPL_GMP: none
CHPL_HWLOC: bundled
CHPL_RE2: bundled
CHPL_LLVM: none *
CHPL_AUX_FILESYS: none
CHPL_LIB_PIC: pic - Back-end compiler and version, e.g.
gcc --version
orclang --version
: any - (For Cray systems only) Output of
module list
: N/A