PyQT executable generated using Cython & cl crashes

943 views
Skip to first unread message

vayal

unread,
Jan 4, 2017, 7:30:00 AM1/4/17
to cython-users
I am converting a PyQt application to exe on Windows and noticed a crash when running the generated exe where the debugger breaks at PyUnicodeUCS2_AsASCIIString.

Please find below the reduced  test case to reproduce the issue.

import sys
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QMainWindow, QPushButton, QApplication


class MainWindow(QMainWindow):
   
def __init__(self):
       
QMainWindow.__init__(self)
       
self.btn1 = QPushButton("Click me", self)
       
if len(sys.argv) > 1:
           
self.connect(self.btn1, SIGNAL("clicked()"), self.doit) # No crash
       
else:
           
self.btn1.clicked.connect(self.doit) # Crash


   
def doit(self):
       
print "Click"


if __name__ == "__main__":
    app
= QApplication(sys.argv)
    myapp
= MainWindow()
    myapp
.show()
    sys
.exit(app.exec_())


The code crashes only when using new style QT signals/slots, but not when using old style semantics.
The same code above when compiled to exe using nuitka, works without crashing.

More details
* Cython 0.25.2
* Python 2.7.10 (64 bit)
* PyQT4 4.11.4
* Visual studio 2010

vayal

unread,
Jan 10, 2017, 2:14:37 AM1/10/17
to cython-users

On Wednesday, 4 January 2017 18:00:00 UTC+5:30, vayal wrote:
I am converting a PyQt application to exe on Windows and noticed a crash when running the generated exe where the debugger breaks at PyUnicodeUCS2_AsASCIIString.

Is this a Cython issue and should I file a bug for this? 

Robert Bradshaw

unread,
Jan 18, 2017, 12:11:32 AM1/18/17
to cython...@googlegroups.com
Hopefully some Windows user can reproduce and debug this. Do you have a full backtrace that leads to the crash? I am really scratching my head as to what we could be doing to cause this...


Denis Akhiyarov

unread,
Jan 19, 2017, 2:09:21 AM1/19/17
to cython-users
this works fine in pure python 3.5 64-bit on Windows 10 and Qt5. how did you compile to executable? did you use setup.py or did you build the generated .C file (with --embed flag to Cython) from VS?

vayal

unread,
Jan 19, 2017, 10:58:59 AM1/19/17
to cython-users

On Thursday, 19 January 2017 12:39:21 UTC+5:30, Denis Akhiyarov wrote:
this works fine in pure python 3.5 64-bit on Windows 10 and Qt5. how did you compile to executable? did you use setup.py or did you build the generated .C file (with --embed flag to Cython) from VS?

This is how I built the executable (from VS2010 x64 command prompt):

cython --embed app.py
cl app
.c /IC:\Python27\include /link /libpath:C:\Python27\libs


 

vayal

unread,
Jan 19, 2017, 10:58:59 AM1/19/17
to cython-users

On Wednesday, 18 January 2017 10:41:32 UTC+5:30, Robert Bradshaw wrote:
Hopefully some Windows user can reproduce and debug this. Do you have a full backtrace that leads to the crash? I am really scratching my head as to what we could be doing to cause this...

Below is the stack trace:

> python27.dll!PyUnicodeUCS2_AsASCIIString()  + 0x4 bytes
 
[Frames below may be incorrect and/or missing, no symbols loaded for python27.dll]
  sip
.pyd!00007ffc3d67b5f6()
 
QtCore.pyd!initQtCore()  + 0x110dff bytes
 
QtCore.pyd!initQtCore()  + 0x111320 bytes
  python27
.dll!PyCFunction_Call()  + 0x76 bytes
  app
.exe!initapp()  + 0x2fc5 bytes
  app
.exe!initapp()  + 0x321d bytes
  app
.exe!initapp()  + 0x30cb bytes
  app
.exe!00007ff651bf23fb()
  app
.exe!00007ff651bf1026()
  app
.exe!initapp()  + 0x5251 bytes
  app
.exe!initapp()  + 0x5340 bytes
  app
.exe!initapp()  + 0x50aa bytes
  python27
.dll!PyObject_Call()  + 0x65 bytes
  python27
.dll!PyMethod_New()  + 0x911 bytes
  python27
.dll!PyObject_Call()  + 0x65 bytes
  python27
.dll!PyType_Ready()  + 0x2c5e bytes
  python27
.dll!PyType_ClearCache()  + 0x206 bytes
  app
.exe!initapp()  + 0x2fc5 bytes
  app
.exe!initapp()  + 0x5df2 bytes
  app
.exe!initapp()  + 0x1710 bytes
  app
.exe!initapp()  + 0x6696 bytes
  app
.exe!initapp()  + 0x78eb bytes
  kernel32
.dll!BaseThreadInitThunk()  + 0xd bytes
  ntdll
.dll!RtlUserThreadStart()  + 0x1d bytes


 

Denis Akhiyarov

unread,
Jan 19, 2017, 11:24:37 AM1/19/17
to cython-users

Don't you need also to link against Qt library?

Pyinstaller, cx_freeze, and most likely nuitka come with special hooks for this. At least these tools analyze for dependencies.

There is pyqt disutils tools available to handle just this:

https://pypi.python.org/pypi/pyqt-distutils

Not sure how to do this from VS or command-line cl.exe.


--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/Faxs4WWHGrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vayal

unread,
Jan 19, 2017, 12:57:52 PM1/19/17
to cython-users

On Thursday, 19 January 2017 21:54:37 UTC+5:30, Denis Akhiyarov wrote:

Don't you need also to link against Qt library?


No. The generated executable is *not* standalone and still depends on the Python runtime and pyqt.
These need to be installed for me to run the exe file.
Not to forget that the script I provided was just a sample to reproduce the issue.
If I run the script with a command line argument it takes the "if" path which doesn't crash whereas the "else" path causes a crash.

Denis Akhiyarov

unread,
Jan 20, 2017, 8:48:43 AM1/20/17
to cython-users
The correct Visual Studio (VC++) cl.exe compiler to use is from VS 2008 for Python 2.7. In fact Microsoft team specifically provides a compiler for Python 2.7:

https://www.microsoft.com/en-us/download/details.aspx?id=44266

You are using VS 2010 and so mixing likely incompatible MSVC(++) runtimes.

Denis Akhiyarov

unread,
Jan 20, 2017, 1:34:16 PM1/20/17
to cython-users
I reproduced your issue with Python 2.7, 32bit and PyQt4. This app works fine with pyinstaller, but fails with cython. But I cannot debug it, since I do not have full VS 2008 setup.
When compiled with Python 3.5, 32bit, VS 2015, PyQt5, the app works fine. Here are the changes:

```
import sys
#from PyQt4.QtCore import SIGNAL
from PyQt5.QtCore import pyqtSignal as SIGNAL
#from PyQt4.QtGui import QMainWindow, QPushButton, QApplication
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
print('imports done')

class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.btn1 = QPushButton("Click me", self)
if len(sys.argv) > 1:
self.connect(self.btn1, SIGNAL("clicked()"), self.doit) # No crash
else:

print('before crash')
self.btn1.clicked.connect(self.doit) # Crash


def doit(self):
print("Click")

```

vayal

unread,
Jan 25, 2017, 1:35:00 AM1/25/17
to cython-users
Denis,
Thanks for you support so far. Just wanted to get some clarity on this. It is it mandatory that all the binaries used should be compiled with the same VS version that was used to build Python?
I compile Python files to pyd using Cython. My Python files use PyQT which in turn use QT, both of these libraries I compiled from source using VS2010, should I recompile them with VS2008?

Denis Akhiyarov

unread,
Jan 25, 2017, 1:55:03 PM1/25/17
to cython-users
Yes, all CPython extensions, including QT should be compiled with the same VS version as CPython itself was compiled with. Otherwise it will work until you see random crashes like described in this article:

http://siomsystems.com/mixing-visual-studio-versions/

I'm not sure if this helps, but someone recompiled Python 2.7 with VS 2010 and VS 2015:

http://www.p-nand-q.com/python/building-python-27-with-visual_studio.html

This doesn't mean that it passes all CPython tests and has the same level of support like with VS 2008.

Another reason to upgrade to Python 3.4+ !

You can also use compatible mingw(py) instead of MSVC to build extensions, but not with Python 3.5+ due to UCRT issues.

Reply all
Reply to author
Forward
0 new messages