pywintypes throws errors only in HexChat not in Python 2.7 or PythonWin #480

Open
XcomReborn opened this Issue Mar 30, 2013 · 14 comments

Projects

None yet

3 participants

@XcomReborn

I have written two python scripts for HexChat. The first was a winamp controller that sent commands to a winamp.py script to control winamp based on user text commands read from IRC.

The python script would work on first load, but after unloading and reloading it would throw an error.

File "C:\Python27\Lib\winamp2.py", line 27, in <module>
import win32api, win32con, win32gui, win32process, pywintypes
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
__import_pywin32_system_module__("pywintypes", globals())
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 114, in __import_pywin32_system_module__
assert sys.modules[modname] is old_mod
TypeError: 'NoneType' object has no attribute '__getitem__'

This was fixed by removing the import pywintypes from the winamp2.py file and replacing the use of it from

except pywintypes.error, e:

to

except Exception, e

Secondly a similar error has occurred with pywintype that I haven' fixed yet.

I am trying to use the import pyttsx library to do text to speech.

Simply:

import pyttsx

engine = pyttsx.init()
engine.say('Greetings!')
engine.runAndWait()

This works in python 2.7 on in Idle and pythonwin but throws this error in HexChat.

 Traceback (most recent call last):
   File "C:\Users\aaa\AppData\Roaming\HexChat\xxxxx.py", line 10, in <module>
     engine = pyttsx.init()
   File "C:\Python27\lib\site-packages\pyttsx\__init__.py", line 39, in init
     eng = Engine(driverName, debug)
   File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__
     self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
   File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 64, in __init__
     self._module = __import__(name, globals(), locals(), [driverName])
   File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 19, in <module>
     import win32com.client
   File "C:\Python27\lib\site-packages\win32com\__init__.py", line 6, in <module>
     import pythoncom
   File "C:\Python27\lib\site-packages\pythoncom.py", line 2, in <module>
     import pywintypes
   File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
     __import_pywin32_system_module__("pywintypes", globals())
   File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 114, in __import_pywin32_system_module__
     assert sys.modules[modname] is old_mod
 TypeError: 'NoneType' object has no attribute '__getitem__'

I asked on Stackoverflow and they seem to this is problem is with the python interpreter in HexChat.

@Arnavion
hexchat member

Please show us both your scripts and link to the SO discussion.

@XcomReborn

The discussion was here : http://chat.stackoverflow.com/transcript/0?m=8557434#8557434

How do you want me to upload the scripts ?

@XcomReborn

The shortest no working example is this

__module_name__ = "Xcom comsBot"
__module_version__ = "1.0.0"
__module_description__ = "Xcom ComsBot"
__doc__ = "\consbot (color / say / shutup / sorry) \help"

#import text to speech
import pyttsx


engine = pyttsx.init()
engine.say('Greetings!')
engine.runAndWait()


print "\0034",__module_name__, __module_version__,"has been loaded\003"
@TingPing
hexchat member

Crashes on first load for me.

@Arnavion
hexchat member

After looking at pyttx's documentation, it seems to me that runAndWait() is supposed to block forever. You can't call this function in a Hexchat plugin then, since this will effectively lock up Hexchat. I haven't tested the plugin yet, but are you seeing this lock-up that you think is a crash? Or is it actually a crash?

Also, the docs say this function blocks and triggers callbacks to respond to events. If it is trying to do so via python threads, then this is also a problem, since the embedded Python cannot run threads. @TingPing will know more about this.

Edit: http://pyttsx.readthedocs.org/en/v1.1/engine.html#pyttsx.init and http://pyttsx.readthedocs.org/en/v1.1/engine.html#pyttsx.engine.Engine.runAndWait

@TingPing
hexchat member

Can't say i do know more =\. Afaik threads work on Linux and on there it insta-crashes.

@XcomReborn

The other one for winamp uses this library

http://code.google.com/p/pywinamp/downloads/list

A simple example would be

__module_name__ = "Xcom Winamp2 Test"
__module_version__ = "1.0.0"
__module_description__ = "Xcom Winamp2 TEST"
__doc__ = "To be implemented"


import sys
if not 'winamp' in sys.modules: import winamp

winamp = winamp.Winamp()

winamp.play()



print "\0034",__module_name__, __module_version__,"has been loaded\003"

this is the one that crashes on second load

@Arnavion
hexchat member

Please keep one issue per bug report. Please answer the question I put to you in my last comment.

@XcomReborn

The reason why I give two separate examples is because they both mention the same pywintypes in the error message, so I thought it may be the same error at work.

Answering your question, it is a crash rather than a hang.

@Arnavion
hexchat member

Are you saying the script you pasted in #480 (comment) also gives you an error about pywintypes?

@XcomReborn

See error messages posted in the original message

@XcomReborn

To be clear, both the script to run text to speech in #480 and the script to control winamp both show an error in pywintypes. I fixed the one to control winamp by editing the original library to remove pywintypes import and use another exception type. I have not managed to fix the TTS one yet. but it says the same thing about pywintypes

@XcomReborn

Any news on this? If not can to recommend a way to implement Text to Speech using python in HexChat?

@Arnavion
hexchat member

I'll look at it in the coming weekend. Thanks for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment