From aed049222da18578cf2549072389e8af571072d7 2012-07-17 11:58:13 From: Thomas Kluyver Date: 2012-07-17 11:58:13 Subject: [PATCH] Merge pull request #2140 from bfroehle/2to3_has_key 2to3: Apply `has_key` fixer. --- diff --git a/IPython/config/loader.py b/IPython/config/loader.py index 99ea72f..20499d7 100644 --- a/IPython/config/loader.py +++ b/IPython/config/loader.py @@ -82,7 +82,7 @@ class Config(dict): def _merge(self, other): to_update = {} for k, v in other.iteritems(): - if not self.has_key(k): + if k not in self: to_update[k] = v else: # I have this key if isinstance(v, Config): diff --git a/IPython/config/tests/test_loader.py b/IPython/config/tests/test_loader.py index 9e475d2..9b23a18 100644 --- a/IPython/config/tests/test_loader.py +++ b/IPython/config/tests/test_loader.py @@ -204,11 +204,11 @@ class TestConfig(TestCase): c = Config() c.a = 10 self.assertEquals(c.a, 10) - self.assertEquals(c.has_key('b'), False) + self.assertEquals('b' in c, False) def test_auto_section(self): c = Config() - self.assertEquals(c.has_key('A'), True) + self.assertEquals('A' in c, True) self.assertEquals(c._has_section('A'), False) A = c.A A.foo = 'hi there' diff --git a/IPython/core/alias.py b/IPython/core/alias.py index 3a93318..1598fc1 100644 --- a/IPython/core/alias.py +++ b/IPython/core/alias.py @@ -164,7 +164,7 @@ class AliasManager(Configurable): self.alias_table[name] = (nargs, cmd) def undefine_alias(self, name): - if self.alias_table.has_key(name): + if name in self.alias_table: del self.alias_table[name] def validate_alias(self, name, cmd): diff --git a/IPython/core/displaypub.py b/IPython/core/displaypub.py index 388757e..997532b 100644 --- a/IPython/core/displaypub.py +++ b/IPython/core/displaypub.py @@ -102,7 +102,7 @@ class DisplayPublisher(Configurable): """ # The default is to simply write the plain text data using io.stdout. - if data.has_key('text/plain'): + if 'text/plain' in data: print(data['text/plain'], file=io.stdout) def clear_output(self, stdout=True, stderr=True, other=True): diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 57aa162..5414f4f 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -228,7 +228,7 @@ class CodeMagics(Magics): if opts_prev: args = '_%s' % last_call[0] - if not shell.user_ns.has_key(args): + if args not in shell.user_ns: args = last_call[1] # use last_call to remember the state of the previous call, but don't diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index d89f17f..2b38eaa 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -263,7 +263,7 @@ python-profiler package from non-free.""") print '\n*** Profile printout saved to text file',\ repr(text_file)+'.',sys_exit - if opts.has_key('r'): + if 'r' in opts: return stats else: return None diff --git a/IPython/core/magics/namespace.py b/IPython/core/magics/namespace.py index a29b133..d689ebc 100644 --- a/IPython/core/magics/namespace.py +++ b/IPython/core/magics/namespace.py @@ -214,9 +214,9 @@ class NamespaceMagics(Magics): psearch = shell.inspector.psearch # select case options - if opts.has_key('i'): + if 'i' in opts: ignore_case = True - elif opts.has_key('c'): + elif 'c' in opts: ignore_case = False else: ignore_case = not shell.wildcards_case_sensitive @@ -656,7 +656,7 @@ class NamespaceMagics(Magics): opts, regex = self.parse_options(parameter_s,'f') - if opts.has_key('f'): + if 'f' in opts: ans = True else: try: diff --git a/IPython/core/plugin.py b/IPython/core/plugin.py index 204994b..4520b39 100644 --- a/IPython/core/plugin.py +++ b/IPython/core/plugin.py @@ -35,7 +35,7 @@ class PluginManager(Configurable): def register_plugin(self, name, plugin): if not isinstance(plugin, Plugin): raise TypeError('Expected Plugin, got: %r' % plugin) - if self.plugins.has_key(name): + if name in self.plugins: raise KeyError('Plugin with name already exists: %r' % name) self.plugins[name] = plugin diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 49dfb0e..3824e90 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -892,7 +892,7 @@ class VerboseTB(TBTools): for name_full in unique_names: name_base = name_full.split('.',1)[0] if name_base in frame.f_code.co_varnames: - if locals.has_key(name_base): + if name_base in locals: try: value = repr(eval(name_full,locals)) except: @@ -901,7 +901,7 @@ class VerboseTB(TBTools): value = undefined name = tpl_local_var % name_full else: - if frame.f_globals.has_key(name_base): + if name_base in frame.f_globals: try: value = repr(eval(name_full,frame.f_globals)) except: diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py index a30e1e2..6402e86 100644 --- a/IPython/extensions/storemagic.py +++ b/IPython/extensions/storemagic.py @@ -119,7 +119,7 @@ class StoreMagics(Magics): ip = self.shell db = ip.db # delete - if opts.has_key('d'): + if 'd' in opts: try: todel = args[0] except IndexError: @@ -130,11 +130,11 @@ class StoreMagics(Magics): except: raise UsageError("Can't delete variable '%s'" % todel) # reset - elif opts.has_key('z'): + elif 'z' in opts: for k in db.keys('autorestore/*'): del db[k] - elif opts.has_key('r'): + elif 'r' in opts: refresh_variables(ip) diff --git a/IPython/external/pexpect/_pexpect.py b/IPython/external/pexpect/_pexpect.py index 6e9d9a7..4ed7c30 100644 --- a/IPython/external/pexpect/_pexpect.py +++ b/IPython/external/pexpect/_pexpect.py @@ -1832,7 +1832,7 @@ def which (filename): if os.access (filename, os.X_OK): return filename - if not os.environ.has_key('PATH') or os.environ['PATH'] == '': + if 'PATH' not in os.environ or os.environ['PATH'] == '': p = os.defpath else: p = os.environ['PATH'] diff --git a/IPython/frontend/qt/console/ipython_widget.py b/IPython/frontend/qt/console/ipython_widget.py index 5d83e1a..46dcd7f 100644 --- a/IPython/frontend/qt/console/ipython_widget.py +++ b/IPython/frontend/qt/console/ipython_widget.py @@ -216,13 +216,13 @@ class IPythonWidget(FrontendWidget): content = msg['content'] prompt_number = content.get('execution_count', 0) data = content['data'] - if data.has_key('text/html'): + if 'text/html' in data: self._append_plain_text(self.output_sep, True) self._append_html(self._make_out_prompt(prompt_number), True) html = data['text/html'] self._append_plain_text('\n', True) self._append_html(html + self.output_sep2, True) - elif data.has_key('text/plain'): + elif 'text/plain' in data: self._append_plain_text(self.output_sep, True) self._append_html(self._make_out_prompt(prompt_number), True) text = data['text/plain'] @@ -245,10 +245,10 @@ class IPythonWidget(FrontendWidget): metadata = msg['content']['metadata'] # In the regular IPythonWidget, we simply print the plain text # representation. - if data.has_key('text/html'): + if 'text/html' in data: html = data['text/html'] self._append_html(html, True) - elif data.has_key('text/plain'): + elif 'text/plain' in data: text = data['text/plain'] self._append_plain_text(text, True) # This newline seems to be needed for text and html output. diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index 06172d9..ca5e620 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -117,15 +117,15 @@ class RichIPythonWidget(IPythonWidget): content = msg['content'] prompt_number = content.get('execution_count', 0) data = content['data'] - if data.has_key('image/svg+xml'): + if 'image/svg+xml' in data: self._pre_image_append(msg, prompt_number) self._append_svg(data['image/svg+xml'], True) self._append_html(self.output_sep2, True) - elif data.has_key('image/png'): + elif 'image/png' in data: self._pre_image_append(msg, prompt_number) self._append_png(decodestring(data['image/png'].encode('ascii')), True) self._append_html(self.output_sep2, True) - elif data.has_key('image/jpeg') and self._jpg_supported: + elif 'image/jpeg' in data and self._jpg_supported: self._pre_image_append(msg, prompt_number) self._append_jpg(decodestring(data['image/jpeg'].encode('ascii')), True) self._append_html(self.output_sep2, True) @@ -142,17 +142,17 @@ class RichIPythonWidget(IPythonWidget): metadata = msg['content']['metadata'] # Try to use the svg or html representations. # FIXME: Is this the right ordering of things to try? - if data.has_key('image/svg+xml'): + if 'image/svg+xml' in data: self.log.debug("display: %s", msg.get('content', '')) svg = data['image/svg+xml'] self._append_svg(svg, True) - elif data.has_key('image/png'): + elif 'image/png' in data: self.log.debug("display: %s", msg.get('content', '')) # PNG data is base64 encoded as it passes over the network # in a JSON structure so we decode it. png = decodestring(data['image/png'].encode('ascii')) self._append_png(png, True) - elif data.has_key('image/jpeg') and self._jpg_supported: + elif 'image/jpeg' in data and self._jpg_supported: self.log.debug("display: %s", msg.get('content', '')) jpg = decodestring(data['image/jpeg'].encode('ascii')) self._append_jpg(jpg, True) diff --git a/IPython/lib/guisupport.py b/IPython/lib/guisupport.py index cdcfaf0..e2fc107 100644 --- a/IPython/lib/guisupport.py +++ b/IPython/lib/guisupport.py @@ -77,7 +77,7 @@ def get_app_wx(*args, **kwargs): import wx app = wx.GetApp() if app is None: - if not kwargs.has_key('redirect'): + if 'redirect' not in kwargs: kwargs['redirect'] = False app = wx.PySimpleApp(*args, **kwargs) return app diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index 9e4f08c..b0e9a67 100644 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -176,7 +176,7 @@ class InputHookManager(object): """ if gui is None: self._apps = {} - elif self._apps.has_key(gui): + elif gui in self._apps: del self._apps[gui] def enable_wx(self, app=None): @@ -225,7 +225,7 @@ class InputHookManager(object): This merely sets PyOS_InputHook to NULL. """ - if self._apps.has_key(GUI_WX): + if GUI_WX in self._apps: self._apps[GUI_WX]._in_event_loop = False self.clear_inputhook() @@ -265,7 +265,7 @@ class InputHookManager(object): This merely sets PyOS_InputHook to NULL. """ - if self._apps.has_key(GUI_QT4): + if GUI_QT4 in self._apps: self._apps[GUI_QT4]._in_event_loop = False self.clear_inputhook() @@ -364,7 +364,7 @@ class InputHookManager(object): glut_close, glut_display, \ glut_idle, inputhook_glut - if not self._apps.has_key( GUI_GLUT ): + if GUI_GLUT not in self._apps: glut.glutInit( sys.argv ) glut.glutInitDisplayMode( glut_display_mode ) # This is specific to freeglut diff --git a/IPython/parallel/controller/dictdb.py b/IPython/parallel/controller/dictdb.py index fd8be3f..eaf106a 100644 --- a/IPython/parallel/controller/dictdb.py +++ b/IPython/parallel/controller/dictdb.py @@ -133,7 +133,7 @@ class DictDB(BaseDB): def add_record(self, msg_id, rec): """Add a new Task Record, by msg_id.""" - if self._records.has_key(msg_id): + if msg_id in self._records: raise KeyError("Already have msg_id %r"%(msg_id)) self._records[msg_id] = rec diff --git a/IPython/parallel/util.py b/IPython/parallel/util.py index 5b3e5a6..61cbb5d 100644 --- a/IPython/parallel/util.py +++ b/IPython/parallel/util.py @@ -243,11 +243,11 @@ def _pull(keys): user_ns = globals() if isinstance(keys, (list,tuple, set)): for key in keys: - if not user_ns.has_key(key): + if key not in user_ns: raise NameError("name '%s' is not defined"%key) return map(user_ns.get, keys) else: - if not user_ns.has_key(keys): + if keys not in user_ns: raise NameError("name '%s' is not defined"%keys) return user_ns.get(keys) diff --git a/IPython/utils/ipstruct.py b/IPython/utils/ipstruct.py index c610ad2..ca53e2e 100644 --- a/IPython/utils/ipstruct.py +++ b/IPython/utils/ipstruct.py @@ -84,7 +84,7 @@ class Struct(dict): ... this is not allowed """ - if not self._allownew and not self.has_key(key): + if not self._allownew and key not in self: raise KeyError( "can't create new attribute %s when allow_new_attr(False)" % key) dict.__setitem__(self, key, value) @@ -212,7 +212,7 @@ class Struct(dict): {'b': 30} """ for k in other.keys(): - if self.has_key(k): + if k in self: del self[k] return self @@ -262,7 +262,7 @@ class Struct(dict): >>> s.hasattr('get') False """ - return self.has_key(key) + return key in self def allow_new_attr(self, allow = True): """Set whether new attributes can be created in this Struct. diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 7065f53..eae3f0d 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -465,7 +465,7 @@ class HasTraits(object): def _add_notifiers(self, handler, name): - if not self._trait_notifiers.has_key(name): + if name not in self._trait_notifiers: nlist = [] self._trait_notifiers[name] = nlist else: @@ -474,7 +474,7 @@ class HasTraits(object): nlist.append(handler) def _remove_notifiers(self, handler, name): - if self._trait_notifiers.has_key(name): + if name in self._trait_notifiers: nlist = self._trait_notifiers[name] try: index = nlist.index(handler) diff --git a/setup.py b/setup.py index 9074f38..42d02d4 100755 --- a/setup.py +++ b/setup.py @@ -269,10 +269,11 @@ if 'setuptools' in sys.modules: # so we explicitly disable some 2to3 fixes to be sure we aren't forgetting # anything. setuptools_extra_args['use_2to3_exclude_fixers'] = [ - 'lib2to3.fixes.fix_except', 'lib2to3.fixes.fix_apply', - 'lib2to3.fixes.fix_repr', + 'lib2to3.fixes.fix_except', + 'lib2to3.fixes.fix_has_key', 'lib2to3.fixes.fix_next', + 'lib2to3.fixes.fix_repr', ] from setuptools.command.build_py import build_py setup_args['cmdclass'] = {'build_py': record_commit_info('IPython', build_cmd=build_py)}