py3: change of getattr -> getattro

issue 0000995
This commit is contained in:
looooo
2017-06-02 11:53:39 +02:00
committed by wmayer
parent 3168f8ed3f
commit bc696e5ab5
5 changed files with 215 additions and 92 deletions

View File

@@ -2,7 +2,9 @@
# -*- coding: utf-8 -*-
# (c) 2006 Juergen Riegel
import template,os,sys
from __future__ import print_function # this allows py2 to print(str1,str2) correctly
from . import template
import os,sys
import generateBase.generateModel_Module
import generateBase.generateTools
@@ -11,20 +13,29 @@ class TemplateClassPyExport (template.ModelTemplate):
#self.ParentNamespace = "Base"
#self.Namespace = "Base"
encoding = sys.getfilesystemencoding()
path = self.path.decode(encoding)
exportName = self.export.Name.decode(encoding)
dirname = self.dirname.decode(encoding)
print "TemplateClassPyExport",path + exportName
path = self.path
if hasattr(path,"decode"): # this is python2. Otherwise this is unicode already
path = path.decode(encoding)
exportName = self.export.Name
if hasattr(exportName,"decode"): # this is python2. Otherwise this is unicode already
exportName = exportName.decode(encoding)
dirname = self.dirname
if hasattr(dirname,"decode"): # this is python2. Otherwise this is unicode already
dirname = dirname.decode(encoding)
print("TemplateClassPyExport",path + exportName)
# Imp.cpp must not exist, neither in path nor in dirname
if(not os.path.exists(path + exportName + "Imp.cpp")):
if(not os.path.exists(dirname + exportName + "Imp.cpp")):
file = open(path + exportName + "Imp.cpp",'w')
file = open(path + exportName + "Imp.cpp",'wb')
generateBase.generateTools.replace(self.TemplateImplement,locals(),file)
file = open(path + exportName + ".cpp",'w')
file.close()
file = open(path + exportName + ".cpp",'wb')
generateBase.generateTools.replace(self.TemplateModule,locals(),file)
file = open(path + exportName + ".h",'w')
file.close()
file = open(path + exportName + ".h",'wb')
generateBase.generateTools.replace(self.TemplateHeader,locals(),file)
#file.write( generateBase.generateTools.replace(self.Template,locals()))
file.close()
TemplateHeader = """
// This file is generated by src/Tools/generateTemaplates/templateClassPyExport.py out of the XML file
@@ -104,8 +115,10 @@ public:
static PyObject * number_subtract_handler (PyObject *self, PyObject *other);
/// callback for the number_multiply_handler
static PyObject * number_multiply_handler (PyObject *self, PyObject *other);
#if PY_MAJOR_VERSION < 3
/// callback for the number_divide_handler
static PyObject * number_divide_handler (PyObject *self, PyObject *other);
#endif
/// callback for the number_remainder_handler
static PyObject * number_remainder_handler (PyObject *self, PyObject *other);
/// callback for the number_divmod_handler
@@ -132,18 +145,24 @@ public:
static PyObject * number_xor_handler (PyObject *self, PyObject *other);
/// callback for the number_or_handler
static PyObject * number_or_handler (PyObject *self, PyObject *other);
#if PY_MAJOR_VERSION < 3
/// callback for the number_coerce_handler
static int number_coerce_handler (PyObject **self, PyObject **other);
#endif
/// callback for the number_int_handler
static PyObject * number_int_handler (PyObject *self);
#if PY_MAJOR_VERSION < 3
/// callback for the number_long_handler
static PyObject * number_long_handler (PyObject *self);
#endif
/// callback for the number_float_handler
static PyObject * number_float_handler (PyObject *self);
#if PY_MAJOR_VERSION < 3
/// callback for the number_oct_handler
static PyObject * number_oct_handler (PyObject *self);
/// callback for the number_hex_handler
static PyObject * number_hex_handler (PyObject *self);
#endif
//@}
-
+ if (self.export.Sequence):
@@ -206,8 +225,8 @@ public:
/// setter for special attributes (e.g. dynamic ones)
/// Output: Success=1, Failure=-1, Ignore=0
int setCustomAttributes(const char* attr, PyObject *obj);
PyObject *_getattr(char *attr); // __getattr__ function
int _setattr(char *attr, PyObject *value); // __setattr__ function
PyObject *_getattro(PyObject *attro); // __getattr__ function
int _setattro(PyObject *attro, PyObject *value); // __setattr__ function
-
/// getter for the object handled by this class
@@ -245,8 +264,7 @@ using namespace @self.export.Namespace@;
/// Type structure of @self.export.Name@
PyTypeObject @self.export.Name@::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
PyVarObject_HEAD_INIT(&PyType_Type,0)
+ if (self.export.PythonName):
"@self.export.PythonName@", /*tp_name*/
= else:
@@ -257,8 +275,8 @@ PyTypeObject @self.export.Name@::Type = {
/* methods */
PyDestructor, /*tp_dealloc*/
0, /*tp_print*/
__getattr, /*tp_getattr*/
__setattr, /*tp_setattr*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
__repr, /*tp_repr*/
+ if (self.export.NumberProtocol):
@@ -275,20 +293,24 @@ PyTypeObject @self.export.Name@::Type = {
0, /*tp_hash*/
0, /*tp_call */
0, /*tp_str */
0, /*tp_getattro*/
0, /*tp_setattro*/
__getattro, /*tp_getattro*/
__setattro, /*tp_setattro*/
/* --- Functions to access object as input/output buffer ---------*/
0, /* tp_as_buffer */
/* --- Flags to define presence of optional/expanded features */
#if PY_MAJOR_VERSION >= 3
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT, /*tp_flags */
#else
+ if (self.export.RichCompare and self.export.NumberProtocol):
Py_TPFLAGS_HAVE_CLASS|Py_TPFLAGS_HAVE_RICHCOMPARE|Py_TPFLAGS_CHECKTYPES, /*tp_flags */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_RICHCOMPARE|Py_TPFLAGS_CHECKTYPES, /*tp_flags */
= elif (self.export.RichCompare):
Py_TPFLAGS_HAVE_CLASS|Py_TPFLAGS_HAVE_RICHCOMPARE, /*tp_flags */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_RICHCOMPARE, /*tp_flags */
= elif (self.export.NumberProtocol):
Py_TPFLAGS_HAVE_CLASS|Py_TPFLAGS_CHECKTYPES, /*tp_flags */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES, /*tp_flags */
= else:
Py_TPFLAGS_HAVE_CLASS, /*tp_flags */
Py_TPFLAGS_DEFAULT, /*tp_flags */
-
#endif
"@self.export.Documentation.UserDocu.replace('\\n','\\\\n\\"\\n \\"')@", /*tp_doc */
0, /*tp_traverse */
0, /*tp_clear */
@@ -320,6 +342,9 @@ PyTypeObject @self.export.Name@::Type = {
0, /*tp_weaklist */
0, /*tp_del */
0 /*tp_version_tag */
#if PY_MAJOR_VERSION >= 3
,0 /*tp_finalize */
#endif
};
/// Methods structure of @self.export.Name@
@@ -343,7 +368,9 @@ PyNumberMethods @self.export.Name@::Number[] = { {
number_add_handler,
number_subtract_handler,
number_multiply_handler,
#if PY_MAJOR_VERSION < 3
number_divide_handler,
#endif
number_remainder_handler,
number_divmod_handler,
number_power_handler,
@@ -357,12 +384,20 @@ PyNumberMethods @self.export.Name@::Number[] = { {
number_and_handler,
number_xor_handler,
number_or_handler,
#if PY_MAJOR_VERSION < 3
number_coerce_handler,
#endif
number_int_handler,
#if PY_MAJOR_VERSION < 3
number_long_handler,
#else
0,
#endif
number_float_handler,
#if PY_MAJOR_VERSION < 3
number_oct_handler,
number_hex_handler,
#endif
NULL,
NULL,
NULL,
@@ -379,6 +414,9 @@ PyNumberMethods @self.export.Name@::Number[] = { {
NULL,
NULL,
NULL
#if PY_MAJOR_VERSION >= 3
,NULL /*nb_inplace_matrix_multiply*/
#endif
} };
-
@@ -674,8 +712,14 @@ PyObject *@self.export.Name@::_repr(void)
//--------------------------------------------------------------------------
// @self.export.Name@ Attributes
//--------------------------------------------------------------------------
PyObject *@self.export.Name@::_getattr(char *attr) // __getattr__ function: note only need to handle new state
PyObject *@self.export.Name@::_getattro(PyObject *attro) // __getattr__ function: note only need to handle new state
{
char *attr;
#if PY_MAJOR_VERSION >= 3
attr = PyUnicode_AsUTF8(attro);
#else
attr = PyString_AsString(attro);
#endif
try {
// getter method for special Attributes (e.g. dynamic ones)
PyObject *r = getCustomAttributes(attr);
@@ -694,7 +738,7 @@ PyObject *@self.export.Name@::_getattr(char *attr) // __getattr__ function: n
catch(const std::exception& e) // catch other c++ exceptions
{
std::string str;
str += "FC++ exception thrown (";
str += "C++ exception thrown (";
str += e.what();
str += ")";
Base::Console().Error(str.c_str());
@@ -729,20 +773,26 @@ PyObject *@self.export.Name@::_getattr(char *attr) // __getattr__ function: n
}
#endif // DONT_CATCH_CXX_EXCEPTIONS
PyObject *rvalue = Py_FindMethod(Methods, this, attr);
if (rvalue == NULL)
{
PyErr_Clear();
return @self.export.Father@::_getattr(attr);
}
else
{
return rvalue;
PyMethodDef *ml = Methods;
for (; ml->ml_name != NULL; ml++) {
if (attr[0] == ml->ml_name[0] &&
strcmp(attr+1, ml->ml_name+1) == 0)
return PyCFunction_New(ml, this);
}
PyErr_Clear();
return @self.export.Father@::_getattro(attro);
}
int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state
int @self.export.Name@::_setattro(PyObject *attro, PyObject *value) // __setattr__ function: note only need to handle new state
{
char *attr;
#if PY_MAJOR_VERSION >= 3
attr = PyUnicode_AsUTF8(attro);
#else
attr = PyString_AsString(attro);
#endif
try {
// setter for special Attributes (e.g. dynamic ones)
int r = setCustomAttributes(attr, value);
@@ -768,7 +818,7 @@ int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ fun
catch(const std::exception& e) // catch other c++ exceptions
{
std::string str;
str += "FC++ exception thrown (";
str += "C++ exception thrown (";
str += e.what();
str += ")";
Base::Console().Error(str.c_str());
@@ -802,7 +852,7 @@ int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ fun
}
#endif // DONT_CATCH_CXX_EXCEPTIONS
return @self.export.Father@::_setattr(attr, value);
return @self.export.Father@::_setattro(attro, value);
}
-