You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Call to Py_Initialize changes signal handler configuration #449
Python version:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Operating System:
Win 7, 64 bit
Details
What I'm trying to do:
Console application with CTRL+C handling.
According to https://docs.python.org/2/c-api/init.html, Py_Initialize() changes the current signal configuration. In practice, signals like interrupt from CTRL+C in a console application seems to be ignored, which is not the desired behavior. This means that the event handler configured as this Console.CancelKeyPress += Console_CancelKeyPress; does nothing.
It seems Py_InitializeEx(0) should be used for general applications that does not include a python terminal. Alternatively, whether to use the Py_InitalizeEx(0) could be a argument to Python.Initialize()
To reproduce try running this:
classProgram{staticvoidMain(string[]args){PythonEngine.Initialize();// Comment this out to support CTRL+C again.Console.WriteLine("Waiting 5s.. Try CTRL+C.");System.Threading.Thread.Sleep(5000);Console.WriteLine("Done");}}
1. Yes
According to the linked docs that your provided this Py_InitializeEx should
be the preferred way when embedding CPython.
However we should keep the old option as optional argument, in case someone
handles this somehow with Python exception.
So I would add deprecating warning to mention that next version we would
switch to recommended C-API behavior.
2.
You can actually start your pull request with the unit test to show that
the current implementation of Py_Initialize fails to interrupt with Ctrl+C.
And then add the implementation once the failure is obviously from both
Appveyor and Travis CI.
As a side note so you cannot capture Ctrl+C from Python exception currently?
Activity
den-run-ai commentedon Apr 4, 2017
@rmadsen-ks this change looks reasonable! Can you submit a pull request?
rmadsen-ks commentedon Apr 4, 2017
@denfromufa, sure, I can do that tomorrow.
den-run-ai commentedon Apr 4, 2017
Fixed pythonnet#449, by added initSigs as an optional parameter to Py…
Fixed pythonnet#449, by added initSigs as an optional parameter to Py…
Fixed pythonnet#449, by added initSigs as an optional parameter to Py…
Fixed pythonnet#449, by added initSigs as an optional parameter to Py…
Fixed pythonnet#449, by added initSigs as an optional parameter to Py…