Rob Gaddi | 16 Apr 02:01 2014

Can't create small bitfields

I'm trying to build up bitfields that are smaller than the word size of
the machine, only 1 or 2 bytes.  Unfortunately, the smallest Structure
I can get is 4 bytes.  Any ideas on what I'm doing wrong?  OS is
x86_64 Ubuntu, Python is 2.7.4 or 3.3.1 with equal results.  Sample code
below.

---

from __future__ import print_function
from ctypes import *

class Struct(Structure):
    _pack_ = 1

Struct._fields_ = [
        ('low', c_uint, 4),
        ('high', c_uint, 4),
    ]

x = Struct(low = 1, high = 2)

print('Structure Size:', sizeof(Struct))
print('Instance Size:', sizeof(x))
print('Structure Fields:', Struct.low, Struct.high)
print('Instance Fields:', x.low, x.high)

arr = (c_uint8 * sizeof(x)).from_buffer(x)
print('Bytes:', ','.join('{0:02X}'.format(b) for b in arr))

---
(Continue reading)

Roberto Bouza | 3 Apr 20:15 2014
Picon

Compiling ctypes statically?

Hello,

I have a python binary compiled statically, when I try to do the same with ctypes (outside of python compilation using the tar.gz) I get error because is trying to build the .so instead of a .a

Sorry I'm a bit lost with this, but anyways here is the compilation flags:

$> LDFLAGS="-Wl,-no-export-dynamic -static-libgcc -static -ldl -L/usr/local/lib -L/usr/local/lib64 -lev" CPPFLAGS="-static -fPIC -I/usr/local/include" /usr/local/python-static/bin/python setup.py build

Here is the output:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/crtbeginT.o: relocation R_X86_64_32 against `__DTOR_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/crtbeginT.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

Any help will be appreciated.

Thanks.
--
Roberto Bouza Fraga.
www: http://www.robertobouza.com
t: http://twitter.com/bouzafr - <at> bouzafr
e: bouzafr <at> gmail.com
------------------------------------------------------------------------------
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
muhammad waseem sarwar | 12 Dec 07:44 2013
Picon

Regarding external binding of functions

Hi,

I am new to ctypes and I have a questions regarding external binding
of function using ctypes. The question is as follows,

Q. I have a simple library that uses function pointer of
hello_printf(const char *format, ...) in one of its API. While using
this library in C, I point the function pointer of hello_printf to
printf and code works seamlessly.
hello_printf is not the API but is used in the implementation of one
of the API's. The reason for this is that I want external application
using the library to provide implementation of printf to provide
flexibility and is common practice in C/C++.

I want to know that how I can achieve this external binding of
function pointer using ctypes i.e. point hello_printf() to printf in
python so on calling the API, it gets the right printf implementation.

I am not sure if I explained it well and I can post the code if there
is ambiguity in question.

Help would be highly appreciated and thanks in advance.

Regards,
Waseem

------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
Nicholas Earl | 29 Sep 00:53 2013
Picon

Return struct causes segfault

I'm struggling with returning a struct value using ctypes. Mainly, it keeps segfaulting when I try and get the values from the returned object. I have my python script setup like this:

import ctypes
import numpy as np
from numpy.ctypeslib import ndpointer


class PhotoInfo(ctypes.Structure):
    _fields_ = [
        ('times', ctypes.POINTER(ctypes.c_double)),
        ('fluxes', ctypes.POINTER(ctypes.c_double)),
    ]


def main():
    lib = ctypes.cdll.LoadLibrary('cfile.so')

    start = lib.start
    start.restype = ctypes.POINTER(PhotoInfo)
    start.argtypes = [
        ctypes.c_int,
        ndpointer(ctypes.c_double),
    ]

    N = 3
    mass = np.array([0.00020335520, 5.977884E-05, 9.320397E-08])

    phin = start(N, mass)

    print phin[0].times[0]


And in my c file:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

typedef struct {
  double *times;
  double *fluxes;
} PhotoInfo;

extern "C" {
  PhotoInfo *start(int N, double *mass);
}

PhotoInfo *start(int N, double *mass) {
  PhotoInfo phin;
  vector<double> times;
  vector<double> fluxes;

// Here, I read in a file and fill times and fluxes with doubles using
// times.push_back(t) and fluxes.push_back(f)\

  phin.times = &times[0];
  phin.fluxes = &times[0];
  return &phin;
}


Any guidance on this would be greatly appreciated!

Nicholas Earl
Graduate Research Assistant
San Diego State University
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60133471&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Mathias Legerer | 27 Sep 13:33 2013
Picon

Accessing Array in Struct

Hi, I have a Struct in C defined like this:

typedef struct _xmySTRUCT
   {

   char   chSomeChar;
   <...>
   double dProperty[ 10];
   double dProptype[ 10];
   char   szPropname[ 10][  21];
   <...>
   } xmySTRUCT, *PxmySTRUCT;


In Python I defined the struct like this:
class MyStructPy(Structure):
   _fields_ = [   ("somechar"           , c_char                          )
                , ("property"           , c_double                  *  10 )
                , ("proptype"           , c_double                  *  10 )
                , ("propname"           , c_char * 21               *  10 )
]


I let my C-Applicatino load Data in the struct and then I try to read out the values in Python.
When I try to access the array of doubles, I get some incorrect values (e.g.
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
2705755772498316500000000000000000000000.000000
2706128776157293300000000000000000000000.000000
2705755777574079800000000000000000000000.000000
0.000000

And strangely the Values in Char-Array all seem to miss the first character.
Thats why I think the problem has something to do with pointers?

I have several such structs in C without Arrays of doubles or Char-Arrays and they work fine, but these arrays make problems.
Can somebody point me in the direction, what I do wrong?

Thank you,
Mathias
 
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60133471&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Nicholas Earl | 27 Sep 06:37 2013
Picon

Help with struct arrays

Hello all! I'm having difficulties accessing the information in a returned struct object. I'm quite new to C/C++, but I've had plenty of experience with C#, so I think I have my foot in the door.

I have a struct defined as

class PhotoInfo(ctypes.Structure):
    _fields_ = [
        ('times', ctypes.POINTER(ctypes.c_double)),
        ('fluxes', ctypes.POINTER(ctypes.c_double)),
    ]


with the restype defined like

def main():
    lib = ctypes.cdll.LoadLibrary('photoroutine.so')

    start = lib.start
    PhotoInfoPtr = ctypes.POINTER(PhotoInfo)
    start.restype = PhotoInfoPtr


But when I call

phin = start()

I can't get to the time and flux info the returned object has. I've tried using

phin.contents.times

but it just gives me a location; i.e. <__main__.LP_c_double object at 0x159fef0>

How do I get the actual array of times and fluxes?

Thanks for your time!

Nicholas Earl
Graduate Research Assistant
San Diego State University
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60133471&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Brad Basler | 30 Aug 22:47 2013

Writing to memory buffer in a .dll?

I am attempting to use Python w/ctypes to control an embedded 
programming tool (see UM0151, STMicroelectronics).

I had this working before as a basic test, but not really realizing what 
I'm doing differently now. I do not seem to be writing to the code image.

Here's the function prototype I am trying to interface with.
Prototype: char far *EGetImagePtr(DWORD dwAreaId, long *MemSize)

This returns a pointer that is allocated on the heap by the .dll

I can read from this buffer, but I don't appear to be able to write to it.

where my function was defined by:
     EPRCore.dll_eprcore.EGetImagePtr.argtypes = 
[ctypes.c_uint,ctypes.c_void_p]

     EPRCore.dll_eprcore.EGetImagePtr.restype = 
ctypes.POINTER(ctypes.c_char)

ptr = self.myEPR.EGetImagePtr(self.memHandle,ctypes.byref(self.memSize))
ptr[0x8112] = 0x00;
ptr[0x8113] = 0x01;
ptr[0x8114] = 0x02;
ptr[0x8115] = 0x03;

When I program the board, the values aren't modified (serial number). 
What's going on here?

-Thanks
-Brad

--

-- 
Brad Basler
brad <at> midnitesolar.com
360-403-7207 Ext. 27

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
zhangxaochen | 15 Aug 13:26 2013

Why ctypes always returns True?

My c code is:
    extern "C" __declspec(dllexport) bool isTrue( bool b){ return b; }
py code is:
    lib=ctypes.cdll.LoadLibrary('xx.dll') #dll generated from the c code above
    b=lib.isTrue(False) 
    print c_bool(b).value

no matter i call lib.isTrue() with True or False, the output is always 'True'.....
could someone explain?





发件人:张琛
联系电话:13575752056(杭州)
联系地址:浙江省杭州市西湖区浙大路38号  浙大玉泉校区32舍203       邮编:310027
MOTTO:There is no secret ingredient...



------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Nyirő Gergő | 8 Aug 15:31 2013
Picon

struct of struct as return value

Hello,

I have a problem with structure as a return value, structure of structure to be correct.

Here is c snippet that demonstrate the interface

```c
#ifndef foo_h
#define foo_h

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#define C_CALL  __cdecl

#else
#define DECLDIR __declspec(dllimport)
#define C_CALL
#endif

#pragma pack(1)

typedef struct in{
  float a;
  float b;
} In;


typedef struct data{
  float c;
  float d;
} Data;

typedef struct out{
  Data front;
  Data rear;
} Out;

#pragma pack(0)

DECLDIR Out C_CALL calc(In in);

#endif
```

I compile it with mingw32-gcc:
$ i686-pc-mingw32-gcc-4.5.2 -c foo.c -D DLL_EXPORT -Wall
$ i686-pc-mingw32-gcc-4.5.2 -shared -o foo.dll foo.o -Wl,--out-implib,libfoo.a,--output-def,foo.def

That should be the python wrapper:
```python
from ctypes import Structure, c_float, CDLL

class In(Structure):
  _fields_ = [
    ('a', c_float),
    ('b', c_float),
  ]
  _pack_ = 1

class Data(Structure):
  _fields_ = [
    ('c', c_float),
    ('d', c_float),
  ]
  _pack_ = 1

class Out(Structure):
  _fields_ = [
    ('front', Data),
    ('rear',  Data),
  ]
  _pack_ = 1

lib = CDLL('foo.dll')

lib.calc.argtypes = In,
lib.calc.restype = Out

data = In(1.0, 2.0)

result = lib.calc(data)
```

The wrapper worked until, I used only a plain structure as a return value,
and it is working if I use pointer the get back the data, but unfortunatelly
I cannot change on the interface.

How should I handle this output?

thanks for your advice in advance,
gergo

------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Urs Schulz | 7 Jul 11:35 2013
Picon

Weird bug? with CFUNCTYPE and glfw


Hi folks,

I found a weird bug (or at least I don't understand whats happening)
when writing some Python 3 code using GLFW 3.0.1. Unfortunately I was
not able to reproduce the error with a selfwritten small C library, it
only occours when using glfw on linux (testet on arch 64 bit).

I reduced the problem to the following minimal code:
#!/usr/bin/env python3

import ctypes as c

class GLFWwindow(c.Structure):
    pass
GLFWwindow_p = c.POINTER(GLFWwindow)
GLFWkeyfun = c.CFUNCTYPE(None, GLFWwindow_p, c.c_int, c.c_int, c.c_int,
c.c_int)

lib = c.CDLL("libglfw.so")
lib.glfwCreateWindow.restype = GLFWwindow_p

lib.glfwInit()
win = lib.glfwCreateWindow(800, 600, "Title".encode(), None, None)
lib.glfwSetKeyCallback(win, GLFWkeyfun(lambda w, key, scancode, action,
mods: print(key, action)))

#def cb(*args):
#    print(args)

while True:
    lib.glfwPollEvents()

The window is created successfully, but when I press a key I get the
following errors in the console:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 260, in 'calling callback function'
TypeError: 'LP_GLFWwindow' object is not callable
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 260, in 'calling callback function'
TypeError: 'LP_GLFWwindow' object is not callable

(this block for each press and release). I don't know why it tries to
call a LP_GLFWwindow object...
But if I now uncomment the cb function (and not! changing any other
thing inside the code) the output becomes even more weird:

(<__main__.LP_GLFWwindow object at 0x7ff51e03edd0>, 87, 25, 1, 0)
(<__main__.LP_GLFWwindow object at 0x7ff51e03edd0>, 87, 25, 0, 0)

But I have never registered cb as a callback to glfwSetKeyCallback.

So I tried changing to Python 2.7.5. The behaviour changes to this:

Output without the cb-function:

> 87 1
> [1]    1795 segmentation fault (core dumped)  ./test2.py

and with cb-function:

> (<__main__.LP_GLFWwindow object at 0x7f72fab86680>, 87, 25, 1, 0)
> [1]    1758 segmentation fault (core dumped)  ./test2.py

The code header changed to:

#!/usr/bin/env python2
from __future__ import print_function

the rest of the code is exactly the same.

I have the following idea that may explain this:
Python is freeing the lambda function, because GLFWkeyfun does not
properly increment the reference counter on it. After this it creates
the cb function, and stores it on the same memory where the lambda lived
before, so this one gets called instead. But I don't know much about
Python or ctypes internals so this is just speculation.

I googled a lot but found nothing on this. I am using Python 3.3.2 and
GLFW 3.0.1 both in the recent version from the arch repos (64 bit). You
can find the glfw documentation at [1]. I'm not even sure if this is a
bug or just missusage of the ctypes module or maybe even a bug inside
gcc or glfw. If you need any other information (including hardware or
system information) or require me to test stuff, please tell me.

Cheers

References:
[1] http://www.glfw.org/docs/3.0/group__input.html

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
Stephen | 5 Jul 15:10 2013
Picon

ctypes: buffers and memoryviews


Debian 3.9.6-1 x86_64 GNU/Linux

Python 3.2.4 (default, May  8 2013, 20:55:18)
[GCC 4.7.3] on linux2
 >>> import ctypes
 >>> ctypes.__version__
'1.1.0'

Hello,

In python 2.x I would use ctypes to create a structure and write a 
buffer for transmission of a message.

I would use a recipe I found online that followed the format

class CMD_HEADER(ctypes.Structure):
     _pack_ = 2
     _fields_ = [ ('command', ctypes.c_ushort ),
                  ('length',   ctypes.c_uint   )
                  ]

     def CreateTxBuffer(self):
         return buffer(self)[:]

     def receiveSome(self, bytes):
         fit = min(len(bytes), ctypes.sizeof(self))
         ctypes.memmove(ctypes.addressof(self), bytes, fit)

This has worked for me well.

Now python3 has removed buffer, and seems I should use memoryview instead
I can't seem to find a python 3 recipe online to achieve the same so I 
came up with the following modification to CreateTxBuffer

     def CreateTxBuffer(self):
         return memoryview(self)

Seems to work on surface, but I'm having all sorts of trouble using the 
resultant memoryview.

for the following sample program

import ctypes
from binascii import hexlify

class CMD_HEADER(ctypes.Structure):
     _pack_ = 2
     _fields_ = [ ('command', ctypes.c_ushort ),
                  ('length',   ctypes.c_uint   )
                  ]
     def CreateTxBuffer(self):
         return memoryview(self)

     def receiveSome(self, bytes):
         fit = min(len(bytes), ctypes.sizeof(self))
         ctypes.memmove(ctypes.addressof(self), bytes, fit)

# create a ctype struct and populate it with data
msg = CMD_HEADER()
msg.command = 0x123
msg.length=2

# Get access to the underlying message structure data (should be a 
memory view)
txbuf = msg.CreateTxBuffer()

#create a bytearry with same data then create memoryview to that byte array
bb = bytearray( txbuf )
mv = memoryview(bb)

print("type={0} , len={1} , shape={2}, buf={3}".format(type(txbuf), 
len(txbuf), txbuf.shape, hexlify(txbuf)) )
print("type={0} , len={1} , shape={2}, buf={3}".format(type(mv), 
len(mv), mv.shape, hexlify(mv)) )

I get the following output, indicating the memory view I get from ctypes 
is behaving differently than what I thought it should.
type=<class 'memoryview'> , len=1 , shape=None, buf=b'230102000000'
type=<class 'memoryview'> , len=6 , shape=(6,), buf=b'230102000000'

Notice the 'ctypes' memoryview I dont have correct len or shape

I'm getting out of my depth here, not really sure what I've done wrong.
So I'd like to ask is there a correct way to create the tx buffer in 
python 3, obviously my method is not correct.

Thanks
Steve

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev

Gmane