Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ctypes - python2.7.3 vs python3.2.3

Reply
Thread Tools

ctypes - python2.7.3 vs python3.2.3

 
 
Rolf
Guest
Posts: n/a
 
      08-28-2012
ctypes works as I would expect with python2.7.3.

However, when I upgrade to python3.2.3 things don't seem to work right. Look below for details.

I am not sure where I am going wrong.

Shared Library
==============
#include <stdint.h>
#include <string.h>

extern "C"
{
int main();
uint32_t myfunction (char **);
}

uint32_t myfunction (char ** _mydata)
{
char mydata[16];

strcpy(mydata, "Hello Dude!");

*_mydata = mydata;

return 0;
}

int main()
{
return 0;
}

Python 2.7.3 which works as I would expect
==========================================
> python2.7 -V

Python 2.7.3

> cat py27.py

#!/usr/bin/env python2.7

from __future__ import print_function
from __future__ import unicode_literals

from ctypes import *

lib = CDLL('libtest.so')
o_result = c_char_p()
lib.myfunction(pointer(o_result))
print(repr(o_result.value))

> ./py27.py

'Hello Dude!'

Python 3.2.3 return string gets mangled
=======================================
> python3 -V

Python 3.2.3

> cat py3.py

#!/usr/bin/env python3

from ctypes import *

lib = CDLL('libtest.so')
o_result = c_char_p()
lib.myfunction(pointer(o_result))
print(repr(o_result.value))

> ./py3.py

b'\xd8\xb0y\to Dude!'

Every time I run it, I get a different set of values.
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      08-28-2012
In <18eb8025-7545-4d10-9e76-> Rolf <> writes:

> uint32_t myfunction (char ** _mydata)
> {
> char mydata[16];


> strcpy(mydata, "Hello Dude!");


> *_mydata = mydata;


> return 0;
> }


mydata is an auto variable, which goes out of scope when myfunction()
exits. *_mydata ends up pointing to garbage.

--
John Gordon A is for Amy, who fell down the stairs
B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      08-28-2012
On 28/08/2012 22:35, Rolf wrote:
> ctypes works as I would expect with python2.7.3.
>
> However, when I upgrade to python3.2.3 things don't seem to work right. Look below for details.
>
> I am not sure where I am going wrong.
>
> Shared Library
> ==============
> #include <stdint.h>
> #include <string.h>
>
> extern "C"
> {
> int main();
> uint32_t myfunction (char **);
> }
>
> uint32_t myfunction (char ** _mydata)
> {
> char mydata[16];
>
> strcpy(mydata, "Hello Dude!");
>
> *_mydata = mydata;
>
> return 0;
> }
>
> int main()
> {
> return 0;
> }
>

[snip]
What you're doing in 'myfunction' looks wrong to start with. It's
returning the address of the local array 'mydata' which allocated on
the stack when the function is entered. When the function is left it's
deallocated, so the address becomes a dangling pointer. That it gave a
reasonable result with Python 2.7.3 is down to pure luck.
 
Reply With Quote
 
Jan Kuiken
Guest
Posts: n/a
 
      08-29-2012
On 8/28/12 23:51 , John Gordon wrote:
> In <18eb8025-7545-4d10-9e76-> Rolf <> writes:
>
>> uint32_t myfunction (char ** _mydata)
>> {
>> char mydata[16];

>
>> strcpy(mydata, "Hello Dude!");

>
>> *_mydata = mydata;

>
>> return 0;
>> }

>
> mydata is an auto variable, which goes out of scope when myfunction()
> exits. *_mydata ends up pointing to garbage.
>


I'm not completely sure, but i think this can be solved by using:

static char mydata[16];

(Btw.: I don't know why you use char ** _mydata, i would use
char * _mydata, but then again, i'm not very familiar with
ctypes)

Jan Kuiken

 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      09-07-2012
In <9a74$503e88dd$546bb230$ et> Jan Kuiken <> writes:

> >> uint32_t myfunction (char ** _mydata)
> >> {
> >> char mydata[16];

> >
> >> strcpy(mydata, "Hello Dude!");

> >
> >> *_mydata = mydata;

> >
> >> return 0;
> >> }

> >
> > mydata is an auto variable, which goes out of scope when myfunction()
> > exits. *_mydata ends up pointing to garbage.


> I'm not completely sure, but i think this can be solved by using:


> static char mydata[16];


That will solve the immediate problem, however it makes myfunction()
non-reentrant.

> (Btw.: I don't know why you use char ** _mydata, i would use
> char * _mydata, but then again, i'm not very familiar with
> ctypes)


He uses char **_mydata because he wants myfunction()'s caller to see the
new value of _mydata, which it wouldn't if it were just char *_mydata.

--
John Gordon A is for Amy, who fell down the stairs
B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WindowsXP/ CTypes - How to convert ctypes array to a string? dudeja.rajat@gmail.com Python 0 08-19-2008 10:20 AM
RE: [ctypes-users] [Ann] ctypes 0.9.0 released Henk Punt Python 0 07-23-2004 10:34 PM
[Q] ctypes callbacks with Delphi achrist@easystreet.com Python 8 10-19-2003 05:26 PM
midi with ctypes Anton Vredegoor Python 3 08-09-2003 06:59 PM
ctypes ques ryan Python 1 08-04-2003 06:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57