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

- Default policy for functions/methods that return const T& is now

return_value_policy<copy_const_reference>().


[SVN r18077]
This commit is contained in:
Bruno da Silva de Oliveira
2003-03-24 23:25:14 +00:00
parent 3d01e6af89
commit 44b2e1ef8b
10 changed files with 119 additions and 63 deletions

View File

@@ -7,6 +7,7 @@ from CodeUnit import CodeUnit
from EnumExporter import EnumExporter
from makeid import makeid
from copy import deepcopy
import exporterutils
#==============================================================================
# ClassExporter
@@ -246,24 +247,6 @@ class ClassExporter(Exporter):
code = '%s("%s", &%s)' % (def_, name, fullname)
self.Add('inside', code)
printed_policy_warnings = {}
def CheckPolicy(self, m):
'Warns the user if this method needs a policy'
def IsString(type):
return type.const and type.name == 'char' and isinstance(type, PointerType)
needs_policy = isinstance(m.result, (ReferenceType, PointerType))
if IsString(m.result):
needs_policy = False
has_policy = self.info[m.name].policy is not None
if needs_policy and not has_policy:
warning = '---> Error: Method "%s" needs a policy.' % m.FullName()
if warning not in self.printed_policy_warnings:
print warning
print
self.printed_policy_warnings[warning] = 1
def ExportMethods(self):
'Export all the non-virtual methods of this class'
@@ -305,24 +288,28 @@ class ClassExporter(Exporter):
methods = [x for x in self.public_members if IsExportable(x)]
for method in methods:
if self.info[method.name].exclude:
continue # skip this method
method_info = self.info[method.name]
# skip this method if it was excluded by the user
if method_info.exclude:
continue
name = self.info[method.name].rename or method.name
# rename the method if the user requested
name = method_info.rename or method.name
# warn the user if this method needs a policy and doesn't have one
self.CheckPolicy(method)
method_info.policy = exporterutils.HandlePolicy(method, method_info.policy)
# check for policies
policy = self.info[method.name].policy or ''
policy = method_info.policy or ''
if policy:
policy = ', %s%s()' % (namespaces.python, policy.Code())
# check for overloads
overload = ''
if method.minArgs != method.maxArgs:
# add the overloads for this method
overload_name = OverloadName(method)
DeclareOverloads(method)
overload_name = OverloadName(method)
overload = ', %s%s()' % (namespaces.pyste, overload_name)
# build the .def string to export the method
@@ -337,7 +324,7 @@ class ClassExporter(Exporter):
code = '.staticmethod("%s")' % name
self.Add('inside', code)
# add wrapper code if this method has one
wrapper = self.info[method.name].wrapper
wrapper = method_info.wrapper
if wrapper and wrapper.code:
self.Add('declaration', wrapper.code)

View File

@@ -2,8 +2,12 @@ from Exporter import Exporter
from policies import *
from declarations import *
from settings import *
import exporterutils
#==============================================================================
# FunctionExporter
#==============================================================================
class FunctionExporter(Exporter):
'Generates boost.python code to export the given function.'
@@ -14,7 +18,7 @@ class FunctionExporter(Exporter):
def Export(self, codeunit, exported_names):
decls = self.GetDeclarations(self.info.name)
for decl in decls:
self.CheckPolicy(decl)
self.info.policy = exporterutils.HandlePolicy(decl, self.info.policy)
self.ExportDeclaration(decl, len(decls) == 1, codeunit)
self.GenerateOverloads(decls, codeunit)
@@ -23,17 +27,6 @@ class FunctionExporter(Exporter):
return self.info.name
def CheckPolicy(self, func):
'Warns the user if this function needs a policy'
def IsString(type):
return type.const and type.name == 'char' and isinstance(type, PointerType)
needs_policy = isinstance(func.result, (ReferenceType, PointerType))
if IsString(func.result):
needs_policy = False
if needs_policy and self.info.policy is None:
print '---> Error: Function "%s" needs a policy.' % func.FullName()
print
def ExportDeclaration(self, decl, unique, codeunit):
name = self.info.rename or decl.name
defs = namespaces.python + 'def("%s", ' % name

View File

@@ -3,6 +3,8 @@ Various helpers for interface files.
'''
from settings import *
from policies import *
from declarations import *
#==============================================================================
# FunctionWrapper
@@ -24,3 +26,39 @@ class FunctionWrapper(object):
return namespaces.pyste + self.name
else:
return self.name
#==============================================================================
# HandlePolicy
#==============================================================================
def HandlePolicy(function, policy):
'''Show a warning to the user if the function needs a policy and doesn't
have one. Return a policy to the function, which is the given policy itself
if it is not None, or a default policy for this method.
'''
def IsString(type):
'Return True if the Type instance can be considered a string'
return type.const and type.name == 'char' and isinstance(type, PointerType)
result = function.result
# basic test if the result type demands a policy
needs_policy = isinstance(result, (ReferenceType, PointerType))
# if the function returns const char*, a policy is not needed
if IsString(result):
needs_policy = False
# if returns a const T&, set the default policy
if policy is None and result.const and isinstance(result, ReferenceType):
policy = return_value_policy(copy_const_reference)
# show a warning to the user, if needed
if needs_policy and policy is None:
global _printed_warnings
warning = '---> Error: Method "%s" needs a policy.' % m.FullName()
if warning not in _printed_warnings:
print warning
print
# avoid double prints of the same warning
_printed_warnings[warning] = 1
return policy
_printed_warnings = {} # used to avoid double-prints in HandlePolicy

View File

@@ -25,13 +25,8 @@ import exporterutils
import settings
from policies import *
from CppParser import CppParser, CppParserError
from Exporter import Exporter
from FunctionExporter import FunctionExporter
from ClassExporter import ClassExporter
from IncludeExporter import IncludeExporter
from HeaderExporter import HeaderExporter
__VERSION__ = '0.5.9'
__VERSION__ = '0.6'
def GetDefaultIncludes():
if 'INCLUDE' in os.environ: