Skip to content

Call to Py_Initialize changes signal handler configuration #449

Closed

Description

@rmadsen-ks

Environment

  • Pythonnet version:
    2.3.0
  • 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:

class Program
    {
        static void Main(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");
        }
    }

Activity

  1. den-run-ai commented on Apr 4, 2017

    @den-run-ai
    Contributor

    @rmadsen-ks this change looks reasonable! Can you submit a pull request?

  2. rmadsen-ks commented on Apr 4, 2017

    @rmadsen-ks
    ContributorAuthor

    @denfromufa, sure, I can do that tomorrow.

    1. Should it be an optional argument to PythonEngine.Initialize?
    2. Should there be a unit test testing the issue? (might be very complicated to implement)
  3. den-run-ai commented on Apr 4, 2017

    @den-run-ai
    Contributor
  4. added a commit that references this issue on Aug 21, 2018
    d9562fe
  5. added a commit that references this issue on Oct 29, 2018
    422aafe
  6. added 3 commits that reference this issue on Nov 13, 2018
    bba6604
    f933540
    7765474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions