1

I am trying to execute this program taken from the online python documentation(https://docs.python.org/3.6/library/mmap.html) and its about the implementation of mmap.

import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
    f.write(b"Hello Python!\n")

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    mm = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print(mm.readline())  # prints b"Hello Python!\n"
    # read content via slice notation
    print(mm[:5])  # prints b"Hello"
    # update content using slice notation;
    # note that new content must have same size
    mm[6:] = b" world!\n"
    # ... and read again using standard file methods
    mm.seek(0)
    print(mm.readline())  # prints b"Hello  world!\n"
    # close the map
    mm.close()

Unfortunately, on my system when I execute on command line I get these errors.

user@logger$ python3 mmap1.py 
Traceback (most recent call last):
  File "mmap1.py", line 9, in <module>
    mm = mmap.mmap(f.fileno(), 0)
OSError: [Errno 22] Invalid argument
user@logger$ python mmap1.py 
Traceback (most recent call last):
  File "mmap1.py", line 9, in <module>
    mm = mmap.mmap(f.fileno(), 0)
mmap.error: [Errno 22] Invalid argument
user@logger$ which python
/usr/bin/python
user@logger$ python --version
Python 2.7.15rc1
user@logger$ python3 --version
Python 3.6.7

About my system, Ubuntu Linux on Virtual machine - VirtualBox:

user@logger$ uname -a
Linux user-vm 4.15.0-42-generic #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

On windows, using PyCharm I am able to get this program executed without any issues. On a virtual machine Linux, however, I get these errors. What am I missing.

Thank you.

linuxstack
  • 757
  • 8
  • 19
  • maybe because your root program is called mmap1.py? Try changing it to something else... could be a namespace issue – eatmeimadanish Dec 11 '18 at 19:22
  • Hello @eatmeimadanish , I renamed it: mv mmap1.py mmap_test.py But it still the same issue. I have copied it in a file and executing it just like other helloworld.py python file. Nothing special about the mmap_test.py. I don't know if this could be a bug as per python? https://mail.python.org/pipermail/python-bugs-list/2005-November/030968.html – linuxstack Dec 11 '18 at 19:30
  • works for me, can't reproduce. `Linux zatopek 4.15.0-39-generic #42~16.04.1-Ubuntu SMP Wed Oct 24 17:09:54 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux` – tink Dec 11 '18 at 20:52
  • what shows up when you `print(dir(mmap))` – eatmeimadanish Dec 12 '18 at 01:51
  • print(dir(mmap)) ['ACCESS_COPY', 'ACCESS_READ', 'ACCESS_WRITE', 'ALLOCATIONGRANULARITY', 'MAP_ANON', 'MAP_ANONYMOUS', 'MAP_DENYWRITE', 'MAP_EXECUTABLE', 'MAP_PRIVATE', 'MAP_SHARED', 'PAGESIZE', 'PROT_EXEC', 'PROT_READ', 'PROT_WRITE', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'error', 'mmap'] – linuxstack Dec 12 '18 at 03:49

0 Answers0