From ff0f4fe46ef02ada1d83e846ed38b3cdace5b110 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Sun, 18 Mar 2012 13:52:59 -0400 Subject: [PATCH] fixed error handling code. Now more robust against corrupt chunks! --- overviewer_core/cache.py | 3 ++- overviewer_core/nbt.py | 28 +++++++++++++++------------- overviewer_core/world.py | 10 +++++----- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/overviewer_core/cache.py b/overviewer_core/cache.py index 6c5a5a2..d21dbc5 100644 --- a/overviewer_core/cache.py +++ b/overviewer_core/cache.py @@ -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 diff --git a/overviewer_core/nbt.py b/overviewer_core/nbt.py index faf94bf..3d69f8b 100644 --- a/overviewer_core/nbt.py +++ b/overviewer_core/nbt.py @@ -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 # 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)) diff --git a/overviewer_core/world.py b/overviewer_core/world.py index f8cbc57..8511a2c 100644 --- a/overviewer_core/world.py +++ b/overviewer_core/world.py @@ -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