Re: Get char** global variable

Ervin Hegedüs <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
Hi again,

On Thu, Sep 03, 2020 at 04:25:10PM +0200, Ervin Hegedüs wrote:
> I have a global variable with list of C strings:
> 
> char ** custom_variables;
> 
> I'ld like to access to this var, so I followed the documentation:
> 
> http://www.swig.org/Doc4.0/Python.html#Python_nn59
> 
...


okay, looks like I found the right way:

%typemap(varout) char ** {
  int len,i;
  len = 0;
  if ($1) {
    while ($1[len]) len++;
  }
  printf("output: %p\n", $1);
  printf("len: %d\n", len);
  $result = PyList_New(len);
  for (i = 0; i < len; i++) {
    PyList_SetItem($result,i,PyUnicode_FromString($1[i]));
  }
}

Just for the record:

* the "out" typemap isn't good here, the "varout" is the correct
* $input isn't available here, $1 is the "input"
* $result is the product in Python

Now I get what I expect:

print(mytest.cvar.custom_variables)

["FooBar", "BarFoo"]


Thanks.


a.



_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.