Added code to fall back to the DefaultTable (and therefore to hex XML dumps) when an exception occurs during decompilation.
git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@44 4cde692c-a291-49d1-8350-778aa11640f8
diff --git a/Lib/fontTools/ttLib/__init__.py b/Lib/fontTools/ttLib/__init__.py
index 3aa1dd5..37e2b4f 100644
--- a/Lib/fontTools/ttLib/__init__.py
+++ b/Lib/fontTools/ttLib/__init__.py
@@ -41,7 +41,7 @@
"""
#
-# $Id: __init__.py,v 1.9 1999-12-29 13:06:08 Just Exp $
+# $Id: __init__.py,v 1.10 2000-01-03 22:58:42 Just Exp $
#
__version__ = "1.0a6"
@@ -205,7 +205,10 @@
else:
print report
xmltag = tag2xmltag(tag)
- writer.begintag(xmltag)
+ if hasattr(table, "ERROR"):
+ writer.begintag(xmltag, ERROR="decompilation error")
+ else:
+ writer.begintag(xmltag)
writer.newline()
if tag == "glyf":
table.toXML(writer, self, progress)
@@ -289,6 +292,7 @@
return self.tables[tag]
except KeyError:
if self.reader is not None:
+ import traceback
if self.verbose:
debugmsg("reading '%s' table from disk" % tag)
data = self.reader[tag]
@@ -297,7 +301,18 @@
self.tables[tag] = table
if self.verbose:
debugmsg("decompiling '%s' table" % tag)
- table.decompile(data, self)
+ try:
+ table.decompile(data, self)
+ except:
+ print "An exception accurred during the decompilation of the '%s' table" % tag
+ from tables.DefaultTable import DefaultTable
+ import StringIO
+ file = StringIO.StringIO()
+ traceback.print_exc(file=file)
+ table = DefaultTable(tag)
+ table.ERROR = file.getvalue()
+ self.tables[tag] = table
+ table.decompile(data, self)
return table
else:
raise KeyError, "'%s' table not found" % tag
diff --git a/Lib/fontTools/ttLib/tables/DefaultTable.py b/Lib/fontTools/ttLib/tables/DefaultTable.py
index 745f237..e960ff3 100644
--- a/Lib/fontTools/ttLib/tables/DefaultTable.py
+++ b/Lib/fontTools/ttLib/tables/DefaultTable.py
@@ -15,6 +15,11 @@
return self.data
def toXML(self, writer, ttFont):
+ if hasattr(self, "ERROR"):
+ writer.comment("An error occurred during the decompilation of this table")
+ writer.newline()
+ writer.comment(self.ERROR)
+ writer.newline()
writer.begintag("hexdata")
writer.newline()
writer.dumphex(self.compile(ttFont))
diff --git a/Lib/fontTools/ttLib/xmlImport.py b/Lib/fontTools/ttLib/xmlImport.py
index 8f1e344..8b54093 100644
--- a/Lib/fontTools/ttLib/xmlImport.py
+++ b/Lib/fontTools/ttLib/xmlImport.py
@@ -1,5 +1,6 @@
from fontTools import ttLib
from fontTools.misc.textTools import safeEval
+from fontTools.ttLib.tables.DefaultTable import DefaultTable
import types
import string
import Numeric, array
@@ -131,8 +132,8 @@
self.progress.set(pos / 100)
self.lastpos = pos
stack = self.locator.stack
- stacksize = len(stack)
- if not stacksize:
+ stackSize = len(stack)
+ if not stackSize:
if name <> "ttFont":
raise xml_parse_error, "illegal root tag: %s" % name
sfntVersion = attrs.get("sfntVersion", "\000\001\000\000")
@@ -140,7 +141,7 @@
sfntVersion = safeEval('"' + sfntVersion + '"')
self.ttFont.sfntVersion = sfntVersion
self.content_stack.append([])
- elif stacksize == 1:
+ elif stackSize == 1:
msg = "Parsing '%s' table..." % ttLib.xmltag2tag(name)
if self.progress:
self.progress.setlabel(msg)
@@ -149,17 +150,19 @@
else:
print msg
tag = ttLib.xmltag2tag(name)
- tableclass = ttLib.getTableClass(tag)
- if tableclass is None:
- from fontTools.ttLib.tables.DefaultTable import DefaultTable
- tableclass = DefaultTable
+ if attrs.has_key("ERROR"):
+ tableClass = DefaultTable
+ else:
+ tableClass = ttLib.getTableClass(tag)
+ if tableClass is None:
+ tableClass = DefaultTable
if self.ttFont.has_key(tag):
self.current_table = self.ttFont[tag]
else:
- self.current_table = tableclass(tag)
+ self.current_table = tableClass(tag)
self.ttFont[tag] = self.current_table
self.content_stack.append([])
- elif stacksize == 2:
+ elif stackSize == 2:
self.content_stack.append([])
self.root = (name, attrs, self.content_stack[-1])
else:
@@ -174,10 +177,10 @@
def handle_end_tag(self, name):
del self.content_stack[-1]
stack = self.locator.stack
- stacksize = len(stack)
- if stacksize == 1:
+ stackSize = len(stack)
+ if stackSize == 1:
self.root = None
- elif stacksize == 2:
+ elif stackSize == 2:
self.current_table.fromXML(self.root, self.ttFont)
self.root = None