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: Enhance Argument Clinic's NoneType return converter to give `void`
Type: enhancement Stage: resolved
Components: Argument Clinic Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: arhadthedev, larry, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2022-03-26 12:59 by arhadthedev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 32126 closed arhadthedev, 2022-03-26 13:05
Messages (5)
msg416062 - (view) Author: Oleg Iarygin (arhadthedev) * Date: 2022-03-26 12:59
The attached PR makes the following possible (note that the impl has a `void` return type):

    /*[clinic input]
    _io._IOBase.writelines -> NoneType
        lines: object
        /
    [clinic start generated code]*/

    static void
    _io__IOBase_writelines_impl(PyObject *self, PyObject *lines)
    /*[clinic end generated code: output=f3feca36db72dbd1 input=286ba711cb7291ad]*/

Previously, the return type would be `Object *` with generated replacement of non-Py_None values to NULL on the other side.

So now there is no need to track whether NULL or Py_None should be returned. Or should it be Py_RETURN_NONE? Argument Clinic does it by itself returning NULL on errors and PyNone otherwise:

    static PyObject *
    _io__IOBase_writelines(PyObject *self, PyObject *lines)
    {
        PyObject *return_value = NULL;

        _io__IOBase_writelines_impl(self, lines);
        if (PyErr_Occurred()) {
            goto exit;
        }
        return_value = Py_None;
        Py_INCREF(Py_None);

    exit:
        return return_value;
    }
msg416064 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-26 14:19
The function should return different values for success and error. Functions which do not do this have bad design.
msg416066 - (view) Author: Oleg Iarygin (arhadthedev) * Date: 2022-03-26 14:40
> The function should return different values for success and error

It does, and a `void` return type enforces it. Here is the trick:

> An important convention throughout the Python interpreter is the following: when a function fails, it should set an exception condition and return an error value (usually -1 or a NULL pointer).
>
> - https://docs.python.org/3/extending/extending.html#intermezzo-errors-and-exceptions

Previously, a function could return NULL but forget to call `PyErr_*()`.  With `void`, however, the function has no other choise but to properly set the error indicator so the Clinic's part will see it and return NULL finishing the convention.
msg416104 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-27 06:26
It is simpler and faster to return NULL than call PyErr_Occurred(). There is a special macro PY_RETURN_NONE, so there is no problem with returning None either.

I do not think it would make the code better.
msg416147 - (view) Author: Oleg Iarygin (arhadthedev) * Date: 2022-03-28 07:34
Actually, you're right. For now, PyErr_Occurred is a GIL lock plus a memory access. While the access is cheap because of a L1 cache hit, the GIL takes its toll in a hot path.

So I'm closing the PR until GIL removal is done so no performance penalty will be imposed.

I could use _PyErr_Occurred because "Currently Argument Clinic is considered internal-only for CPython", but it requires extra modifications of the clinic that is undesirable.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91284
2022-03-28 07:34:15arhadthedevsetmessages: + msg416147
2022-03-27 06:26:32serhiy.storchakasetstatus: open -> closed
resolution: rejected
messages: + msg416104

stage: patch review -> resolved
2022-03-26 14:40:00arhadthedevsetmessages: + msg416066
2022-03-26 14:19:25serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg416064
2022-03-26 13:05:29arhadthedevsetkeywords: + patch
stage: patch review
pull_requests: + pull_request30205
2022-03-26 12:59:28arhadthedevcreate