diff --git a/pyreadline/get_doc.py b/pyreadline/get_doc.py index 515fbe7..e6af010 100644 --- a/pyreadline/get_doc.py +++ b/pyreadline/get_doc.py @@ -1,7 +1,8 @@ from __future__ import print_function, unicode_literals, absolute_import import sys, textwrap +from .py3k_compat import callable -rlmain = sys.modules["pyreadline.rlmain"] +rlmain = sys.modules["readline"] rl = rlmain.rl def get_doc(rl): diff --git a/pyreadline/logserver.py b/pyreadline/logserver.py index e9813d3..06088c9 100644 --- a/pyreadline/logserver.py +++ b/pyreadline/logserver.py @@ -6,17 +6,16 @@ # the file COPYING, distributed as part of this software. #***************************************************************************** from __future__ import print_function, unicode_literals, absolute_import -import cPickle + import logging import logging.handlers -import SocketServer import struct, socket - +from .unicode_helper import ensure_unicode try: import msvcrt except ImportError: msvcrt = None - print "problem" + print("problem") port = logging.handlers.DEFAULT_TCP_LOGGING_PORT @@ -26,8 +25,8 @@ def check_key(): if msvcrt is None: return False else: - if msvcrt.kbhit() != 0: - q = msvcrt.getch() + if msvcrt.kbhit(): + q = ensure_unicode(msvcrt.getch()) return q return "" @@ -35,9 +34,9 @@ def check_key(): singleline=False def main(): - print "Starting TCP logserver on port:", port - print "Press q to quit logserver", port - print "Press c to clear screen", port + print("Starting TCP logserver on port:", port) + print("Press q to quit logserver", port) + print("Press c to clear screen", port) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(("", port)) @@ -45,14 +44,14 @@ def main(): while 1: try: data, addr = s.recvfrom(100000) - print data, + print(data, end="") except socket.timeout: key = check_key().lower() if "q" == key: - print "Quitting logserver" + print("Quitting logserver") break elif "c" == key: - print "\n" * 100 + print("\n" * 100) if __name__ == "__main__": main() \ No newline at end of file diff --git a/pyreadline/py3k_compat.py b/pyreadline/py3k_compat.py new file mode 100644 index 0000000..94afeba --- /dev/null +++ b/pyreadline/py3k_compat.py @@ -0,0 +1,22 @@ +from __future__ import print_function, unicode_literals, absolute_import +import sys + +if sys.version_info[0] >= 3: + import collections + PY3 = True + def callable(x): + return isinstance(x, collections.Callable) + + def execfile(fname, glob, loc=None): + loc = loc if (loc is not None) else glob + exec(compile(open(fname).read(), fname, 'exec'), glob, loc) + + unicode = str + bytes = bytes + +else: + PY3 = False + callable = callable + execfile = execfile + bytes = str + unicode = unicode diff --git a/pyreadline/rlmain.py b/pyreadline/rlmain.py index 403a105..60b9849 100644 --- a/pyreadline/rlmain.py +++ b/pyreadline/rlmain.py @@ -12,6 +12,7 @@ import sys,os,re,time from glob import glob from . import release +from .py3k_compat import callable, execfile import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history @@ -404,7 +405,7 @@ class BaseReadline(object): if os.path.isfile(inputrcpath): try: execfile(inputrcpath, loc, loc) - except Exception,x: + except Exception as x: raise import traceback print("Error reading .pyinputrc", file=sys.stderr) diff --git a/pyreadline/unicode_helper.py b/pyreadline/unicode_helper.py index edf74f1..124c05c 100644 --- a/pyreadline/unicode_helper.py +++ b/pyreadline/unicode_helper.py @@ -5,9 +5,10 @@ # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #***************************************************************************** -from __future__ import print_function, unicode_literals, absolute_import import sys +from .py3k_compat import unicode, bytes + try: pyreadline_codepage = sys.stdout.encoding except AttributeError: @@ -32,6 +33,7 @@ def ensure_unicode(text): return text.decode("ascii", "replace") return text + def ensure_str(text): """Convert unicode to str using pyreadline_codepage""" if isinstance(text, unicode):