Python 3.4, pywin32 Build 219.
If the ActiveScripting host is a non-console Windows application,
then any non-fatal output to sys.stdout (direct or indirect)
leads to fatal script error, namely:
"TypeError: Objects of type 'bytes' can not be converted to Unicode."
The reason is in "framework.py":
class SafeOutput:
...
def write(self,message):
try:
self.redir.write(message)
except:
win32api.OutputDebugString(message.encode('mbcs'))
Python 3 is stricter than Python 2
in detecting whether valid stdout is present or not.
For non-console apps MSVC CRT sets OS file handle for stdout
to a special placeholder value of -2 (_NO_CONSOLE_FILENO).
Python 2 didn't detect it (if I'm right),
but Python 3 does and concludes that there is no valid sys.stdout.
Consequently, self.redir.write(message) fails,
and the control comes to "except" block,
where win32api.OutputDebugString(message.encode('mbcs'))
also fails with the exception as described above.
It is win32api.OutputDebugString(...) who fails, not encode(...).
I suppose that win32api.OutputDebugString(...) now requires Unicode
(though I failed to confirm it studying the C code and modern MSVC projects),
while it accepted ASCII in the past.
I'm not alone in stumbling over this bug,
the discussion here
http://stackoverflow.com/questions/9252477/500-server-error-for-python-in-asp-script
also mentions it.
As far as I understand, the fix is to just get rid of encode(...):
win32api.OutputDebugString(message)
which is done in the attached patch.
For now, I worked around the bug by redirecting stdout to NUL device in my host app.
Python 3 inherits handles, thinks that stdout is OK and doesn't throw TypeError anymore.
But still a bug.
The trace:
Traceback (most recent call last):
File "C:\Program Files\Python34\lib\site-packages\win32com\server\policy.py", line 325, in InvokeEx
return self.invokeex(dispid, lcid, wFlags, args, kwargs, serviceProvider)
File "C:\Program Files\Python34\lib\site-packages\win32com\server\policy.py", line 586, in invokeex
return func(*args)
File "C:\Program Files\Python34\lib\site-packages\win32comext\axscript\client\framework.py", line 572, in SetScriptSite
trace("Debugging extensions (axdebug) module does not exist - debugging is disabled..")
File "C:\Program Files\Python34\lib\site-packages\win32comext\axscript\client\framework.py", line 94, in trace
print(arg, end=' ')
File "C:\Program Files\Python34\lib\site-packages\win32comext\axscript\client\framework.py", line 64, in write
win32api.OutputDebugString(message.encode('mbcs'))
TypeError: Objects of type 'bytes' can not be converted to Unicode.
Thanks, applied here:
http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/rev/4e527a8f8266
Thank you.
Couldn't you presume, please, where will the next build be available?
Probably a few weeks to a couple of months :)