Pass multidimensional array (matrix) to c function using ctypes
Hello, I would like to pass a two dimensional array to C function in a dll. I use ctypes to call the function. I compile the dll with visual studio 2008 express and my C source code looks like this. #include #ifdef __cplusplus extern "C" { // only need to export C interface if // used by C++ source code using namespace std; #endif __declspec(dllexport) int print(double** ptr, int ny, int nx) { int i, j; for(i=0; i Furthermore, I am wondering if there is a fast way to use a numpy 2D array instead or alternatively to cast the ctypes array into a numpy array. Has someone an idea to help me? Thank you very much Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: Pass multidimensional array (matrix) to c function using ctypes
Thanks a lot. This solves my problem and I understand now much better what is going on. Best regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Manipulating pointers in C using ctypes
Hello! I have to ask a newbie question about manipulating pointers in C using ctypes. I have a C dll with two functions. The first one creates a pointer and returns it to python. The second one takes a pointer as an argument shows its address and the value at which it is pointing. This I have implemented using the following code --- pointers.c #include #ifdef __cplusplus extern "C" { // only need to export C interface if // used by C++ source code using namespace std; #endif __declspec(dllexport) void* create() { double number = 2.2; double* ptr = &number; printf("Pointer address \t %p \n", ptr); printf("Value at pointer \t %f \n", ptr[0]); return (void*) ptr; } __declspec(dllexport) int show(double* ptr) { printf("Pointer address \t %p \n", ptr); printf("Pointer value \t %f\n", *ptr); *ptr = 2.4; printf("New pointer value \t %f\n", *ptr); return 0; } #ifdef __cplusplus } #endif Please note that in the second function, the show function, I want to manipulate the value at which the pointer points. Now, I call this function from python with the following script. --- pointers.py -- import ctypes as ct # Load dll pointers = ct.cdll.LoadLibrary('pointers.dll') getattr(pointers, 'create') getattr(pointers, 'show') pointers.create.restype = ct.c_void_p # Create pointer in C ptr = pointers.create() # Show pointer address and value print 'Adress returned to python ' +hex(ptr) pointers.show(ct.c_void_p(ptr)) --- Calling this script gives me the following output: Pointer address 0021E508 Value at pointer 2.20 Adress returned to python 0x21e508 Pointer address 0021E508 Pointer value0.00(2.2 expected) New pointer value2.40 (2.4 expected) But the script returns also an error. WindowsError: exception: access violation reading 0x4003 WARNING: Failure executing file: Another thing that I find strange is that the return value of pointers.create is actually an integer instead of an ct.c_void_p object. Moreover, I also tried to directly manipulate the address of the pointer given as an argument to pointers.show. But when it returns to python the pointer points still at the same address as before the function call. Can someone help me with this problem? I would be very glad about an answer. With kind regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: Manipulating pointers in C using ctypes
Thanks for valuable answers. Both solutions work and I understand my mistake. Best regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list
passing a socket to a subprocess in windows
Hello! I have a problem with passing a socket to a subprocess in windows. It works in Linux and for windows there is a workaround in the Python doc. However, this workaround does not work. It was already noted by other people and they Python issue tracker http://bugs.python.org/issue5879 *** the example from http://docs.python.org/library/multiprocessing.html?highlight=multiprocessing#module-multiprocessing named " # Example where a pool of http servers share a single listening socket # " does not work on windows. Reason: s = socket.fromfd(fd, family, type_, proto) in line 156 of reduction.py fails, because fromfd is not available on windows. Sad thing: reduction.py was put into processing.py exactly to solve that problem (i.e. reduction.py is provided as workaround for socket.fromfd not available on windows, from the documentation: if sys.platform == 'win32': import multiprocessing.reduction # make sockets pickable/inheritable the solution within processing was: try: fromfd = socket.fromfd except AttributeError: def fromfd(fd, family, type, proto=0): s = socket._socket.socket() _processing.changeFd(s, fd, family, type, proto) return s but: _multiprocessing has no longer a method changeFd. Harald *** Has someone information about this or can help me to solve the problem. Thanks in advance Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: Package for fast plotting of many data points in Python?
Hi, thanks for your repleys. I have tried matplotlib but it is extremely slow. I think it is more optimized for good looking plots instead of speed. I do not know the Python bindings of gnuplot and Veusz. To clarify the issue again, by 25000 data points I mean 25000 pixels, i.e. corresponding (x,y) pairs. Thus the mapping to one pixel on the screen is not unique. Now, that I have to implement this problem on my own I am very impressed by the plotting capabilities of LabView which brings thounds of data points on screen in an extremely short time. Thanks for your help. Best regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Fast reading and unpacking of binary data (struct module)
Hi, I have a Python newbie question about reading data from a binary file. I have an huge binary file from an external program. I want to read and process the data in this file in a reasonable time. It turns out that the reading of the data itself and the processing do not need most of the time. However, when using the read(bytes) method Python returns a string representing the binary information in hex. This string I have to "cast/translate" into a number (in my case a signed short). For this I am using the method struct.unpack from the struct module. This unpacking part of the program takes by far the most time. Is there a way to speed this up or to do it the unpacking more cleverly than with the struct module? Thanks in advance. With kind regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list