From 929badf4c63782c5b8bbbae02bd134e434db7b05 Mon Sep 17 00:00:00 2001 From: Bruno da Silva de Oliveira Date: Sun, 17 Aug 2003 19:35:00 +0000 Subject: [PATCH] Added support for insertion of user code in the generated code [SVN r19664] --- pyste/NEWS | 3 +++ pyste/src/Pyste/CodeExporter.py | 21 +++++++++++++++++++++ pyste/src/Pyste/infos.py | 16 ++++++++++++++++ pyste/src/Pyste/pyste.py | 10 +++++++--- pyste/tests/code_test.h | 4 ++++ pyste/tests/code_test.pyste | 9 +++++++++ pyste/tests/code_testUT.py | 14 ++++++++++++++ 7 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 pyste/src/Pyste/CodeExporter.py create mode 100644 pyste/tests/code_test.h create mode 100644 pyste/tests/code_test.pyste create mode 100644 pyste/tests/code_testUT.py diff --git a/pyste/NEWS b/pyste/NEWS index 936e2e03..31ff0034 100644 --- a/pyste/NEWS +++ b/pyste/NEWS @@ -1,3 +1,6 @@ +17 August 2003 +Added support for insertion of user code in the generated code. + 16 August 2003 Applied a patch by Gottfried Ganssauge that adds exception specifiers to wrapper functions and pointer declarations. Thanks a lot Gottfried!! diff --git a/pyste/src/Pyste/CodeExporter.py b/pyste/src/Pyste/CodeExporter.py new file mode 100644 index 00000000..e89d72aa --- /dev/null +++ b/pyste/src/Pyste/CodeExporter.py @@ -0,0 +1,21 @@ +from Exporter import Exporter + +#============================================================================== +# CodeExporter +#============================================================================== +class CodeExporter(Exporter): + + def __init__(self, info): + Exporter.__init__(self, info) + + + def Name(self): + return self.info.code + + + def Export(self, codeunit, exported_names): + codeunit.Write(self.info.section, self.info.code) + + + def WriteInclude(self, codeunit): + pass diff --git a/pyste/src/Pyste/infos.py b/pyste/src/Pyste/infos.py index c457f208..ee977a94 100644 --- a/pyste/src/Pyste/infos.py +++ b/pyste/src/Pyste/infos.py @@ -7,6 +7,7 @@ from IncludeExporter import IncludeExporter from EnumExporter import EnumExporter from HeaderExporter import HeaderExporter from VarExporter import VarExporter +from CodeExporter import CodeExporter from exporterutils import FunctionWrapper from utils import makeid @@ -174,6 +175,21 @@ class VarInfo(DeclarationInfo): exporter.interface_file = exporters.current_interface +#============================================================================== +# CodeInfo +#============================================================================== +class CodeInfo(DeclarationInfo): + + def __init__(self, code, section): + DeclarationInfo.__init__(self) + self._Attribute('code', code) + self._Attribute('section', section) + exporter = CodeExporter(InfoWrapper(self)) + if exporter not in exporters.exporters: + exporters.exporters.append(exporter) + exporter.interface_file = exporters.current_interface + + #============================================================================== # InfoWrapper #============================================================================== diff --git a/pyste/src/Pyste/pyste.py b/pyste/src/Pyste/pyste.py index b3d49958..37a26095 100644 --- a/pyste/src/Pyste/pyste.py +++ b/pyste/src/Pyste/pyste.py @@ -43,7 +43,7 @@ from CppParser import CppParser, CppParserError import time from declarations import Typedef -__VERSION__ = '0.9.14' +__version__ = '0.9.15' def RecursiveIncludes(include): 'Return a list containg the include dir and all its subdirectories' @@ -75,7 +75,7 @@ def ProcessIncludes(includes): def ParseArguments(): def Usage(): - print __doc__ % __VERSION__ + print __doc__ % __version__ sys.exit(1) try: @@ -125,7 +125,7 @@ def ParseArguments(): elif opt in ['-h', '--help']: Usage() elif opt in ['-v', '--version']: - print 'Pyste version %s' % __VERSION__ + print 'Pyste version %s' % __version__ sys.exit(2) elif opt == '--generate-main': generate_main = True @@ -193,6 +193,10 @@ def CreateContext(): context['manage_new_object'] = manage_new_object # utils context['Wrapper'] = exporterutils.FunctionWrapper + context['header_code'] = lambda code: infos.CodeInfo(code, 'include') + context['declaration_code'] = lambda code: infos.CodeInfo(code, 'declaration') + context['global_declaration_code'] = lambda code: infos.CodeInfo(code, 'declaration-outside') + context['module_code'] = lambda code: infos.CodeInfo(code, 'module') return context diff --git a/pyste/tests/code_test.h b/pyste/tests/code_test.h new file mode 100644 index 00000000..4e073c78 --- /dev/null +++ b/pyste/tests/code_test.h @@ -0,0 +1,4 @@ + +struct A { + int x; +}; diff --git a/pyste/tests/code_test.pyste b/pyste/tests/code_test.pyste new file mode 100644 index 00000000..bdd528e9 --- /dev/null +++ b/pyste/tests/code_test.pyste @@ -0,0 +1,9 @@ +Class('A', 'code_test.h') +header_code('#include \n') +global_declaration_code(''' +int get(A& a) { return a.x; } + +std::string foo() { return "Hello!"; } +''') +module_code(' def("get", &get);\n') +module_code(' def("foo", &foo);\n') diff --git a/pyste/tests/code_testUT.py b/pyste/tests/code_testUT.py new file mode 100644 index 00000000..4655e724 --- /dev/null +++ b/pyste/tests/code_testUT.py @@ -0,0 +1,14 @@ +import unittest +from _code_test import * + +class CodeTest(unittest.TestCase): + + def testIt(self): + a = A() + a.x = 12 + self.assertEqual(get(a), 12) + self.assertEqual(foo(), "Hello!") + + +if __name__ == '__main__': + unittest.main()