[pyrepl-checkins] r53568 - pyrepl/trunk/pyrepl/pyrepl

arigo at codespeak.net arigo at codespeak.net
Tue Apr 8 12:23:10 CEST 2008


Author: arigo
Date: Tue Apr  8 12:23:09 2008
New Revision: 53568

Added:
   pyrepl/trunk/pyrepl/pyrepl/readline.py
      - copied, changed from r53567, pypy/dist/pypy/lib/readline.py
   pyrepl/trunk/pyrepl/pyrepl/simple_interact.py   (contents, props changed)
Log:
Add these modules directly into pyrepl, where they can be
reused to give the same CPython-like-but-multiline prompt
on top of CPython.


Copied: pyrepl/trunk/pyrepl/pyrepl/readline.py (from r53567, pypy/dist/pypy/lib/readline.py)
==============================================================================
--- pypy/dist/pypy/lib/readline.py	(original)
+++ pyrepl/trunk/pyrepl/pyrepl/readline.py	Tue Apr  8 12:23:09 2008
@@ -1,5 +1,6 @@
 """A compatibility wrapper reimplementing the 'readline' standard module
-on top of pyrepl.  Not all functionalities are supported.
+on top of pyrepl.  Not all functionalities are supported.  Contains
+extensions for multiline input.
 """
 
 import sys, os
@@ -29,7 +30,10 @@
            'set_history_length',
            'set_pre_input_hook',
            'set_startup_hook',
-           'write_history_file']
+           'write_history_file',
+           # ---- multiline extensions ----
+           'multiline_input',
+           ]
 
 # ____________________________________________________________
 

Added: pyrepl/trunk/pyrepl/pyrepl/simple_interact.py
==============================================================================
--- (empty file)
+++ pyrepl/trunk/pyrepl/pyrepl/simple_interact.py	Tue Apr  8 12:23:09 2008
@@ -0,0 +1,42 @@
+"""This is an alternative to python_reader which tries to emulate
+the CPython prompt as closely as possible, with the exception of
+allowing multiline input and multiline history entries.
+"""
+
+import sys
+from pyrepl.readline import multiline_input
+
+
+def run_multiline_interactive_console(mainmodule):
+    import code
+    if mainmodule is None:
+        import __main__ as mainmodule
+    console = code.InteractiveConsole(mainmodule.__dict__)
+
+    def more_lines(unicodetext):
+        # ooh, look at the hack:
+        src = "#coding:utf-8\n"+unicodetext.encode('utf-8')
+        try:
+            code = console.compile(src, '<input>', 'single')
+        except (OverflowError, SyntaxError, ValueError):
+            return False
+        else:
+            return code is None
+
+    while 1:
+        try:
+            ps1 = getattr(sys, 'ps1', '>>> ')
+            ps2 = getattr(sys, 'ps2', '... ')
+            try:
+                statement = multiline_input(more_lines, ps1, ps2)
+            except EOFError:
+                break
+            # XXX with Alt-Enter we can actually enter more than one
+            # statement, and compile() ignores everything after the
+            # first statement in 'single' mode...  We should either
+            # find some obscure workaround or tweak PyPy's compiler.
+            more = console.push(statement)
+            assert not more
+        except KeyboardInterrupt:
+            console.write("\nKeyboardInterrupt\n")
+            console.resetbuffer()


More information about the pyrepl-checkins mailing list