[pypy-svn] r39661 - in pypy/dist/pypy/module: __builtin__ readline

arigo at codespeak.net arigo at codespeak.net
Fri Mar 2 12:50:56 CET 2007


Author: arigo
Date: Fri Mar  2 12:50:52 2007
New Revision: 39661

Modified:
   pypy/dist/pypy/module/__builtin__/app_io.py
   pypy/dist/pypy/module/readline/__init__.py
   pypy/dist/pypy/module/readline/c_readline.py
Log:
(hpk, arigo)

Link the readline module's readline() function with raw_input().
Should give basic (no-completion) readline support in a translated
pypy-c.


Modified: pypy/dist/pypy/module/__builtin__/app_io.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/app_io.py	(original)
+++ pypy/dist/pypy/module/__builtin__/app_io.py	Fri Mar  2 12:50:52 2007
@@ -35,22 +35,31 @@
 On Unix, GNU readline is used if enabled.  The prompt string, if given,
 is printed without a trailing newline before reading."""
     try:
-        sys.stdin
+        stdin = sys.stdin
     except AttributeError:
-        raise RuntimeError("[raw_]input: lost sys.stdin");
+        raise RuntimeError("[raw_]input: lost sys.stdin")
     try:
-        sys.stdout
+        stdout = sys.stdout
     except AttributeError:
-        raise RuntimeError("[raw_]input: lost sys.stdout");
+        raise RuntimeError("[raw_]input: lost sys.stdout")
+
+    # hook for the readline module
+    if (hasattr(sys, '__raw_input__') and
+        isinstance(stdin, file)  and stdin.fileno() == 0 and stdin.isatty() and
+        isinstance(stdout, file) and stdout.fileno() == 1):
+        if prompt is None:
+            prompt = ''
+        return sys.__raw_input__(prompt)
+
     if prompt is not None:
-        sys.stdout.write(prompt)
+        stdout.write(prompt)
         try:
-            flush = sys.stdout.flush
+            flush = stdout.flush
         except AttributeError:
             pass
         else:
             flush()
-    line = sys.stdin.readline()
+    line = stdin.readline()
     if not line:    # inputting an empty line gives line == '\n'
         raise EOFError
     if line[-1] == '\n':

Modified: pypy/dist/pypy/module/readline/__init__.py
==============================================================================
--- pypy/dist/pypy/module/readline/__init__.py	(original)
+++ pypy/dist/pypy/module/readline/__init__.py	Fri Mar  2 12:50:52 2007
@@ -10,9 +10,9 @@
     """Importing this module enables command line editing using GNU readline."""
     # the above line is the doc string of the translated module  
 
-    def init(self, space):
+    def setup_after_space_initialization(self):
         from pypy.module.readline import c_readline 
-        c_readline.setup_readline(space, self)
+        c_readline.setup_readline(self.space, self)
 
     interpleveldefs = {
         'readline'    : 'interp_readline.readline',

Modified: pypy/dist/pypy/module/readline/c_readline.py
==============================================================================
--- pypy/dist/pypy/module/readline/c_readline.py	(original)
+++ pypy/dist/pypy/module/readline/c_readline.py	Fri Mar  2 12:50:52 2007
@@ -1,5 +1,7 @@
 from ctypes import *
 from pypy.rpython.rctypes.tool.ctypes_platform import configure, Library
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import ObjSpace, interp2app
 
 #------------------------------------------------------------
 # configuration for binding to external readline library 
@@ -30,9 +32,15 @@
 # special initialization of readline 
 
 def setup_readline(space, w_module):
-    # XXX ... 
     c_rl_initialize()
-    space.readline_func = readline_func
-
-def readline_func(s):
-    return c_readline(s)
+    # install sys.__raw_input__, a hook that will be used by raw_input()
+    space.setitem(space.sys.w_dict, space.wrap('__raw_input__'),
+                  space.wrap(app_readline_func))
+
+def readline_func(space, prompt):
+    res = c_readline(prompt)
+    if res is None:
+        raise OperationError(space.w_EOFError, space.w_None)
+    return space.wrap(res)
+readline_func.unwrap_spec = [ObjSpace, str]
+app_readline_func = interp2app(readline_func)



More information about the Pypy-commit mailing list