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: Python3.7 crash in PyCFunction_New due to broken _PyObject_GC_TRACK
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, etejedor, ncoghlan, vstinner
Priority: normal Keywords:

Created on 2018-12-04 12:35 by etejedor, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
reproducer.cpp etejedor, 2018-12-04 12:35
Messages (5)
msg331040 - (view) Author: Enric Tejedor Saavedra (etejedor) Date: 2018-12-04 12:35
Attached is a reproducer that calls PyCFunction_New. The reproducer runs normally with Python 3.6.5, but it crashes with Python 3.7.1. The reason seems to be that the _PyObject_GC_TRACK macro ends up being called and it is broken in Python3.7.

A fix for that macro seems to have been committed to master:

https://github.com/python/cpython/pull/10507
msg331047 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-04 14:24
> Python3.7 crash in PyCFunction_New due to broken _PyObject_GC_TRACK

It's unrelated. Your must not use the Python API before Python is initialized. If you modify your code like that, it works as expected:

int main()
{
Py_Initialize();

  PyMethodDef methoddef_ = {
    const_cast< char* >( "myfun" ),
    (PyCFunction) myfun,
    METH_O,
    NULL
  };

  PyObject* myFunPtr = PyCFunction_New( &methoddef_, NULL );

Py_Finalize();
  return 0;
}

I don't think that it's a regression.

Python initialization is now well documented:
https://docs.python.org/dev/c-api/init.html

The documentation starts with:

"In an application embedding Python, the Py_Initialize() function must be called before using any other Python/C API functions; with the exception of a few functions and the global configuration variables."

PyCFunction_New() is an example of function of the Python C API.
msg331048 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-04 14:29
I close the issue as "not a bug".

Even if the code "worked" in Python 3.6, it worked because of a mistake :-)
msg331111 - (view) Author: Enric Tejedor Saavedra (etejedor) Date: 2018-12-05 13:34
Hi Victor,

Thank you for clarifying.

If the call to PyCFunction_New is done from a C extension module, is it also necessary to call Py_Initialize()?
msg331113 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-05 14:07
> If the call to PyCFunction_New is done from a C extension module, is it also necessary to call Py_Initialize()?

Py_Initialize() must always be called first.
History
Date User Action Args
2022-04-11 14:59:08adminsetgithub: 79589
2018-12-05 14:07:25vstinnersetmessages: + msg331113
2018-12-05 13:34:05etejedorsetmessages: + msg331111
2018-12-04 14:29:58vstinnersetstatus: open -> closed

nosy: + ncoghlan, eric.snow
messages: + msg331048

resolution: not a bug
stage: resolved
2018-12-04 14:24:56vstinnersetmessages: + msg331047
2018-12-04 14:20:32vstinnersetnosy: + vstinner
2018-12-04 12:35:18etejedorcreate