示例#1
0
    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)
示例#2
0
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))
示例#3
0
 def test_compile_literal(self, form):
     self.assertEqual(form, eval(compiler.Compiler().quoted(form)))
示例#4
0
 def test_compile_pickle(self, form):
     self.assertEqual(form, eval(compiler.Compiler().pickle(form)))