Permalink
Browse files

Merge branch 'takowl-ipy3-preparation' into trunk

This branch cleans up our trunk to make a 2to3 run be as painless as
possible, while keeping our code fully 2.6-compatible.

Closes gh-159 (pull request).
  • Loading branch information...
2 parents 263d8f1 + 468a14f commit 572d3d799133b9dcf1f39b318d9ef906104bd12b @fperez fperez committed Oct 10, 2010
View
@@ -4,3 +4,5 @@ docs/source/api/generated
*.pyc
build
*.egg-info
+*.py~
+*.bak
View
@@ -47,7 +47,7 @@
# Release data
__author__ = ''
-for author, email in release.authors.values():
+for author, email in release.authors.itervalues():
__author__ += author + ' <' + email + '>\n'
__license__ = release.license
__version__ = release.version
@@ -112,7 +112,7 @@ def _config_changed(self, name, old, new):
# dynamically create the section with name self.__class__.__name__.
if new._has_section(sname):
my_config = new[sname]
- for k, v in traits.items():
+ for k, v in traits.iteritems():
# Don't allow traitlets with config=True to start with
# uppercase. Otherwise, they are confused with Config
# subsections. But, developers shouldn't have uppercase
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# coding: utf-8
"""A simple configuration system.
@@ -73,7 +74,7 @@ def __init__(self, *args, **kwds):
def _merge(self, other):
to_update = {}
- for k, v in other.items():
+ for k, v in other.iteritems():
if not self.has_key(k):
to_update[k] = v
else: # I have this key
@@ -92,15 +93,17 @@ def _is_section_key(self, key):
else:
return False
- def has_key(self, key):
+ def __contains__(self, key):
if self._is_section_key(key):
return True
else:
- return dict.has_key(self, key)
+ return super(Config, self).__contains__(key)
+ # .has_key is deprecated for dictionaries.
+ has_key = __contains__
def _has_section(self, key):
if self._is_section_key(key):
- if dict.has_key(self, key):
+ if super(Config, self).__contains__(key):
return True
return False
@@ -363,7 +366,7 @@ def _parse_args(self, args):
def _convert_to_config(self):
"""self.parsed_data->self.config"""
- for k, v in vars(self.parsed_data).items():
+ for k, v in vars(self.parsed_data).iteritems():
exec_str = 'self.config.' + k + '= v'
exec exec_str in locals(), globals()
@@ -336,7 +336,7 @@ def cd_completer(self, event):
return [compress_user(relpath, tilde_expand, tilde_val)]
# if no completions so far, try bookmarks
- bks = self.db.get('bookmarks',{}).keys()
+ bks = self.db.get('bookmarks',{}).iterkeys()
bkmatches = [s for s in bks if s.startswith(event.symbol)]
if bkmatches:
return bkmatches
@@ -260,7 +260,7 @@ def add(self, ent):
def all(self):
d = self.db.hdict('shadowhist')
- items = [(i,s) for (s,i) in d.items()]
+ items = [(i,s) for (s,i) in d.iteritems()]
items.sort()
return items
@@ -22,13 +22,12 @@
import abc
import atexit
import codeop
-import exceptions
-import new
import os
import re
import string
import sys
import tempfile
+import types
from contextlib import nested
from IPython.config.configurable import Configurable
@@ -102,7 +101,7 @@ def softspace(file, newvalue):
def no_op(*a, **kw): pass
-class SpaceInInput(exceptions.Exception): pass
+class SpaceInInput(Exception): pass
class Bunch: pass
@@ -512,7 +511,7 @@ def save_sys_module_state(self):
def restore_sys_module_state(self):
"""Restore the state of the sys module."""
try:
- for k, v in self._orig_sys_module_state.items():
+ for k, v in self._orig_sys_module_state.iteritems():
setattr(sys, k, v)
except AttributeError:
pass
@@ -550,7 +549,7 @@ def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
# accepts it. Probably at least check that the hook takes the number
# of args it's supposed to.
- f = new.instancemethod(hook,self,self.__class__)
+ f = types.MethodType(hook, self)
# check if the hook is for strdispatcher first
if str_key is not None:
@@ -1249,7 +1248,7 @@ def init_history(self):
def init_shadow_hist(self):
try:
self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
- except exceptions.UnicodeDecodeError:
+ except UnicodeDecodeError:
print "Your ipython_dir can't be decoded to unicode!"
print "Please set HOME environment variable to something that"
print r"only has ASCII characters, e.g. c:\home"
@@ -1414,7 +1413,7 @@ def dummy_handler(self,etype,value,tb):
if handler is None: handler = dummy_handler
- self.CustomTB = new.instancemethod(handler,self,self.__class__)
+ self.CustomTB = types.MethodType(handler, self)
self.custom_exceptions = exc_tuple
def excepthook(self, etype, value, tb):
@@ -1756,8 +1755,7 @@ def set_custom_completer(self, completer, pos=0):
The position argument (defaults to 0) is the index in the completers
list where you want the completer to be inserted."""
- newcomp = new.instancemethod(completer,self.Completer,
- self.Completer.__class__)
+ newcomp = types.MethodType(completer, self.Completer)
self.Completer.matchers.insert(pos,newcomp)
def set_readline_completer(self):
@@ -1828,12 +1826,11 @@ def foo_impl(self,parameter_s=''):
print 'Magic function. Passed parameter is between < >:'
print '<%s>' % parameter_s
print 'The self object is:',self
-
+ newcomp = types.MethodType(completer, self.Completer)
self.define_magic('foo',foo_impl)
"""
- import new
- im = new.instancemethod(func,self, self.__class__)
+ im = types.MethodType(func, self)
old = getattr(self, "magic_" + magicname, None)
setattr(self, "magic_" + magicname, im)
return old
@@ -237,7 +237,7 @@ def sort_transformers(self):
This must be called after the priority of a transformer is changed.
The :meth:`register_transformer` method calls this automatically.
"""
- self._transformers.sort(cmp=lambda x,y: x.priority-y.priority)
+ self._transformers.sort(key=lambda x: x.priority)
@property
def transformers(self):
@@ -273,7 +273,7 @@ def sort_checkers(self):
This must be called after the priority of a checker is changed.
The :meth:`register_checker` method calls this automatically.
"""
- self._checkers.sort(cmp=lambda x,y: x.priority-y.priority)
+ self._checkers.sort(key=lambda x: x.priority)
@property
def checkers(self):
@@ -28,7 +28,7 @@
# Note: at the bottom, there's a slightly more complete version of this that
# can be useful during development of code here.
-def mini_interactive_loop(raw_input):
+def mini_interactive_loop(input_func):
"""Minimal example of the logic of an interactive interpreter loop.
This serves as an example, and it is used by the test system with a fake
@@ -43,7 +43,7 @@ def mini_interactive_loop(raw_input):
while isp.push_accepts_more():
indent = ' '*isp.indent_spaces
prompt = '>>> ' + indent
- line = indent + raw_input(prompt)
+ line = indent + input_func(prompt)
isp.push(line)
# Here we just return input so we can use it in a test suite, but a real
@@ -356,7 +356,7 @@ def check_ns(self, lines, ns):
# We can't check that the provided ns is identical to the test_ns,
# because Python fills test_ns with extra keys (copyright, etc). But
# we can check that the given dict is *contained* in test_ns
- for k,v in ns.items():
+ for k,v in ns.iteritems():
self.assertEqual(test_ns[k], v)
def test_simple(self):
@@ -33,7 +33,7 @@ def test_rehashx():
# Practically ALL ipython development systems will have more than 10 aliases
yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
- for key, val in _ip.alias_manager.alias_table.items():
+ for key, val in _ip.alias_manager.alias_table.iteritems():
# we must strip dots from alias names
nt.assert_true('.' not in key)
@@ -712,14 +712,14 @@ def nullrepr(value, repr=text_repr): return ''
head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
exc, ' '*(75-len(str(etype))-len(pyver)),
- pyver, string.rjust(date, 75) )
+ pyver, date.rjust(75) )
head += "\nA problem occured executing Python code. Here is the sequence of function"\
"\ncalls leading up to the error, with the most recent (innermost) call last."
else:
# Simplified header
head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
- string.rjust('Traceback (most recent call last)',
- 75 - len(str(etype)) ) )
+ 'Traceback (most recent call last)'.\
+ rjust(75 - len(str(etype)) ) )
frames = []
# Flush cache before calling inspect. This helps alleviate some of the
# problems with python 2.3's inspect.py.
@@ -78,7 +78,6 @@
import string
import sys
from tokenize import tokenprog
-from types import StringType
class ItplError(ValueError):
def __init__(self, text, pos):
@@ -144,7 +143,7 @@ def __init__(self, format,codec='utf_8',encoding_errors='backslashreplace'):
pos = 0
while 1:
- dollar = string.find(format, "$", pos)
+ dollar = format.find("$", pos)
if dollar < 0: break
nextchar = format[dollar+1]
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# configobj.py
# A config file reader/writer that supports nested sections in config files.
# Copyright (C) 2005-2008 Michael Foord, Nicola Larosa
@@ -32,22 +33,7 @@
pass
from types import StringTypes
from warnings import warn
-try:
- from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF16_BE, BOM_UTF16_LE
-except ImportError:
- # Python 2.2 does not have these
- # UTF-8
- BOM_UTF8 = '\xef\xbb\xbf'
- # UTF-16, little endian
- BOM_UTF16_LE = '\xff\xfe'
- # UTF-16, big endian
- BOM_UTF16_BE = '\xfe\xff'
- if sys.byteorder == 'little':
- # UTF-16, native endianness
- BOM_UTF16 = BOM_UTF16_LE
- else:
- # UTF-16, native endianness
- BOM_UTF16 = BOM_UTF16_BE
+from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF16_BE, BOM_UTF16_LE
# A dictionary mapping BOM to
# the encoding to decode with, and what to set the
@@ -101,21 +87,6 @@ def match_utf8(encoding):
tsquot = '"""%s"""'
tdquot = "'''%s'''"
-try:
- enumerate
-except NameError:
- def enumerate(obj):
- """enumerate for Python 2.2."""
- i = -1
- for item in obj:
- i += 1
- yield i, item
-
-try:
- True, False
-except NameError:
- True, False = 1, 0
-
__version__ = '4.5.2'
@@ -814,7 +785,7 @@ def merge(self, indict):
>>> c2
{'section1': {'option1': 'False', 'subsection': {'more_options': 'False'}}}
"""
- for key, val in indict.items():
+ for key, val in indict.iteritems():
if (key in self and isinstance(self[key], dict) and
isinstance(val, dict)):
self[key].merge(val)
@@ -1438,7 +1409,7 @@ def _handle_bom(self, infile):
enc = BOM_LIST[self.encoding.lower()]
if enc == 'utf_16':
# For UTF16 we try big endian and little endian
- for BOM, (encoding, final_encoding) in BOMS.items():
+ for BOM, (encoding, final_encoding) in BOMS.iteritems():
if not final_encoding:
# skip UTF8
continue
@@ -1468,7 +1439,7 @@ def _handle_bom(self, infile):
return self._decode(infile, self.encoding)
# No encoding specified - so we need to check for UTF8/UTF16
- for BOM, (encoding, final_encoding) in BOMS.items():
+ for BOM, (encoding, final_encoding) in BOMS.iteritems():
if not line.startswith(BOM):
continue
else:
@@ -2481,7 +2452,7 @@ def flatten_errors(cfg, res, levels=None, results=None):
if levels:
levels.pop()
return results
- for (key, val) in res.items():
+ for (key, val) in res.iteritems():
if val == True:
continue
if isinstance(cfg.get(key), dict):
Oops, something went wrong.

0 comments on commit 572d3d7

Please sign in to comment.