def test_hangwatch(): from syn.base_utils import hangwatch, message from time import sleep accum = [] assert sum(accum) == 0 def thing1(): accum.append(1) hangwatch(1, thing1) assert sum(accum) == 1 def thing2(): sleep(.05) assert_raises(RuntimeError, hangwatch, .01, thing2) def thing3(msg='foo'): raise NotImplementedError(msg) with capture(): assert_raises(NotImplementedError, hangwatch, 1, thing3) try: with capture(): hangwatch(1, thing3, 'bar') except NotImplementedError as e: assert message(e) == 'bar'
def run(self, command, env, **kwargs): try: eval(command) return 0 except Exception as e: eprint(message(e)) return 1
def coerce(self, value, **kwargs): if self.query(value): return value try: if self.call_coerce: return self.type.coerce(value, **kwargs) return self.type(value) except Exception as e: raise TypeError('Cannot coerce {} to type {}: {}' .format(value, self.type, message(e)))
def validate(self): '''Raise an exception if the object is missing required attributes, or if the attributes are of an invalid type. ''' optional = self._attrs.optional optional_none = self._opts.optional_none for attr, typ in self._attrs.types.items(): if not hasattr(self, attr): if attr in optional: continue raise AttributeError('Required attribute {} not defined'. format(attr)) val = getattr(self, attr) if optional_none: if attr in optional and val is None: continue res, e = typ.query_exception(val) if not res: raise TypeError('Validation error for attribute {}: {}'. format(attr, message(e)))
def validate(self): '''Raise an exception if the object is missing required attributes, or if the attributes are of an invalid type. ''' optional = self._attrs.optional optional_none = self._opts.optional_none for attr, typ in self._attrs.types.items(): if not hasattr(self, attr): if attr in optional: continue raise AttributeError( 'Required attribute {} not defined'.format(attr)) val = getattr(self, attr) if optional_none: if attr in optional and val is None: continue res, e = typ.query_exception(val) if not res: raise TypeError('Validation error for attribute {}: {}'.format( attr, message(e)))
def run(self, env, **kwargs): verbose = kwargs.get('verbose', False) preview = kwargs.get('preview', False) # Called from Task.run_preview() run_preview = kwargs.get('run_preview', False) silent = kwargs.get('silent', env.settings.get('silent', False)) pre = '' if preview and not run_preview: pre = kwargs.get('preview_pre', '') command, context_name = self.resolve_macros(env, **kwargs) if not context_name: context = env.default_context else: context = env.contexts[context_name] if callable(context.run): return context.run(command, env, **kwargs) cmd = context.run_command(command, env, **kwargs) if verbose: sys.stdout.write(pre + cmd + '\n') sys.stdout.flush() if not preview: args = shlex.split(cmd) if args[0] == 'yatr': try: from .main import _main _main(*args[1:]) return 0 except Exception as e: eprint(message(e)) return 1 return command_(cmd, silent)
def coerce(self, value, **kwargs): try: self.check(value) except TypeError as e: raise TypeError('Cannot coerce {}: {}'.format(value, message(e))) return value
def test_message(): e = TypeError('err') assert message(e) == 'err' assert message(TypeError()) == ''
def failure(self): return MatchFailure(message=message(self), seq=self.seq, fails=self.fails)
def test_nested_context(): from syn.base_utils import nested_context, assign class Foo(object): pass assert not hasattr(Foo, 'a') assert not hasattr(Foo, 'b') with nested_context([assign]*2, [(Foo, 'a', 1), (Foo, 'b', 2)]): assert Foo.a == 1 assert Foo.b == 2 assert not hasattr(Foo, 'a') assert not hasattr(Foo, 'b') @contextmanager def plus1(x): yield x+1 @contextmanager def plus2(x): yield x+2 @contextmanager def plus3(x): yield x+3 x = 1 with nested_context([partial(plus1, x), partial(plus2, x), partial(plus3, x)]) as (w, y, z): assert x == 1 assert w == 2 assert y == 3 assert z == 4 @contextmanager def bad(x): raise TypeError('a really really bad error') @contextmanager def terrible(x): if False: yield # pragma: no cover try: with nested_context([plus1, bad], [[x]]*2) as (y, z): pass except TypeError as e: assert message(e) == 'a really really bad error' try: with nested_context([plus1, terrible], [[x]]*2) as (y, z): pass except RuntimeError as e: assert message(e) == "generator didn't yield" try: with nested_context([plus1, plus2], [[5]]*2) as (y, z): assert y == 6 assert z == 7 raise TypeError('foobarbaz') except TypeError as e: assert message(e) == 'foobarbaz' @contextmanager def listy(x): yield [x]*x with nested_context([listy, listy], [[1], [2]]) as ret: assert ret == ([1], [2, 2]) with nested_context([listy, listy], [[1], [2]], extend=True) as ret: assert ret == (1, 2, 2)