Lenard Lindstrom | 1 Dec 2007 06:48

Re: Need assistance with pointers to pointers and casting

Yes, its interesting to see what the final code looks like. I'm not 
really an expert on the preferred way to use ctypes. But personally I 
would make the output_buffer class in EssBase.fetch_row a class 
attribute to avoid redeclaring it unnecessarily. But I don't know 
whether that would have a significant impact on performance.

David Welden wrote:
> If interested, you can review the results at 
> http://essbasepy.googlecode.com. Feel free to make any additional 
> suggestions on cleaning up the code or in properly utilizing the 
> ctypes module.
>  
> Thanks again.

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
Charles Duffy | 3 Dec 2007 02:05
Gravatar

Segfault in csvn (svn ctypes bindings) on Linux/amd64 only when python/ctypes compiled with -O2

Issue w/ ctypes from Python 2.5.1 on x86_64 Linux, only when Python has
been compiled with -O2: Pointers which should be null are somehow
getting 0x7fff (the same region used for some completely valid pointers
elsewhere on the stack) substituted into their high bits. lock_tokens is
an example here; it should be null, and its present value is inaccurate.
Recompiling the Python interpreter with -O1 (and changing nothing else)
masks this issue.

Before some recompiles this was occurring in a different location (see
http://permalink.gmane.org/gmane.comp.version-control.subversion.devel/94549); 

at that point, it was exceedingly clear that a pointer being given the
value 0x7fff00000000 was passed in null.

I can provide a sandboxed Linux/amd64 host to use in reproducing this
issue, if necessary.

---

Breakpoint 1, svn_ra_local__get_commit_editor (session=0xd32928,
editor=0xa53918, edit_baton=0xa53670, log_msg=0x2b3056dfdd44 "create
blahdir and descendents", callback=0x2b305cf75f00, callback_baton=0x0,
lock_tokens=0x7fff00000000,
     keep_locks=0, pool=0xd40848) at
subversion/libsvn_ra_local/ra_plugin.c:497
497     {
(gdb) print lock_tokens
$5 = (apr_hash_t *) 0x7fff00000000
(gdb) bt
#0  svn_ra_local__get_commit_editor (session=0xd32928, editor=0xa53918,
(Continue reading)

oyster | 4 Dec 2007 09:01
Picon

how to change a string, define variable number of arguments

hi, I am programming on IUP- Portable User
Interface(http://www.tecgraf.puc-rio.br/iup/)  in my m$ windows. I met
some problem
1.  what should the code be since the string is returned in argument
arq in function iupGetFile?
2. how to specilize the argtypes for
IupMessagef   (char *title, char *format, ...);
whcih takes a variable number of arguments
thanx

[code=getfile.py]
from iup import *
IupOpen()
a=''
IupGetFile(a)
print a
IupMainLoop()
IupClose()
[/code]

[code=iup.py]
import ctypes
iup=ctypes.windll.LoadLibrary('iup.dll')

_IupGetFile=iup.IupGetFile
_IupGetFile.argstypes=[ctypes.c_char_p]
_IupGetFile.restype=ctypes.c_int
def IupGetFile(arq):
    return _IupGetFile( arq)            #how to change the arq?

(Continue reading)

J.M. Seitz | 4 Dec 2007 19:11
Picon

4-bytes Missing from Function Call

Hey All,

I am stuck here. I am attempting to make a SymEnumSymbols call which is
exported from dbghlp.dll, here is what it looks like:

[code]
print "Address: %08x" % address
print "H Process: %08x" % dbg.h_process
mask = c_char_p("!kernel32")

dbghelp.SymEnumSymbols(dbg.h_process,address,mask,enum_func,None)
[/code]

I have the enum_func correctly defined as a callback function, and all
the structs, etc. are initialized correctly, I just can't seem to get
the parameter count properly.

The MSDN definition looks like this:

BOOL WINAPI SymEnumSymbols(
  __in  HANDLE hProcess,
  __in  ULONG64 BaseOfDll,
  __in  PCTSTR Mask,
  __in  PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
  __in  PVOID UserContext
);

And the error I continue to get is this:

Address: 00400000
(Continue reading)

Lenard Lindstrom | 5 Dec 2007 03:25

Re: 4-bytes Missing from Function Call

J.M. Seitz wrote:
> Hey All,
>
> I am stuck here. I am attempting to make a SymEnumSymbols call which is
> exported from dbghlp.dll, here is what it looks like:
>
> [code]
> print "Address: %08x" % address
> print "H Process: %08x" % dbg.h_process
> mask = c_char_p("!kernel32")
>
> dbghelp.SymEnumSymbols(dbg.h_process,address,mask,enum_func,None)
> [/code]
>
> I have the enum_func correctly defined as a callback function, and all
> the structs, etc. are initialized correctly, I just can't seem to get
> the parameter count properly.
>
> The MSDN definition looks like this:
>
> BOOL WINAPI SymEnumSymbols(
>   __in  HANDLE hProcess,
>   __in  ULONG64 BaseOfDll,
>   __in  PCTSTR Mask,
>   __in  PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
>   __in  PVOID UserContext
> );
>
>
> And the error I continue to get is this:
(Continue reading)

Lenard Lindstrom | 5 Dec 2007 04:16

Re: how to change a string, define variable number of arguments

oyster wrote:
> hi, I am programming on IUP- Portable User
> Interface(http://www.tecgraf.puc-rio.br/iup/)  in my m$ windows. I met
> some problem
> 1.  what should the code be since the string is returned in argument
> arq in function iupGetFile?
>   
Does the function overwrite an array of characters you pass it as an 
argument? If so then set argtypes to [POINTER(c_char)] and use 
create_string_buffer to allocate a buffer (or just declare a character 
array). The buffer's value attribute the null terminated string as a 
Python string.
**


> 2. how to specilize the argtypes for
> IupMessagef   (char *title, char *format, ...);
> whcih takes a variable number of arguments
> thanx
>
>
>   

A function prototype, the argtypes attribute, only works with fixed 
arguments. Instead let ctypes deduce the appropriate types. For example:

IupMessagef("A title", " A format", c_double(some_float), some_int)

Python ascii strings become c_char_p and integers become c_int. For 
other types pass a ctypes type instance, such as with the c_double in 
(Continue reading)

Lenard Lindstrom | 5 Dec 2007 04:43

Re: 4-bytes Missing from Function Call

By passing an instance of a ctypes type to a function the argument can 
be forced to the appropriate C type:

dbghelp.SymEnumSymbols(dbg.h_process,c_uint64(address),mask,enum_func,None)

Here address is converted to a c_uint64.

The argtypes and restype attributes are a way to specify a foreign 
function's argument at return types. They are described in section 
14.14.2.3 of the Python 2.5 Python Library Reference. The function will 
then do the correct argument conversions automatically.

Justin Seitz wrote:
> Sorry just got this on my blackberry, how could I make that an 8 byte int? As well what do you mean by argtypes?
>    
>
> -----Original Message-----
> From: Lenard Lindstrom <len-l <at> telus.net>
> Date: Tue, 04 Dec 2007 18:25:54 
> To:ctypes ML <ctypes-users <at> lists.sourceforge.net>
> Subject: Re: [ctypes-users] 4-bytes Missing from Function Call
>
> J.M. Seitz wrote:
>   
>> Hey All,
>>
>> I am stuck here. I am attempting to make a SymEnumSymbols call which is
>> exported from dbghlp.dll, here is what it looks like:
>>
>> [code]
(Continue reading)

hicham rahali | 5 Dec 2007 13:32
Picon

How to get a free memory size using ctypes on Unix platforms

Hi,
can you plaise help me to get a free memory size,
i am using python and i want to calculate  free memory for all unix platforms.
thanks for your help,
hicham
-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
oyster | 6 Dec 2007 10:00
Picon

problem with callback function

I am still fighting with IUP - Portable User
Interface (http://www.tecgraf.puc-rio.br/iup/). Now I have a new
problem with function pointer(see details below)

Can somebody help me?

And you can download what I have made till now from [
http://pyguiviactypes.googlepages.com/buggy_iup.zip ] with some
running examples.
Thanx

BTW: I disabled recieving digest from the maillist, and only read it
form www.sourceforge.net, thus can not answer the people who had
helped me. But I do thank you  :)
(ps1.I prefer to a google maillist, where I can read/post on the webpage
 ps2.if the ctypes manual give the example with C protype, I think it
could be more firiendly to C newbie like me)

[That is the C Part]
typedef int (*Icallback)(Ihandle*);

//action: name of the action generated when the user types something
Ihandle*  IupText (char* action);

//name: name of an action.
//func: address of a C function
Icallback   IupSetFunction   (const char *name, Icallback func);

int acao_numero (Ihandle *self, int c);
int main()
{
...
campo = IupText("acao_numero");                           /* creates
TEXT field */
IupSetFunction ("acao_numero", (Icallback)acao_numero);   /* registers
callback */
...
}
[/That is the C Part]

[That is the Python part]
Icallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.POINTER(Ihandle))
_IupText=iup.IupText
_IupText.argstypes=[ctypes.c_char_p]
_IupText.restype=ctypes.POINTER(Ihandle)
def IupText(action):
    return _IupText(action)

def acao_numero (me, c):
    pass

def main():
   ...
   campo = IupText("acao_numero")
   IupSetFunction ("acao_numero", Icallback(acao_numero))
   ...

TypeError: acao_numero() takes exactly 2 arguments (1 given)
[/That is the Python part]

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
Lenard Lindstrom | 6 Dec 2007 20:07

Re: problem with callback function

oyster wrote:
> I am still fighting with IUP - Portable User
> Interface (http://www.tecgraf.puc-rio.br/iup/). Now I have a new
> problem with function pointer(see details below)
>
> Can somebody help me?
>
> And you can download what I have made till now from [
> http://pyguiviactypes.googlepages.com/buggy_iup.zip ] with some
> running examples.
> Thanx
>
> BTW: I disabled recieving digest from the maillist, and only read it
> form www.sourceforge.net, thus can not answer the people who had
> helped me. But I do thank you  :)
> (ps1.I prefer to a google maillist, where I can read/post on the webpage
>  ps2.if the ctypes manual give the example with C protype, I think it
> could be more firiendly to C newbie like me)
>   

It does not help when the library being wrapped does uses advanced C tricks.

>
> [That is the C Part]
> typedef int (*Icallback)(Ihandle*);
>
> //action: name of the action generated when the user types something
> Ihandle*  IupText (char* action);
>
> //name: name of an action.
> //func: address of a C function
> Icallback   IupSetFunction   (const char *name, Icallback func);
>
> int acao_numero (Ihandle *self, int c);
> int main()
> {
> ...
> campo = IupText("acao_numero");                           /* creates
> TEXT field */
> IupSetFunction ("acao_numero", (Icallback)acao_numero);   /* registers
>   
Here the function acao_numero, which takes two arguments, is cast as a 
function that only takes a single argument.

> callback */
> ...
> }
> [/That is the C Part]
>
>
>
> [That is the Python part]
> Icallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.POINTER(Ihandle))
> _IupText=iup.IupText
> _IupText.argstypes=[ctypes.c_char_p]
> _IupText.restype=ctypes.POINTER(Ihandle)
> def IupText(action):
>     return _IupText(action)
>
> def acao_numero (me, c):
>     pass
>
> def main():
>    ...
>    campo = IupText("acao_numero")
>    IupSetFunction ("acao_numero", Icallback(acao_numero))
>   

At the end of section 14.14.1.17 Callback functions of the Python 
Library Reference for Python 2.5 it mentions that a Python reference to 
a CFUNCTYPE object or Python will garbage collect it.

>    ...
>
> TypeError: acao_numero() takes exactly 2 arguments (1 given)
> [/That is the Python part]
>
>   

So try this:

def py_acao_numero (me, c):
    pass
acao_numero = ctypes.CFUNCTYPE(ctype.c_int,
                               ctypes.POINTER(Ihandle),
                               ctypes.c_int)(py_acao_numero)

def main():
   ...
   campo = IupText("acao_numero")
   IupSetFunction ("acao_numero", ctypes.cast(acao_numero, Icallback))
   ...

Alternatively, do not set the argtypes attribute of IupSetFunction and let ctypes deduce argument types. Then:

IupSetFunction("acao_numero", acao_numero)

When passed as an argument a Python string is by default converted to a 
(char *). The callback is a C data object so has an associated C type.

--

-- 
Lenard Lindstrom
<len-l <at> telus.net>

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4

Gmane