unable to resize mmap object

Carl Banks invalidemail at aerojockey.com
Tue May 2 16:20:39 EDT 2006


Fabiano Sidler wrote:
> Hi folks!
>
> I created an mmap object like so:
> --- snip ---
> from mmap import mmap,MAP_ANONYMOUS,MAP_PRIVATE
> fl = file('/dev/zero','rw')
> mm = mmap(fl.fileno(), 1, MAP_PRIVATE|MAP_ANONYMOUS)
> --- snap ---
>
> Now, when I try to resize mm to 10 byte
> --- snip ---
> mm.resize(10)
> --- snap ---
> I get an EnvironmentError:[Errno 22] Invalid argument.
>
> How can I implement a resizeable anonymous memory mapping?
> Thanks for your reply!

Bug in Python. Here's the reason (from the changelog):

- Bug #728515: mmap.resize() now resizes the file on Unix as it did
  on Windows.

The mmap module resizes the file in question by calling ftruncate.
Only problem is, you can't truncate /dev/zero.  I'm going to file this
as a bug; probably on Unix mmap should only resize the file if it's a
regular file (so fstat it first).

Frankly, I'm not so sure matching Windows behavior is a great idea.
mmap module seems to be having an identity crisis.  Is it a low-level
system call, or a high-level, OS-independent way to access files as
blocks of memory?  The modules is moving towards the latter (what with
Unix mmap accepting ACCESS-style flags, and now this file-resizing
behavior).  I actually favor a two-level approach similar to file I/O:
there would low-level system calls in the os module, and high-level
mmap object.  (The high-level object would go all the way.  It would
accept a filename rather than a file descriptor, anonymous blocks would
be handled OS-independently, rather than mapping /dev/zero, and so on.)

Carl Banks




More information about the Python-list mailing list