Skip to content

Commit 6491193

Browse files
committed
Update the examples to use a PyThreadRef parameter.
1 parent 803b58f commit 6491193

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

peps/pep-0788.rst

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ With this PEP, you'd implement it like this:
654654
return -1;
655655
}
656656
657-
if (PyThreadState_Ensure(ref) < 0) {
657+
PyThreadRef thread;
658+
if (PyThreadState_Ensure(ref, &thread) < 0) {
658659
PyInterpreterRef_Close(ref);
659660
puts("Out of memory.\n", stderr);
660661
return -1;
@@ -665,7 +666,7 @@ With this PEP, you'd implement it like this:
665666
free(to_write);
666667
PyErr_Print();
667668
668-
PyThreadState_Release();
669+
PyThreadState_Release(thread);
669670
PyInterpreterRef_Close(ref);
670671
return res < 0;
671672
}
@@ -756,14 +757,15 @@ This is the same code, rewritten to use the new functions:
756757
thread_func(void *arg)
757758
{
758759
PyInterpreterRef interp = (PyInterpreterRef)arg;
759-
if (PyThreadState_Ensure(interp) < 0) {
760+
PyThreadRef thread;
761+
if (PyThreadState_Ensure(interp, &thread) < 0) {
760762
PyInterpreterRef_Close(interp);
761763
return -1;
762764
}
763765
if (PyRun_SimpleString("print(42)") < 0) {
764766
PyErr_Print();
765767
}
766-
PyThreadState_Release();
768+
PyThreadState_Release(&thread);
767769
PyInterpreterRef_Close(interp);
768770
return 0;
769771
}
@@ -803,7 +805,8 @@ release the interpreter reference, allowing the interpreter to shut down.
803805
thread_func(void *arg)
804806
{
805807
PyInterpreterRef ref = (PyInterpreterRef)arg;
806-
if (PyThreadState_Ensure(ref) < 0) {
808+
PyThreadRef thread;
809+
if (PyThreadState_Ensure(ref, &thread) < 0) {
807810
PyInterpreterRef_Close(ref);
808811
return -1;
809812
}
@@ -813,7 +816,7 @@ release the interpreter reference, allowing the interpreter to shut down.
813816
if (PyRun_SimpleString("print(42)") < 0) {
814817
PyErr_Print();
815818
}
816-
PyThreadState_Release();
819+
PyThreadState_Release(thread);
817820
return 0;
818821
}
819822
@@ -859,14 +862,15 @@ deadlock the interpreter if it's not released.
859862
return -1;
860863
}
861864
862-
if (PyThreadState_Ensure(ref) < 0) {
865+
PyThreadRef thread;
866+
if (PyThreadState_Ensure(ref, &thread) < 0) {
863867
PyInterpreterRef_Close(ref);
864868
return -1;
865869
}
866870
if (PyRun_SimpleString("print(42)") < 0) {
867871
PyErr_Print();
868872
}
869-
PyThreadState_Release();
873+
PyThreadState_Release(thread);
870874
PyInterpreterRef_Close(ref);
871875
return 0;
872876
}
@@ -917,14 +921,15 @@ interpreter here.
917921
return;
918922
}
919923
920-
if (PyThreadState_Ensure(ref) < 0) {
924+
PyThreadRef thread;
925+
if (PyThreadState_Ensure(ref, &thread) < 0) {
921926
PyInterpreterRef_Close(ref);
922927
return -1;
923928
}
924929
if (PyRun_SimpleString("print(42)") < 0) {
925930
PyErr_Print();
926931
}
927-
PyThreadState_Release();
932+
PyThreadState_Release(thread);
928933
PyInterpreterRef_Close(ref);
929934
return 0;
930935
}

0 commit comments

Comments
 (0)