Skip to content

Commit 4f2dbed

Browse files
author
Bruno da Silva de Oliveira
committed
bugs in Enum and export_values option
[SVN r20121]
1 parent bec2de0 commit 4f2dbed

File tree

7 files changed

+75
-13
lines changed

7 files changed

+75
-13
lines changed

pyste/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
19 September 2003
2+
Better support for unnamed enums, plus they are by default exported to the parent's namespace. Normal enums can have the same behaviour using the function export_values on the Enum object.
3+
14
10 September 2003
25
A new variable is accessible in the Pyste files: INTERFACE_FILE contains the
36
full path of the pyste file.

pyste/src/Pyste/CppParser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _IncludeParams(self, filename):
5555
if not filedir:
5656
filedir = '.'
5757
includes.insert(0, filedir)
58-
includes = ['-I "%s"' % x for x in includes]
58+
includes = ['-I "%s"' % self.Unixfy(x) for x in includes]
5959
return ' '.join(includes)
6060

6161

@@ -88,6 +88,10 @@ def AppendTail(self, filename, tail):
8888
return temp
8989

9090

91+
def Unixfy(self, path):
92+
return path.replace('\\', '/')
93+
94+
9195
def ParseWithGCCXML(self, header, tail):
9296
'''Parses the given header using gccxml and GCCXMLParser.
9397
'''
@@ -103,6 +107,8 @@ def ParseWithGCCXML(self, header, tail):
103107
defines = self._DefineParams()
104108
# call gccxml
105109
cmd = 'gccxml %s %s %s -fxml=%s'
110+
filename = self.Unixfy(filename)
111+
xmlfile = self.Unixfy(xmlfile)
106112
status = os.system(cmd % (includes, defines, filename, xmlfile))
107113
if status != 0 or not os.path.isfile(xmlfile):
108114
raise CppParserError, 'Error executing gccxml'

pyste/src/Pyste/EnumExporter.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,71 @@ def SetDeclarations(self, declarations):
1919
else:
2020
self.enum = None
2121

22-
22+
TYPE_COUNT = 0
23+
2324
def Export(self, codeunit, exported_names):
2425
if not self.info.exclude:
2526
indent = self.INDENT
2627
in_indent = self.INDENT*2
2728
rename = self.info.rename or self.enum.name
2829
full_name = self.enum.FullName()
29-
if rename == "$_0" or rename == '._0':
30-
full_name = "int"
30+
unnamed_enum = False
31+
if rename.startswith('$_') or rename.startswith('._'):
32+
unnamed_enum = True
33+
self.ExportUniqueInt(codeunit)
34+
full_name = namespaces.pyste + 'UniqueInt<%d>' % EnumExporter.TYPE_COUNT
35+
EnumExporter.TYPE_COUNT += 1
3136
rename = "unnamed"
3237
code = indent + namespaces.python
3338
code += 'enum_< %s >("%s")\n' % (full_name, rename)
3439
for name in self.enum.values:
3540
rename = self.info[name].rename or name
3641
value_fullname = self.enum.ValueFullName(name)
3742
code += in_indent + '.value("%s", %s)\n' % (rename, value_fullname)
43+
if self.info.export_values or unnamed_enum:
44+
code += in_indent + '.export_values()\n'
3845
code += indent + ';\n\n'
3946
codeunit.Write('module', code)
40-
exported_names[self.Name()] = 1
47+
exported_names[self.enum.FullName()] = 1
48+
49+
50+
UNIQUE_INT_EXPORTED = False
51+
52+
def ExportUniqueInt(self, codeunit):
53+
if not EnumExporter.UNIQUE_INT_EXPORTED:
54+
write = lambda s: codeunit.Write('declaration', s)
55+
write('// Unique type for unnamed enums\n')
56+
write('template<int num>\n')
57+
write('struct UniqueInt {\n')
58+
write(' int v;\n')
59+
write(' enum { value=num };\n')
60+
write(' UniqueInt(int v_):\n')
61+
write(' v(v_)\n')
62+
write(' {}\n')
63+
write(' operator int() const\n')
64+
write(' { return v; }\n')
65+
write('};\n')
66+
EnumExporter.UNIQUE_INT_EXPORTED = True
67+
68+
69+
#def Export(self, codeunit, exported_names):
70+
# if not self.info.exclude:
71+
# indent = self.INDENT
72+
# in_indent = self.INDENT*2
73+
# rename = self.info.rename or self.enum.name
74+
# full_name = self.enum.FullName()
75+
# if rename == "$_0" or rename == '._0':
76+
# full_name = "int"
77+
# rename = "unnamed"
78+
# code = indent + namespaces.python
79+
# code += 'enum_< %s >("%s")\n' % (full_name, rename)
80+
# for name in self.enum.values:
81+
# rename = self.info[name].rename or name
82+
# value_fullname = self.enum.ValueFullName(name)
83+
# code += in_indent + '.value("%s", %s)\n' % (rename, value_fullname)
84+
# code += indent + ';\n\n'
85+
# codeunit.Write('module', code)
86+
# exported_names[self.Name()] = 1
4187

4288

4389
def Name(self):

pyste/src/Pyste/infos.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def __init__(self, name, include):
125125
self._Attribute('name', name)
126126
self._Attribute('include', include)
127127
self._Attribute('exclude', False)
128+
self._Attribute('export_values', False)
128129
exporter = EnumExporter(InfoWrapper(self))
129130
if exporter not in exporters.exporters:
130131
exporters.exporters.append(exporter)
@@ -236,3 +237,7 @@ def add_method(info, name, rename=None):
236237

237238
def final(info):
238239
info._Attribute('no_override', True)
240+
241+
242+
def export_values(info):
243+
info._Attribute('export_values', True)

pyste/src/Pyste/pyste.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import time
4444
import declarations
4545

46-
__version__ = '0.9.24'
46+
__version__ = '0.9.25'
4747

4848
def RecursiveIncludes(include):
4949
'Return a list containg the include dir and all its subdirectories'
@@ -182,6 +182,7 @@ def CreateContext():
182182
context['holder'] = infos.holder
183183
context['add_method'] = infos.add_method
184184
context['final'] = infos.final
185+
context['export_values'] = infos.export_values
185186
# policies
186187
context['return_internal_reference'] = return_internal_reference
187188
context['with_custodian_and_ward'] = with_custodian_and_ward

pyste/tests/enums.pyste

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
h = AllFromHeader('enums.h')
22
rename(h.color.red, 'Red')
33
rename(h.color.blue, 'Blue')
4+
export_values(h.color)
45
rename(h.X.choices.bad, 'Bad')
56
rename(h.X.choices.good, 'Good')
67
rename(h.X.choices, 'Choices')

pyste/tests/enumsUT.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
class EnumsTest(unittest.TestCase):
55

66
def testIt(self):
7-
self.assertEqual(int(color.Red), 0)
8-
self.assertEqual(int(color.Blue), 1)
7+
self.assertEqual(int(Red), 0)
8+
self.assertEqual(int(Blue), 1)
99

1010
self.assertEqual(int(X.Choices.Good), 1)
1111
self.assertEqual(int(X.Choices.Bad), 2)
12-
x = X()
13-
self.assertEqual(x.set(x.Choices.Good), 1)
14-
self.assertEqual(x.set(x.Choices.Bad), 2)
15-
self.assertEqual(unnamed.x, 0)
16-
self.assertEqual(unnamed.y, 1)
12+
a = X()
13+
self.assertEqual(a.set(a.Choices.Good), 1)
14+
self.assertEqual(a.set(a.Choices.Bad), 2)
15+
self.assertEqual(x, 0)
16+
self.assertEqual(y, 1)
1717

1818

1919
if __name__ == '__main__':

0 commit comments

Comments
 (0)