def _ParseClifSource(stream, dump_path): """Parse PYTD into serialized protobuf.""" if FLAGS.py3output: init = [ 'type str = `UnicodeFromBytes` as bytes', 'type unicode = `UnicodeFromBytes` as bytes', 'from builtins import chr' ] else: init = [ 'type str = bytes', 'type unicode = `UnicodeFromBytes` as bytes', 'from __builtin__ import chr' ] p = pytd2proto.Postprocessor(config_headers=FLAGS.prepend, include_paths=FLAGS.include_paths, preamble='\n'.join(init)) try: pb = p.Translate(stream) except Exception as e: # pylint:disable=broad-except stream.seek(0) print('\nLine', '.123456789' * 4) for i, s in enumerate(stream): print('%4d:%s\\n' % (i + 1, s.rstrip('\n'))) stream.close() raise _ParseError(e) if FLAGS.dump_dir: _DumpProto(dump_path, '.ipb', pb) return pb.SerializeToString()
def ClifEqual(self, pytd, clif, types=None, include_typemaps=False, add_extra_init=True): pytd = textwrap.dedent(pytd) with open(TMP_FILE, 'w') as pytd_file: pytd_file.write(pytd) p = pytd2proto.Postprocessor(config_headers=types, include_paths=[os.environ['CLIF_DIR']]) with open(TMP_FILE, 'r') as pytd_file: try: pb = p.Translate(pytd_file) except: print('\nLine', '.123456789' * 4) for i, s in enumerate(pytd.splitlines()): print('%4d:%s\\n' % (i + 1, s)) raise if not include_typemaps: del pb.typemaps[:] for m in pb.macros: m.definition = b'' out = text_format.MessageToString(pb) expected = textwrap.dedent(clif).replace('cpp_type: "string"', 'cpp_type: "std::string"') if add_extra_init: expected += 'extra_init: "PyEval_InitThreads();"\n' self.assertMultiLineEqual(out, expected)
def _ParseFile(pytd, type_headers): with open(TMP_FILE, 'w') as pytd_file: pytd_file.write(pytd) p = pytd2proto.Postprocessor(config_headers=type_headers, include_paths=[os.environ['CLIF_DIR']]) with open(TMP_FILE, 'r') as pytd_file: pb = p.Translate(pytd_file) return pb
def assertNsEqual(self, proto, ns): pytd = textwrap.dedent(proto) with open(TMP_FILE, 'w') as pytd_file: pytd_file.write(pytd) p = pytd2proto.Postprocessor( config_headers=['clif/python/types.h'], include_paths=[os.environ['CLIF_DIR']]) with open(TMP_FILE, 'r') as pytd_file: ast = p.Translate(pytd_file) m = pyext.Module('my.path.py.ext') for d in ast.decls: list(m.WrapDecl(d)) if m.types: different_ns = set(types.Namespace(t) for t in m.types) self.assertEqual(set(ns), different_ns) else: self.assertFalse(ns)