Skip to content

Commit c5a24f1

Browse files
committedJan 27, 2021
Move code to init Python GIL/threads into separate C file to avoid compiler deprecation warnings
Signed-off-by: Christopher Arndt <[email protected]>
1 parent 5f0a8a2 commit c5a24f1

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed
 

‎MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include tox.ini
66
include *.rst
77
include src/_rtmidi.pyx
88
include src/_rtmidi.cpp
9+
include src/pyinit.h
910
include src/rtmidi/RtMidi.cpp
1011
include src/rtmidi/RtMidi.h
1112

‎src/_rtmidi.pyx

+3-6
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,10 @@ else:
131131
string_types = (str,)
132132

133133

134-
# Init Python threads and GIL, because RtMidi calls Python from native threads.
135-
# See http://permalink.gmane.org/gmane.comp.python.cython.user/5837
136-
cdef extern from "Python.h":
137-
void PyEval_InitThreads()
138-
139-
PyEval_InitThreads()
134+
cdef extern from "pyinit.h":
135+
void py_init()
140136

137+
py_init()
141138

142139
# Declarations for RtMidi C++ classes and their methods we use
143140

‎src/pyinit.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <Python.h>
2+
/*
3+
* This code initializes Python threads and GIL, because RtMidi calls Python
4+
* from native threads.
5+
*
6+
* See http://permalink.gmane.org/gmane.comp.python.cython.user/5837
7+
*
8+
* *PyEval_InitThreads* is a no-op since Python.37 and deprecated since
9+
* Python 3.6. Now *Py_Initialize* initializes the GIL.
10+
*
11+
* The calls are in this separate C file instead of in the main .pyx file so
12+
* that we can use pre-compiler conditionals and don't get a compiler
13+
* deprecation warning on Python 3.9+ for including *PyEval_InitThreads*.
14+
*/
15+
16+
void py_init() {
17+
#if PY_MAJOR_VERSION >= 3 and PY_MINOR_VERSION >= 7
18+
Py_Initialize();
19+
#else
20+
PyEval_InitThreads();
21+
#endif
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.