This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [C API] Make the PyDescrObject structure opaque: PyDescr_NAME() and PyDescr_TYPE()
Type: Stage: resolved
Components: C API Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2022-01-26 16:14 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pydescr_set_type.patch vstinner, 2022-01-26 16:15
pydescr_new.patch vstinner, 2022-01-26 16:17
pythoncapi_compat_pydescr_new.patch vstinner, 2022-01-26 16:18
swig_pydescr_new.patch vstinner, 2022-01-26 16:22
Messages (7)
msg411765 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:14
While working on the PEP 674 implementation, I noticed that PyDescr_NAME() and PyDescr_TYPE() macros are used as l-value by two projects: M2Crypto and mecab-python3. Both are code generated by SWIG.

M2Crypto-0.38.0/src/SWIG/_m2crypto_wrap.c and mecab-python3-1.0.4/src/MeCab/MeCab_wrap.cpp contain the function:

SWIGINTERN PyGetSetDescrObject *
SwigPyStaticVar_new_getset(PyTypeObject *type, PyGetSetDef *getset) {

  PyGetSetDescrObject *descr;
  descr = (PyGetSetDescrObject *)PyType_GenericAlloc(SwigPyStaticVar_Type(), 0);
  assert(descr);
  Py_XINCREF(type);
  PyDescr_TYPE(descr) = type;
  PyDescr_NAME(descr) = PyString_InternFromString(getset->name);
  descr->d_getset = getset;
  if (PyDescr_NAME(descr) == NULL) {
    Py_DECREF(descr);
    descr = NULL;
  }
  return descr;
}

ref: https://bugs.python.org/issue45476#msg407410


SwigPyStaticVar_new_getset() is more of less an outdated copy of Python 2.7 descr_copy() of Python Objects/descrobject.c.


I tried to prevent using the PyDescr_NAME() and PyDescr_TYPE() macros as l-value in two ways:

* Add PyDescr_SET_NAME() and PyDescr_SET_Type()
* Add PyDescr_New()

The problem of the PyDescr_SET_NAME() and PyDescr_SET_Type() approach is that SWIG code is outdated: it doesn't initialized the PyDescrObject.d_qualname member added to Python 3.3 (bpo-13577, commit 9d57481f043cb9b94bfc45c1ee041415d915cf8a).

So I implemented PyDescr_New() function: in CPython, it means basically to rename descr_new() to PyDescr_New(). Problem: implementating this function for older Python versions is non trivial. Code that I wrote for the pythoncapi_compat project:

// bpo-45476 added PyDescr_New() to Python 3.11.0a5
#if PY_VERSION_HEX < 0x030B00A5
PyDescrObject *
PyDescr_New(PyTypeObject *descr_type, PyTypeObject *type, const char *name)
{
    assert(descr_type != NULL);
    assert(type != NULL);
    assert(name != NULL);

    PyObject *name_obj;
#if PY_MAJOR_VERSION >= 3
    name_obj = PyUnicode_InternFromString(name);
#else
    name_obj = PyString_InternFromString(name);
#endif
    if (name_obj == NULL) {
        return NULL;
    }

    PyDescrObject *descr = (PyDescrObject *)PyType_GenericAlloc(descr_type, 0);
    if (descr == NULL) {
        Py_DECREF(name_obj);
        return NULL;
    }

    
    descr->d_type = (PyTypeObject*)Py_NewRef(type);
    descr->d_name = name_obj;
    // PyDescrObject.d_qualname was added to Python 3.3.0a1
#if PY_VERSION_HEX >= 0x030300A1
    descr->d_qualname = NULL;
#endif
    return descr;
}
#endif


The even more complicated part is to write unit tests for this function in pythoncapi_compat. It requires to implement a whole type with tp_traverse tp_dealloc functions.

In Python, this function must be at least documented.

At the end, IMO it's not really worth it to add so much complexity to Python and pythoncapi_compat just for 2 projects using SWIG. I created this issue to keep a track of my analysis, if someone else tries to do something similar tomorrow.


I attached my WIP patches to keep a copy of this work, in case if we need to revisit this idea later.

For the PEP 674, IMO it's better to leave the two PyDescr_NAME() and PyDescr_TYPE() macros unchanged. There is no need in the short term to make PyDescrObject structure and structures using it opaque in the short term. Well, SWIG code looks incorrect (it doesn't initialize d_qualname), but a fix just for SWIG should be enough.
msg411766 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:15
pydescr_set_type.patch: Python patch to add PyDescr_SET_TYPE() and PyDescr_SET_NAME() functions.
msg411767 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:17
pydescr_new.patch: Python patch adding the PyDescr_New() function.
msg411768 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:18
pythoncapi_compat_pydescr_new.patch: pythoncapi_compat patch adding PyDescr_New() function with tests.
msg411769 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:20
swig_pydescr_new.patch: SWIG patch adding PyDescr_New() and using it.
msg411770 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:23
Since making PyDescrObject opaque is not really worth it, I close this issue.

I mean, it's worth it, but there are more important structures like PyObject, PyTypeObject or PyListObject.
msg411773 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-26 16:34
> The problem of the PyDescr_SET_NAME() and PyDescr_SET_Type() approach is that SWIG code is outdated: it doesn't initialized the PyDescrObject.d_qualname member added to Python 3.3 (bpo-13577, commit 9d57481f043cb9b94bfc45c1ee041415d915cf8a).

I checked: Oh, in practice, it's not an issue since PyType_GenericAlloc() initializes the memory to zero. So the code "just works".
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90696
2022-01-26 16:35:59vstinnersettitle: [C API] Make the PyDescrObject structure opaque -> [C API] Make the PyDescrObject structure opaque: PyDescr_NAME() and PyDescr_TYPE()
2022-01-26 16:34:02vstinnersetmessages: + msg411773
2022-01-26 16:23:19vstinnersetstatus: open -> closed
resolution: rejected
messages: + msg411770

stage: resolved
2022-01-26 16:22:15vstinnersetfiles: + swig_pydescr_new.patch
2022-01-26 16:22:06vstinnersetfiles: - swig_pydescr_new.patch
2022-01-26 16:20:52vstinnersetfiles: + swig_pydescr_new.patch

messages: + msg411769
2022-01-26 16:18:09vstinnersetfiles: + pythoncapi_compat_pydescr_new.patch

messages: + msg411768
2022-01-26 16:17:05vstinnersetfiles: + pydescr_new.patch

messages: + msg411767
2022-01-26 16:15:15vstinnersetfiles: + pydescr_set_type.patch
keywords: + patch
messages: + msg411766
2022-01-26 16:14:46vstinnercreate