aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/libshiboken
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2018-08-13 01:50:26 +0200
committerChristian Tismer <tismer@stackless.com>2018-08-21 13:50:37 +0000
commit28e82039e7b0c611f78d4b15dc7fb2ba232bb76c (patch)
tree365788a607ffa679645d20ee81c94b99269faec3 /sources/shiboken2/libshiboken
parent877c9be79d660fe1317a6fb46c585fb688abc21e (diff)
Implement Proper Name Mangling
When types have attributes starting with two underscores but ending with at most one, Python uses name mangling to create a unique private variable. PySide needs to obey this rule in the tp_getattro methods. We implemented it as an optimized _Pep_PrivateMangle function that solves the problem internally without exposing the _Py_Mangle function. Remark: I think the exclusion of the _Py_Mangle function is another oversight in the Limited API. Task-number: PYSIDE-772 Change-Id: I0bfc2418dae439e963a16e37443f2099c6980696 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/shiboken2/libshiboken')
-rw-r--r--sources/shiboken2/libshiboken/pep384impl.cpp100
-rw-r--r--sources/shiboken2/libshiboken/pep384impl.h9
-rw-r--r--sources/shiboken2/libshiboken/sbkpython.h6
3 files changed, 107 insertions, 8 deletions
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"