kmonsoor / pyglet

Automatically exported from code.google.com/p/pyglet
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

XLibWindow._event_text_symbol assumes one-character text #452

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes, a keystroke may generate more than one character. I encountered
this problem because I'm using the Compose key (see wikipedia).

What steps will reproduce the problem?
1. Put for example the following lines in your ~/.XCompose file:
<Multi_key> <a> <space>                   : "a " # a
2. Enable XIM, map the Compose key on your keyboard & restart X
3. Run a pyglet app and press: Compose, a, space

Paste in the traceback or error message:

Traceback (most recent call last):                                        

  File "main.py", line 23, in <module>                                    

    pyglet.app.run()                                                      

  File "/var/lib/python-support/python2.6/pyglet/app/__init__.py", line
264, in run                             
    EventLoop().run()                                                     

  File "/var/lib/python-support/python2.6/pyglet/app/xlib.py", line 83, in
run                                  
    window.dispatch_platform_event(e)                                     

  File "/var/lib/python-support/python2.6/pyglet/window/xlib/__init__.py",
line 1169, in dispatch_platform_event
    event_handler(e)                                                      

  File "/var/lib/python-support/python2.6/pyglet/window/xlib/__init__.py",
line 1316, in _event_key             
    text, symbol = self._event_text_symbol(ev)                            

  File "/var/lib/python-support/python2.6/pyglet/window/xlib/__init__.py",
line 1241, in _event_text_symbol     
    if text and unicodedata.category(text) == 'Cc' and text != '\r':      

TypeError: need a single Unicode character as parameter         

pyglet 1.1 with Python 2.5: Paste in the output of `python -m pyglet.info`
Other: Paste in the output of tools/gl_info.py (included in source distro):

Any additional info (platform/language/hardware) that may be relevant?

Suggested patch:

--- pyglet/window/xlib/__init__.py    2008-09-16 17:33:09.000000000 +0300
+++ pyglet/window/xlib/__init__.py 2009-09-04 19:18:00.000000000 +0300
@@ -1238,8 +1238,12 @@
                 text = buffer.value[:count].decode(encoding)

             # Don't treat Unicode command codepoints as text, except Return.
-            if text and unicodedata.category(text) == 'Cc' and text != '\r':
+            if not text:
                 text = None
+            else:
+                for char in text:
+                    if unicodedata.category(char) == 'Cc' and char != '\r':
+                        text = None

         symbol = symbol.value

Original issue reported on code.google.com by encu...@gmail.com on 4 Sep 2009 at 4:20

GoogleCodeExporter commented 9 years ago
Patched in maintenance r2534

Original comment by benjamin...@gmail.com on 1 Nov 2009 at 7:38

GoogleCodeExporter commented 9 years ago
My Thinkpad's "Fn" button generates some kind of exotic astral character when 
pressed (namely, 0x1008ff2b), which causes Pyglet to explode:

Traceback (most recent call last):
  File ".../game/mainwindow.py", line 107, in run
    window.run()
  File ".../python2.6/site-packages/pyglet/app/__init__.py", line 264, in run
    EventLoop().run()
  File ".../python2.6/site-packages/pyglet/app/xlib.py", line 82, in run
    window.dispatch_platform_event(e)
  File ".../python2.6/site-packages/pyglet/window/xlib/__init__.py", line 1169, in dispatch_platform_event
    event_handler(e)
  File ".../python2.6/site-packages/pyglet/window/xlib/__init__.py", line 1326, in _event_key
    text, symbol = self._event_text_symbol(ev)
  File ".../python2.6/site-packages/pyglet/window/xlib/__init__.py", line 1262, in _event_text_symbol
    symbol = ord(unichr(symbol).lower())
ValueError: unichr() arg not in range(0x110000) (wide Python build)

Proposed patch:

--- pyglet/window/xlib/__init__.py  2011-02-21 04:14:33.189914001 +0200
+++ pyglet/window/xlib/__init__.py        2011-02-21 04:20:01.000000000 +0200
@@ -1259,14 +1259,11 @@
         # raw key code to a user key.
         if symbol and symbol not in key._key_names and ev.xkey.keycode:
             # Issue 353: Symbol is uppercase when shift key held down.
-            try:
-                symbol = ord(unichr(symbol).lower())
-            except ValueError:
-                pass
-            else:
-                # If still not recognised, use the keycode
-                if symbol not in key._key_names:
-                    symbol = key.user_key(ev.xkey.keycode)
+            symbol = ord(unichr(symbol).lower())
+
+            # If still not recognised, use the keycode
+            if symbol not in key._key_names:
+                symbol = key.user_key(ev.xkey.keycode)

         if filtered:
             # The event was filtered, text must be ignored, but the symbol is

Original comment by encu...@gmail.com on 21 Feb 2011 at 2:21