Skip to content

Commit

Permalink
Start using py3compat module.
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Sep 7, 2011
1 parent eb1cd97 commit ee492f8
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 93 deletions.
18 changes: 6 additions & 12 deletions IPython/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
# Imports
#-----------------------------------------------------------------------------

import __builtin__
import __builtin__ as builtin_mod
import re
import sys

from IPython.external import argparse
from IPython.utils.path import filefind, get_ipython_dir
from IPython.utils import warn
from IPython.utils import py3compat, warn

#-----------------------------------------------------------------------------
# Exceptions
Expand Down Expand Up @@ -131,7 +131,7 @@ def __getitem__(self, key):
# that you can't have section or attribute names that are
# builtins.
try:
return getattr(__builtin__, key)
return getattr(builtin_mod, key)
except AttributeError:
pass
if is_section_key(key):
Expand All @@ -146,7 +146,7 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
# Don't allow names in __builtin__ to be modified.
if hasattr(__builtin__, key):
if hasattr(builtin_mod, key):
raise ConfigError('Config variable names cannot have the same name '
'as a Python builtin: %s' % key)
if self._is_section_key(key):
Expand Down Expand Up @@ -312,7 +312,7 @@ def get_config():
namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
fs_encoding = sys.getfilesystemencoding() or 'ascii'
conf_filename = self.full_filename.encode(fs_encoding)
execfile(conf_filename, namespace)
py3compat.execfile(conf_filename, namespace)

def _convert_to_config(self):
if self.data is None:
Expand Down Expand Up @@ -586,13 +586,7 @@ def _add_arguments(self, aliases=None, flags=None):
def _parse_args(self, args):
"""self.parser->self.parsed_data"""
# decode sys.argv to support unicode command-line options
uargs = []
for a in args:
if isinstance(a, str):
# don't decode if we already got unicode
a = a.decode(sys.stdin.encoding or
sys.getdefaultencoding())
uargs.append(a)
uargs = [py3compat.cast_unicode(a) for a in args]
self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)

def _convert_to_config(self):
Expand Down
8 changes: 4 additions & 4 deletions IPython/core/completerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Our own imports
from IPython.core.completer import expand_user, compress_user
from IPython.core.error import TryNext
from IPython.utils import py3compat

# FIXME: this should be pulled in with the right call via the component system
from IPython.core.ipapi import get as get_ipython
Expand Down Expand Up @@ -66,10 +67,9 @@ def shlex_split(x):
# Example:
# %run "c:/python -> ['%run','"c:/python']

# shlex.split has unicode bugs, so encode first to str
if isinstance(x, unicode):
# don't raise errors on encoding:
x = x.encode(sys.stdin.encoding or sys.getdefaultencoding(), 'replace')
# shlex.split has unicode bugs in Python 2, so encode first to str
if not py3compat.PY3:
x = py3compat.cast_bytes(x)

endofline = []
while x != '':
Expand Down
38 changes: 17 additions & 21 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import with_statement
from __future__ import absolute_import

import __builtin__
import __builtin__ as builtin_mod
import __future__
import abc
import ast
Expand Down Expand Up @@ -61,6 +61,7 @@
from IPython.external.Itpl import ItplNS
from IPython.utils import PyColorize
from IPython.utils import io
from IPython.utils import py3compat
from IPython.utils.doctestreload import doctest_reload
from IPython.utils.io import ask_yes_no, rprint
from IPython.utils.ipstruct import Struct
Expand Down Expand Up @@ -979,13 +980,13 @@ def make_user_namespaces(self, user_ns=None, user_global_ns=None):
# Set __name__ to __main__ to better match the behavior of the
# normal interpreter.
user_ns = {'__name__' :'__main__',
'__builtin__' : __builtin__,
'__builtins__' : __builtin__,
py3compat.builtin_mod_name: builtin_mod,
'__builtins__' : builtin_mod,
}
else:
user_ns.setdefault('__name__','__main__')
user_ns.setdefault('__builtin__',__builtin__)
user_ns.setdefault('__builtins__',__builtin__)
user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod)
user_ns.setdefault('__builtins__',builtin_mod)

if user_global_ns is None:
user_global_ns = user_ns
Expand Down Expand Up @@ -1255,14 +1256,15 @@ def _ofind(self, oname, namespaces=None):
Has special code to detect magic functions.
"""
#oname = oname.strip()
oname = oname.strip()
#print '1- oname: <%r>' % oname # dbg
try:
oname = oname.strip().encode('ascii')
#print '2- oname: <%r>' % oname # dbg
except UnicodeEncodeError:
print 'Python identifiers can only contain ascii characters.'
return dict(found=False)
if not py3compat.PY3:
try:
oname = oname.encode('ascii')
#print '2- oname: <%r>' % oname # dbg
except UnicodeError:
print 'Python identifiers can only contain ascii characters.'
return dict(found=False)

alias_ns = None
if namespaces is None:
Expand Down Expand Up @@ -1701,7 +1703,8 @@ def refill_readline_hist(self):
include_latest=True):
if cell.strip(): # Ignore blank lines
for line in cell.splitlines():
self.readline.add_history(line.encode(stdin_encoding, 'replace'))
self.readline.add_history(py3compat.unicode_to_str(line,
stdin_encoding))

def set_next_input(self, s):
""" Sets the 'default' input string for the next command line.
Expand Down Expand Up @@ -1907,8 +1910,6 @@ def foo_impl(self,parameter_s=''):
self.define_magic('foo',foo_impl)
"""

import new
im = types.MethodType(func,self)
old = getattr(self, "magic_" + magicname, None)
setattr(self, "magic_" + magicname, im)
Expand Down Expand Up @@ -2175,15 +2176,10 @@ def safe_execfile(self, fname, *where, **kw):
# behavior of running a script from the system command line, where
# Python inserts the script's directory into sys.path
dname = os.path.dirname(fname)

if isinstance(fname, unicode):
# execfile uses default encoding instead of filesystem encoding
# so unicode filenames will fail
fname = fname.encode(sys.getfilesystemencoding() or sys.getdefaultencoding())

with prepended_to_syspath(dname):
try:
execfile(fname,*where)
py3compat.execfile(fname,*where)
except SystemExit, status:
# If the call was made with 0 or None exit status (sys.exit(0)
# or sys.exit() ), don't bother showing a traceback, as both of
Expand Down
4 changes: 2 additions & 2 deletions IPython/core/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Imports
#-----------------------------------------------------------------------------

import __builtin__
import __builtin__ as builtin_mod
import __future__
import bdb
import inspect
Expand Down Expand Up @@ -1729,7 +1729,7 @@ def magic_run(self, parameter_s ='',runner=None,
# Since this seems to be done by the interpreter itself, the best
# we can do is to at least restore __builtins__ for the user on
# exit.
self.shell.user_ns['__builtins__'] = __builtin__
self.shell.user_ns['__builtins__'] = builtin_mod

# Ensure key global structures are restored
sys.argv = save_argv
Expand Down
27 changes: 13 additions & 14 deletions IPython/core/splitinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import re
import sys

from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Main function
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -56,11 +58,7 @@ def split_user_input(line, pattern=None):
manner.
"""
# We need to ensure that the rest of this routine deals only with unicode
if type(line)==str:
codec = sys.stdin.encoding
if codec is None:
codec = 'utf-8'
line = line.decode(codec)
line = py3compat.cast_unicode(line, sys.stdin.encoding or 'utf-8')

if pattern is None:
pattern = line_split
Expand All @@ -75,15 +73,16 @@ def split_user_input(line, pattern=None):
pre = re.match('^(\s*)(.*)',line).groups()[0]
else:
pre,ifun,the_rest = match.groups()

# ifun has to be a valid python identifier, so it better encode into
# ascii. We do still make it a unicode string so that we consistently
# return unicode, but it will be one that is guaranteed to be pure ascii
try:
ifun = unicode(ifun.encode('ascii'))
except UnicodeEncodeError:
the_rest = ifun + u' ' + the_rest
ifun = u''

if not py3compat.PY3:
# ifun has to be a valid python identifier, so it better encode into
# ascii. We do still make it a unicode string so that we consistently
# return unicode, but it will be one that is guaranteed to be pure ascii
try:
ifun = unicode(ifun.encode('ascii'))
except UnicodeEncodeError:
the_rest = ifun + u' ' + the_rest
ifun = u''

#print 'line:<%s>' % line # dbg
#print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
Expand Down
5 changes: 4 additions & 1 deletion IPython/core/tests/tclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
See test_run for details."""

from __future__ import print_function

import sys

# We want to ensure that while objects remain available for immediate access,
Expand All @@ -10,10 +12,11 @@
class C(object):
def __init__(self,name):
self.name = name
self.p = print
self.flush_stdout = sys.stdout.flush

def __del__(self):
print 'tclass.py: deleting object:',self.name
self.p('tclass.py: deleting object:',self.name)
self.flush_stdout()

try:
Expand Down
3 changes: 2 additions & 1 deletion IPython/core/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from IPython.core.application import BaseIPythonApplication
from IPython.testing import decorators as testdec
from IPython.utils import py3compat

@testdec.onlyif_unicode_paths
def test_unicode_cwd():
Expand Down Expand Up @@ -35,7 +36,7 @@ def test_unicode_ipdir():

old_ipdir1 = os.environ.pop("IPYTHONDIR", None)
old_ipdir2 = os.environ.pop("IPYTHON_DIR", None)
os.environ["IPYTHONDIR"] = ipdir.encode("utf-8")
os.environ["IPYTHONDIR"] = py3compat.unicode_to_str(ipdir, "utf-8")
try:
app = BaseIPythonApplication()
# The lines below are copied from Application.initialize()
Expand Down
3 changes: 2 additions & 1 deletion IPython/core/tests/test_compilerop.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

# Our own imports
from IPython.core import compilerop
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Test functions
Expand Down Expand Up @@ -51,7 +52,7 @@ def test_cache():
def setUp():
# Check we're in a proper Python 2 environment (some imports, such
# as GTK, can change the default encoding, which can hide bugs.)
nt.assert_equal(sys.getdefaultencoding(), "ascii")
nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")

def test_cache_unicode():
cp = compilerop.CachingCompiler()
Expand Down
3 changes: 2 additions & 1 deletion IPython/core/tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
# our own packages
from IPython.utils.tempdir import TemporaryDirectory
from IPython.core.history import HistoryManager, extract_hist_ranges
from IPython.utils import py3compat

def setUp():
nt.assert_equal(sys.getdefaultencoding(), "ascii")
nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")

def test_history():
ip = get_ipython()
Expand Down
2 changes: 1 addition & 1 deletion IPython/core/tests/test_inputsplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_get_input_encoding():
nt.assert_true(isinstance(encoding, basestring))
# simple-minded check that at least encoding a simple string works with the
# encoding we got.
nt.assert_equal('test'.encode(encoding), 'test')
nt.assert_equal(u'test'.encode(encoding), b'test')


class NoInputEncodingTestCase(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions IPython/core/tests/test_iplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def doctest_tb_plain():
div0()
...line 8, in div0
x/y
ZeroDivisionError: integer division or modulo by zero
ZeroDivisionError: ...
"""


Expand Down Expand Up @@ -107,7 +107,7 @@ def doctest_tb_context():
9
10 def sysexit(stat, mode):
<BLANKLINE>
ZeroDivisionError: integer division or modulo by zero
ZeroDivisionError: ...
"""


Expand Down Expand Up @@ -144,7 +144,7 @@ def doctest_tb_verbose():
9
10 def sysexit(stat, mode):
<BLANKLINE>
ZeroDivisionError: integer division or modulo by zero
ZeroDivisionError: ...
"""


Expand Down
2 changes: 1 addition & 1 deletion IPython/frontend/terminal/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def raw_input(self, prompt=''):
self.set_readline_completer()

try:
line = self.raw_input_original(prompt).decode(self.stdin_encoding)
line = py3compat.str_to_unicode(self.raw_input_original(prompt))
except ValueError:
warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
" or sys.stdout.close()!\nExiting IPython!")
Expand Down
3 changes: 1 addition & 2 deletions IPython/lib/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@
#
#*****************************************************************************

import exceptions
import os
import re
import shlex
Expand All @@ -182,7 +181,7 @@

__all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']

class DemoError(exceptions.Exception): pass
class DemoError(Exception): pass

def re_mark(mark):
return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
Expand Down
4 changes: 2 additions & 2 deletions IPython/testing/globalipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#-----------------------------------------------------------------------------

# stdlib
import __builtin__
import __builtin__ as builtin_mod
import os
import sys
from types import MethodType
Expand Down Expand Up @@ -131,7 +131,7 @@ def update(self,other):
# aggressive low-level cleaning of the execution namespace, we need to
# correct for that ourselves, to ensure consitency with the 'real'
# ipython.
self['__builtins__'] = __builtin__
self['__builtins__'] = builtin_mod

def __delitem__(self, key):
"""Part of the test suite checks that we can release all
Expand Down
Loading

0 comments on commit ee492f8

Please sign in to comment.