Controlling buffer alignment in file.read()

H

Haralanov, Mitko

Hi all,

I am using Python to read from a binary device file which requires that allread sizes are in 8byte multiples and the user's buffer is 8byte aligned.

I am currently using a file object and the file.read() method. However, theissue is that the file.read() method allocates the buffer passed to C function under the covers and, therefore, the alignment is arbitrary.

Is there a way that I can get file.read() to use an 8byte aligned buffer?

Thanks,
- Mitko
 
G

Gregory Ewing

I am using Python to read from a binary device file which requires that all
read sizes are in 8byte multiples and the user's buffer is 8byte aligned.

Is there a way that I can get file.read() to use an 8byte aligned buffer?

For control at that level you'd be better off using
direct system calls, i.e. os.open() and os.read(),
then you can read exacty the number of bytes you want.
 
H

Haralanov, Mitko

For control at that level you'd be better off using
direct system calls, i.e. os.open() and os.read(),
then you can read exacty the number of bytes you want.

The problem is not controlling the number of bytes read. That part seems tobe working.
The issue is that the buffer into which the data is placed needs to be of certain alignment (8byte-aligned). Python does not seem to have a way that allows me to control that.

Thanks,
- Mitko
 
G

Gregory Ewing

The problem is not controlling the number of bytes read. That part seems to
be working. The issue is that the buffer into which the data is placed needs
to be of certain alignment (8byte-aligned). Python does not seem to have a
way that allows me to control that.

Hmmm, that could be tricky. Have you tried using os.read()?
If you're lucky, Python will be using a malloc() call or
equivalent to create a str/bytes object to read the data
into, and that will return something platform-aligned.

If you're unlucky, there's probably no pure-Python
solution, and you might need to write a small C or
Cython module to accomplish this trick.
 
L

Laurent Pointal

The problem is not controlling the number of bytes read. That part seems
to be working. The issue is that the buffer into which the data is placed
needs to be of certain alignment (8byte-aligned). Python does not seem to
have a way that allows me to control that.

Thanks,
- Mitko

Did you try to set buffering parameter to 0 (unbuffered) ?

Eventually going to os.open() which map tp low level (eventually followed by
an os.fdopen())

http://docs.python.org/2/library/os.html#os.open

A+
Laurent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top