Re: [PyQt] Q(Core)Application.exec_() is not interruptable

2008-05-01 Thread Christoph Burgmer
Am Donnerstag, 1. Mai 2008 schrieb İsmail Dönmez:
 Following code can't be interrupted with CTRL-C :
  import sys
  from PyQt4.QtCore import QCoreApplication
  QCoreApplication.exec_(sys.argv)

+1 on the question.

I tried a lot like using module signal to trap a Ctrl+C but nothing seems to 
work. I guess the C++ port takes away keyboard events from Python. Any help 
appreciated.

Christoph

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Q(Core)Application.exec_() is not interruptable

2008-05-01 Thread Giovanni Bajo
On Thu, 2008-05-01 at 20:24 +0200, Christoph Burgmer wrote:
 Am Donnerstag, 1. Mai 2008 schrieb İsmail Dönmez:
  Following code can't be interrupted with CTRL-C :
   import sys
   from PyQt4.QtCore import QCoreApplication
   QCoreApplication.exec_(sys.argv)
 
 +1 on the question.
 
 I tried a lot like using module signal to trap a Ctrl+C but nothing seems to 
 work. I guess the C++ port takes away keyboard events from Python. Any help 
 appreciated.

CTRL+C causes a signal to be sent to the process. Python catches the
signal, and sets a global variable, something like CTRL_C_PRESSED =
True. Then, whenever the Python interpreter gets to execute a new
opcode, it sees the variable set and raises a KeybordInterrupt.

This means that CTRL+C works only if the Python interpreter is spinning.
If the interpreter is executing an extension module written in C that
executes a long-running operation, CTRL+C won't interrupt it, unless it
explicitly cooperates with Python. Eg: time.sleep() is theoretically a
blocking operation, but the implementation of that function cooperates
with the Python interpreter to make CTRL+C work.

This is all by design: CTRL+C is meant to do a clean abort; this is
why it gets turned into an exception by Python (so that the cleanups are
executed during stack unwind), and its support by extension modules is
sort of opt-in. If you want to totally abort the process, without
giving it a chance to cleanup, you can use CTRL+\.

When Python calls QApplication::exec() (the C++ function), Qt doesn't
know how to cooperate with Python for CTRL+C, and this is why it does
not work. I don't think there's a good way to make it work; you may
want to see if you can handle it through a global event filter.
-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com


___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Q(Core)Application.exec_() is not interruptable

2008-05-01 Thread Ewald de Wit
On Thursday 01 May 2008 14:28:31 İsmail Dönmez wrote:
 Following code can't be interrupted with CTRL-C :
  import sys
  from PyQt4.QtCore import QCoreApplication
  QCoreApplication.exec_(sys.argv)

 Is this somehow intended and is there a way to overcome this?

You can catch the signal like this:

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

import sys
from PyQt4.QtCore import QCoreApplication
app = QCoreApplication(sys.argv)
app.exec_()

This is tested to work on linux, windows and osx.

--
  --  Ewald

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Q(Core)Application.exec_() is not interruptable

2008-05-01 Thread İsmail Dönmez
Hi,

On Thu, May 1, 2008 at 10:54 PM, Ewald de Wit [EMAIL PROTECTED] wrote:
 On Thursday 01 May 2008 14:28:31 İsmail Dönmez wrote:
   Following code can't be interrupted with CTRL-C :
import sys
from PyQt4.QtCore import QCoreApplication
QCoreApplication.exec_(sys.argv)
  
   Is this somehow intended and is there a way to overcome this?

  You can catch the signal like this:

  import signal
  signal.signal(signal.SIGINT, signal.SIG_DFL)


  import sys
  from PyQt4.QtCore import QCoreApplication
  app = QCoreApplication(sys.argv)
  app.exec_()

That indeed works, thanks for the tip!

-- 
Never learn by your mistakes, if you do you may never dare to try again.

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt