diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/pyside2/libpyside/pysidesignal.cpp | 2 | ||||
-rw-r--r-- | sources/pyside2/tests/QtWidgets/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sources/pyside2/tests/QtWidgets/private_mangle_test.py | 121 | ||||
-rw-r--r-- | sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 4 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/pep384impl.cpp | 100 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/pep384impl.h | 9 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/sbkpython.h | 6 |
7 files changed, 232 insertions, 11 deletions
diff --git a/sources/pyside2/libpyside/pysidesignal.cpp b/sources/pyside2/libpyside/pysidesignal.cpp index f3ba84d3b..2d423a634 100644 --- a/sources/pyside2/libpyside/pysidesignal.cpp +++ b/sources/pyside2/libpyside/pysidesignal.cpp @@ -352,8 +352,6 @@ PyObject* signalInstanceConnect(PyObject* self, PyObject* args, PyObject* kwds) if (isMethod || isFunction) { PyObject *function = isMethod ? PyMethod_GET_FUNCTION(slot) : slot; PyCodeObject *objCode = reinterpret_cast<PyCodeObject *>(PyFunction_GET_CODE(function)); - PyFunctionObject *function_obj = reinterpret_cast<PyFunctionObject *>(function); - functionName = Shiboken::String::toCString(PepFunction_GetName(function_obj)); useSelf = isMethod; slotArgs = PepCode_GET_FLAGS(objCode) & CO_VARARGS ? -1 : PepCode_GET_ARGCOUNT(objCode); if (useSelf) diff --git a/sources/pyside2/tests/QtWidgets/CMakeLists.txt b/sources/pyside2/tests/QtWidgets/CMakeLists.txt index 4bc17ebee..36f1ba80a 100644 --- a/sources/pyside2/tests/QtWidgets/CMakeLists.txt +++ b/sources/pyside2/tests/QtWidgets/CMakeLists.txt @@ -79,6 +79,7 @@ PYSIDE_TEST(keep_reference_test.py) PYSIDE_TEST(missing_symbols_test.py) PYSIDE_TEST(paint_event_test.py) PYSIDE_TEST(parent_method_test.py) +PYSIDE_TEST(private_mangle_test.py) PYSIDE_TEST(python_properties_test.py) PYSIDE_TEST(qabstracttextdocumentlayout_test.py) PYSIDE_TEST(qaction_test.py) diff --git a/sources/pyside2/tests/QtWidgets/private_mangle_test.py b/sources/pyside2/tests/QtWidgets/private_mangle_test.py new file mode 100644 index 000000000..31a870691 --- /dev/null +++ b/sources/pyside2/tests/QtWidgets/private_mangle_test.py @@ -0,0 +1,121 @@ +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of Qt for Python. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU Lesser General Public License Usage +## Alternatively, this file may be used under the terms of the GNU Lesser +## General Public License version 3 as published by the Free Software +## Foundation and appearing in the file LICENSE.LGPL3 included in the +## packaging of this file. Please review the following information to +## ensure the GNU Lesser General Public License version 3 requirements +## will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 2.0 or (at your option) the GNU General +## Public license version 3 or any later version approved by the KDE Free +## Qt Foundation. The licenses are as published by the Free Software +## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-2.0.html and +## https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +""" +This is the example from https://bugreports.qt.io/browse/PYSIDE-772 +with no interaction as a unittest. +""" + +import unittest +from PySide2.QtCore import Signal +from PySide2.QtWidgets import QApplication, QWidget +from PySide2 import QtWidgets + +class Harness(QWidget): + clicked = Signal() + + def __init__(self): + QWidget.__init__(self) + self.clicked.connect(self.method) + self.clicked.connect(self._method) + self.clicked.connect(self.__method) + + def method(self): # Public method + self.method_result = self.sender() + + def _method(self): # Private method + self.method__result = self.sender() + + def __method(self): # Name mangled method + self.method___result = self.sender() + + +class _Under(QWidget): + clicked = Signal() + + def __init__(self): + QWidget.__init__(self) + self.clicked.connect(self.method) + self.clicked.connect(self._method) + self.clicked.connect(self.__method) + + def method(self): # Public method + self.method_result = self.sender() + + def _method(self): # Private method + self.method__result = self.sender() + + def __method(self): # Name mangled method + self.method___result = self.sender() + + +class TestMangle(unittest.TestCase): + + def setUp(self): + QApplication() + + def tearDown(self): + del QtWidgets.qApp + + def testPrivateMangle(self): + harness = Harness() + harness.clicked.emit() + self.assertEqual(harness.method_result, harness) + self.assertEqual(harness.method__result, harness) + self.assertEqual(harness.method___result, harness) + self.assertTrue("method" in type(harness).__dict__) + self.assertTrue("_method" in type(harness).__dict__) + self.assertFalse("__method" in type(harness).__dict__) + self.assertTrue("_Harness__method" in type(harness).__dict__) + + def testPrivateMangleUnder(self): + harness = _Under() + harness.clicked.emit() + self.assertEqual(harness.method_result, harness) + self.assertEqual(harness.method__result, harness) + self.assertEqual(harness.method___result, harness) + # make sure that we skipped over the underscore in "_Under" + self.assertTrue("method" in type(harness).__dict__) + self.assertTrue("_method" in type(harness).__dict__) + self.assertFalse("__method" in type(harness).__dict__) + self.assertTrue("_Under__method" in type(harness).__dict__) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 5e3f890f8..b0d778231 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -5122,7 +5122,9 @@ void CppGenerator::writeGetattroFunction(QTextStream& s, GeneratorContext &conte s << INDENT << "if (Shiboken::Object::isUserType(" PYTHON_SELF_VAR ")) {" << endl; { Indentation indent(INDENT); - s << INDENT << "PyObject* meth = PyDict_GetItem(reinterpret_cast<PyTypeObject *>(Py_TYPE(" PYTHON_SELF_VAR "))->tp_dict, name);" << endl; + // PYSIDE-772: Perform optimized name mangling. + s << INDENT << "Shiboken::AutoDecRef tmp(_Pep_PrivateMangle(" PYTHON_SELF_VAR ", name));" << endl; + s << INDENT << "PyObject *meth = PyDict_GetItem(Py_TYPE(" PYTHON_SELF_VAR ")->tp_dict, tmp);" << endl; s << INDENT << "if (meth)" << endl; { Indentation indent(INDENT); diff --git a/sources/shiboken2/libshiboken/pep384impl.cpp b/sources/shiboken2/libshiboken/pep384impl.cpp index e1dd7518f..39fdc37ad 100644 --- a/sources/shiboken2/libshiboken/pep384impl.cpp +++ b/sources/shiboken2/libshiboken/pep384impl.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "pep384impl.h" +#include <autodecref.h> extern "C" { @@ -465,7 +466,7 @@ _PepUnicode_AsString(PyObject *str) /* * We need to keep the string alive but cannot borrow the Python object. * Ugly easy way out: We re-code as an interned bytes string. This - * produces a pseudo-leak as long there are new strings. + * produces a pseudo-leak as long as there are new strings. * Typically, this function is used for name strings, and the dict size * will not grow so much. */ @@ -765,13 +766,7 @@ PepFunction_Get(PyObject *ob, const char *name) return ret; } -/***************************************************************************** - * - * Support for funcobject.h - * - */ - -// this became necessary after Windows was activated. +// This became necessary after Windows was activated. PyTypeObject *PepFunction_TypePtr = NULL; @@ -820,6 +815,95 @@ PepType_GetNameStr(PyTypeObject *type) /***************************************************************************** * + * Extra support for name mangling + * + */ + +#ifdef Py_LIMITED_API +// We keep these definitions local, because they don't work in Python 2. +#define PyUnicode_GET_LENGTH(op) PyUnicode_GetLength((PyObject *)(op)) +#define PyUnicode_READ_CHAR(u, i) PyUnicode_ReadChar((PyObject *)(u), (i)) +#endif + +PyObject * +_Pep_PrivateMangle(PyObject *self, PyObject *name) +{ + /* + * Name mangling: __private becomes _classname__private. + * This function is modelled after _Py_Mangle, but is optimized + * a little for our purpose. + */ +#if PY_VERSION_HEX < 0X03000000 + const char *namestr = PyString_AsString(name); + if (namestr == NULL || namestr[0] != '_' || namestr[1] != '_') { + Py_INCREF(name); + return name; + } + size_t nlen = strlen(namestr); + /* Don't mangle __id__ or names with dots. */ + if ((namestr[nlen-1] == '_' && namestr[nlen-2] == '_') + || strchr(namestr, '.')) { + Py_INCREF(name); + return name; + } +#else + if (PyUnicode_READ_CHAR(name, 0) != '_' || + PyUnicode_READ_CHAR(name, 1) != '_') { + Py_INCREF(name); + return name; + } + size_t nlen = PyUnicode_GET_LENGTH(name); + /* Don't mangle __id__ or names with dots. */ + if ((PyUnicode_READ_CHAR(name, nlen-1) == '_' && + PyUnicode_READ_CHAR(name, nlen-2) == '_') || + PyUnicode_FindChar(name, '.', 0, nlen, 1) != -1) { + Py_INCREF(name); + return name; + } +#endif + Shiboken::AutoDecRef privateobj(PyObject_GetAttrString( + reinterpret_cast<PyObject *>(Py_TYPE(self)), "__name__")); +#ifndef Py_LIMITED_API + return _Py_Mangle(privateobj, name); +#else + // For some reason, _Py_Mangle is not in the Limited API. Why? + size_t plen = PyUnicode_GET_LENGTH(privateobj); + /* Strip leading underscores from class name */ + size_t ipriv = 0; + while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') + ipriv++; + if (ipriv == plen) { + Py_INCREF(name); + return name; /* Don't mangle if class is just underscores */ + } + plen -= ipriv; + + if (plen + nlen >= PY_SSIZE_T_MAX - 1) { + PyErr_SetString(PyExc_OverflowError, + "private identifier too large to be mangled"); + return NULL; + } + size_t const amount = ipriv + 1 + plen + nlen; + size_t const big_stack = 1000; + wchar_t bigbuf[big_stack]; + wchar_t *resbuf = amount <= big_stack ? bigbuf : (wchar_t *)malloc(sizeof(wchar_t) * amount); + if (!resbuf) + return 0; + /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ + resbuf[0] = '_'; + if (PyUnicode_AsWideChar(privateobj, resbuf + 1, ipriv + plen) < 0) + return 0; + if (PyUnicode_AsWideChar(name, resbuf + ipriv + plen + 1, nlen) < 0) + return 0; + PyObject *result = PyUnicode_FromWideChar(resbuf + ipriv, 1 + plen + nlen); + if (amount > big_stack) + free(resbuf); + return result; +#endif // Py_LIMITED_API +} + +/***************************************************************************** + * * Module Initialization * */ diff --git a/sources/shiboken2/libshiboken/pep384impl.h b/sources/shiboken2/libshiboken/pep384impl.h index d06947f06..b566a6218 100644 --- a/sources/shiboken2/libshiboken/pep384impl.h +++ b/sources/shiboken2/libshiboken/pep384impl.h @@ -452,6 +452,15 @@ LIBSHIBOKEN_API PyObject *PyTime_FromTime( /***************************************************************************** * + * Extra support for name mangling + * + */ + +// PYSIDE-772: This function supports the fix, but is not meant as public. +LIBSHIBOKEN_API PyObject *_Pep_PrivateMangle(PyObject *self, PyObject *name); + +/***************************************************************************** + * * Extra support for signature.cpp * */ diff --git a/sources/shiboken2/libshiboken/sbkpython.h b/sources/shiboken2/libshiboken/sbkpython.h index fbac016eb..29e25605a 100644 --- a/sources/shiboken2/libshiboken/sbkpython.h +++ b/sources/shiboken2/libshiboken/sbkpython.h @@ -42,7 +42,13 @@ #include "sbkversion.h" +/* + * Python 2 has function _Py_Mangle directly in Python.h . + * This creates wrong language binding unless we define 'extern "C"' here. + */ +extern "C" { #include <Python.h> +} #include <structmember.h> // Now we have the usual variables from Python.h . #include "python25compat.h" |