classification
Title: `mmap.mmap(..., 0)` doesn't work as advertised in docs (fails with EINVAL); relies on compatibility behavior no longer permitted by [some] Unix OSes
Type: Stage:
Components: Interpreter Core Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ngie
Priority: normal Keywords:

Created on 2020-05-26 20:44 by ngie, last changed 2020-05-26 20:45 by ngie.

Messages (2)
msg370029 - (view) Author: Enji Cooper (ngie) * Date: 2020-05-26 20:44
The documentation for mmap.mmap() claims that passing a length of 0 will map in an entire file, but unfortunately that doesn't work as shown below:

>>> mmap.mmap(-1, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

I've double-checked that this isn't an OS specific bug on the following OS platforms:

* Fedora 31
* FreeBSD 
* OSX Catalina

The errno == EINVAL issue is documented in the OS X Catalina manpage as follows:

```
     [EINVAL]           The len argument was negative or zero. Historically, the system call would not return an error if the argument was zero.  See other potential additional
                        restrictions in the COMPATIBILITY section below.

...

COMPATIBILITY
     mmap() now returns with errno set to EINVAL in places that historically succeeded.  The rules have changed as follows:

     o   The flags parameter must specify either MAP_PRIVATE or MAP_SHARED.

     o   The len parameter must not be 0.

     o   The off parameter must be a multiple of pagesize, as returned by sysconf().
```

POSIX concurs: https://pubs.opengroup.org/onlinepubs/9699919799/ .

So, in short -- python's mmap.mmap(.., 0) implementation relies on behavior which is now not permitted in multiple OSes and is not permitted per the POSIX spec for mmap(2).

1. https://docs.python.org/3/library/mmap.html#mmap.mmap
msg370030 - (view) Author: Enji Cooper (ngie) * Date: 2020-05-26 20:45
Sidenote: all versions tested were 3.8.2:

pinklady:freebsd ngie$ /usr/local/opt/python@3.8/bin/python3 -V
Python 3.8.2
History
Date User Action Args
2020-05-26 20:45:19ngiesetmessages: + msg370030
2020-05-26 20:44:43ngiecreate