Beispiel #1
0
  def testFutureFeatures(self):
    testcases = [
        ('from __future__ import print_function',
         imputil.FutureFeatures(print_function=True)),
        ("""\
        "module docstring"

        from __future__ import print_function
        """, imputil.FutureFeatures(print_function=True)),
        ("""\
        "module docstring"

        from __future__ import print_function, with_statement
        from __future__ import nested_scopes
        """, imputil.FutureFeatures(print_function=True)),
        ('from __future__ import absolute_import',
         imputil.FutureFeatures(absolute_import=True)),
        ('from __future__ import absolute_import, print_function',
         imputil.FutureFeatures(absolute_import=True, print_function=True)),
        ('foo = 123\nfrom __future__ import print_function',
         imputil.FutureFeatures()),
        ('import os\nfrom __future__ import print_function',
         imputil.FutureFeatures()),
    ]

    for tc in testcases:
      source, want = tc
      mod = pythonparser.parse(textwrap.dedent(source))
      _, got = imputil.parse_future_features(mod)
      self.assertEqual(want.__dict__, got.__dict__)
Beispiel #2
0
    def testFutureFeatures(self):
        testcases = [
            ('from __future__ import print_function',
             imputil.FutureFeatures(print_function=True)),
            ("""\
        "module docstring"

        from __future__ import print_function
        """, imputil.FutureFeatures(print_function=True)),
            ("""\
        "module docstring"

        from __future__ import print_function, with_statement
        from __future__ import nested_scopes
        """, imputil.FutureFeatures(print_function=True)),
            ('from __future__ import absolute_import',
             imputil.FutureFeatures(absolute_import=True)),
            ('from __future__ import absolute_import, print_function',
             imputil.FutureFeatures(absolute_import=True,
                                    print_function=True)),
            ('foo = 123\nfrom __future__ import print_function',
             imputil.FutureFeatures()),
            ('import os\nfrom __future__ import print_function',
             imputil.FutureFeatures()),
        ]

        for tc in testcases:
            source, want = tc
            mod = pythonparser.parse(textwrap.dedent(source))
            _, got = imputil.parse_future_features(mod)
            self.assertEqual(want.__dict__, got.__dict__)
Beispiel #3
0
def _ParseAndVisit(source):
  mod = pythonparser.parse(source)
  _, future_features = imputil.parse_future_features(mod)
  importer = imputil.Importer(None, 'foo', 'foo.py', False)
  b = block.ModuleBlock(importer, '__main__', '<test>',
                        source, future_features)
  visitor = stmt.StatementVisitor(b)
  visitor.visit(mod)
  return visitor
Beispiel #4
0
def main(script=None, modname=None):
    assert script and modname, 'Script "%s" or Modname "%s" is empty' % (
        script, modname)

    gopath = os.getenv('GOPATH', None)
    if not gopath:
        print >> sys.stderr, 'GOPATH not set'
        return 1

    with open(script) as py_file:
        py_contents = py_file.read()
    try:
        mod = pythonparser.parse(py_contents)
    except SyntaxError as e:
        print >> sys.stderr, '{}: line {}: invalid syntax: {}'.format(
            e.filename, e.lineno, e.text)
        return 2

    # Do a pass for compiler directives from `from __future__ import *` statements
    try:
        future_node, future_features = imputil.parse_future_features(mod)
    except util.CompileError as e:
        print >> sys.stderr, str(e)
        return 2

    importer = imputil.Importer(gopath, modname, script,
                                future_features.absolute_import)
    full_package_name = modname.replace('.', '/')
    mod_block = block.ModuleBlock(importer, full_package_name, script,
                                  py_contents, future_features)

    visitor = stmt.StatementVisitor(mod_block, future_node)
    # Indent so that the module body is aligned with the goto labels.
    with visitor.writer.indent_block():
        try:
            visitor.visit(mod)
        except util.ParseError as e:
            print >> sys.stderr, str(e)
            return 2

    writer = util.Writer(sys.stdout)
    tmpl = textwrap.dedent("""\
      package $package
      import πg "grumpy"
      var Code *πg.Code
      func init() {
      \tCode = πg.NewCode("<module>", $script, nil, 0, func(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
      \t\tvar πR *πg.Object; _ = πR
      \t\tvar πE *πg.BaseException; _ = πE""")
    writer.write_tmpl(tmpl,
                      package=modname.split('.')[-1],
                      script=util.go_str(script))
    with writer.indent_block(2):
        for s in sorted(mod_block.strings):
            writer.write('ß{} := πg.InternStr({})'.format(s, util.go_str(s)))
        writer.write_temp_decls(mod_block)
        writer.write_block(mod_block, visitor.writer.getvalue())
    writer.write_tmpl(textwrap.dedent("""\
    \t\treturn nil, πE
    \t})
    \tπg.RegisterModule($modname, Code)
    }"""),
                      modname=util.go_str(modname))
    return 0