@jbrockmendel pointed out in #49034 that we don't consistently validate the return of Py_* functions in the C API. The net effect of this is that our extensions are much harder to refactor, as our lack of checks allows at worst for undefined behavior and at best for segfaults.

If someone were to audit the extensions we could easily add in error handling where missing and shore up these extensions. See also https://docs.python.org/3/c-api/exceptions.html#

Comment From: MarcoGorelli

Does it have to check NULL, or are other checks allowed?

From a "quick and dirty" check, here's a couple I found:

https://github.com/pandas-dev/pandas/blob/009c4c622c739941ae3dfde4bc4117eb25172500/pandas/_libs/src/ujson/python/date_conversions.c#L92-L117

https://github.com/pandas-dev/pandas/blob/348d43f7bf63465dd8f6cca4e1bd4b608fb58597/pandas/_libs/src/parser/io.c#L68

https://github.com/pandas-dev/pandas/blob/2410fca2c62898fb29659d5b93273a65515d695b/pandas/_libs/src/ujson/python/JSONtoObj.c#L121

https://github.com/pandas-dev/pandas/blob/2410fca2c62898fb29659d5b93273a65515d695b/pandas/_libs/src/ujson/python/JSONtoObj.c#L240-L244

https://github.com/pandas-dev/pandas/blob/2f44dbaba3aac3b47b5da351b36634dffab09e98/pandas/_libs/src/ujson/python/objToJSON.c#L185-L192

but I don't know which are allowed and which not. tz != Py_None looks fine, for example? But in the second example, I don't see any validation of func (or does Py_XDECREF(func) count?)

Comment From: WillAyd

Those are all good examples of incorrect code. Yea they should all be checking for NULL

Comment From: WillAyd

At least all PyObject funcs. Other Py funcs May not return a pointer so could have different requirements (ex: -1 for integral return values)

Comment From: jbrockmendel

ive asked enough times to know this a pipe dream, but this logic would be so much more maintainable if it lived in cython