Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf: account for Python GIL explicit initialization deprecation from Python 3.7 to 3.9 #1909

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pxr/base/tf/pyInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ TfPyInitialize()

if (!Py_IsInitialized()) {

// Starting with Python 3.7, the GIL is initialized as part of
// Py_Initialize(). Python 3.9 deprecated explicit GIL initialization.
#if PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 7
if (!ArchIsMainThread() && !PyEval_ThreadsInitialized()) {
// Python claims that PyEval_InitThreads "should be called in the
// main thread before creating a second thread or engaging in any
// other thread operations." So we'll issue a warning here.
TF_WARN("Calling PyEval_InitThreads() for the first time outside "
"the 'main thread'. Python doc says not to do this.");
}
#endif

const std::string s = ArchGetExecutablePath();

Expand Down Expand Up @@ -113,9 +117,10 @@ TfPyInitialize()
sigaction(SIGINT, &origSigintHandler, NULL);
#endif

#if PY_MAJOR_VERSION > 2
// In python 3 PyEval_InitThreads must be called after Py_Initialize()
// see https://docs.python.org/3/c-api/init.html
#if PY_MAJOR_VERSION > 2 && PY_MINOR_VERSION < 7
// In Python 3 (before 3.7), PyEval_InitThreads must be called
// after Py_Initialize().
// see https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
//
// Initialize Python threading. This grabs the GIL. We'll release it
// at the end of this function.
Expand Down
4 changes: 4 additions & 0 deletions pxr/base/tf/pyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,12 @@ void Tf_PyInitWrapModule(
const char* packageTag,
const char* packageTag2)
{
// Starting with Python 3.7, the GIL is initialized as part of
// Py_Initialize(). Python 3.9 deprecated explicit GIL initialization.
#if PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 7
// Ensure the python GIL is created.
PyEval_InitThreads();
#endif

// Tell the tracing mechanism that python is alive.
Tf_PyTracingPythonInitialized();
Expand Down