[pypy-commit] pyrepl py3ksupport: add a modified version of _minimal_curses, get to the point of kind of running pythoni on python3

RonnyPfannschmidt noreply at buildbot.pypy.org
Fri Oct 28 11:22:44 CEST 2011


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: py3ksupport
Changeset: r152:19d9ded8abdc
Date: 2011-10-28 11:22 +0200
http://bitbucket.org/pypy/pyrepl/changeset/19d9ded8abdc/

Log:	add a modified version of _minimal_curses, get to the point of kind
	of running pythoni on python3

diff --git a/pyrepl/_minimal_curses.py b/pyrepl/_minimal_curses.py
new file mode 100644
--- /dev/null
+++ b/pyrepl/_minimal_curses.py
@@ -0,0 +1,69 @@
+"""Minimal '_curses' module, the low-level interface for curses module
+which is not meant to be used directly.
+
+Based on ctypes.  It's too incomplete to be really called '_curses', so
+to use it, you have to import it and stick it in sys.modules['_curses']
+manually.
+
+Note that there is also a built-in module _minimal_curses which will
+hide this one if compiled in.
+"""
+
+import ctypes, ctypes.util
+
+class error(Exception):
+    pass
+
+
+def _find_clib():
+    trylibs = ['ncurses', 'curses']
+
+    for lib in trylibs:
+        path = ctypes.util.find_library(lib)
+        if path:
+            return path
+    raise ImportError("curses library not found")
+
+_clibpath = _find_clib()
+clib = ctypes.cdll.LoadLibrary(_clibpath)
+
+clib.setupterm.argtypes = [ctypes.c_char_p, ctypes.c_int,
+                           ctypes.POINTER(ctypes.c_int)]
+clib.setupterm.restype = ctypes.c_int
+
+clib.tigetstr.argtypes = [ctypes.c_char_p]
+clib.tigetstr.restype = ctypes.POINTER(ctypes.c_char)
+
+clib.tparm.argtypes = [ctypes.c_char_p] + 9 * [ctypes.c_int]
+clib.tparm.restype = ctypes.c_char_p
+
+OK = 0
+ERR = -1
+
+# ____________________________________________________________
+
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+ at builtinify
+def setupterm(termstr, fd):
+    err = ctypes.c_int(0)
+    result = clib.setupterm(termstr, fd, ctypes.byref(err))
+    if result == ERR:
+        raise error("setupterm() failed (err=%d)" % err.value)
+
+ at builtinify
+def tigetstr(cap):
+    if not isinstance(cap, bytes):
+        cap = cap.encode('ascii')
+    result = clib.tigetstr(cap)
+    if ctypes.cast(result, ctypes.c_void_p).value == ERR:
+        return None
+    return ctypes.cast(result, ctypes.c_char_p).value
+
+ at builtinify
+def tparm(str, i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0, i8=0, i9=0):
+    result = clib.tparm(str, i1, i2, i3, i4, i5, i6, i7, i8, i9)
+    if result is None:
+        raise error("tparm() returned NULL")
+    return result
diff --git a/pyrepl/curses.py b/pyrepl/curses.py
--- a/pyrepl/curses.py
+++ b/pyrepl/curses.py
@@ -19,21 +19,5 @@
 # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
-# Some try-import logic for two purposes: avoiding to bring in the whole
-# pure Python curses package if possible; and, in _curses is not actually
-# present, falling back to _minimal_curses (which is either a ctypes-based
-# pure Python module or a PyPy built-in module).
-try:
-    import _curses
-except ImportError:
-    try:
-        import _minimal_curses as _curses
-    except ImportError:
-        # Who knows, maybe some environment has "curses" but not "_curses".
-        # If not, at least the following import gives a clean ImportError.
-        import _curses
 
-setupterm = _curses.setupterm
-tigetstr = _curses.tigetstr
-tparm = _curses.tparm
-error = _curses.error
+from ._minimal_curses import setupterm, tigetstr, tparm, error
diff --git a/pyrepl/keymap.py b/pyrepl/keymap.py
--- a/pyrepl/keymap.py
+++ b/pyrepl/keymap.py
@@ -170,8 +170,9 @@
         r.extend(k)
     return r
 
-def compile_keymap(keymap, empty=''):
+def compile_keymap(keymap, empty=b''):
     r = {}
+    import pprint
     for key, value in keymap.items():
         r.setdefault(key[0], {})[key[1:]] = value
     for key, value in r.items():
diff --git a/pyrepl/python_reader.py b/pyrepl/python_reader.py
--- a/pyrepl/python_reader.py
+++ b/pyrepl/python_reader.py
@@ -32,6 +32,12 @@
     import cPickle as pickle
 except ImportError:
     import pickle
+
+try:
+    unicode
+except:
+    unicode = str
+
 try:
     import imp
     imp.find_module("twisted")
@@ -69,7 +75,7 @@
         text = r.get_unicode()
         try:
             # ooh, look at the hack:
-            code = r.compiler("#coding:utf-8\n"+text.encode('utf-8'))
+            code = r.compiler("#coding:utf-8\n"+text)
         except (OverflowError, SyntaxError, ValueError):
             self.finish = 1
         else:
@@ -188,8 +194,7 @@
     def execute(self, text):
         try:
             # ooh, look at the hack:            
-            code = self.compile("# coding:utf8\n"+text.encode('utf-8'),
-                                '<input>', 'single')
+            code = self.compile(text, '<input>', 'single')
         except (OverflowError, SyntaxError, ValueError):
             self.showsyntaxerror("<input>")
         else:
diff --git a/pyrepl/unix_console.py b/pyrepl/unix_console.py
--- a/pyrepl/unix_console.py
+++ b/pyrepl/unix_console.py
@@ -30,6 +30,7 @@
 class InvalidTerminal(RuntimeError):
     pass
 
+
 _error = (termios.error, curses.error, InvalidTerminal)
 
 # there are arguments for changing this to "refresh"
@@ -58,7 +59,7 @@
 
 del r, maybe_add_baudrate
 
-delayprog = re.compile("\\$<([0-9]+)((?:/|\\*){0,2})>")
+delayprog = re.compile(b"\\$<([0-9]+)((?:/|\\*){0,2})>")
 
 try:
     poll = select.poll
@@ -157,7 +158,7 @@
         self.__move = self.__move_short
 
         self.event_queue = unix_eventqueue.EventQueue(self.input_fd)
-        self.partial_char = ''
+        self.partial_char = b''
         self.cursor_visible = 1
 
     def change_encoding(self, encoding):
@@ -303,6 +304,7 @@
         self.__buffer.append((text, 0))
 
     def __write_code(self, fmt, *args):
+
         self.__buffer.append((curses.tparm(fmt, *args), 1))
 
     def __maybe_write_code(self, fmt, *args):
@@ -405,7 +407,7 @@
     def push_char(self, char):
         self.partial_char += char
         try:
-            c = unicode(self.partial_char, self.encoding)
+            c = self.partial_char.decode(self.encoding)
         except UnicodeError as e:
             if len(e.args) > 4 and \
                    e.args[4] == 'unexpected end of data':
@@ -413,7 +415,7 @@
             else:
                 raise
         else:
-            self.partial_char = ''
+            self.partial_char = b''
             self.event_queue.push(c)
         
     def get_event(self, block=1):
diff --git a/pyrepl/unix_eventqueue.py b/pyrepl/unix_eventqueue.py
--- a/pyrepl/unix_eventqueue.py
+++ b/pyrepl/unix_eventqueue.py
@@ -26,6 +26,11 @@
 from pyrepl import curses
 from termios import tcgetattr, VERASE
 import os
+try:
+    unicode
+except NameError:
+    unicode = str
+
 
 _keynames = {
     "delete" : "kdch1",
@@ -54,7 +59,7 @@
             if keycode:
                 our_keycodes[keycode] = unicode(key)
         if os.isatty(fd):
-            our_keycodes[tcgetattr(fd)[6][VERASE]] = u'backspace'
+            our_keycodes[tcgetattr(fd)[6][VERASE]] = unicode('backspace')
         self.k = self.ck = keymap.compile_keymap(our_keycodes)
         self.events = []
         self.buf = []


More information about the pypy-commit mailing list