From 858e1aba676bda554a6d72de9863186e7b1ca0d1 Mon Sep 17 00:00:00 2001
From: Bruno da Silva de Oliveira
Date: Fri, 23 May 2003 20:37:35 +0000
Subject: [PATCH] - Support for global variables - Bug fixes in ClassExporter
[SVN r18518]
---
pyste/NEWS | 4 +++
pyste/doc/global_variables.html | 49 +++++++++++++++++++++++++++++++++
pyste/doc/introduction.html | 2 +-
pyste/doc/pyste.txt | 12 ++++++++
pyste/doc/smart_pointers.html | 5 ++--
pyste/example/vars.h | 19 +++++++++++++
pyste/example/vars.pyste | 1 +
pyste/index.html | 5 ++++
pyste/src/ClassExporter.py | 2 +-
pyste/src/HeaderExporter.py | 2 ++
pyste/src/VarExporter.py | 31 +++++++++++++++++++++
pyste/src/declarations.py | 4 +--
pyste/src/exporterutils.py | 10 +++----
pyste/src/infos.py | 16 ++++++++++-
pyste/src/pyste.py | 14 +++++++---
pyste/tests/example_varsUT.py | 17 ++++++++++++
pyste/tests/nt_build_all.bat | 1 +
17 files changed, 178 insertions(+), 16 deletions(-)
create mode 100644 pyste/doc/global_variables.html
create mode 100644 pyste/example/vars.h
create mode 100644 pyste/example/vars.pyste
create mode 100644 pyste/src/VarExporter.py
create mode 100644 pyste/tests/example_varsUT.py
diff --git a/pyste/NEWS b/pyste/NEWS
index 28bcbbfe..3505706d 100644
--- a/pyste/NEWS
+++ b/pyste/NEWS
@@ -1,3 +1,7 @@
+23 May 2003
+Support for global variables added.
+Various bug fixes.
+
08 May 2003
Fixed bug where in a certain cases the GCCXMLParser would end up with multiple
declarations of the same class
diff --git a/pyste/doc/global_variables.html b/pyste/doc/global_variables.html
new file mode 100644
index 00000000..84b3b9d3
--- /dev/null
+++ b/pyste/doc/global_variables.html
@@ -0,0 +1,49 @@
+
+
+
+Global Variables
+
+
+
+
+
+
+
+
+
+ Global Variables
+
+
+
+
+
+
+
+
+
+
+
+
+To export global variables, use the Var construct:
+
+ Var("myglobal", "foo.h")
+
+
+Beware of non-const global variables: changes in Python won't reflect in C++!
+If you really must change them in Python, you will have to write some accessor
+functions, and export those.
+Permission to copy, use, modify, sell and distribute this document
+ is granted provided this copyright notice appears in all copies. This document
+ is provided "as is" without express or implied warranty, and with
+ no claim as to its suitability for any purpose.
+
+
diff --git a/pyste/doc/introduction.html b/pyste/doc/introduction.html
index d86179e2..ccbbda4d 100644
--- a/pyste/doc/introduction.html
+++ b/pyste/doc/introduction.html
@@ -57,7 +57,7 @@ this will create a file "hello.cpp" in the directory where th
run.
Pyste supports the following features:
-
Functions
Classes
Class Templates
Virtual Methods
Overloading
Attributes
Enums (both "free" enums and class enums)
Nested Classes
Support for boost::shared_ptr and std::auto_ptr
+
Functions
Classes
Class Templates
Virtual Methods
Overloading
Attributes
Enums (both "free" enums and class enums)
Nested Classes
Support for boost::shared_ptr and std::auto_ptr
Global Variables
diff --git a/pyste/doc/pyste.txt b/pyste/doc/pyste.txt
index 945b2364..b37c6833 100644
--- a/pyste/doc/pyste.txt
+++ b/pyste/doc/pyste.txt
@@ -48,6 +48,7 @@ Pyste supports the following features:
* Enums (both "free" enums and class enums)
* Nested Classes
* Support for [^boost::shared_ptr] and [^std::auto_ptr]
+* Global Variables
[page Running Pyste]
@@ -465,3 +466,14 @@ For [^std::auto_ptr]'s, use the function [^use_auto_ptr].
This system is temporary, and in the future the converters will automatically be
exported if needed, without the need to tell Pyste about them explicitly.
+
+[page:1 Global Variables]
+
+To export global variables, use the [^Var] construct:
+
+ Var("myglobal", "foo.h")
+
+Beware of non-const global variables: changes in Python won't reflect in C++!
+If you really must change them in Python, you will have to write some accessor
+functions, and export those.
+
diff --git a/pyste/doc/smart_pointers.html b/pyste/doc/smart_pointers.html
index e63b49ff..d3ff5923 100644
--- a/pyste/doc/smart_pointers.html
+++ b/pyste/doc/smart_pointers.html
@@ -4,6 +4,7 @@
Smart Pointers
+
@@ -20,7 +21,7 @@
-
+
@@ -61,7 +62,7 @@ exported if needed, without the need to tell Pyste about them explicitly.
-
+
diff --git a/pyste/example/vars.h b/pyste/example/vars.h
new file mode 100644
index 00000000..23de92b5
--- /dev/null
+++ b/pyste/example/vars.h
@@ -0,0 +1,19 @@
+
+struct Color
+{
+ Color(int r_ = 0, int g_ = 0, int b_ = 0):
+ r(r_), g(g_), b(b_)
+ {}
+ Color( const Color &c):
+ r(c.r), g(c.g), b(c.b)
+ {}
+ int r;
+ int g;
+ int b;
+};
+
+const Color black = Color(0, 0, 0);
+const Color red = Color(255, 0, 0);
+const Color green = Color(0, 255, 0);
+const Color blue = Color(0, 0, 255);
+Color in_use = black;
diff --git a/pyste/example/vars.pyste b/pyste/example/vars.pyste
new file mode 100644
index 00000000..3fd9d689
--- /dev/null
+++ b/pyste/example/vars.pyste
@@ -0,0 +1 @@
+AllFromHeader('vars.h')
diff --git a/pyste/index.html b/pyste/index.html
index 0abccae0..b9c21920 100644
--- a/pyste/index.html
+++ b/pyste/index.html
@@ -65,6 +65,11 @@
Smart Pointers
+
diff --git a/pyste/src/ClassExporter.py b/pyste/src/ClassExporter.py
index c5536f6a..e6d5bb6b 100644
--- a/pyste/src/ClassExporter.py
+++ b/pyste/src/ClassExporter.py
@@ -751,7 +751,7 @@ class _VirtualWrapperGenerator(object):
all_methods = [x for x in self.class_.members if IsVirtual(x)]
for base in self.bases:
- base_methods = [x.Copy() for x in self.bases.members if IsVirtual(x)]
+ base_methods = [x.Copy() for x in base if IsVirtual(x)]
for base_method in base_methods:
base_method.class_ = self.class_.FullName()
all_methods.append(base_method)
diff --git a/pyste/src/HeaderExporter.py b/pyste/src/HeaderExporter.py
index 1d5fda1b..141f4da4 100644
--- a/pyste/src/HeaderExporter.py
+++ b/pyste/src/HeaderExporter.py
@@ -2,6 +2,7 @@ from Exporter import Exporter
from ClassExporter import ClassExporter
from FunctionExporter import FunctionExporter
from EnumExporter import EnumExporter
+from VarExporter import VarExporter
from infos import *
from declarations import *
import os.path
@@ -47,6 +48,7 @@ class HeaderExporter(Exporter):
Class : ClassExporter,
Enumeration : EnumExporter,
Function : FunctionExporter,
+ Variable : VarExporter,
}
exporter_class = dispatch_table.get(type(decl))
diff --git a/pyste/src/VarExporter.py b/pyste/src/VarExporter.py
new file mode 100644
index 00000000..6e4bf370
--- /dev/null
+++ b/pyste/src/VarExporter.py
@@ -0,0 +1,31 @@
+from Exporter import Exporter
+from settings import *
+
+
+#==============================================================================
+# VarExporter
+#==============================================================================
+class VarExporter(Exporter):
+ '''Exports a global variable.
+ '''
+
+ def __init__(self, info):
+ Exporter.__init__(self, info)
+
+
+ def Export(self, codeunit, exported_names):
+ if self.info.exclude: return
+ decl = self.GetDeclaration(self.info.name)
+ if not decl.type.const:
+ msg = '---> Warning: The global variable "%s" is non-const:\n' \
+ ' changes in Python will not reflect in C++.'
+ print msg % self.info.name
+ print
+ rename = self.info.rename or self.info.name
+ code = self.INDENT + namespaces.python
+ code += 'scope().attr("%s") = %s;\n' % (rename, self.info.name)
+ codeunit.Write('module', code)
+
+
+ def Order(self):
+ return self.info.name
diff --git a/pyste/src/declarations.py b/pyste/src/declarations.py
index a037c04d..b8f1ef9d 100644
--- a/pyste/src/declarations.py
+++ b/pyste/src/declarations.py
@@ -243,8 +243,8 @@ class Method(Function):
self.name,
self.class_,
self.result,
- self.params[:],
- self.visib,
+ self.parameters[:],
+ self.visibility,
self.virtual,
self.abstract,
self.static,
diff --git a/pyste/src/exporterutils.py b/pyste/src/exporterutils.py
index 6dafe39a..27bdd095 100644
--- a/pyste/src/exporterutils.py
+++ b/pyste/src/exporterutils.py
@@ -10,11 +10,11 @@ from declarations import *
# FunctionWrapper
#==============================================================================
class FunctionWrapper(object):
- '''Holds information about a wrapper for a function or a method. It is in 2
- parts: the name of the Wrapper, and its code. The code is placed in the
- declaration section of the module, while the name is used to def' the
- function or method (with the pyste namespace prepend to it). If code is None,
- the name is left unchanged.
+ '''Holds information about a wrapper for a function or a method. It is
+ divided in 2 parts: the name of the Wrapper, and its code. The code is
+ placed in the declaration section of the module, while the name is used to
+ def' the function or method (with the pyste namespace prepend to it). If
+ code is None, the name is left unchanged.
'''
def __init__(self, name, code=None):
diff --git a/pyste/src/infos.py b/pyste/src/infos.py
index c0c8013d..d4534532 100644
--- a/pyste/src/infos.py
+++ b/pyste/src/infos.py
@@ -6,6 +6,7 @@ from FunctionExporter import FunctionExporter
from IncludeExporter import IncludeExporter
from EnumExporter import EnumExporter
from HeaderExporter import HeaderExporter
+from VarExporter import VarExporter
from exporterutils import FunctionWrapper
from utils import makeid
@@ -86,7 +87,7 @@ class IncludeInfo(DeclarationInfo):
exporter = IncludeExporter(InfoWrapper(self))
exporters.exporters.append(exporter)
-
+
#==============================================================================
# templates
#==============================================================================
@@ -148,6 +149,19 @@ class HeaderInfo(DeclarationInfo):
exporters.exporters.append(exporter)
+#==============================================================================
+# VarInfo
+#==============================================================================
+class VarInfo(DeclarationInfo):
+
+ def __init__(self, name, include):
+ DeclarationInfo.__init__(self)
+ self._Attribute('name', name)
+ self._Attribute('include', include)
+ exporter = VarExporter(InfoWrapper(self))
+ exporters.exporters.append(exporter)
+
+
#==============================================================================
# InfoWrapper
#==============================================================================
diff --git a/pyste/src/pyste.py b/pyste/src/pyste.py
index 62cf3439..6aa4f7b6 100644
--- a/pyste/src/pyste.py
+++ b/pyste/src/pyste.py
@@ -2,9 +2,12 @@
Pyste version %s
Usage:
- pyste [options] --module= interface-files
+ pyste [options] interface-files
where options are:
+ --module= the name of the module that will be generated.
+ Defaults to the first interface filename, without
+ the extension.
-I add an include path
-D define symbol
--multiple create various cpps, instead of only one
@@ -34,7 +37,7 @@ from policies import *
from CppParser import CppParser, CppParserError
import time
-__VERSION__ = '0.7.9'
+__VERSION__ = '0.8.0'
def RecursiveIncludes(include):
'Return a list containg the include dir and all its subdirectories'
@@ -104,8 +107,10 @@ def ParseArguments():
print 'Unknown option:', opt
Usage()
- if not files or not module:
- Usage()
+ if not files:
+ Usage()
+ if not module:
+ module = os.path.splitext(files[0])[0]
if not out:
out = module
if not multiple:
@@ -123,6 +128,7 @@ def CreateContext():
context['Template'] = infos.ClassTemplateInfo
context['Enum'] = infos.EnumInfo
context['AllFromHeader'] = infos.HeaderInfo
+ context['Var'] = infos.VarInfo
# functions
context['rename'] = infos.rename
context['set_policy'] = infos.set_policy
diff --git a/pyste/tests/example_varsUT.py b/pyste/tests/example_varsUT.py
new file mode 100644
index 00000000..226b0118
--- /dev/null
+++ b/pyste/tests/example_varsUT.py
@@ -0,0 +1,17 @@
+import unittest
+import vars
+
+
+class VarsTest(unittest.TestCase):
+
+ def testIt(self):
+ def testColor(c, r, g, b):
+ self.assertEqual(c.r, r)
+ self.assertEqual(c.g, g)
+ self.assertEqual(c.b, b)
+ testColor(vars.black, 0, 0, 0)
+ testColor(vars.red, 255, 0, 0)
+ testColor(vars.green, 0, 255, 0)
+ testColor(vars.blue, 0, 0, 255)
+
+
diff --git a/pyste/tests/nt_build_all.bat b/pyste/tests/nt_build_all.bat
index db5bc1fd..60f9abef 100644
--- a/pyste/tests/nt_build_all.bat
+++ b/pyste/tests/nt_build_all.bat
@@ -13,3 +13,4 @@ call nt_build_pyste.bat virtual2 %1
call nt_build_pyste.bat wrappertest %1
call nt_build_pyste.bat opaque %1
call nt_build_pyste.bat inherit %1
+call nt_build_pyste.bat vars %1