Data unchanged when passing data to Python in multiprocessing shared memory
Dennis Lee Bieber
wlfraed at ix.netcom.com
Wed Feb 2 13:41:57 EST 2022
On Wed, 2 Feb 2022 19:16:19 +0100 (CET), Jen Kris <jenkris at tutanota.com>
declaimed the following:
>It's not clear to me from the struct module whether it can actually auto-detect endianness. I think it must be specified, just as I had to do with int.from_bytes(). In my case endianness was dictated by how the four bytes were populated, starting with the zero bytes on the left.
Which is why I also suggested maybe looking at the various network/host
translation calls. They are in the socket module of Python, and should also
be available in most C standard libraries...
https://docs.python.org/3/library/socket.html#other-functions
"""
socket.ntohl(x)
Convert 32-bit positive integers from network to host byte order. On
machines where the host byte order is the same as network byte order, this
is a no-op; otherwise, it performs a 4-byte swap operation.
socket.ntohs(x)
Convert 16-bit positive integers from network to host byte order. On
machines where the host byte order is the same as network byte order, this
is a no-op; otherwise, it performs a 2-byte swap operation.
Changed in version 3.10: Raises OverflowError if x does not fit in a
16-bit unsigned integer.
socket.htonl(x)
Convert 32-bit positive integers from host to network byte order. On
machines where the host byte order is the same as network byte order, this
is a no-op; otherwise, it performs a 4-byte swap operation.
socket.htons(x)
Convert 16-bit positive integers from host to network byte order. On
machines where the host byte order is the same as network byte order, this
is a no-op; otherwise, it performs a 2-byte swap operation.
Changed in version 3.10: Raises OverflowError if x does not fit in a
16-bit unsigned integer.
"""
https://docs.python.org/3/library/struct.html
"""
Byte Order, Size, and Alignment
By default, C types are represented in the machine’s native format and byte
order, and properly aligned by skipping pad bytes if necessary (according
to the rules used by the C compiler).
Alternatively, the first character of the format string can be used to
indicate the byte order, size and alignment of the packed data, according
to the following table:
Character Byte order Size Alignment
@ native native native
= native standard none
< little-endian standard none
> big-endian standard none
! network (= big-endian) <<<<<<
standard none <<<<<<
If the first character is not one of these, '@' is assumed.
"""
Since all the programs in your situation are running on the same
machine, it would appear that at least one of them is NOT formatting
integers in native host mode -- and I don't think it is Python.
https://www.tutorialspoint.com/unix_sockets/network_byte_orders.htm
"""
These functions are macros and result in the insertion of conversion source
code into the calling program. On little-endian machines, the code will
change the values around to network byte order. On big-endian machines, no
code is inserted since none is needed; the functions are defined as null.
"""
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Python-list
mailing list