Skip to content

Commit 929badf

Browse files
author
Bruno da Silva de Oliveira
committed
Added support for insertion of user code in the generated code
[SVN r19664]
1 parent c4a3f2c commit 929badf

7 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎pyste/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
17 August 2003
2+
Added support for insertion of user code in the generated code.
3+
14
16 August 2003
25
Applied a patch by Gottfried Ganssauge that adds exception specifiers to
36
wrapper functions and pointer declarations. Thanks a lot Gottfried!!

‎pyste/src/Pyste/CodeExporter.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from Exporter import Exporter
2+
3+
#==============================================================================
4+
# CodeExporter
5+
#==============================================================================
6+
class CodeExporter(Exporter):
7+
8+
def __init__(self, info):
9+
Exporter.__init__(self, info)
10+
11+
12+
def Name(self):
13+
return self.info.code
14+
15+
16+
def Export(self, codeunit, exported_names):
17+
codeunit.Write(self.info.section, self.info.code)
18+
19+
20+
def WriteInclude(self, codeunit):
21+
pass

‎pyste/src/Pyste/infos.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from EnumExporter import EnumExporter
88
from HeaderExporter import HeaderExporter
99
from VarExporter import VarExporter
10+
from CodeExporter import CodeExporter
1011
from exporterutils import FunctionWrapper
1112
from utils import makeid
1213

@@ -174,6 +175,21 @@ def __init__(self, name, include):
174175
exporter.interface_file = exporters.current_interface
175176

176177

178+
#==============================================================================
179+
# CodeInfo
180+
#==============================================================================
181+
class CodeInfo(DeclarationInfo):
182+
183+
def __init__(self, code, section):
184+
DeclarationInfo.__init__(self)
185+
self._Attribute('code', code)
186+
self._Attribute('section', section)
187+
exporter = CodeExporter(InfoWrapper(self))
188+
if exporter not in exporters.exporters:
189+
exporters.exporters.append(exporter)
190+
exporter.interface_file = exporters.current_interface
191+
192+
177193
#==============================================================================
178194
# InfoWrapper
179195
#==============================================================================

‎pyste/src/Pyste/pyste.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import time
4444
from declarations import Typedef
4545

46-
__VERSION__ = '0.9.14'
46+
__version__ = '0.9.15'
4747

4848
def RecursiveIncludes(include):
4949
'Return a list containg the include dir and all its subdirectories'
@@ -75,7 +75,7 @@ def ProcessIncludes(includes):
7575
def ParseArguments():
7676

7777
def Usage():
78-
print __doc__ % __VERSION__
78+
print __doc__ % __version__
7979
sys.exit(1)
8080

8181
try:
@@ -125,7 +125,7 @@ def Usage():
125125
elif opt in ['-h', '--help']:
126126
Usage()
127127
elif opt in ['-v', '--version']:
128-
print 'Pyste version %s' % __VERSION__
128+
print 'Pyste version %s' % __version__
129129
sys.exit(2)
130130
elif opt == '--generate-main':
131131
generate_main = True
@@ -193,6 +193,10 @@ def CreateContext():
193193
context['manage_new_object'] = manage_new_object
194194
# utils
195195
context['Wrapper'] = exporterutils.FunctionWrapper
196+
context['header_code'] = lambda code: infos.CodeInfo(code, 'include')
197+
context['declaration_code'] = lambda code: infos.CodeInfo(code, 'declaration')
198+
context['global_declaration_code'] = lambda code: infos.CodeInfo(code, 'declaration-outside')
199+
context['module_code'] = lambda code: infos.CodeInfo(code, 'module')
196200
return context
197201

198202

‎pyste/tests/code_test.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
struct A {
3+
int x;
4+
};

‎pyste/tests/code_test.pyste‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Class('A', 'code_test.h')
2+
header_code('#include <string>\n')
3+
global_declaration_code('''
4+
int get(A& a) { return a.x; }
5+
6+
std::string foo() { return "Hello!"; }
7+
''')
8+
module_code(' def("get", &get);\n')
9+
module_code(' def("foo", &foo);\n')

‎pyste/tests/code_testUT.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
from _code_test import *
3+
4+
class CodeTest(unittest.TestCase):
5+
6+
def testIt(self):
7+
a = A()
8+
a.x = 12
9+
self.assertEqual(get(a), 12)
10+
self.assertEqual(foo(), "Hello!")
11+
12+
13+
if __name__ == '__main__':
14+
unittest.main()

0 commit comments

Comments
 (0)