0

fixed error handling code. Now more robust against corrupt chunks!

This commit is contained in:
Andrew Brown
2012-03-18 13:52:59 -04:00
parent e9cf747513
commit ff0f4fe46e
3 changed files with 22 additions and 19 deletions

View File

@@ -127,7 +127,8 @@ class LRUCache(object):
def __delitem__(self, key):
# Used to flush the cache of this key
link = self.cache[key]
cache = self.cache
link = cache[key]
del cache[key]
link.left.right = link.right
link.right.left = link.left

View File

@@ -42,7 +42,17 @@ def load_region(fileobj):
for accessing the chunks inside."""
return MCRFileReader(fileobj)
class CorruptNBTError(Exception):
class CorruptionError(Exception):
pass
class CorruptRegionError(CorruptionError):
"""An exception raised when the MCRFileReader class encounters an
error during region file parsing.
"""
pass
class CorruptChunkError(CorruptionError):
pass
class CorruptNBTError(CorruptionError):
"""An exception raised when the NBTFileReader class encounters
something unexpected in an NBT file."""
pass
@@ -180,16 +190,6 @@ class NBTFileReader(object):
except (struct.error, ValueError), e:
raise CorruptNBTError("could not parse nbt: %s" % (str(e),))
class CorruptionError(Exception):
pass
class CorruptRegionError(CorruptionError):
"""An exception raised when the MCRFileReader class encounters an
error during region file parsing.
"""
pass
class CorruptChunkError(CorruptionError):
pass
# For reference, the MCR format is outlined at
# <http://www.minecraftwiki.net/wiki/Beta_Level_Format>
class MCRFileReader(object):
@@ -215,7 +215,7 @@ class MCRFileReader(object):
timestamp_data = self._file.read(4096)
if not len(timestamp_data) == 4096:
raise CorruptRegionError("invalid timestamp table")
# turn this data into a useful list
self._locations = self._table_format.unpack(location_data)
self._timestamps = self._table_format.unpack(timestamp_data)
@@ -289,7 +289,7 @@ class MCRFileReader(object):
is_gzip = False
else:
# unsupported!
raise CorruptRegionError("unsupported chunk compression type: %i" % (compression))
raise CorruptRegionError("unsupported chunk compression type: %i (should be 1 or 2)" % (compression,))
# turn the rest of the data into a StringIO object
# (using data_length - 1, as we already read 1 byte for compression)
@@ -300,5 +300,7 @@ class MCRFileReader(object):
try:
return NBTFileReader(data, is_gzip=is_gzip).read_all()
except CorruptionError:
raise
except Exception, e:
raise CorruptChunkError("Misc error parsing chunk: " + str(e))

View File

@@ -246,7 +246,7 @@ class RegionSet(object):
# This is populated below. It is a mapping from (x,y) region coords to filename
self.regionfiles = {}
# This holds up to 16 open regionfile objects
# This holds a cache of open regionfile objects
self.regioncache = cache.LRUCache(size=16, destructor=lambda regionobj: regionobj.close())
for x, y, regionfile in self._iterate_regionfiles():
@@ -319,8 +319,8 @@ class RegionSet(object):
if regionfile is None:
raise ChunkDoesntExist("Chunk %s,%s doesn't exist (and neither does its region)" % (x,z))
# Try 3 times to load and parse this chunk before giving up and raising
# an error
# Try a few times to load and parse this chunk before giving up and
# raising an error
tries = 5
while True:
try:
@@ -337,8 +337,8 @@ class RegionSet(object):
time.sleep(0.5)
continue
else:
logging.warning("Tried %d times to read chunk %d,%d but I got error %s",
tries, x, z, e)
logging.warning("Tried several times to read chunk %d,%d. Giving up.",
x, z, e)
logging.debug("Full traceback:", exc_info=1)
# Let this exception propagate out through the C code into
# tileset.py, where it is caught and gracefully continues