mmap file won't open properly with Linux

Discussion in 'Python' started by Parzival, Nov 8, 2003.

  1. Parzival

    Parzival Guest

    I have a program which works well on under Windows, which
    I am trying to get to work under Linux (Mandrake 9.1)

    It accesses a binary file with mmap. Under Windows,
    the file maps and reads withut porblems, but under linux
    an attempt to access the mapped data fails with a cryptic
    error message. I thought that perhaps the file
    was corrupted, so I recreated it in my Linux environment
    and examined it with a hex editor, and it appears to be
    perfect.

    The mmap call does not raise an exception, so how can
    the subsequent access to the fail?

    Traceback (most recent call last):
    File "./ACSatlasBuilder.py", line 292, in ?
    acs.writeAll(limit=None)
    File "./ACSatlasBuilder.py", line 45, in writeAll
    header += self.writeLocations(fin, fout, limit)
    File "./ACSatlasBuilder.py", line 65, in writeLocations
    ACS = Tz.TzDatabase("ACStt.dat")
    File "/home/parz/xAtlas/Tz.py", line 25, in __init__
    header = struct.unpack(">16i", self.Map[0:16*4])
    ValueError: mmap closed or invalid


    Here is the relevant portion of the program source:

    DefTzFile = "Ott.dat"

    class TzDatabase:

    minyear = idt.date(1830)
    magic0 = 0x108bead + 0
    magic1 = 0x108bead + 1
    nsegments = 4

    def __init__(self, filename=DefTzFile):
    self.filename = filename
    self.fd = os.open(filename, os.O_RDONLY)
    self.Map = mmap.mmap(self.fd, 0, access=mmap.ACCESS_READ)

    #### exception occurs here: #####
    header = struct.unpack(">16i", self.Map[0:16*4])
    if header[0] == self.magic0:
    self.ztSeg, self.ttSeg, self.txSeg, self.znSeg =
    range(self.nsegments)
    elif header[0] == self.magic1:
    self.ttSeg, self.txSeg, self.ztSeg, self.znSeg =
    range(self.nsegments)
    else:
    assert False, 'File %s is not a TzDatabase file' % filename


    (-: Stop, smile, and enjoy your breathing :)
    -- Parzival
    -- Reply-to is confuggled: parzp (@) shaw (.) ca
     
    Parzival, Nov 8, 2003
    #1
    1. Advertising

  2. Parzival

    Jeff Epler Guest

    Read the documentation more carefully. The "0 means length of file"
    trick only works on windows, apparently.

    You can use os.fstat(fd).st_size to get the current size of an open
    file, or os.stat(filename).st_size to get the current size of a file by
    name.

    Jeff
     
    Jeff Epler, Nov 8, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl Mackey

    mmap -- memory mapped file

    Carl Mackey, Jun 29, 2006, in forum: Python
    Replies:
    1
    Views:
    2,594
    Alex Martelli
    Jun 29, 2006
  2. Manu
    Replies:
    0
    Views:
    546
  3. Alexandru Palade

    mmap'ing the same file

    Alexandru Palade, Jul 29, 2011, in forum: C Programming
    Replies:
    3
    Views:
    406
    Ben Bacarisse
    Aug 3, 2011
  4. jackster the jackle

    Net::Telnet won't close properly

    jackster the jackle, Dec 16, 2007, in forum: Ruby
    Replies:
    0
    Views:
    120
    jackster the jackle
    Dec 16, 2007
  5. N. Demos

    Form script won't work properly in IE

    N. Demos, Mar 16, 2005, in forum: Javascript
    Replies:
    5
    Views:
    120
    N. Demos
    Mar 17, 2005
Loading...

Share This Page