With the next code:
shmem = mmap.mmap(0, 20, "MumbleLink", mmap.ACCESS_READ)
print("size: "+str(struct.calcsize("IL3f3f3f512s3f")))
print(struct.unpack("IL3f3f3f512s3f", shmem))
I get this output:
size: 568
Traceback (most recent call last):
File "C:\Users\Saelyth\Desktop\test.py", line 8, in <module>
print(struct.unpack("IL3f3f3f512s3f", shmem))
struct.error: unpack requires a bytes object of length 568
Why does it tells me that it requires an object of length 568 if calcsize says it's already 568?
Also, worth to mention that I had been googling (and checked This) for an answer of what is IL3f3f3f512s3f or how can you create your own string to read memory of for example 1024, not 568, but so far I had no luck. A detailed answer on how that part of struct works would help, or point me in the right direction of how to understand how to calculate the string that I need to use to unpack shmem.
This is related to This question.
-
What version of Python? Py2 treats mmaps as strings, Py3 as bytearraysMad Physicist– Mad Physicist2017年04月07日 19:38:59 +00:00Commented Apr 7, 2017 at 19:38
1 Answer 1
You only check the size in output, that size is not available to your memory map. So change your first line to:
shmem = mmap.mmap(0, 568, "MumbleLink", mmap.ACCESS_READ);
Which matches the size of struct. If you want, you can get the size first, then create the memory map to match it (second parameter.)