From 801682c78660ab4e92a6034b4ac4a0412e663efe Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sun, 2 Mar 2014 07:48:20 +0000 Subject: [PATCH] Some Py3 fixes for PyCrust git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@76040 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- buildtools/config.py | 6 ++---- wx/py/PyAlaCarte.py | 1 - wx/py/PyAlaMode.py | 1 - wx/py/PyAlaModeTest.py | 1 - wx/py/PyCrust.py | 4 ++-- wx/py/PyFilling.py | 13 +++++++------ wx/py/PyShell.py | 4 ++-- wx/py/PySlices.py | 4 ++-- wx/py/PySlicesShell.py | 4 ++-- wx/py/PyWrap.py | 1 - wx/py/editor.py | 3 +-- wx/py/filling.py | 1 - wx/py/images.py | 4 ++-- wx/py/introspect.py | 10 +++++----- wx/py/shell.py | 8 +++++++- wx/py/sliceshell.py | 27 +++++++++++++++++++-------- 16 files changed, 51 insertions(+), 41 deletions(-) diff --git a/buildtools/config.py b/buildtools/config.py index 702ea996..b5f0304e 100644 --- a/buildtools/config.py +++ b/buildtools/config.py @@ -816,10 +816,8 @@ def myExecfile(filename, ns): if sys.version_info < (3,): execfile(filename, ns) else: - f = open(filename, 'r') - source = f.read() - f.close() - exec(source, ns) + with open(filename, 'r') as f: + exec(f.read(), ns) def textfile_open(filename, mode='rt'): diff --git a/wx/py/PyAlaCarte.py b/wx/py/PyAlaCarte.py index 07859308..931e0c48 100644 --- a/wx/py/PyAlaCarte.py +++ b/wx/py/PyAlaCarte.py @@ -17,7 +17,6 @@ class App(wx.App): wx.App.__init__(self, redirect=False) def OnInit(self): - wx.InitAllImageHandlers() self.frame = py.editor.EditorFrame(filename=self.filename) self.frame.Show() self.SetTopWindow(self.frame) diff --git a/wx/py/PyAlaMode.py b/wx/py/PyAlaMode.py index a40a3bcc..57e080a9 100644 --- a/wx/py/PyAlaMode.py +++ b/wx/py/PyAlaMode.py @@ -17,7 +17,6 @@ class App(wx.App): wx.App.__init__(self, redirect=False) def OnInit(self): - wx.InitAllImageHandlers() self.frame = py.editor.EditorNotebookFrame(filename=self.filename) self.frame.Show() self.SetTopWindow(self.frame) diff --git a/wx/py/PyAlaModeTest.py b/wx/py/PyAlaModeTest.py index 2f9a47aa..9137065e 100644 --- a/wx/py/PyAlaModeTest.py +++ b/wx/py/PyAlaModeTest.py @@ -17,7 +17,6 @@ class App(wx.App): wx.App.__init__(self, redirect=False) def OnInit(self): - wx.InitAllImageHandlers() self.frame = py.editor.EditorShellNotebookFrame(filename=self.filename) self.frame.Show() self.SetTopWindow(self.frame) diff --git a/wx/py/PyCrust.py b/wx/py/PyCrust.py index 65cb1ea8..8fa5a799 100644 --- a/wx/py/PyCrust.py +++ b/wx/py/PyCrust.py @@ -6,7 +6,7 @@ # main namespace to look as much as possible like the regular Python # shell environment. import __main__ -original = __main__.__dict__.keys() +original = list(__main__.__dict__.keys()) __author__ = "Patrick K. O'Brien " @@ -53,7 +53,7 @@ def main(): md = __main__.__dict__ keepers = original keepers.append('App') - for key in md.keys(): + for key in list(md.keys()): if key not in keepers: del md[key] # Create an application instance. diff --git a/wx/py/PyFilling.py b/wx/py/PyFilling.py index b9d188bd..c869e2bb 100644 --- a/wx/py/PyFilling.py +++ b/wx/py/PyFilling.py @@ -6,14 +6,15 @@ __author__ = "Patrick K. O'Brien " # We use this object to get more introspection when run standalone. app = None -import filling +from . import filling # These are imported just to have something interesting to inspect. -import crust -import interpreter -import introspect -import pseudo -import shell +from . import crust +from . import interpreter +from . import introspect +from . import pseudo +from . import shell + import sys import wx diff --git a/wx/py/PyShell.py b/wx/py/PyShell.py index d3868698..c42152a9 100644 --- a/wx/py/PyShell.py +++ b/wx/py/PyShell.py @@ -6,7 +6,7 @@ # main namespace to look as much as possible like the regular Python # shell environment. import __main__ -original = __main__.__dict__.keys() +original = list(__main__.__dict__.keys()) __author__ = "Patrick K. O'Brien " @@ -51,7 +51,7 @@ def main(): md = __main__.__dict__ keepers = original keepers.append('App') - for key in md.keys(): + for key in list(md.keys()): if key not in keepers: del md[key] # Create an application instance. diff --git a/wx/py/PySlices.py b/wx/py/PySlices.py index 8a748bcd..796f6dcd 100644 --- a/wx/py/PySlices.py +++ b/wx/py/PySlices.py @@ -6,7 +6,7 @@ # main namespace to look as much as possible like the regular Python # shell environment. import __main__ -original = __main__.__dict__.keys() +original = list(__main__.__dict__.keys()) __author__ = "Patrick K. O'Brien / " __author__ += "David N. Mashburn " @@ -69,7 +69,7 @@ def main(filename=None): keepers = original keepers.append('App') keepers.append('filename') - for key in md.keys(): + for key in list(md.keys()): if key not in keepers: del md[key] # Create an application instance. diff --git a/wx/py/PySlicesShell.py b/wx/py/PySlicesShell.py index a292f5a5..6f68d2c7 100644 --- a/wx/py/PySlicesShell.py +++ b/wx/py/PySlicesShell.py @@ -6,7 +6,7 @@ # main namespace to look as much as possible like the regular Python # shell environment. import __main__ -original = __main__.__dict__.keys() +original = list(__main__.__dict__.keys()) __author__ = "Patrick K. O'Brien " @@ -65,7 +65,7 @@ def main(filename=None): keepers = original keepers.append('App') keepers.append('filename') - for key in md.keys(): + for key in list(md.keys()): if key not in keepers: del md[key] # Create an application instance. diff --git a/wx/py/PyWrap.py b/wx/py/PyWrap.py index e237300d..5496266e 100644 --- a/wx/py/PyWrap.py +++ b/wx/py/PyWrap.py @@ -17,7 +17,6 @@ import os import sys def wrap(app): - wx.InitAllImageHandlers() frame = py.crust.CrustFrame() frame.SetSize((750, 525)) frame.Show(True) diff --git a/wx/py/editor.py b/wx/py/editor.py index f290349a..b6b85f8c 100644 --- a/wx/py/editor.py +++ b/wx/py/editor.py @@ -276,8 +276,7 @@ class EditorNotebookFrame(EditorFrame): intro = 'Py %s' % version.VERSION import imp module = imp.new_module('__main__') - import __builtin__ - module.__dict__['__builtins__'] = __builtin__ + module.__dict__['__builtins__'] = __builtins__ namespace = module.__dict__.copy() self.crust = crust.Crust(parent=self.notebook, intro=intro, locals=namespace) self.shell = self.crust.shell diff --git a/wx/py/filling.py b/wx/py/filling.py index d1786976..447afc00 100644 --- a/wx/py/filling.py +++ b/wx/py/filling.py @@ -344,7 +344,6 @@ class App(wx.App): """PyFilling standalone application.""" def OnInit(self): - wx.InitAllImageHandlers() self.fillingFrame = FillingFrame() self.fillingFrame.Show(True) self.SetTopWindow(self.fillingFrame) diff --git a/wx/py/images.py b/wx/py/images.py index 9aeb0e35..6a906f13 100644 --- a/wx/py/images.py +++ b/wx/py/images.py @@ -3,7 +3,7 @@ __author__ = "Patrick K. O'Brien / David Mashburn " import wx -from wx.lib.six import StringIO +from wx.lib.six import BytesIO def getPyIcon(shellName='PyCrust'): icon = wx.Icon() @@ -14,7 +14,7 @@ def getPyBitmap(shellName='PyCrust'): return wx.Bitmap(getPyImage(shellName)) def getPyImage(shellName='PyCrust'): - stream = StringIO(getPyData(shellName)) + stream = BytesIO(getPyData(shellName)) return wx.Image(stream) def getPyData(shellName='PyCrust'): diff --git a/wx/py/introspect.py b/wx/py/introspect.py index 23100fc3..1a697fcb 100644 --- a/wx/py/introspect.py +++ b/wx/py/introspect.py @@ -8,7 +8,7 @@ import inspect import tokenize import types import wx -from wx.lib.six import StringIO, PY3 +from wx.lib.six import BytesIO, PY3 def getAutoCompleteList(command='', locals=None, includeMagic=1, includeSingle=1, includeDouble=1): @@ -80,7 +80,7 @@ def getAttributeNames(object, includeMagic=1, includeSingle=1, # e.g. ITK http://www.itk.org/ attributes = [attribute for attribute in attributes \ if type(attribute) == str] - attributes.sort(lambda x, y: cmp(x.upper(), y.upper())) + attributes.sort(key=lambda x: x.upper()) if not includeSingle: attributes = filter(lambda item: item[0]!='_' \ or item[1:2]=='_', attributes) @@ -139,9 +139,9 @@ def getAllAttributeNames(object): except: # Must catch all because object might have __getattr__. pass else: - if isinstance(bases, types.TupleType): + if isinstance(bases, tuple): for base in bases: - if type(base) is types.TypeType: + if type(base) is type: # Break a circular reference. Happens in Python 2.2. pass else: @@ -304,7 +304,7 @@ def getTokens(command): except UnicodeEncodeError: pass # otherwise leave it alone - f = StringIO(command) + f = BytesIO(command) # tokens is a list of token tuples, each looking like: # (type, string, (srow, scol), (erow, ecol), line) tokens = [] diff --git a/wx/py/shell.py b/wx/py/shell.py index 5f450b83..994fdb48 100644 --- a/wx/py/shell.py +++ b/wx/py/shell.py @@ -8,6 +8,7 @@ __author__ = "Patrick K. O'Brien " import wx from wx import stc +from wx.lib.six import PY3 import keyword import os @@ -409,7 +410,12 @@ class Shell(editwindow.EditWindow): """Execute the user's PYTHONSTARTUP script if they have one.""" if startupScript and os.path.isfile(startupScript): text = 'Startup script executed: ' + startupScript - self.push('print(%r); execfile(%r)' % (text, startupScript)) + if PY3: + self.push('print(%r)' % text) + self.push('with open(%r, "r") as f:\n' + ' exec(f.read())\n' % (startupScript)) + else: + self.push('print(%r); execfile(%r)' % (text, startupScript)) self.interp.startupScript = startupScript else: self.push('') diff --git a/wx/py/sliceshell.py b/wx/py/sliceshell.py index cd3a64a6..693be14d 100644 --- a/wx/py/sliceshell.py +++ b/wx/py/sliceshell.py @@ -15,6 +15,7 @@ __author__ += "Patrick K. O'Brien " import wx from wx import stc +from wx.lib.six import PY3 import keyword import os @@ -622,7 +623,7 @@ class SlicesShell(editwindow.EditWindow): # Import a default interpreter class if one isn't provided. if InterpClass == None: - from interpreter import Interpreter + from .interpreter import Interpreter else: Interpreter = InterpClass @@ -963,13 +964,18 @@ class SlicesShell(editwindow.EditWindow): This sets "close", "exit" and "quit" to a helpful string. """ - import __builtin__ - __builtin__.close = __builtin__.exit = __builtin__.quit = \ + from wx.lib.six import PY3 + if PY3: + import builtins + else: + import __builtin__ + builtins = __builtin__ + builtins.close = builtins.exit = builtins.quit = \ 'Click on the close button to leave the application.' - __builtin__.cd = cd - __builtin__.ls = ls - __builtin__.pwd = pwd - __builtin__.sx = sx + builtins.cd = cd + builtins.ls = ls + builtins.pwd = pwd + builtins.sx = sx def quit(self): @@ -992,7 +998,12 @@ class SlicesShell(editwindow.EditWindow): """Execute the user's PYTHONSTARTUP script if they have one.""" if startupScript and os.path.isfile(startupScript): text = 'Startup script executed: ' + startupScript - self.push('print(%r); execfile(%r)' % (text, startupScript)) + if PY3: + self.push('print(%r)' % text) + self.push('with open(%r, "r") as f:\n' + ' exec(f.read())\n' % (startupScript)) + else: + self.push('print(%r); execfile(%r)' % (text, startupScript)) self.interp.startupScript = startupScript else: self.push('')