|
| 1 | +--- sources/shiboken2/libshiboken/pep384impl.cpp.orig 2022-07-17 22:14:14.000000000 +0300 |
| 2 | ++++ sources/shiboken2/libshiboken/pep384impl.cpp 2022-07-17 22:20:39.000000000 +0300 |
| 3 | +@@ -707,6 +707,76 @@ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | ++#if PY_VERSION_HEX >= 0x03000000 |
| 8 | ++PyObject * |
| 9 | ++_Py_Mangle(PyObject *privateobj, PyObject *ident) |
| 10 | ++{ |
| 11 | ++ /* Name mangling: __private becomes _classname__private. |
| 12 | ++ This is independent from how the name is used. */ |
| 13 | ++ PyObject *result; |
| 14 | ++ size_t nlen, plen, ipriv; |
| 15 | ++ Py_UCS4 maxchar; |
| 16 | ++ if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
| 17 | ++ PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 18 | ++ PyUnicode_READ_CHAR(ident, 1) != '_') { |
| 19 | ++ Py_INCREF(ident); |
| 20 | ++ return ident; |
| 21 | ++ } |
| 22 | ++ nlen = PyUnicode_GET_LENGTH(ident); |
| 23 | ++ plen = PyUnicode_GET_LENGTH(privateobj); |
| 24 | ++ /* Don't mangle __id__ or names with dots. |
| 25 | ++ |
| 26 | ++ The only time a name with a dot can occur is when |
| 27 | ++ we are compiling an import statement that has a |
| 28 | ++ package name. |
| 29 | ++ |
| 30 | ++ TODO(jhylton): Decide whether we want to support |
| 31 | ++ mangling of the module name, e.g. __M.X. |
| 32 | ++ */ |
| 33 | ++ if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 34 | ++ PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 35 | ++ PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
| 36 | ++ Py_INCREF(ident); |
| 37 | ++ return ident; /* Don't mangle __whatever__ */ |
| 38 | ++ } |
| 39 | ++ /* Strip leading underscores from class name */ |
| 40 | ++ ipriv = 0; |
| 41 | ++ while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 42 | ++ ipriv++; |
| 43 | ++ if (ipriv == plen) { |
| 44 | ++ Py_INCREF(ident); |
| 45 | ++ return ident; /* Don't mangle if class is just underscores */ |
| 46 | ++ } |
| 47 | ++ plen -= ipriv; |
| 48 | ++ |
| 49 | ++ if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 50 | ++ PyErr_SetString(PyExc_OverflowError, |
| 51 | ++ "private identifier too large to be mangled"); |
| 52 | ++ return NULL; |
| 53 | ++ } |
| 54 | ++ |
| 55 | ++ maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 56 | ++ if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 57 | ++ maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 58 | ++ |
| 59 | ++ result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 60 | ++ if (!result) |
| 61 | ++ return 0; |
| 62 | ++ /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 63 | ++ PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
| 64 | ++ if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 65 | ++ Py_DECREF(result); |
| 66 | ++ return NULL; |
| 67 | ++ } |
| 68 | ++ if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 69 | ++ Py_DECREF(result); |
| 70 | ++ return NULL; |
| 71 | ++ } |
| 72 | ++ assert(_PyUnicode_CheckConsistency(result, 1)); |
| 73 | ++ return result; |
| 74 | ++} |
| 75 | ++#endif |
| 76 | ++ |
| 77 | + #ifdef Py_LIMITED_API |
| 78 | + // We keep these definitions local, because they don't work in Python 2. |
| 79 | + # define PyUnicode_GET_LENGTH(op) PyUnicode_GetLength((PyObject *)(op)) |
0 commit comments