David Cournapeau | 1 Feb 06:27 2007
Picon
Picon

Safest way to convert any python numerical value to c_int64 ?

Hi,

    I am using ctypes to use a C library which functions have a 64 bits 
integer argument (and expects it to be positive). I am using 
ctypes.c_int64 to wrap the argument when calling the C function, but I 
was wondering about the best way to treat a value given by the user:

    c_int64(10) -> c_lonlong(10L)
    c_int64(2 ** 31) ->  c_longlong(2147483648L)
    c_int64(2 ** 63) -> c_longlong(-9223372036854775808L) !

    I thought about converting the user value into a long, check that 
log2(value) is below 63, but this is not pretty... Is there a better way 
to detect an overflow ?

    cheers,

    David

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
Thomas Heller | 1 Feb 08:33 2007

Re: Safest way to convert any python numerical value to c_int64 ?

David Cournapeau schrieb:
> Hi,
> 
>     I am using ctypes to use a C library which functions have a 64 bits 
> integer argument (and expects it to be positive). I am using 
> ctypes.c_int64 to wrap the argument when calling the C function, but I 
> was wondering about the best way to treat a value given by the user:
> 
>     c_int64(10) -> c_lonlong(10L)
>     c_int64(2 ** 31) ->  c_longlong(2147483648L)
>     c_int64(2 ** 63) -> c_longlong(-9223372036854775808L) !
> 
>     I thought about converting the user value into a long, check that 
> log2(value) is below 63, but this is not pretty... Is there a better way 
> to detect an overflow ?
> 

Can't you simply do this:

if c_int64(my_value).value != my_value:
    # overflow detected

Thomas

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
(Continue reading)

Uwe Schmitt | 1 Feb 13:57 2007
Picon

problem with pointer to array of doubles


Hi,

I have a C-Funktion like

     calc_data(int *res_size, *double result[]);

which allocates memory, and gives back number
of data and pointer to the resulting array.

In C I use it like

      int res_size;
      double* res_array;

      calc_data(&res_size, &res_array);

which works fine, I cann access the result using

     (*res_array)[i]   for 0 <= i < res_size

My problem: I do not know how to declare
the second parameter in ctypes. I tried

     res_array = pointer(c_double)
     res_size  = c_int()

     lib.calc_data(byref(res_size), byref(res_array))

Which gives the right value for res_size, but
(Continue reading)

Jeff Bauer | 1 Feb 16:32 2007

application error on exit

I have what I thought would be an ideal situation
to use ctypes. I'm attempting to use it on a DLL
with the following signature:

__declspec(dllimport) void process_record(char *, char *);

An example .cpp file accompanying the DLL has code
like this:

    int main(int argc, char **argv)
    {
       char record[100];
       char code[6];
         ...
       process_record(code, record);

When I invoke process_record under Python, everything's
fine while the interpreter is running:

    >>> from ctypes import *
    >>> record = create_string_buffer("My Record", 100)
    >>> code = create_string_buffer('\000' * 6)
    >>> cdll.grouper.process_record(code, record)
    >>> print code.value
    '9999'

But when the interpreter exits, either interactively
or from a script, the following Program Error popup
gets displayed(*)

(Continue reading)

Simon Burton | 1 Feb 19:57 2007

wrapping libc's pipe


pipe is in unistd.h on my (linux) machine:

$ h2xml.py unistd.h -o unistd.xml
creating xml output file ...
running: gccxml /tmp/tmpEfGLkq.cpp -fxml=unistd.xml
$
$ xml2py.py unistd.xml -o unistd.py
$ grep pipe unistd.xml
  <Function id="_115" name="pipe" returns="_129" throw="" context="_1" location="f0:369" file="f0"
line="369" extern="1">
    <Argument name="__pipedes" type="_329" location="f0:369" file="f0" line="369"/>
$ vim unistd.py
$
$ grep pipe unistd.py
$

So it's in the xml file but not in the final unistd.py file.

Simon.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
Thomas Heller | 1 Feb 20:45 2007

Re: wrapping libc's pipe

Simon Burton schrieb:
> pipe is in unistd.h on my (linux) machine:
> 
> $ h2xml.py unistd.h -o unistd.xml
> creating xml output file ...
> running: gccxml /tmp/tmpEfGLkq.cpp -fxml=unistd.xml
> $
> $ xml2py.py unistd.xml -o unistd.py
> $ grep pipe unistd.xml
>   <Function id="_115" name="pipe" returns="_129" throw="" context="_1" location="f0:369" file="f0"
line="369" extern="1">
>     <Argument name="__pipedes" type="_329" location="f0:369" file="f0" line="369"/>
> $ vim unistd.py
> $
> $ grep pipe unistd.py
> $
> 
> So it's in the xml file but not in the final unistd.py file.

xml2py does not try to guess the shared library where the function is defined,
and the header file does not include this information.  So, you have to specify
the libs to search for the functions on the command line with the -l<libs>.

theller <at> tubu:~/devel/theller/ctypeslib$ python2.5 -m ctypeslib.xml2py unistd.xml -lc -o pipe.py
theller <at> tubu:~/devel/theller/ctypeslib$ fgrep pipe pipe.py
pipe = _libraries['libc.so.6'].pipe
pipe.restype = c_int
pipe.argtypes = [POINTER(c_int)]
           '_SC_NL_TEXTMAX', 'alarm', '_SC_MAPPED_FILES', 'pipe',
theller <at> tubu:~/devel/theller/ctypeslib$
(Continue reading)

Thomas Heller | 1 Feb 21:35 2007

Re: problem with pointer to array of doubles

Uwe Schmitt schrieb:
> Hi,
> 
> I have a C-Funktion like
> 
>      calc_data(int *res_size, *double result[]);
> 
> which allocates memory, and gives back number
> of data and pointer to the resulting array.
> 
> In C I use it like
> 
>       int res_size;
>       double* res_array;
> 
>       calc_data(&res_size, &res_array);
> 
> which works fine, I cann access the result using
> 
>      (*res_array)[i]   for 0 <= i < res_size

I think you mean this instead:
       res_array[i]

> 
> My problem: I do not know how to declare
> the second parameter in ctypes. I tried
> 
>      res_array = pointer(c_double)
>      res_size  = c_int()
(Continue reading)

Thomas Heller | 1 Feb 21:37 2007

Re: application error on exit

Jeff Bauer schrieb:
> I have what I thought would be an ideal situation
> to use ctypes. I'm attempting to use it on a DLL
> with the following signature:
> 
> __declspec(dllimport) void process_record(char *, char *);
> 
> An example .cpp file accompanying the DLL has code
> like this:
> 
>     int main(int argc, char **argv)
>     {
>        char record[100];
>        char code[6];
>          ...
>        process_record(code, record);
> 
> When I invoke process_record under Python, everything's
> fine while the interpreter is running:
> 
>     >>> from ctypes import *
>     >>> record = create_string_buffer("My Record", 100)
>     >>> code = create_string_buffer('\000' * 6)
>     >>> cdll.grouper.process_record(code, record)
>     >>> print code.value
>     '9999'
> 
> But when the interpreter exits, either interactively
> or from a script, the following Program Error popup
> gets displayed(*)
(Continue reading)

Gary Bishop | 2 Feb 19:38 2007
Picon

Structured formal parameter with tuple actual parameter aborts

With a structure like:

class CvRect(ctypes.Structure):
    _fields_ = [("x", ctypes.c_int),
                ("y", ctypes.c_int),
                ("width", ctypes.c_int),
                ("height", ctypes.c_int)]

and a FF like:

cvSetImageROI = _cxDLL.cvSetImageROI
cvSetImageROI.restype = None # void
cvSetImageROI.argtypes = [
    ctypes.c_void_p, # IplImage* image
    CvRect # CvRect rect
    ]

The call

cvSetImageROI(img, CvRect(1,2,3,4))

works fine but the mistaken call:

cvSetImageROI(img, (1,2,3,4))

Produces the "Send Error Report" dialog from Windows.

Other erroneous arguments produce a message about incorrect argument
type but the tuple triggers some other behavior.

(Continue reading)

Thomas Heller | 2 Feb 22:24 2007

Re: Structured formal parameter with tuple actual parameter aborts

Gary Bishop schrieb:
> With a structure like:
> 
> class CvRect(ctypes.Structure):
>     _fields_ = [("x", ctypes.c_int),
>                 ("y", ctypes.c_int),
>                 ("width", ctypes.c_int),
>                 ("height", ctypes.c_int)]
> 
> and a FF like:
> 
> cvSetImageROI = _cxDLL.cvSetImageROI
> cvSetImageROI.restype = None # void
> cvSetImageROI.argtypes = [
>     ctypes.c_void_p, # IplImage* image
>     CvRect # CvRect rect
>     ]
> 
> The call
> 
> cvSetImageROI(img, CvRect(1,2,3,4))
> 
> works fine but the mistaken call:
> 
> cvSetImageROI(img, (1,2,3,4))
> 
> Produces the "Send Error Report" dialog from Windows.
> 
> Other erroneous arguments produce a message about incorrect argument
> type but the tuple triggers some other behavior.
(Continue reading)


Gmane