473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ctypes, arrays and pointers

Does anyone know how to do the equivalent of this using ctypes?

image_data = malloc(width * height * components);
row_pointers = png_get_rows(pn g_ptr, info_ptr);
for (y = 0; y < height; y++)
memcpy(&image_d ata[width * components * y],
row_pointers[height-y-1],
width * components);

That is, I need to get the address of a location *within* an allocated
array. This is what I've got:

image_data = create_string_b uffer(width * height * components)
address = addressof(image _data)
row_pointers = libpng.png_get_ rows(png_ptr, info_ptr)
for y in xrange(height):
row = string_at(addre ss + width * components * y)
memmove(row, row_pointers[height-y-1], width * components)

but that's not correct. Either the addressof() or string_at() are not
working according to my understanding from the docs, because that code
segfaults. FWIW, I also tried:

row = image_data + width * components * y

but you can't add an int to a c_char_Array_24 15600.

Sadly the docs don't seem to be finished :(

http://docs.python.org/dev/lib/ctype...-pointers.html
Oct 4 '06 #1
2 3633
Richard Jones schrieb:
Does anyone know how to do the equivalent of this using ctypes?

image_data = malloc(width * height * components);
row_pointers = png_get_rows(pn g_ptr, info_ptr);
for (y = 0; y < height; y++)
memcpy(&image_d ata[width * components * y],
row_pointers[height-y-1],
width * components);

That is, I need to get the address of a location *within* an allocated
array. This is what I've got:

image_data = create_string_b uffer(width * height * components)
address = addressof(image _data)
row_pointers = libpng.png_get_ rows(png_ptr, info_ptr)
for y in xrange(height):
row = string_at(addre ss + width * components * y)
memmove(row, row_pointers[height-y-1], width * components)

but that's not correct. Either the addressof() or string_at() are not
working according to my understanding from the docs, because that code
segfaults. FWIW, I also tried:

row = image_data + width * components * y

but you can't add an int to a c_char_Array_24 15600.
This feature request has now come up the second time, so I guess I will
have to find a solution for it. Someone suggested a byref_at(obj, offset) method
which would return a kind of pointer to 'obj' plus offset, and even provided
a patch for it (on the ctypes-users lists, maybe even in the ctypes or Python
SF tracker). Unfortunately that was after the feature freeze for Python 2.5.
Sadly the docs don't seem to be finished :(

http://docs.python.org/dev/lib/ctype...-pointers.html

This problem wouldn't have been described in the docs anyway, but
keep on bugging me about the docs:

Thomas

Oct 6 '06 #2
Thomas Heller wrote:
Richard Jones schrieb:
> row = image_data + width * components * y

but you can't add an int to a c_char_Array_24 15600.

This feature request has now come up the second time, so I guess I will
have to find a solution for it. Someone suggested a byref_at(obj, offset)
method which would return a kind of pointer to 'obj' plus offset, and even
provided a patch for it (on the ctypes-users lists, maybe even in the
ctypes or Python SF tracker). Unfortunately that was after the feature
freeze for Python 2.5.
The following will do what I wanted:

def ptr_add(ptr, offset):
address = addressof(ptr.c ontents) + offset
return pointer(type(pt r.contents).fro m_address(addre ss))

Thanks to Alex Holkner for the tip.
Richard

Oct 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2590
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call functions exposed from dlls/shared libraries and has extensive facilities to create, access and manipulate
3
5552
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This Simple MAPI module was Ian's completed version of an example I had posted in an earlier message. In it I had to set some pointer fields in a C structure to NULL. I was not happy with the solution I used so I made an inquiry on the ctypes-users...
3
5006
by: rubbishemail | last post by:
Hello, i've got a problem with pointers in the following function which i want to use: I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value) The function is supposed to read out the status of a digital port of analog digital interface card. I got this function from Dask.h which came with the card. The relevant lines concerning this function are the following:
2
2331
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId : the class Id of objects owning attributes to
12
10129
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv, ctypes.c_void_p).value != None) Yes?
3
4231
by: p.lavarre | last post by:
Subject: Python CTypes translation of (pv != NULL) And so then the next related Faq is: Q: How should I test for ((void *) -1)? A: (pv == 0xffffFFFF) works often.
2
4436
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
3
4808
by: Ron Garret | last post by:
CTypes on a 64-bit machine appears to be truncating pointers to 32 bits: $ uname -a Linux monster1 2.6.18-6-amd64 #1 SMP Mon Jun 16 22:30:01 UTC 2008 x86_64 GNU/Linux $ cat foo.c void* foo(void* x) { return x; } $ gcc -fPIC -shared foo.c -o foo.so $ file foo.so foo.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not
2
1993
by: Robert Kern | last post by:
dcharno wrote: Well, stack alignment would be a problem with how the shared library gets compiled, nothing to do with ctypes (I think). However, if you are passing in arrays from ctypes, *they* may also be misaligned. Try to check the addresses of the ctypes values you are passing in. I'm not entirely sure how to do that, though. -- Robert Kern
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.