py and pyw behave differently for PyQt4 script if print function is presented

Issue #8 closed
Vova Kolobok created an issue

Python 2.7.8 32 bit
Windows 8.1 Enterprise 32 bit

If PyQt4 script contains print function then pyw fail to launch the script, while py work just fine

  1. command py test.py привет
    result 2015-09-30 17-48-22 python.png

  2. command pyw test.py привет
    result: nothing at all, no window, no errors
    However if comment out line 34 (print function) then pyw works just fine.

test.py:

#!python2
from __future__ import print_function
import ctypes, sys
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('MarkupViewer')

# enable unicode filenames
from ctypes import POINTER, byref, cdll, c_int, windll
from ctypes.wintypes import LPCWSTR, LPWSTR

GetCommandLineW = cdll.kernel32.GetCommandLineW
GetCommandLineW.argtypes = []
GetCommandLineW.restype = LPCWSTR

CommandLineToArgvW = windll.shell32.CommandLineToArgvW
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
CommandLineToArgvW.restype = POINTER(LPWSTR)

cmd = GetCommandLineW()
argc = c_int(0)
argv = CommandLineToArgvW(cmd, byref(argc))
if argc.value > 0:
    # Remove Python executable and commands if present
    start = argc.value - len(sys.argv)
    sys.argv = [argv[i] for i in xrange(start, argc.value)]


from PyQt4 import QtGui


class test(QtGui.QLabel):
    def __init__(self, *args):
        QtGui.QLabel.__init__(self, None)
        self.text = args[0]
        print(self.text)
        self.setText(self.text)
        self.show()

text = sys.argv[1]
app = QtGui.QApplication(sys.argv)
test = test(text)
app.exec_()

Comments (5)

  1. Vinay Sajip repo owner

    This may be because the print function behaves differently in the two environments. I suggest wrapping the print statement in a try: / except: block - what happens then?

  2. Vova Kolobok Account Deactivated reporter

    what happens then?

    It fixes the issue.

    But can it be resolve in pyw (or maybe some kind of envvar)? I mean wrapping print each time is kinda tedious.

  3. Vova Kolobok Account Deactivated reporter

    I’ve replace print with

            try:
                print(self.text)
            except Exception as e:
                self.text += str(e)
    

    result: 2015-09-30 19-32-25 pythonw.png

    Calling set PYTHONIOENCODING=utf8 & pyw test.py привет fixes the issue completely.

    It would be nice if pyw ignored print exceptions or something, since print doesn’t make any difference in context of pyw.

  4. Vinay Sajip repo owner

    It would be nice if pyw ignored print exceptions or something, since print doesn’t make any difference in context of pyw.

    Well, you can still print() to files ... anyway, this is not a launcher bug, so marking as Invalid.

  5. Vova Kolobok Account Deactivated reporter

    I’ve call pythonw.exe directly and got the same result, so it is bug in CPython2.7.8.

  6. Log in to comment