Skip to content

Commit dd14ccb

Browse files
author
Bruno da Silva de Oliveira
committed
- --multiple now generates one cpp per pyste file.
[SVN r18944]
1 parent ba0fcd2 commit dd14ccb

8 files changed

Lines changed: 75 additions & 39 deletions

File tree

pyste/TODO

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
instance)
99

1010
- Virtual operators
11+
12+
13+
- staticmethod bug

pyste/src/Pyste/Exporter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Exporter:
1111
def __init__(self, info, parser_tail=None):
1212
self.info = info
1313
self.parser_tail = parser_tail
14+
self.interface_file = None
1415

1516

1617
def Name(self):

pyste/src/Pyste/HeaderExporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def HandleExporter(self, decl, exporter_type, codeunit, exported_names):
6464
exporter = exporter_type(info)
6565
exporter.SetDeclarations(self.declarations)
6666
exporter.SetParsedHeader(self.parser_header)
67-
codeunit.SetCurrent(exporter.Unit())
67+
codeunit.SetCurrent(self.interface_file, exporter.Unit())
6868
exporter.GenerateCode(codeunit, exported_names)
6969

7070

pyste/src/Pyste/MultipleCodeUnit.py

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,40 @@ class MultipleCodeUnit(object):
1616
def __init__(self, modulename, outdir):
1717
self.modulename = modulename
1818
self.outdir = outdir
19-
self.codeunits = {} # maps from a header to a SingleCodeUnit
19+
self.codeunits = {} # maps from a (filename, function) to a SingleCodeUnit
2020
self.functions = []
2121
self._current = None
22+
self.all = SingleCodeUnit(None, None)
2223

2324

24-
def _FunctionName(self, code_unit_name):
25-
return '_Export_%s' % utils.makeid(code_unit_name)
25+
def _FunctionName(self, export_name):
26+
return 'Export_%s' % utils.makeid(export_name)
2627

2728

28-
def _FileName(self, code_unit_name):
29-
filename = os.path.basename(code_unit_name)
29+
def _FileName(self, interface_file):
30+
filename = os.path.basename(interface_file)
3031
filename = '_%s.cpp' % os.path.splitext(filename)[0]
3132
return os.path.join(self.outdir, filename)
3233

3334

34-
def SetCurrent(self, code_unit_name):
35+
def SetCurrent(self, interface_file, export_name):
3536
'Changes the current code unit'
36-
try:
37-
if code_unit_name is not None:
38-
codeunit = self.codeunits[code_unit_name]
39-
else:
40-
codeunit = None
41-
except KeyError:
42-
filename = self._FileName(code_unit_name)
43-
function_name = self._FunctionName(code_unit_name)
44-
codeunit = SingleCodeUnit(None, filename)
45-
codeunit.module_definition = 'void %s()' % function_name
46-
self.codeunits[code_unit_name] = codeunit
47-
if code_unit_name != '__all__':
48-
self.functions.append(function_name)
49-
self._current = codeunit
37+
if export_name is None:
38+
self._current = None
39+
elif export_name is '__all__':
40+
self._current = self.all
41+
else:
42+
filename = self._FileName(interface_file)
43+
function = self._FunctionName(export_name)
44+
try:
45+
codeunit = self.codeunits[(filename, function)]
46+
except KeyError:
47+
codeunit = SingleCodeUnit(None, filename)
48+
codeunit.module_definition = 'void %s()' % function
49+
self.codeunits[(filename, function)] = codeunit
50+
if function not in self.functions:
51+
self.functions.append(function)
52+
self._current = codeunit
5053

5154

5255
def Current(self):
@@ -74,16 +77,31 @@ def _CreateOutputDir(self):
7477
def Save(self):
7578
# create the directory where all the files will go
7679
self._CreateOutputDir();
80+
# order all code units by filename, and merge them all
81+
codeunits = {} # filename => list of codeunits
82+
# the main_unit holds all the include, declaration and declaration-outside sections
83+
# to keep them all in the top of the source file
84+
main_unit = None
85+
for (filename, _), codeunit in self.codeunits.items():
86+
if filename not in codeunits:
87+
codeunits[filename] = [codeunit]
88+
main_unit = codeunit
89+
main_unit.Merge(self.all)
90+
else:
91+
for section in ('include', 'declaration', 'declaration-outside'):
92+
main_unit.code[section] = main_unit.code[section] + codeunit.code[section]
93+
codeunit.code[section] = ''
94+
codeunits[filename].append(codeunit)
7795
# write all the codeunits, merging first the contents of
7896
# the special code unit named __all__
79-
__all__ = self.codeunits.get('__all__')
80-
for name, codeunit in self.codeunits.items():
81-
if name != '__all__':
82-
if __all__:
83-
codeunit.Merge(__all__)
84-
codeunit.Save()
97+
for codeunits in codeunits.values():
98+
append = False
99+
for codeunit in codeunits:
100+
codeunit.Save(append)
101+
if not append:
102+
append = True
85103
# generate the main cpp
86-
filename = os.path.join(self.outdir, self.modulename + '.cpp')
104+
filename = os.path.join(self.outdir, '_main.cpp')
87105
fout = SmartFile(filename, 'w')
88106
fout.write(utils.left_equals('Include'))
89107
fout.write('#include <boost/python.hpp>\n\n')

pyste/src/Pyste/SingleCodeUnit.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, modulename, filename):
2323
# define the avaiable sections
2424
self.code = {}
2525
# include section
26-
self.code['include'] = ''
26+
self.code['include'] = '#include <boost/python.hpp>\n'
2727
# declaration section (inside namespace)
2828
self.code['declaration'] = ''
2929
# declaration (outside namespace)
@@ -58,26 +58,31 @@ def Current(self):
5858
pass
5959

6060

61-
def Save(self):
61+
def Save(self, append=False):
6262
'Writes this code unit to the filename'
6363
space = '\n\n'
64-
fout = SmartFile(self.filename, 'w')
64+
if not append:
65+
flag = 'w'
66+
else:
67+
flag = 'a'
68+
fout = SmartFile(self.filename, flag)
6569
# includes
66-
includes = remove_duplicated_lines(self.code['include'])
67-
fout.write('\n' + left_equals('Includes'))
68-
fout.write('#include <boost/python.hpp>\n')
69-
fout.write(includes)
70-
fout.write(space)
70+
if self.code['include']:
71+
includes = remove_duplicated_lines(self.code['include'])
72+
fout.write('\n' + left_equals('Includes'))
73+
fout.write(includes)
74+
fout.write(space)
7175
# using
72-
if settings.USING_BOOST_NS:
76+
if settings.USING_BOOST_NS and not append:
7377
fout.write(left_equals('Using'))
7478
fout.write('using namespace boost::python;\n\n')
7579
# declarations
7680
declaration = self.code['declaration']
7781
declaration_outside = self.code['declaration-outside']
7882
if declaration_outside or declaration:
7983
fout.write(left_equals('Declarations'))
80-
fout.write(declaration_outside + '\n\n')
84+
if declaration_outside:
85+
fout.write(declaration_outside + '\n\n')
8186
if declaration:
8287
pyste_namespace = namespaces.pyste[:-2]
8388
fout.write('namespace %s {\n\n\n' % pyste_namespace)

pyste/src/Pyste/exporters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11

22
# a list of Exporter instances
33
exporters = []
4+
5+
current_interface = None # the current interface file being processed

pyste/src/Pyste/infos.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(self, name, include, tail=None, otherOption=None):
5959
# create a FunctionExporter
6060
exporter = FunctionExporter(InfoWrapper(self), tail)
6161
exporters.exporters.append(exporter)
62+
exporter.interface_file = exporters.current_interface
6263

6364

6465
#==============================================================================
@@ -74,6 +75,7 @@ def __init__(self, name, include, tail=None, otherInfo=None):
7475
# create a ClassExporter
7576
exporter = ClassExporter(InfoWrapper(self), tail)
7677
exporters.exporters.append(exporter)
78+
exporter.interface_file = exporters.current_interface
7779

7880

7981
#==============================================================================
@@ -86,6 +88,7 @@ def __init__(self, include):
8688
self._Attribute('include', include)
8789
exporter = IncludeExporter(InfoWrapper(self))
8890
exporters.exporters.append(exporter)
91+
exporter.interface_file = exporters.current_interface
8992

9093

9194
#==============================================================================
@@ -135,6 +138,7 @@ def __init__(self, name, include):
135138
self._Attribute('exclude', False)
136139
exporter = EnumExporter(InfoWrapper(self))
137140
exporters.exporters.append(exporter)
141+
exporter.interface_file = exporters.current_interface
138142

139143

140144
#==============================================================================
@@ -147,6 +151,7 @@ def __init__(self, include):
147151
self._Attribute('include', include)
148152
exporter = HeaderExporter(InfoWrapper(self))
149153
exporters.exporters.append(exporter)
154+
exporter.interface_file = exporters.current_interface
150155

151156

152157
#==============================================================================
@@ -160,6 +165,7 @@ def __init__(self, name, include):
160165
self._Attribute('include', include)
161166
exporter = VarExporter(InfoWrapper(self))
162167
exporters.exporters.append(exporter)
168+
exporter.interface_file = exporters.current_interface
163169

164170

165171
#==============================================================================

pyste/src/Pyste/pyste.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def Begin():
158158
includes, defines, module, out, interfaces, multiple = ParseArguments()
159159
# execute the interface files
160160
for interface in interfaces:
161+
exporters.current_interface = interface
161162
context = CreateContext()
162163
execfile(interface, context)
163164
# create the parser
@@ -211,7 +212,7 @@ def Begin():
211212
exported_names = {}
212213
for export in exports:
213214
if multiple:
214-
codeunit.SetCurrent(export.Unit())
215+
codeunit.SetCurrent(export.interface_file, export.Unit())
215216
export.GenerateCode(codeunit, exported_names)
216217
exported_names[export.Name()] = 1
217218
# force collect of cyclic references

0 commit comments

Comments
 (0)