2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

Added support for insertion of user code in the generated code

[SVN r19664]
This commit is contained in:
Bruno da Silva de Oliveira
2003-08-17 19:35:00 +00:00
parent c4a3f2c04f
commit 929badf4c6
7 changed files with 74 additions and 3 deletions

View File

@@ -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!!

View File

@@ -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

View File

@@ -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
#==============================================================================

View File

@@ -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

4
pyste/tests/code_test.h Normal file
View File

@@ -0,0 +1,4 @@
struct A {
int x;
};

View File

@@ -0,0 +1,9 @@
Class('A', 'code_test.h')
header_code('#include <string>\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')

View File

@@ -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()