0

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.

asked Apr 7, 2017 at 19:28
1
  • What version of Python? Py2 treats mmaps as strings, Py3 as bytearrays Commented Apr 7, 2017 at 19:38

1 Answer 1

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.)

answered Apr 7, 2017 at 19:36
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, I was running in circles wondering what was wrong with struct and I forgot the mmap part. Thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.