using cdata in python

View: New views
6 Messages — Rating Filter:   Alert me  

using cdata in python

by Mathieu Malaterre-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,

  I cannot understand how to use cdata to do this simple task. Let say
I have this class:

class A
{
public:
  unsigned int GetLength();
  void GetBuffer(char *strout);
};

In C++ you would use it this way:

A a;
char *buffer = new char[a.GetLength()];
a.GetBuffer(buffer);

I would like that it behave juste like python: readlines() function.
It should return the properly allocated string as a PyString (may
contains NULL character).

Thanks a bunch !

--
Mathieu

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Parent Message unknown Re: using cdata in python

by Jose Luna-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

See: %cstring_output_allocate_size(parm, szparm, release)

Here: http://www.swig.org/Doc1.3/Library.html#Library_nn12

This worked will for me (in python) when I needed to return binary data as a string.

JLuna

----- Original Message ----
From: Mathieu Malaterre <mathieu.malaterre@...>
To: swig-user@...
Sent: Sunday, May 4, 2008 11:44:58 AM
Subject: [Swig-user] using cdata in python

Hi there,

  I cannot understand how to use cdata to do this simple task. Let say
I have this class:

class A
{
public:
  unsigned int GetLength();
  void GetBuffer(char *strout);
};

In C++ you would use it this way:

A a;
char *buffer = new char[a.GetLength()];
a.GetBuffer(buffer);

I would like that it behave juste like python: readlines() function.
It should return the properly allocated string as a PyString (may
contains NULL character).

Thanks a bunch !

--
Mathieu

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user





      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: using cdata in python

by Mathieu Malaterre-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jose,

  Here is what I tried:

%extend gdcm::Image
{
  %cstring_output_allocate_size(char **buffer, unsigned int *size, free(*$1) );
  void my_get_data(char **buffer, unsigned int *size) {
    *size = self->GetBufferLength();
    *buffer = (char*)malloc(*size);
    self->GetBuffer(*buffer);
  }

 When I call it from my python shell I am getting:

>>> i=r.GetImage()
>>> i.my_get_data()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "gdcm.py", line 1138, in my_get_data
    def my_get_data(*args): return _gdcm.Image_my_get_data(*args)
TypeError: Image_my_get_data() takes exactly 3 arguments (1 given)
>>>

What I am doing wrong ?
Thanks again
-Mathieu

On Sun, May 4, 2008 at 7:46 PM, Jose Luna <j-luna@...> wrote:

> See: %cstring_output_allocate_size(parm, szparm, release)
>
>  Here: http://www.swig.org/Doc1.3/Library.html#Library_nn12
>
>  This worked will for me (in python) when I needed to return binary data as a string.
>
>  JLuna
>
>
>
>  ----- Original Message ----
>  From: Mathieu Malaterre <mathieu.malaterre@...>
>  To: swig-user@...
>  Sent: Sunday, May 4, 2008 11:44:58 AM
>  Subject: [Swig-user] using cdata in python
>
>  Hi there,
>
>   I cannot understand how to use cdata to do this simple task. Let say
>  I have this class:
>
>  class A
>  {
>  public:
>   unsigned int GetLength();
>   void GetBuffer(char *strout);
>  };
>
>  In C++ you would use it this way:
>
>  A a;
>  char *buffer = new char[a.GetLength()];
>  a.GetBuffer(buffer);
>
>  I would like that it behave juste like python: readlines() function.
>  It should return the properly allocated string as a PyString (may
>  contains NULL character).
>
>  Thanks a bunch !
>
>  --
>  Mathieu
>
>  -------------------------------------------------------------------------
>  This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
>  Don't miss this year's exciting event. There's still time to save $100.
>  Use priority code J8TL2D2.
>  http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
>  _______________________________________________
>  Swig-user mailing list
>  Swig-user@...
>  https://lists.sourceforge.net/lists/listinfo/swig-user
>
>
>
>
>
>       ____________________________________________________________________________________
>  Be a better friend, newshound, and
>  know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>



--
Mathieu

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: using cdata in python

by Paul Melis-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Not sure if the  %cstring_output_allocate_size constructs works on
methods (the documentation seems to mention only functions).
How about :

%extend gdcm::Image
{
    PyObject* my_get_data()
    {
        PyObject* res;
        unsigned int size = self->GetBufferLength();
        buffer = (char*)malloc(size);
   
        self->GetBuffer(*buffer);      

        // this will make a copy of buffer
        res = PyString_FromStringAndSize(buffer, size);

        free(buffer);      
   
        return res;
    }
}

It's not optimal, as a thrown-away copy of the data is made...

Paul
 
Mathieu Malaterre wrote:

> Hi Jose,
>
>   Here is what I tried:
>
> %extend gdcm::Image
> {
>   %cstring_output_allocate_size(char **buffer, unsigned int *size, free(*$1) );
>   void my_get_data(char **buffer, unsigned int *size) {
>     *size = self->GetBufferLength();
>     *buffer = (char*)malloc(*size);
>     self->GetBuffer(*buffer);
>   }
>
>  When I call it from my python shell I am getting:
>
>  
>>>> i=r.GetImage()
>>>> i.my_get_data()
>>>>        
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "gdcm.py", line 1138, in my_get_data
>     def my_get_data(*args): return _gdcm.Image_my_get_data(*args)
> TypeError: Image_my_get_data() takes exactly 3 arguments (1 given)
>  
>
> What I am doing wrong ?
> Thanks again
> -Mathieu
>
> On Sun, May 4, 2008 at 7:46 PM, Jose Luna <j-luna@...> wrote:
>  
>> See: %cstring_output_allocate_size(parm, szparm, release)
>>
>>  Here: http://www.swig.org/Doc1.3/Library.html#Library_nn12
>>
>>  This worked will for me (in python) when I needed to return binary data as a string.
>>
>>  JLuna
>>
>>
>>
>>  ----- Original Message ----
>>  From: Mathieu Malaterre <mathieu.malaterre@...>
>>  To: swig-user@...
>>  Sent: Sunday, May 4, 2008 11:44:58 AM
>>  Subject: [Swig-user] using cdata in python
>>
>>  Hi there,
>>
>>   I cannot understand how to use cdata to do this simple task. Let say
>>  I have this class:
>>
>>  class A
>>  {
>>  public:
>>   unsigned int GetLength();
>>   void GetBuffer(char *strout);
>>  };
>>
>>  In C++ you would use it this way:
>>
>>  A a;
>>  char *buffer = new char[a.GetLength()];
>>  a.GetBuffer(buffer);
>>
>>  I would like that it behave juste like python: readlines() function.
>>  It should return the properly allocated string as a PyString (may
>>  contains NULL character).
>>
>>  Thanks a bunch !
>>
>>  --
>>  Mathieu
>>
>>  -------------------------------------------------------------------------
>>  This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
>>  Don't miss this year's exciting event. There's still time to save $100.
>>  Use priority code J8TL2D2.
>>  http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
>>  _______________________________________________
>>  Swig-user mailing list
>>  Swig-user@...
>>  https://lists.sourceforge.net/lists/listinfo/swig-user
>>
>>
>>
>>
>>
>>       ____________________________________________________________________________________
>>  Be a better friend, newshound, and
>>  know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>>
>>    
>
>
>
>  


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Parent Message unknown Re: using cdata in python

by Jose Luna-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Mathieu,


>From your other thread, I got the impression that this issue was resolved and you now have it working.  Is that the case?

JLuna



      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: using cdata in python

by Mathieu Malaterre-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, May 5, 2008 at 5:18 PM, Jose Luna <j-luna@...> wrote:
> Mathieu,
>
>
>  >From your other thread, I got the impression that this issue was resolved and you now have it working.  Is that the case?


oooops, sorry. Indeed I forgot to close the thread. I was missing the
%include "cstring.i". %cstring_output_allocate_size seems to be
working like a charm.

Thanks again !

--
Mathieu

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user