def test_post_compile_warn(self): c = compiler.Compiler('oops') with self.assertWarns(compiler.PostCompileWarning): python = c.compile([ ( 'operator..truediv', 0, 0, ), ( 'print', ( 'quote', 'oops', ), ), ]) self.assertIn( """\ __import__('operator').truediv( (0), (0)) # Traceback (most recent call last):""", python) self.assertIn( """\ # ZeroDivisionError: division by zero # print( 'oops')""", python)
def transpile_module( package: resources.Package, resource: Union[str, PurePath], out: Union[None, str, bytes, Path] = None, ): code = resources.read_text(package, resource) path: Path with resources.path(package, resource) as path: out = out or path.with_suffix(".py") if isinstance(package, ModuleType): package = package.__package__ if isinstance(package, os.PathLike): resource = resource.stem with open(out, "w") as f, qualify_context(f"{package}.{resource.split('.')[0]}") as qualsymbol: print("writing to", out) hissp = parse(lex(code)) f.write(compiler.Compiler(qualsymbol, evaluate=True).compile(hissp))
def test_compile_literal(self, form): self.assertEqual(form, eval(compiler.Compiler().quoted(form)))
def test_compile_pickle(self, form): self.assertEqual(form, eval(compiler.Compiler().pickle(form)))