Skip to content

Commit 9c04c7b

Browse files
authored
chore: Delete copy ctor/assign for GIL RAIIs (#4183)
* chore: Delete copy ctor/assign for GIL RAIIs * Fix typo * Delete copy ops for local gil scoped acquire
1 parent 1874f8f commit 9c04c7b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

include/pybind11/detail/internals.h

+2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ PYBIND11_NOINLINE internals &get_internals() {
412412
// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
413413
struct gil_scoped_acquire_local {
414414
gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
415+
gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
416+
gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
415417
~gil_scoped_acquire_local() { PyGILState_Release(state); }
416418
const PyGILState_STATE state;
417419
} gil;

include/pybind11/gil.h

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class gil_scoped_acquire {
8080
inc_ref();
8181
}
8282

83+
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
84+
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
85+
8386
void inc_ref() { ++tstate->gilstate_counter; }
8487

8588
PYBIND11_NOINLINE void dec_ref() {
@@ -144,6 +147,9 @@ class gil_scoped_release {
144147
}
145148
}
146149

150+
gil_scoped_release(const gil_scoped_acquire &) = delete;
151+
gil_scoped_release &operator=(const gil_scoped_acquire &) = delete;
152+
147153
/// This method will disable the PyThreadState_DeleteCurrent call and the
148154
/// GIL won't be acquired. This method should be used if the interpreter
149155
/// could be shutting down when this is called, as thread deletion is not
@@ -178,6 +184,8 @@ class gil_scoped_acquire {
178184

179185
public:
180186
gil_scoped_acquire() { state = PyGILState_Ensure(); }
187+
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
188+
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
181189
~gil_scoped_acquire() { PyGILState_Release(state); }
182190
void disarm() {}
183191
};
@@ -187,6 +195,8 @@ class gil_scoped_release {
187195

188196
public:
189197
gil_scoped_release() { state = PyEval_SaveThread(); }
198+
gil_scoped_release(const gil_scoped_release &) = delete;
199+
gil_scoped_release &operator=(const gil_scoped_acquire &) = delete;
190200
~gil_scoped_release() { PyEval_RestoreThread(state); }
191201
void disarm() {}
192202
};

0 commit comments

Comments
 (0)