def testTransform(self): postconversion = {'str': 'BytesToUnicode', 'ztype': 'AnotherConversion'} output = '\n'.join(postconv.GenPostConvTable(postconversion)) self.assertEqual(postconversion, {'str': '_1', 'ztype': '_2'}) self.assertMultiLineEqual(output, textwrap.dedent(""" #define _0 py::postconv::PASS #define _1 BytesToUnicode #define _2 AnotherConversion"""))
def GenerateBase(self, ast, more_headers): """Extension module generation.""" ast_manipulations.MoveExtendsBackIntoClassesInPlace(ast) self.init += ast.extra_init for s in gen.Headlines( ast.source, [ 'PYTHON', 'absl/memory/memory.h', 'absl/types/optional.h' ] + more_headers + # Container templates calling PyObj* go last. [ 'clif/python/stltypes.h', 'clif/python/slots.h' ], open_ns=self.wrap_namespace): yield s yield '' yield 'using namespace clif;' yield '' yield 'static const char* ThisModuleName = "%s";' % self.path for s in postconv.GenPostConvTable(self.typemap): yield s if astutils.HaveEnum(ast.decls): yield '' yield 'static PyObject *_Enum{}, *_IntEnum{}; // set below in Init()' self.catch_cpp_exceptions = ast.catch_exceptions for d in ast.decls: for s in self.WrapDecl(d): yield s assert not self.nested, 'decl stack not exhausted (in GenBase)' yield '' yield '// Initialize module' if self.methods: for s in gen.MethodDef(self.methods): yield s for s in self.GenTypesReady(): # extends self.init yield s for s in self.GenInitFunction(ast.source): # consumes self.init yield s yield '' yield '} // namespace %s' % self.wrap_namespace if self.types: # Assumed we always want a deterministic output. Since dict/set order # is not, do sorted() order traversal. # # Save sorted types for GenerateHeader. self.types = sorted(self.types, key=types.Order) for ns, ts in itertools.groupby(self.types, types.Namespace): for s in gen.TypeConverters(ns, ts, self.wrap_namespace, self.py3output): yield s if self.static_init: for s in gen.PyModInitFunction( init_name=self.static_init, ns=self.wrap_namespace, py3=self.py3output): yield s
def GenerateBase(self, ast, api_header, more_headers): """Extension module generation.""" self.init += ast.extra_init for s in (gen.Headlines( ast.source, ['PYTHON', 'clif/python/ptr_util.h', 'clif/python/optional.h'] + more_headers + # Container templates calling PyObj* go last. ['clif/python/stltypes.h', 'clif/python/slots.h'], open_ns=self.wrap_namespace)): yield s yield 'using namespace clif;' for s in (postconv.GenPostConvTable(self.typemap)): yield s if astutils.HaveEnum(ast.decls): yield '' yield 'static PyObject *_Enum{}, *_IntEnum{}; // set below in Init()' self.catch_cpp_exceptions = ast.catch_exceptions yield '' for d in ast.decls: for s in (self.WrapDecl(d)): yield s assert not self.nested, 'decl stack not exhausted' yield '' yield '' yield '// Initialize module' if self.methods: for s in (gen.MethodDef(self.methods)): yield s for s in (self.GenTypesReady() # extends self.init ): yield s for s in (self.GenInitFunction(api_header) # consumes self.init ): yield s yield '' yield '} // namespace %s' % self.wrap_namespace if self.types: # Assumed we always want a deterministic output. Since dict/set order # is not, do sorted() order traversal. # # Save sorted types for GenerateHeader. self.types = sorted(self.types, key=types.Order) for ns, ts in itertools.groupby(self.types, types.Namespace): for s in (gen.TypeConverters(ns, ts, self.wrap_namespace, self.py3output)): yield s
def testEmptyTransform(self): output = list(postconv.GenPostConvTable({})) self.assertFalse(output)