You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhance Argument Clinic's NoneType return converter to give void #91284
staticvoid_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:
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).
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.
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.
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.
Activity
arhadthedev commentedon Mar 26, 2022
The attached PR makes the following possible (note that the impl has a
voidreturn type):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:
serhiy-storchaka commentedon Mar 26, 2022
The function should return different values for success and error. Functions which do not do this have bad design.
arhadthedev commentedon Mar 26, 2022
It does, and a
voidreturn type enforces it. Here is the trick:Previously, a function could return NULL but forget to call
PyErr_*(). Withvoid, 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.serhiy-storchaka commentedon Mar 27, 2022
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.
arhadthedev commentedon Mar 28, 2022
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.
arhadthedev commentedon Jul 20, 2022
Not relevant anymore; see #32126 (comment).