Show More
@@ -18,13 +18,13 b' Authors' | |||||
18 | # Imports |
|
18 | # Imports | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | import __builtin__ |
|
21 | import __builtin__ as builtin_mod | |
22 | import re |
|
22 | import re | |
23 | import sys |
|
23 | import sys | |
24 |
|
24 | |||
25 | from IPython.external import argparse |
|
25 | from IPython.external import argparse | |
26 | from IPython.utils.path import filefind, get_ipython_dir |
|
26 | from IPython.utils.path import filefind, get_ipython_dir | |
27 | from IPython.utils import warn |
|
27 | from IPython.utils import py3compat, warn | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Exceptions |
|
30 | # Exceptions | |
@@ -131,7 +131,7 b' class Config(dict):' | |||||
131 | # that you can't have section or attribute names that are |
|
131 | # that you can't have section or attribute names that are | |
132 | # builtins. |
|
132 | # builtins. | |
133 | try: |
|
133 | try: | |
134 |
return getattr( |
|
134 | return getattr(builtin_mod, key) | |
135 | except AttributeError: |
|
135 | except AttributeError: | |
136 | pass |
|
136 | pass | |
137 | if is_section_key(key): |
|
137 | if is_section_key(key): | |
@@ -146,7 +146,7 b' class Config(dict):' | |||||
146 |
|
146 | |||
147 | def __setitem__(self, key, value): |
|
147 | def __setitem__(self, key, value): | |
148 | # Don't allow names in __builtin__ to be modified. |
|
148 | # Don't allow names in __builtin__ to be modified. | |
149 |
if hasattr( |
|
149 | if hasattr(builtin_mod, key): | |
150 | raise ConfigError('Config variable names cannot have the same name ' |
|
150 | raise ConfigError('Config variable names cannot have the same name ' | |
151 | 'as a Python builtin: %s' % key) |
|
151 | 'as a Python builtin: %s' % key) | |
152 | if self._is_section_key(key): |
|
152 | if self._is_section_key(key): | |
@@ -312,7 +312,7 b' class PyFileConfigLoader(FileConfigLoader):' | |||||
312 | namespace = dict(load_subconfig=load_subconfig, get_config=get_config) |
|
312 | namespace = dict(load_subconfig=load_subconfig, get_config=get_config) | |
313 | fs_encoding = sys.getfilesystemencoding() or 'ascii' |
|
313 | fs_encoding = sys.getfilesystemencoding() or 'ascii' | |
314 | conf_filename = self.full_filename.encode(fs_encoding) |
|
314 | conf_filename = self.full_filename.encode(fs_encoding) | |
315 | execfile(conf_filename, namespace) |
|
315 | py3compat.execfile(conf_filename, namespace) | |
316 |
|
316 | |||
317 | def _convert_to_config(self): |
|
317 | def _convert_to_config(self): | |
318 | if self.data is None: |
|
318 | if self.data is None: | |
@@ -586,13 +586,7 b' class ArgParseConfigLoader(CommandLineConfigLoader):' | |||||
586 | def _parse_args(self, args): |
|
586 | def _parse_args(self, args): | |
587 | """self.parser->self.parsed_data""" |
|
587 | """self.parser->self.parsed_data""" | |
588 | # decode sys.argv to support unicode command-line options |
|
588 | # decode sys.argv to support unicode command-line options | |
589 | uargs = [] |
|
589 | uargs = [py3compat.cast_unicode(a) for a in args] | |
590 | for a in args: |
|
|||
591 | if isinstance(a, str): |
|
|||
592 | # don't decode if we already got unicode |
|
|||
593 | a = a.decode(sys.stdin.encoding or |
|
|||
594 | sys.getdefaultencoding()) |
|
|||
595 | uargs.append(a) |
|
|||
596 | self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs) |
|
590 | self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs) | |
597 |
|
591 | |||
598 | def _convert_to_config(self): |
|
592 | def _convert_to_config(self): |
@@ -30,6 +30,7 b' from zipimport import zipimporter' | |||||
30 | # Our own imports |
|
30 | # Our own imports | |
31 | from IPython.core.completer import expand_user, compress_user |
|
31 | from IPython.core.completer import expand_user, compress_user | |
32 | from IPython.core.error import TryNext |
|
32 | from IPython.core.error import TryNext | |
|
33 | from IPython.utils import py3compat | |||
33 |
|
34 | |||
34 | # FIXME: this should be pulled in with the right call via the component system |
|
35 | # FIXME: this should be pulled in with the right call via the component system | |
35 | from IPython.core.ipapi import get as get_ipython |
|
36 | from IPython.core.ipapi import get as get_ipython | |
@@ -66,10 +67,9 b' def shlex_split(x):' | |||||
66 | # Example: |
|
67 | # Example: | |
67 | # %run "c:/python -> ['%run','"c:/python'] |
|
68 | # %run "c:/python -> ['%run','"c:/python'] | |
68 |
|
69 | |||
69 | # shlex.split has unicode bugs, so encode first to str |
|
70 | # shlex.split has unicode bugs in Python 2, so encode first to str | |
70 | if isinstance(x, unicode): |
|
71 | if not py3compat.PY3: | |
71 | # don't raise errors on encoding: |
|
72 | x = py3compat.cast_bytes(x) | |
72 | x = x.encode(sys.stdin.encoding or sys.getdefaultencoding(), 'replace') |
|
|||
73 |
|
73 | |||
74 | endofline = [] |
|
74 | endofline = [] | |
75 | while x != '': |
|
75 | while x != '': |
@@ -17,7 +17,7 b'' | |||||
17 | from __future__ import with_statement |
|
17 | from __future__ import with_statement | |
18 | from __future__ import absolute_import |
|
18 | from __future__ import absolute_import | |
19 |
|
19 | |||
20 | import __builtin__ |
|
20 | import __builtin__ as builtin_mod | |
21 | import __future__ |
|
21 | import __future__ | |
22 | import abc |
|
22 | import abc | |
23 | import ast |
|
23 | import ast | |
@@ -61,6 +61,7 b' from IPython.core.profiledir import ProfileDir' | |||||
61 | from IPython.external.Itpl import ItplNS |
|
61 | from IPython.external.Itpl import ItplNS | |
62 | from IPython.utils import PyColorize |
|
62 | from IPython.utils import PyColorize | |
63 | from IPython.utils import io |
|
63 | from IPython.utils import io | |
|
64 | from IPython.utils import py3compat | |||
64 | from IPython.utils.doctestreload import doctest_reload |
|
65 | from IPython.utils.doctestreload import doctest_reload | |
65 | from IPython.utils.io import ask_yes_no, rprint |
|
66 | from IPython.utils.io import ask_yes_no, rprint | |
66 | from IPython.utils.ipstruct import Struct |
|
67 | from IPython.utils.ipstruct import Struct | |
@@ -979,13 +980,13 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
979 | # Set __name__ to __main__ to better match the behavior of the |
|
980 | # Set __name__ to __main__ to better match the behavior of the | |
980 | # normal interpreter. |
|
981 | # normal interpreter. | |
981 | user_ns = {'__name__' :'__main__', |
|
982 | user_ns = {'__name__' :'__main__', | |
982 |
|
|
983 | py3compat.builtin_mod_name: builtin_mod, | |
983 |
'__builtins__' : |
|
984 | '__builtins__' : builtin_mod, | |
984 | } |
|
985 | } | |
985 | else: |
|
986 | else: | |
986 | user_ns.setdefault('__name__','__main__') |
|
987 | user_ns.setdefault('__name__','__main__') | |
987 |
user_ns.setdefault( |
|
988 | user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod) | |
988 |
user_ns.setdefault('__builtins__', |
|
989 | user_ns.setdefault('__builtins__',builtin_mod) | |
989 |
|
990 | |||
990 | if user_global_ns is None: |
|
991 | if user_global_ns is None: | |
991 | user_global_ns = user_ns |
|
992 | user_global_ns = user_ns | |
@@ -1255,14 +1256,15 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
1255 |
|
1256 | |||
1256 | Has special code to detect magic functions. |
|
1257 | Has special code to detect magic functions. | |
1257 | """ |
|
1258 | """ | |
1258 |
|
|
1259 | oname = oname.strip() | |
1259 | #print '1- oname: <%r>' % oname # dbg |
|
1260 | #print '1- oname: <%r>' % oname # dbg | |
1260 | try: |
|
1261 | if not py3compat.PY3: | |
1261 | oname = oname.strip().encode('ascii') |
|
1262 | try: | |
1262 | #print '2- oname: <%r>' % oname # dbg |
|
1263 | oname = oname.encode('ascii') | |
1263 | except UnicodeEncodeError: |
|
1264 | #print '2- oname: <%r>' % oname # dbg | |
1264 | print 'Python identifiers can only contain ascii characters.' |
|
1265 | except UnicodeError: | |
1265 | return dict(found=False) |
|
1266 | print 'Python identifiers can only contain ascii characters.' | |
|
1267 | return dict(found=False) | |||
1266 |
|
1268 | |||
1267 | alias_ns = None |
|
1269 | alias_ns = None | |
1268 | if namespaces is None: |
|
1270 | if namespaces is None: | |
@@ -1701,7 +1703,8 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
1701 | include_latest=True): |
|
1703 | include_latest=True): | |
1702 | if cell.strip(): # Ignore blank lines |
|
1704 | if cell.strip(): # Ignore blank lines | |
1703 | for line in cell.splitlines(): |
|
1705 | for line in cell.splitlines(): | |
1704 |
self.readline.add_history(line |
|
1706 | self.readline.add_history(py3compat.unicode_to_str(line, | |
|
1707 | stdin_encoding)) | |||
1705 |
|
1708 | |||
1706 | def set_next_input(self, s): |
|
1709 | def set_next_input(self, s): | |
1707 | """ Sets the 'default' input string for the next command line. |
|
1710 | """ Sets the 'default' input string for the next command line. | |
@@ -1907,8 +1910,6 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
1907 |
|
1910 | |||
1908 | self.define_magic('foo',foo_impl) |
|
1911 | self.define_magic('foo',foo_impl) | |
1909 | """ |
|
1912 | """ | |
1910 |
|
||||
1911 | import new |
|
|||
1912 | im = types.MethodType(func,self) |
|
1913 | im = types.MethodType(func,self) | |
1913 | old = getattr(self, "magic_" + magicname, None) |
|
1914 | old = getattr(self, "magic_" + magicname, None) | |
1914 | setattr(self, "magic_" + magicname, im) |
|
1915 | setattr(self, "magic_" + magicname, im) | |
@@ -2175,15 +2176,10 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2175 | # behavior of running a script from the system command line, where |
|
2176 | # behavior of running a script from the system command line, where | |
2176 | # Python inserts the script's directory into sys.path |
|
2177 | # Python inserts the script's directory into sys.path | |
2177 | dname = os.path.dirname(fname) |
|
2178 | dname = os.path.dirname(fname) | |
2178 |
|
||||
2179 | if isinstance(fname, unicode): |
|
|||
2180 | # execfile uses default encoding instead of filesystem encoding |
|
|||
2181 | # so unicode filenames will fail |
|
|||
2182 | fname = fname.encode(sys.getfilesystemencoding() or sys.getdefaultencoding()) |
|
|||
2183 |
|
2179 | |||
2184 | with prepended_to_syspath(dname): |
|
2180 | with prepended_to_syspath(dname): | |
2185 | try: |
|
2181 | try: | |
2186 | execfile(fname,*where) |
|
2182 | py3compat.execfile(fname,*where) | |
2187 | except SystemExit, status: |
|
2183 | except SystemExit, status: | |
2188 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2184 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2189 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2185 | # or sys.exit() ), don't bother showing a traceback, as both of |
@@ -15,7 +15,7 b'' | |||||
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | import __builtin__ |
|
18 | import __builtin__ as builtin_mod | |
19 | import __future__ |
|
19 | import __future__ | |
20 | import bdb |
|
20 | import bdb | |
21 | import inspect |
|
21 | import inspect | |
@@ -1729,7 +1729,7 b' Currently the magic system has the following functions:\\n"""' | |||||
1729 | # Since this seems to be done by the interpreter itself, the best |
|
1729 | # Since this seems to be done by the interpreter itself, the best | |
1730 | # we can do is to at least restore __builtins__ for the user on |
|
1730 | # we can do is to at least restore __builtins__ for the user on | |
1731 | # exit. |
|
1731 | # exit. | |
1732 |
self.shell.user_ns['__builtins__'] = |
|
1732 | self.shell.user_ns['__builtins__'] = builtin_mod | |
1733 |
|
1733 | |||
1734 | # Ensure key global structures are restored |
|
1734 | # Ensure key global structures are restored | |
1735 | sys.argv = save_argv |
|
1735 | sys.argv = save_argv |
@@ -22,6 +22,8 b' Authors:' | |||||
22 | import re |
|
22 | import re | |
23 | import sys |
|
23 | import sys | |
24 |
|
24 | |||
|
25 | from IPython.utils import py3compat | |||
|
26 | ||||
25 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
26 | # Main function |
|
28 | # Main function | |
27 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
@@ -56,11 +58,7 b' def split_user_input(line, pattern=None):' | |||||
56 | manner. |
|
58 | manner. | |
57 | """ |
|
59 | """ | |
58 | # We need to ensure that the rest of this routine deals only with unicode |
|
60 | # We need to ensure that the rest of this routine deals only with unicode | |
59 | if type(line)==str: |
|
61 | line = py3compat.cast_unicode(line, sys.stdin.encoding or 'utf-8') | |
60 | codec = sys.stdin.encoding |
|
|||
61 | if codec is None: |
|
|||
62 | codec = 'utf-8' |
|
|||
63 | line = line.decode(codec) |
|
|||
64 |
|
62 | |||
65 | if pattern is None: |
|
63 | if pattern is None: | |
66 | pattern = line_split |
|
64 | pattern = line_split | |
@@ -75,15 +73,16 b' def split_user_input(line, pattern=None):' | |||||
75 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
73 | pre = re.match('^(\s*)(.*)',line).groups()[0] | |
76 | else: |
|
74 | else: | |
77 | pre,ifun,the_rest = match.groups() |
|
75 | pre,ifun,the_rest = match.groups() | |
78 |
|
76 | |||
79 | # ifun has to be a valid python identifier, so it better encode into |
|
77 | if not py3compat.PY3: | |
80 | # ascii. We do still make it a unicode string so that we consistently |
|
78 | # ifun has to be a valid python identifier, so it better encode into | |
81 | # return unicode, but it will be one that is guaranteed to be pure ascii |
|
79 | # ascii. We do still make it a unicode string so that we consistently | |
82 | try: |
|
80 | # return unicode, but it will be one that is guaranteed to be pure ascii | |
83 | ifun = unicode(ifun.encode('ascii')) |
|
81 | try: | |
84 | except UnicodeEncodeError: |
|
82 | ifun = unicode(ifun.encode('ascii')) | |
85 | the_rest = ifun + u' ' + the_rest |
|
83 | except UnicodeEncodeError: | |
86 | ifun = u'' |
|
84 | the_rest = ifun + u' ' + the_rest | |
|
85 | ifun = u'' | |||
87 |
|
86 | |||
88 | #print 'line:<%s>' % line # dbg |
|
87 | #print 'line:<%s>' % line # dbg | |
89 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg |
|
88 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg |
@@ -2,6 +2,8 b'' | |||||
2 |
|
2 | |||
3 | See test_run for details.""" |
|
3 | See test_run for details.""" | |
4 |
|
4 | |||
|
5 | from __future__ import print_function | |||
|
6 | ||||
5 | import sys |
|
7 | import sys | |
6 |
|
8 | |||
7 | # We want to ensure that while objects remain available for immediate access, |
|
9 | # We want to ensure that while objects remain available for immediate access, | |
@@ -10,10 +12,11 b' import sys' | |||||
10 | class C(object): |
|
12 | class C(object): | |
11 | def __init__(self,name): |
|
13 | def __init__(self,name): | |
12 | self.name = name |
|
14 | self.name = name | |
|
15 | self.p = print | |||
13 | self.flush_stdout = sys.stdout.flush |
|
16 | self.flush_stdout = sys.stdout.flush | |
14 |
|
17 | |||
15 | def __del__(self): |
|
18 | def __del__(self): | |
16 |
|
|
19 | self.p('tclass.py: deleting object:',self.name) | |
17 | self.flush_stdout() |
|
20 | self.flush_stdout() | |
18 |
|
21 | |||
19 | try: |
|
22 | try: |
@@ -6,6 +6,7 b' import tempfile' | |||||
6 |
|
6 | |||
7 | from IPython.core.application import BaseIPythonApplication |
|
7 | from IPython.core.application import BaseIPythonApplication | |
8 | from IPython.testing import decorators as testdec |
|
8 | from IPython.testing import decorators as testdec | |
|
9 | from IPython.utils import py3compat | |||
9 |
|
10 | |||
10 | @testdec.onlyif_unicode_paths |
|
11 | @testdec.onlyif_unicode_paths | |
11 | def test_unicode_cwd(): |
|
12 | def test_unicode_cwd(): | |
@@ -35,7 +36,7 b' def test_unicode_ipdir():' | |||||
35 |
|
36 | |||
36 | old_ipdir1 = os.environ.pop("IPYTHONDIR", None) |
|
37 | old_ipdir1 = os.environ.pop("IPYTHONDIR", None) | |
37 | old_ipdir2 = os.environ.pop("IPYTHON_DIR", None) |
|
38 | old_ipdir2 = os.environ.pop("IPYTHON_DIR", None) | |
38 |
os.environ["IPYTHONDIR"] = ipdir |
|
39 | os.environ["IPYTHONDIR"] = py3compat.unicode_to_str(ipdir, "utf-8") | |
39 | try: |
|
40 | try: | |
40 | app = BaseIPythonApplication() |
|
41 | app = BaseIPythonApplication() | |
41 | # The lines below are copied from Application.initialize() |
|
42 | # The lines below are copied from Application.initialize() |
@@ -23,6 +23,7 b' import nose.tools as nt' | |||||
23 |
|
23 | |||
24 | # Our own imports |
|
24 | # Our own imports | |
25 | from IPython.core import compilerop |
|
25 | from IPython.core import compilerop | |
|
26 | from IPython.utils import py3compat | |||
26 |
|
27 | |||
27 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
28 | # Test functions |
|
29 | # Test functions | |
@@ -51,7 +52,7 b' def test_cache():' | |||||
51 | def setUp(): |
|
52 | def setUp(): | |
52 | # Check we're in a proper Python 2 environment (some imports, such |
|
53 | # Check we're in a proper Python 2 environment (some imports, such | |
53 | # as GTK, can change the default encoding, which can hide bugs.) |
|
54 | # as GTK, can change the default encoding, which can hide bugs.) | |
54 | nt.assert_equal(sys.getdefaultencoding(), "ascii") |
|
55 | nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii") | |
55 |
|
56 | |||
56 | def test_cache_unicode(): |
|
57 | def test_cache_unicode(): | |
57 | cp = compilerop.CachingCompiler() |
|
58 | cp = compilerop.CachingCompiler() |
@@ -16,9 +16,10 b' import nose.tools as nt' | |||||
16 | # our own packages |
|
16 | # our own packages | |
17 | from IPython.utils.tempdir import TemporaryDirectory |
|
17 | from IPython.utils.tempdir import TemporaryDirectory | |
18 | from IPython.core.history import HistoryManager, extract_hist_ranges |
|
18 | from IPython.core.history import HistoryManager, extract_hist_ranges | |
|
19 | from IPython.utils import py3compat | |||
19 |
|
20 | |||
20 | def setUp(): |
|
21 | def setUp(): | |
21 | nt.assert_equal(sys.getdefaultencoding(), "ascii") |
|
22 | nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii") | |
22 |
|
23 | |||
23 | def test_history(): |
|
24 | def test_history(): | |
24 | ip = get_ipython() |
|
25 | ip = get_ipython() |
@@ -125,7 +125,7 b' def test_get_input_encoding():' | |||||
125 | nt.assert_true(isinstance(encoding, basestring)) |
|
125 | nt.assert_true(isinstance(encoding, basestring)) | |
126 | # simple-minded check that at least encoding a simple string works with the |
|
126 | # simple-minded check that at least encoding a simple string works with the | |
127 | # encoding we got. |
|
127 | # encoding we got. | |
128 | nt.assert_equal('test'.encode(encoding), 'test') |
|
128 | nt.assert_equal(u'test'.encode(encoding), b'test') | |
129 |
|
129 | |||
130 |
|
130 | |||
131 | class NoInputEncodingTestCase(unittest.TestCase): |
|
131 | class NoInputEncodingTestCase(unittest.TestCase): |
@@ -75,7 +75,7 b' Traceback (most recent call last):' | |||||
75 | div0() |
|
75 | div0() | |
76 | ...line 8, in div0 |
|
76 | ...line 8, in div0 | |
77 | x/y |
|
77 | x/y | |
78 | ZeroDivisionError: integer division or modulo by zero |
|
78 | ZeroDivisionError: ... | |
79 | """ |
|
79 | """ | |
80 |
|
80 | |||
81 |
|
81 | |||
@@ -107,7 +107,7 b' ZeroDivisionError Traceback (most recent call last)' | |||||
107 | 9 |
|
107 | 9 | |
108 | 10 def sysexit(stat, mode): |
|
108 | 10 def sysexit(stat, mode): | |
109 | <BLANKLINE> |
|
109 | <BLANKLINE> | |
110 | ZeroDivisionError: integer division or modulo by zero |
|
110 | ZeroDivisionError: ... | |
111 | """ |
|
111 | """ | |
112 |
|
112 | |||
113 |
|
113 | |||
@@ -144,7 +144,7 b' ZeroDivisionError Traceback (most recent call last)' | |||||
144 | 9 |
|
144 | 9 | |
145 | 10 def sysexit(stat, mode): |
|
145 | 10 def sysexit(stat, mode): | |
146 | <BLANKLINE> |
|
146 | <BLANKLINE> | |
147 | ZeroDivisionError: integer division or modulo by zero |
|
147 | ZeroDivisionError: ... | |
148 | """ |
|
148 | """ | |
149 |
|
149 | |||
150 |
|
150 |
@@ -332,7 +332,7 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
332 | self.set_readline_completer() |
|
332 | self.set_readline_completer() | |
333 |
|
333 | |||
334 | try: |
|
334 | try: | |
335 |
line = self.raw_input_original(prompt) |
|
335 | line = py3compat.str_to_unicode(self.raw_input_original(prompt)) | |
336 | except ValueError: |
|
336 | except ValueError: | |
337 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
337 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
338 | " or sys.stdout.close()!\nExiting IPython!") |
|
338 | " or sys.stdout.close()!\nExiting IPython!") |
@@ -169,7 +169,6 b" print 'bye!'" | |||||
169 | # |
|
169 | # | |
170 | #***************************************************************************** |
|
170 | #***************************************************************************** | |
171 |
|
171 | |||
172 | import exceptions |
|
|||
173 | import os |
|
172 | import os | |
174 | import re |
|
173 | import re | |
175 | import shlex |
|
174 | import shlex | |
@@ -182,7 +181,7 b' from IPython.utils.text import marquee' | |||||
182 |
|
181 | |||
183 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] |
|
182 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] | |
184 |
|
183 | |||
185 |
class DemoError( |
|
184 | class DemoError(Exception): pass | |
186 |
|
185 | |||
187 | def re_mark(mark): |
|
186 | def re_mark(mark): | |
188 | return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE) |
|
187 | return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE) |
@@ -20,7 +20,7 b' from __future__ import print_function' | |||||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | # stdlib |
|
22 | # stdlib | |
23 | import __builtin__ |
|
23 | import __builtin__ as builtin_mod | |
24 | import os |
|
24 | import os | |
25 | import sys |
|
25 | import sys | |
26 | from types import MethodType |
|
26 | from types import MethodType | |
@@ -131,7 +131,7 b' class ipnsdict(dict):' | |||||
131 | # aggressive low-level cleaning of the execution namespace, we need to |
|
131 | # aggressive low-level cleaning of the execution namespace, we need to | |
132 | # correct for that ourselves, to ensure consitency with the 'real' |
|
132 | # correct for that ourselves, to ensure consitency with the 'real' | |
133 | # ipython. |
|
133 | # ipython. | |
134 |
self['__builtins__'] = |
|
134 | self['__builtins__'] = builtin_mod | |
135 |
|
135 | |||
136 | def __delitem__(self, key): |
|
136 | def __delitem__(self, key): | |
137 | """Part of the test suite checks that we can release all |
|
137 | """Part of the test suite checks that we can release all |
@@ -17,6 +17,8 b' of subprocess utilities, and it contains tools that are common to all of them.' | |||||
17 | import subprocess |
|
17 | import subprocess | |
18 | import sys |
|
18 | import sys | |
19 |
|
19 | |||
|
20 | from IPython.utils import py3compat | |||
|
21 | ||||
20 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
21 | # Function definitions |
|
23 | # Function definitions | |
22 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
@@ -116,8 +118,8 b' def getoutput(cmd):' | |||||
116 |
|
118 | |||
117 | out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT) |
|
119 | out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT) | |
118 | if out is None: |
|
120 | if out is None: | |
119 |
|
|
121 | return '' | |
120 | return out |
|
122 | return py3compat.bytes_to_str(out) | |
121 |
|
123 | |||
122 |
|
124 | |||
123 | def getoutputerror(cmd): |
|
125 | def getoutputerror(cmd): | |
@@ -138,5 +140,6 b' def getoutputerror(cmd):' | |||||
138 |
|
140 | |||
139 | out_err = process_handler(cmd, lambda p: p.communicate()) |
|
141 | out_err = process_handler(cmd, lambda p: p.communicate()) | |
140 | if out_err is None: |
|
142 | if out_err is None: | |
141 |
|
|
143 | return '', '' | |
142 |
|
|
144 | out, err = out_err | |
|
145 | return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err) |
@@ -23,6 +23,7 b' import IPython' | |||||
23 | from IPython.utils import warn |
|
23 | from IPython.utils import warn | |
24 | from IPython.utils.process import system |
|
24 | from IPython.utils.process import system | |
25 | from IPython.utils.importstring import import_item |
|
25 | from IPython.utils.importstring import import_item | |
|
26 | from IPython.utils import py3compat | |||
26 |
|
27 | |||
27 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
28 | # Code |
|
29 | # Code | |
@@ -30,14 +31,6 b' from IPython.utils.importstring import import_item' | |||||
30 |
|
31 | |||
31 | fs_encoding = sys.getfilesystemencoding() |
|
32 | fs_encoding = sys.getfilesystemencoding() | |
32 |
|
33 | |||
33 | def _cast_unicode(s, enc=None): |
|
|||
34 | """Turn 8-bit strings into unicode.""" |
|
|||
35 | if isinstance(s, bytes): |
|
|||
36 | enc = enc or sys.getdefaultencoding() |
|
|||
37 | return s.decode(enc) |
|
|||
38 | return s |
|
|||
39 |
|
||||
40 |
|
||||
41 | def _get_long_path_name(path): |
|
34 | def _get_long_path_name(path): | |
42 | """Dummy no-op.""" |
|
35 | """Dummy no-op.""" | |
43 | return path |
|
36 | return path | |
@@ -203,7 +196,7 b' def get_home_dir():' | |||||
203 | root=os.path.abspath(root).rstrip('\\') |
|
196 | root=os.path.abspath(root).rstrip('\\') | |
204 | if _writable_dir(os.path.join(root, '_ipython')): |
|
197 | if _writable_dir(os.path.join(root, '_ipython')): | |
205 | os.environ["IPYKITROOT"] = root |
|
198 | os.environ["IPYKITROOT"] = root | |
206 |
return |
|
199 | return py3compat.cast_unicode(root, fs_encoding) | |
207 |
|
200 | |||
208 | if os.name == 'posix': |
|
201 | if os.name == 'posix': | |
209 | # Linux, Unix, AIX, OS X |
|
202 | # Linux, Unix, AIX, OS X | |
@@ -218,11 +211,11 b' def get_home_dir():' | |||||
218 | homedir = Popen('echo $HOME', shell=True, |
|
211 | homedir = Popen('echo $HOME', shell=True, | |
219 | stdout=PIPE).communicate()[0].strip() |
|
212 | stdout=PIPE).communicate()[0].strip() | |
220 | if homedir: |
|
213 | if homedir: | |
221 |
return |
|
214 | return py3compat.cast_unicode(homedir, fs_encoding) | |
222 | else: |
|
215 | else: | |
223 | raise HomeDirError('Undefined $HOME, IPython cannot proceed.') |
|
216 | raise HomeDirError('Undefined $HOME, IPython cannot proceed.') | |
224 | else: |
|
217 | else: | |
225 |
return |
|
218 | return py3compat.cast_unicode(homedir, fs_encoding) | |
226 | elif os.name == 'nt': |
|
219 | elif os.name == 'nt': | |
227 | # Now for win9x, XP, Vista, 7? |
|
220 | # Now for win9x, XP, Vista, 7? | |
228 | # For some strange reason all of these return 'nt' for os.name. |
|
221 | # For some strange reason all of these return 'nt' for os.name. | |
@@ -236,7 +229,7 b' def get_home_dir():' | |||||
236 | pass |
|
229 | pass | |
237 | else: |
|
230 | else: | |
238 | if _writable_dir(homedir): |
|
231 | if _writable_dir(homedir): | |
239 |
return |
|
232 | return py3compat.cast_unicode(homedir, fs_encoding) | |
240 |
|
233 | |||
241 | # Now look for a local home directory |
|
234 | # Now look for a local home directory | |
242 | try: |
|
235 | try: | |
@@ -245,7 +238,7 b' def get_home_dir():' | |||||
245 | pass |
|
238 | pass | |
246 | else: |
|
239 | else: | |
247 | if _writable_dir(homedir): |
|
240 | if _writable_dir(homedir): | |
248 |
return |
|
241 | return py3compat.cast_unicode(homedir, fs_encoding) | |
249 |
|
242 | |||
250 | # Now the users profile directory |
|
243 | # Now the users profile directory | |
251 | try: |
|
244 | try: | |
@@ -254,7 +247,7 b' def get_home_dir():' | |||||
254 | pass |
|
247 | pass | |
255 | else: |
|
248 | else: | |
256 | if _writable_dir(homedir): |
|
249 | if _writable_dir(homedir): | |
257 |
return |
|
250 | return py3compat.cast_unicode(homedir, fs_encoding) | |
258 |
|
251 | |||
259 | # Use the registry to get the 'My Documents' folder. |
|
252 | # Use the registry to get the 'My Documents' folder. | |
260 | try: |
|
253 | try: | |
@@ -269,7 +262,7 b' def get_home_dir():' | |||||
269 | pass |
|
262 | pass | |
270 | else: |
|
263 | else: | |
271 | if _writable_dir(homedir): |
|
264 | if _writable_dir(homedir): | |
272 |
return |
|
265 | return py3compat.cast_unicode(homedir, fs_encoding) | |
273 |
|
266 | |||
274 | # A user with a lot of unix tools in win32 may have defined $HOME. |
|
267 | # A user with a lot of unix tools in win32 may have defined $HOME. | |
275 | # Try this as a last ditch option. |
|
268 | # Try this as a last ditch option. | |
@@ -279,7 +272,7 b' def get_home_dir():' | |||||
279 | pass |
|
272 | pass | |
280 | else: |
|
273 | else: | |
281 | if _writable_dir(homedir): |
|
274 | if _writable_dir(homedir): | |
282 |
return |
|
275 | return py3compat.cast_unicode(homedir, fs_encoding) | |
283 |
|
276 | |||
284 | # If all else fails, raise HomeDirError |
|
277 | # If all else fails, raise HomeDirError | |
285 | raise HomeDirError('No valid home directory could be found') |
|
278 | raise HomeDirError('No valid home directory could be found') | |
@@ -302,7 +295,7 b' def get_xdg_dir():' | |||||
302 | # use ~/.config if not set OR empty |
|
295 | # use ~/.config if not set OR empty | |
303 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
296 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') | |
304 | if xdg and _writable_dir(xdg): |
|
297 | if xdg and _writable_dir(xdg): | |
305 |
return |
|
298 | return py3compat.cast_unicode(xdg, fs_encoding) | |
306 |
|
299 | |||
307 | return None |
|
300 | return None | |
308 |
|
301 | |||
@@ -311,7 +304,7 b' def get_ipython_dir():' | |||||
311 | """Get the IPython directory for this platform and user. |
|
304 | """Get the IPython directory for this platform and user. | |
312 |
|
305 | |||
313 | This uses the logic in `get_home_dir` to find the home directory |
|
306 | This uses the logic in `get_home_dir` to find the home directory | |
314 | and the adds .ipython to the end of the path. |
|
307 | and then adds .ipython to the end of the path. | |
315 | """ |
|
308 | """ | |
316 |
|
309 | |||
317 | env = os.environ |
|
310 | env = os.environ | |
@@ -356,13 +349,13 b' def get_ipython_dir():' | |||||
356 | " using a temp directory."%parent) |
|
349 | " using a temp directory."%parent) | |
357 | ipdir = tempfile.mkdtemp() |
|
350 | ipdir = tempfile.mkdtemp() | |
358 |
|
351 | |||
359 |
return |
|
352 | return py3compat.cast_unicode(ipdir, fs_encoding) | |
360 |
|
353 | |||
361 |
|
354 | |||
362 | def get_ipython_package_dir(): |
|
355 | def get_ipython_package_dir(): | |
363 | """Get the base directory where IPython itself is installed.""" |
|
356 | """Get the base directory where IPython itself is installed.""" | |
364 | ipdir = os.path.dirname(IPython.__file__) |
|
357 | ipdir = os.path.dirname(IPython.__file__) | |
365 |
return |
|
358 | return py3compat.cast_unicode(ipdir, fs_encoding) | |
366 |
|
359 | |||
367 |
|
360 | |||
368 | def get_ipython_module_path(module_str): |
|
361 | def get_ipython_module_path(module_str): | |
@@ -377,7 +370,7 b' def get_ipython_module_path(module_str):' | |||||
377 | mod = import_item(module_str) |
|
370 | mod = import_item(module_str) | |
378 | the_path = mod.__file__.replace('.pyc', '.py') |
|
371 | the_path = mod.__file__.replace('.pyc', '.py') | |
379 | the_path = the_path.replace('.pyo', '.py') |
|
372 | the_path = the_path.replace('.pyo', '.py') | |
380 |
return |
|
373 | return py3compat.cast_unicode(the_path, fs_encoding) | |
381 |
|
374 | |||
382 |
|
375 | |||
383 | def expand_path(s): |
|
376 | def expand_path(s): | |
@@ -442,7 +435,7 b' def filehash(path):' | |||||
442 | """Make an MD5 hash of a file, ignoring any differences in line |
|
435 | """Make an MD5 hash of a file, ignoring any differences in line | |
443 | ending characters.""" |
|
436 | ending characters.""" | |
444 | with open(path, "rU") as f: |
|
437 | with open(path, "rU") as f: | |
445 | return md5(f.read()).hexdigest() |
|
438 | return md5(py3compat.str_to_bytes(f.read())).hexdigest() | |
446 |
|
439 | |||
447 | # If the config is unmodified from the default, we'll just delete it. |
|
440 | # If the config is unmodified from the default, we'll just delete it. | |
448 | # These are consistent for 0.10.x, thankfully. We're not going to worry about |
|
441 | # These are consistent for 0.10.x, thankfully. We're not going to worry about |
@@ -10,6 +10,16 b' def decode(s, encoding=None):' | |||||
10 | def encode(u, encoding=None): |
|
10 | def encode(u, encoding=None): | |
11 | encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding() |
|
11 | encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding() | |
12 | return u.encode(encoding, "replace") |
|
12 | return u.encode(encoding, "replace") | |
|
13 | ||||
|
14 | def cast_unicode(s, encoding=None): | |||
|
15 | if isinstance(s, bytes): | |||
|
16 | return decode(s, encoding) | |||
|
17 | return s | |||
|
18 | ||||
|
19 | def cast_bytes(s, encoding=None): | |||
|
20 | if not isinstance(s, bytes): | |||
|
21 | return encode(s, encoding) | |||
|
22 | return s | |||
13 |
|
23 | |||
14 | if sys.version_info[0] >= 3: |
|
24 | if sys.version_info[0] >= 3: | |
15 | PY3 = True |
|
25 | PY3 = True | |
@@ -33,5 +43,6 b' else:' | |||||
33 | str_to_bytes = no_code |
|
43 | str_to_bytes = no_code | |
34 | bytes_to_str = no_code |
|
44 | bytes_to_str = no_code | |
35 |
|
45 | |||
36 | def execfile(fname, glob, loc): |
|
46 | def execfile(fname, glob, loc=None): | |
|
47 | loc = loc if (loc is not None) else glob | |||
37 | exec compile(open(fname).read(), fname, 'exec') in glob, loc |
|
48 | exec compile(open(fname).read(), fname, 'exec') in glob, loc |
General Comments 0
You need to be logged in to leave comments.
Login now