forked from syndicate-lang/preserves
Tighten exceptions
This commit is contained in:
parent
fc1d6afc28
commit
e056394ca6
|
@ -40,12 +40,12 @@ class SchemaEntity:
|
|||
def decode(cls, v):
|
||||
i = cls.try_decode(v)
|
||||
if i is None:
|
||||
raise Exception('Could not decode ' + str(cls))
|
||||
raise ValueError('Could not decode ' + str(cls))
|
||||
return i
|
||||
|
||||
@classmethod
|
||||
def try_decode(cls, v):
|
||||
raise Exception('Subclass responsibility')
|
||||
raise NotImplementedError('Subclass responsibility')
|
||||
|
||||
@classmethod
|
||||
def parse(cls, p, v, args):
|
||||
|
@ -128,10 +128,10 @@ class SchemaEntity:
|
|||
if k not in v: return None
|
||||
if cls.parse(pp, v[k], args) is None: return None
|
||||
return ()
|
||||
raise Exception('Bad schema')
|
||||
raise ValueError('Bad schema')
|
||||
|
||||
def _encode(self):
|
||||
raise Exception('Subclass responsibility')
|
||||
raise NotImplementedError('Subclass responsibility')
|
||||
|
||||
def __repr__(self):
|
||||
n = self._constructor_name()
|
||||
|
@ -144,13 +144,13 @@ class SchemaEntity:
|
|||
return n + ' ' + repr(self._as_dict())
|
||||
|
||||
def _as_dict(self):
|
||||
raise Exception('Subclass responsibility')
|
||||
raise NotImplementedError('Subclass responsibility')
|
||||
|
||||
class Enumeration(SchemaEntity):
|
||||
VARIANTS = None
|
||||
|
||||
def __init__(self):
|
||||
raise Exception('Cannot create instance of Enumeration')
|
||||
raise TypeError('Cannot create instance of Enumeration')
|
||||
|
||||
@classmethod
|
||||
def _set_schema(cls, rootns, module_path, name, schema, _variant, _enumeration):
|
||||
|
@ -174,7 +174,7 @@ class Enumeration(SchemaEntity):
|
|||
return None
|
||||
|
||||
def _encode(self):
|
||||
raise Exception('Cannot encode instance of Enumeration')
|
||||
raise TypeError('Cannot encode instance of Enumeration')
|
||||
|
||||
def safesetattr(o, k, v):
|
||||
if keyword.iskeyword(k):
|
||||
|
@ -199,14 +199,14 @@ class Definition(SchemaEntity):
|
|||
if self.SIMPLE:
|
||||
if self.EMPTY:
|
||||
if len(args) != 0:
|
||||
raise Exception('%s takes no arguments' % (self._constructor_name(),))
|
||||
raise TypeError('%s takes no arguments' % (self._constructor_name(),))
|
||||
else:
|
||||
if len(args) != 1:
|
||||
raise Exception('%s needs exactly one argument' % (self._constructor_name(),))
|
||||
raise TypeError('%s needs exactly one argument' % (self._constructor_name(),))
|
||||
self.value = args[0]
|
||||
else:
|
||||
if len(args) != len(self.FIELD_NAMES):
|
||||
raise Exception('%s needs argument(s) %r' % (self._constructor_name(), self.FIELD_NAMES))
|
||||
raise TypeError('%s needs argument(s) %r' % (self._constructor_name(), self.FIELD_NAMES))
|
||||
i = 0
|
||||
for k in self.FIELD_NAMES:
|
||||
safesetattr(self, k, args[i])
|
||||
|
@ -255,7 +255,7 @@ class Definition(SchemaEntity):
|
|||
return None
|
||||
|
||||
def _encode(self):
|
||||
raise Exception('Not yet implemented')
|
||||
raise NotImplementedError('Not yet implemented')
|
||||
|
||||
def _as_dict(self):
|
||||
return dict((k, getattr(self, k)) for k in self.FIELD_NAMES)
|
||||
|
@ -298,7 +298,7 @@ def gather_defined_field_names(s, acc):
|
|||
elif s.key == DICT:
|
||||
gather_defined_field_names(tuple(s[0].values()), acc)
|
||||
else:
|
||||
raise Exception('Bad schema')
|
||||
raise ValueError('Bad schema')
|
||||
|
||||
def pretty_subclass(C, module_name, class_name):
|
||||
class S(C): pass
|
||||
|
@ -317,7 +317,7 @@ def lookup(ns, module_path, name):
|
|||
return ns[name]
|
||||
|
||||
def definition_not_found(module_path, name):
|
||||
raise Exception('Definition not found: ' + module_path_str(module_path + (name,)))
|
||||
raise KeyError('Definition not found: ' + module_path_str(module_path + (name,)))
|
||||
|
||||
class Namespace:
|
||||
def __init__(self, prefix):
|
||||
|
@ -336,7 +336,7 @@ class Namespace:
|
|||
def __setitem__(self, name, value):
|
||||
name = Symbol(name)
|
||||
if name in self._items:
|
||||
raise Exception('Name conflict: ' + module_path_str(self._prefix + (name.name,)))
|
||||
raise ValueError('Name conflict: ' + module_path_str(self._prefix + (name.name,)))
|
||||
self._items[name] = value
|
||||
|
||||
def __contains__(self, name):
|
||||
|
@ -361,7 +361,7 @@ class Compiler:
|
|||
|
||||
def load_schema(self, module_path, schema):
|
||||
if schema[0][VERSION] != 1:
|
||||
raise Exception('Unsupported Schema version')
|
||||
raise NotImplementedError('Unsupported Schema version')
|
||||
ns = self.root
|
||||
for e in module_path:
|
||||
if not e in ns:
|
||||
|
|
Loading…
Reference in New Issue