mmap file won't open properly with Linux

P

Parzival

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
 
J

Jeff Epler

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top