@@ -520,10 +520,20 @@ With this PEP, you would implement it like this:
520520 return -1;
521521 }
522522
523- char *to_write = do_some_text_mutation(text);
523+ const char *to_write = PyUnicode_AsUTF8(text);
524+ if (to_write == NULL ) {
525+ // Since the exception may be destroyed upon calling PyThreadState_Release (),
526+ // print out the exception ourself.
527+ PyErr_Print ();
528+ PyThreadState_Release (thread_view);
529+ PyInterpreterLock_Release (lock);
530+ return -1;
531+ }
524532 int res = PyFile_WriteString(to_write, file);
525533 free (to_write);
526- PyErr_Print ();
534+ if (res < 0 ) {
535+ PyErr_Print ();
536+ }
527537
528538 PyThreadState_Release (thread_view);
529539 PyInterpreterLock_Release (lock);
@@ -703,8 +713,8 @@ Example: An Asynchronous Callback
703713 static int
704714 async_callback(void *arg)
705715 {
706- ThreadData *data = (ThreadData *)arg;
707- PyInterpreterView view = data ->view;
716+ ThreadData *tdata = (ThreadData *)arg;
717+ PyInterpreterView view = tdata ->view;
708718 PyInterpreterLock lock = PyInterpreterLock_AcquireView(view);
709719 if (lock == 0) {
710720 fputs("Python has shut down!\n", stderr);
@@ -721,6 +731,8 @@ Example: An Asynchronous Callback
721731 }
722732 PyThreadState_Release(thread_view);
723733 PyInterpreterLock_Release(lock);
734+ PyInterpreterView_Close(view);
735+ PyMem_RawFree(tdata);
724736 return 0;
725737 }
726738
0 commit comments