From 123b9cd7ee996709eaa3a03ed19618c40b97eb0f 2013-10-29 16:15:54 From: Thomas Kluyver Date: 2013-10-29 16:15:54 Subject: [PATCH] Fixes for metaclass syntax --- diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index 49edc10..abb3cd2 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -36,7 +36,7 @@ from IPython.lib import pretty from IPython.utils.traitlets import ( Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, ) -from IPython.utils.py3compat import unicode_to_str +from IPython.utils.py3compat import unicode_to_str, with_metaclass #----------------------------------------------------------------------------- @@ -176,7 +176,7 @@ class DisplayFormatter(Configurable): #----------------------------------------------------------------------------- -class FormatterABC(object): +class FormatterABC(with_metaclass(abc.ABCMeta, object)): """ Abstract base class for Formatters. A formatter is a callable class that is responsible for computing the @@ -184,7 +184,6 @@ class FormatterABC(object): an HTML formatter would have a format type of `text/html` and would return the HTML representation of the object when called. """ - __metaclass__ = abc.ABCMeta # The format type of the data returned, usually a MIME type. format_type = 'text/plain' diff --git a/IPython/core/inputtransformer.py b/IPython/core/inputtransformer.py index 56fbb5d..c2b9647 100644 --- a/IPython/core/inputtransformer.py +++ b/IPython/core/inputtransformer.py @@ -6,6 +6,7 @@ from io import StringIO from IPython.core.splitinput import LineInfo from IPython.utils import tokenize2 from IPython.utils.openpy import cookie_comment_re +from IPython.utils.py3compat import with_metaclass from IPython.utils.tokenize2 import generate_tokens, untokenize, TokenError #----------------------------------------------------------------------------- @@ -33,9 +34,8 @@ ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\ ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ] -class InputTransformer(object): +class InputTransformer(with_metaclass(abc.ABCMeta, object)): """Abstract base class for line-based input transformers.""" - __metaclass__ = abc.ABCMeta @abc.abstractmethod def push(self, line): diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index b5474cf..23072e8 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -68,7 +68,8 @@ from IPython.utils.ipstruct import Struct from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename from IPython.utils.pickleshare import PickleShareDB from IPython.utils.process import system, getoutput -from IPython.utils.py3compat import builtin_mod, unicode_type, string_types +from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, + with_metaclass) from IPython.utils.strdispatch import StrDispatch from IPython.utils.syspathcontext import prepended_to_syspath from IPython.utils.text import (format_screen, LSString, SList, @@ -3157,8 +3158,7 @@ class InteractiveShell(SingletonConfigurable): self.restore_sys_module_state() -class InteractiveShellABC(object): +class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): """An abstract base class for InteractiveShell.""" - __metaclass__ = abc.ABCMeta InteractiveShellABC.register(InteractiveShell) diff --git a/IPython/kernel/channelsabc.py b/IPython/kernel/channelsabc.py index 82302e7..b2c7a03 100644 --- a/IPython/kernel/channelsabc.py +++ b/IPython/kernel/channelsabc.py @@ -11,19 +11,18 @@ # Imports #----------------------------------------------------------------------------- -# Standard library imports import abc +from IPython.utils.py3compat import with_metaclass + #----------------------------------------------------------------------------- # Channels #----------------------------------------------------------------------------- -class ChannelABC(object): +class ChannelABC(with_metaclass(abc.ABCMeta, object)): """A base class for all channel ABCs.""" - __metaclass__ = abc.ABCMeta - @abc.abstractmethod def start(self): pass diff --git a/IPython/kernel/clientabc.py b/IPython/kernel/clientabc.py index a4c9580..2b22351 100644 --- a/IPython/kernel/clientabc.py +++ b/IPython/kernel/clientabc.py @@ -11,14 +11,15 @@ # Imports #----------------------------------------------------------------------------- -# Standard library imports import abc +from IPython.utils.py3compat import with_metaclass + #----------------------------------------------------------------------------- # Main kernel client class #----------------------------------------------------------------------------- -class KernelClientABC(object): +class KernelClientABC(with_metaclass(abc.ABCMeta, object)): """KernelManager ABC. The docstrings for this class can be found in the base implementation: @@ -26,8 +27,6 @@ class KernelClientABC(object): `IPython.kernel.client.KernelClient` """ - __metaclass__ = abc.ABCMeta - @abc.abstractproperty def kernel(self): pass diff --git a/IPython/kernel/inprocess/socket.py b/IPython/kernel/inprocess/socket.py index 6196829..d654e52 100644 --- a/IPython/kernel/inprocess/socket.py +++ b/IPython/kernel/inprocess/socket.py @@ -23,14 +23,13 @@ import zmq # Local imports. from IPython.utils.traitlets import HasTraits, Instance, Int +from IPython.utils.py3compat import with_metaclass #----------------------------------------------------------------------------- # Generic socket interface #----------------------------------------------------------------------------- -class SocketABC(object): - __metaclass__ = abc.ABCMeta - +class SocketABC(with_metaclass(abc.ABCMeta, object)): @abc.abstractmethod def recv_multipart(self, flags=0, copy=True, track=False): raise NotImplementedError diff --git a/IPython/kernel/managerabc.py b/IPython/kernel/managerabc.py index d668a2a..3596db1 100644 --- a/IPython/kernel/managerabc.py +++ b/IPython/kernel/managerabc.py @@ -11,19 +11,18 @@ # Imports #----------------------------------------------------------------------------- -# Standard library imports. import abc +from IPython.utils.py3compat import with_metaclass + #----------------------------------------------------------------------------- # Channels #----------------------------------------------------------------------------- -class ChannelABC(object): +class ChannelABC(with_metaclass(abc.ABCMeta, object)): """A base class for all channel ABCs.""" - __metaclass__ = abc.ABCMeta - @abc.abstractmethod def start(self): pass @@ -130,7 +129,7 @@ class HBChannelABC(ChannelABC): # Main kernel manager class #----------------------------------------------------------------------------- -class KernelManagerABC(object): +class KernelManagerABC(with_metaclass(abc.ABCMeta, object)): """KernelManager ABC. The docstrings for this class can be found in the base implementation: @@ -138,8 +137,6 @@ class KernelManagerABC(object): `IPython.kernel.kernelmanager.KernelManager` """ - __metaclass__ = abc.ABCMeta - @abc.abstractproperty def kernel(self): pass diff --git a/IPython/qt/console/console_widget.py b/IPython/qt/console/console_widget.py index c71d9e3..68a605c 100644 --- a/IPython/qt/console/console_widget.py +++ b/IPython/qt/console/console_widget.py @@ -21,6 +21,7 @@ from IPython.config.configurable import LoggingConfigurable from IPython.core.inputsplitter import ESC_SEQUENCES from IPython.qt.rich_text import HtmlExporter from IPython.qt.util import MetaQObjectHasTraits, get_font +from IPython.utils.py3compat import with_metaclass from IPython.utils.text import columnize from IPython.utils.traitlets import Bool, Enum, Integer, Unicode from .ansi_code_processor import QtAnsiCodeProcessor @@ -69,7 +70,7 @@ def is_letter_or_number(char): # Classes #----------------------------------------------------------------------------- -class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): +class ConsoleWidget(with_metaclass(MetaQObjectHasTraits, type('NewBase', (LoggingConfigurable, QtGui.QWidget), {}))): """ An abstract base class for console-type widgets. This class has functionality for: @@ -82,7 +83,6 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): ConsoleWidget also provides a number of utility methods that will be convenient to implementors of a console-style widget. """ - __metaclass__ = MetaQObjectHasTraits #------ Configuration ------------------------------------------------------ diff --git a/IPython/qt/kernel_mixins.py b/IPython/qt/kernel_mixins.py index 55c519e..0f3a09d 100644 --- a/IPython/qt/kernel_mixins.py +++ b/IPython/qt/kernel_mixins.py @@ -168,27 +168,22 @@ class QtHBChannelMixin(ChannelQObject): self.kernel_died.emit(since_last_heartbeat) -class QtKernelRestarterMixin(HasTraits, SuperQObject): +class QtKernelRestarterMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): - __metaclass__ = MetaQObjectHasTraits _timer = None -class QtKernelManagerMixin(HasTraits, SuperQObject): +class QtKernelManagerMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): """ A KernelClient that provides signals and slots. """ - __metaclass__ = MetaQObjectHasTraits - kernel_restarted = QtCore.Signal() -class QtKernelClientMixin(HasTraits, SuperQObject): +class QtKernelClientMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): """ A KernelClient that provides signals and slots. """ - __metaclass__ = MetaQObjectHasTraits - # Emitted when the kernel client has started listening. started_channels = QtCore.Signal() diff --git a/IPython/utils/py3compat.py b/IPython/utils/py3compat.py index 7620d76..190b518 100644 --- a/IPython/utils/py3compat.py +++ b/IPython/utils/py3compat.py @@ -208,3 +208,28 @@ else: else: filename = fname builtin_mod.execfile(filename, *where) + +# Parts below taken from six: +# Copyright (c) 2010-2013 Benjamin Peterson +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + return meta("NewBase", bases, {}) diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 92dea8b..40d663e 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -394,9 +394,7 @@ class MetaHasTraits(type): v.this_class = cls super(MetaHasTraits, cls).__init__(name, bases, classdict) -class HasTraits(object): - - __metaclass__ = MetaHasTraits +class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)): def __new__(cls, *args, **kw): # This is needed because in Python 2.6 object.__new__ only accepts