X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fparser.py;h=9d515a24b9e476343a9d829b1492454095850559;hp=8f253fe7a09d722ebded8d296d4c8f0a9d64def6;hb=d72980b6368d0ac1de1ae1091bfb0582e9afcb1d;hpb=6e7423671150b704dd2accbf4b8d544744271b81 diff --git a/bulletml/parser.py b/bulletml/parser.py index 8f253fe..9d515a2 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -13,6 +13,12 @@ import math from xml.etree.ElementTree import ElementTree +# Python 3 moved this for no really good reason. +try: + from sys import intern +except ImportError: + pass + try: from io import StringIO except ImportError: @@ -25,7 +31,7 @@ from bulletml.errors import Error from bulletml.expr import NumberDef, INumberDef -__all_ = ["ParseError", "BulletML"] +__all__ = ["ParseError", "BulletML"] class ParseError(Error): """Raised when an error occurs parsing the XML structure.""" @@ -64,7 +70,7 @@ class Direction(object): def __init__(self, type, value): if type not in self.VALID_TYPES: raise ValueError("invalid type %r" % type) - self.type = type + self.type = intern(type) self.value = value def __getstate__(self): @@ -132,7 +138,7 @@ class Speed(object): def __init__(self, type, value): if type not in self.VALID_TYPES: raise ValueError("invalid type %r" % type) - self.type = type + self.type = intern(type) self.value = value def __getstate__(self): @@ -358,7 +364,7 @@ class BulletDef(object): direction = None speed = None - def __init__(self, actions=[], direction=None, speed=None, tags=()): + def __init__(self, actions=(), direction=None, speed=None, tags=()): self.direction = direction self.speed = speed self.actions = list(actions) @@ -386,6 +392,7 @@ class BulletDef(object): actions = [] speed = None direction = None + tags = set() for subelem in element.getchildren(): tag = realtag(subelem) if tag == "direction": @@ -397,8 +404,8 @@ class BulletDef(object): elif tag == "actionRef": actions.append(ActionRef.FromXML(doc, subelem)) elif tag == "tag": - self.tags.add(subelem.text) - dfn = cls(actions, direction, speed) + tags.add(subelem.text) + dfn = cls(actions, direction, speed, tags) doc._bullets[element.get("label")] = dfn return dfn @@ -536,7 +543,7 @@ class Offset(object): def __init__(self, type, x, y): if type not in self.VALID_TYPES: raise ValueError("invalid type %r" % type) - self.type = type + self.type = intern(type) self.x = x self.y = y @@ -613,6 +620,7 @@ class FireDef(object): direction = None speed = None offset = None + tags = set() for subelem in element.getchildren(): tag = realtag(subelem) @@ -627,9 +635,9 @@ class FireDef(object): elif tag == "offset": offset = Offset.FromXML(doc, subelem) elif tag == "tag": - self.tags.add(subelem.text) + tags.add(subelem.text) try: - fire = cls(bullet, direction, speed, offset) + fire = cls(bullet, direction, speed, offset, tags) except UnboundLocalError as exc: raise ParseError(str(exc)) else: @@ -704,7 +712,7 @@ class BulletML(object): ) def __init__(self, type="none", actions=None): - self.type = type + self.type = intern(type) self.actions = [] if actions is None else actions def __getstate__(self): @@ -772,7 +780,7 @@ class BulletML(object): else: try: return yaml.load(source) - except Exception, exc: + except Exception as exc: raise ParseError(str(exc)) @classmethod