Quantcast

Buffer interface issue

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Buffer interface issue

Simon Dumortier
Hello,

I'm using SWIG (2.0.12) on Raspberry Pi with Python (2.7.9).
The buffer interface as described in the Doc2.0 section 34.12.2 doesn't seem to work for me:
http://www.swig.org/Doc2.0/SWIGDocumentation.html#Python_nn75

I get the following:
>>> from example import *
>>> p=bytearray(10)
>>> get_path(p)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'get_path', argument 1 of type 'char *'



Here are my c/i files:
# test.c ------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "test.h"

int print_stuff(void) {
        printf("More stuff\n");
        return 14;
}

void get_path(char *s) {
        char hString[10] = "Hello";

        strncpy(s, hString, 10);
}

int main (int argc, char** argv) {
        char zString[10];

        get_path(zString);
        printf("%s\n", zString);
        return 0;
}

# test.i ------------------------------------------------------
/* File: test.i */
%module example
%include "pybuffer.i"

%{
    #include "test.h"
%}

%pybuffer_mutable_string(char *str);
void get_path(char *s);
int print_stuff(void);

---------------------------------------------------------------


Am I doing something wrong?
Thank you,
Simon



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Buffer interface issue

Ervin Hegedüs
Hi Simon,

On Wed, Feb 15, 2017 at 03:33:16PM +0000, Simon Dumortier wrote:

> Hello,
>
> # test.i ------------------------------------------------------
> /* File: test.i */
> %module example
> %include "pybuffer.i"
>
> %{
>     #include "test.h"
> %}
>
> %pybuffer_mutable_string(char *str);
> void get_path(char *s);
> int print_stuff(void);
>
> ---------------------------------------------------------------

the right form of the lines above:

%pybuffer_mutable_string(char *s);

%{
    #include "test.h"
%}
void get_path(char *s);


* may be that's not important, but I've placed the
  pybuffer_mutable_string macro _before_ the header
* but that's important, that the argument name _must_be_ same
  as you're using in your method; I mean, "char *str" in
  your macro is not equal with "char *s" in your method.


Regards,

a.

--
I � UTF-8

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Buffer interface issue

Simon Dumortier
Hi Ervin,

Thank you for your answer, my issue was indeed to have a different argument name (*s Vs. *str).
The SWIG 2.0 documentation might need to be updated though, because I took the example from there:

-----------------------------------------------------
34.12.2 Buffer interface

%include <pybuffer.i>
%pybuffer_mutable_string(char *str);
void get_path(char *s);
-----------------------------------------------------

Regards,
Simon





-----Original Message-----
From: Ervin Hegedüs [mailto:[hidden email]]
Sent: Wednesday, February 15, 2017 2:02 PM
To: Simon Dumortier
Cc: [hidden email]
Subject: Re: [Swig-user] Buffer interface issue

EXTERNAL EMAIL


Hi Simon,

On Wed, Feb 15, 2017 at 03:33:16PM +0000, Simon Dumortier wrote:

> Hello,
>
> # test.i ------------------------------------------------------
> /* File: test.i */
> %module example
> %include "pybuffer.i"
>
> %{
>     #include "test.h"
> %}
>
> %pybuffer_mutable_string(char *str);
> void get_path(char *s);
> int print_stuff(void);
>
> ---------------------------------------------------------------

the right form of the lines above:

%pybuffer_mutable_string(char *s);

%{
    #include "test.h"
%}
void get_path(char *s);


* may be that's not important, but I've placed the
  pybuffer_mutable_string macro _before_ the header
* but that's important, that the argument name _must_be_ same
  as you're using in your method; I mean, "char *str" in
  your macro is not equal with "char *s" in your method.


Regards,

a.

--
I � UTF-8
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Buffer interface issue

William S Fulton


On 15 February 2017 at 20:51, Simon Dumortier <[hidden email]> wrote:
Hi Ervin,

Thank you for your answer, my issue was indeed to have a different argument name (*s Vs. *str).
The SWIG 2.0 documentation might need to be updated though, because I took the example from there:

-----------------------------------------------------
34.12.2 Buffer interface

%include <pybuffer.i>
%pybuffer_mutable_string(char *str);
void get_path(char *s);
-----------------------------------------------------


Thanks for pointing this out, I've updated it for the next release.

William
 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...