##// END OF EJS Templates
Use StringIO.StringIO on Python 2....
Thomas Kluyver -
Show More
@@ -27,8 +27,6 b' Authors:'
27 import abc
27 import abc
28 import sys
28 import sys
29 import warnings
29 import warnings
30 # We must use StringIO, as cStringIO doesn't handle unicode properly.
31 from io import StringIO
32
30
33 # Our own imports
31 # Our own imports
34 from IPython.config.configurable import Configurable
32 from IPython.config.configurable import Configurable
@@ -36,7 +34,12 b' from IPython.lib import pretty'
36 from IPython.utils.traitlets import (
34 from IPython.utils.traitlets import (
37 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
35 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
38 )
36 )
39 from IPython.utils.py3compat import unicode_to_str, with_metaclass
37 from IPython.utils.py3compat import unicode_to_str, with_metaclass, PY3
38
39 if PY3:
40 from io import StringIO
41 else:
42 from StringIO import StringIO
40
43
41
44
42 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
@@ -1,14 +1,18 b''
1 import abc
1 import abc
2 import functools
2 import functools
3 import re
3 import re
4 from io import StringIO
5
4
6 from IPython.core.splitinput import LineInfo
5 from IPython.core.splitinput import LineInfo
7 from IPython.utils import tokenize2
6 from IPython.utils import tokenize2
8 from IPython.utils.openpy import cookie_comment_re
7 from IPython.utils.openpy import cookie_comment_re
9 from IPython.utils.py3compat import with_metaclass
8 from IPython.utils.py3compat import with_metaclass, PY3
10 from IPython.utils.tokenize2 import generate_tokens, untokenize, TokenError
9 from IPython.utils.tokenize2 import generate_tokens, untokenize, TokenError
11
10
11 if PY3:
12 from io import StringIO
13 else:
14 from StringIO import StringIO
15
12 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
13 # Globals
17 # Globals
14 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
@@ -20,7 +20,6 b' import bdb'
20 import os
20 import os
21 import sys
21 import sys
22 import time
22 import time
23 from io import StringIO
24
23
25 # cProfile was added in Python2.5
24 # cProfile was added in Python2.5
26 try:
25 try:
@@ -43,7 +42,7 b' from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,'
43 line_cell_magic, on_off, needs_local_scope)
42 line_cell_magic, on_off, needs_local_scope)
44 from IPython.testing.skipdoctest import skip_doctest
43 from IPython.testing.skipdoctest import skip_doctest
45 from IPython.utils import py3compat
44 from IPython.utils import py3compat
46 from IPython.utils.py3compat import builtin_mod, iteritems
45 from IPython.utils.py3compat import builtin_mod, iteritems, PY3
47 from IPython.utils.contexts import preserve_keys
46 from IPython.utils.contexts import preserve_keys
48 from IPython.utils.io import capture_output
47 from IPython.utils.io import capture_output
49 from IPython.utils.ipstruct import Struct
48 from IPython.utils.ipstruct import Struct
@@ -52,6 +51,10 b' from IPython.utils.path import get_py_filename, unquote_filename, shellglob'
52 from IPython.utils.timing import clock, clock2
51 from IPython.utils.timing import clock, clock2
53 from IPython.utils.warn import warn, error
52 from IPython.utils.warn import warn, error
54
53
54 if PY3:
55 from io import StringIO
56 else:
57 from StringIO import StringIO
55
58
56 #-----------------------------------------------------------------------------
59 #-----------------------------------------------------------------------------
57 # Magic implementation classes
60 # Magic implementation classes
@@ -28,7 +28,6 b' import sys'
28 import tempfile
28 import tempfile
29 import unittest
29 import unittest
30 from os.path import join
30 from os.path import join
31 from io import StringIO
32
31
33 # third-party
32 # third-party
34 import nose.tools as nt
33 import nose.tools as nt
@@ -37,7 +36,12 b' import nose.tools as nt'
37 from IPython.testing.decorators import skipif, skip_win32, onlyif_unicode_paths
36 from IPython.testing.decorators import skipif, skip_win32, onlyif_unicode_paths
38 from IPython.testing import tools as tt
37 from IPython.testing import tools as tt
39 from IPython.utils import io
38 from IPython.utils import io
40 from IPython.utils.py3compat import unicode_type
39 from IPython.utils.py3compat import unicode_type, PY3
40
41 if PY3:
42 from io import StringIO
43 else:
44 from StringIO import StringIO
41
45
42 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
43 # Globals
47 # Globals
@@ -12,7 +12,6 b' from __future__ import absolute_import'
12 import io
12 import io
13 import os
13 import os
14 import sys
14 import sys
15 from io import StringIO
16 from unittest import TestCase
15 from unittest import TestCase
17
16
18 try:
17 try:
@@ -38,6 +37,11 b' from IPython.utils.io import capture_output'
38 from IPython.utils.tempdir import TemporaryDirectory
37 from IPython.utils.tempdir import TemporaryDirectory
39 from IPython.utils.process import find_cmd
38 from IPython.utils.process import find_cmd
40
39
40 if py3compat.PY3:
41 from io import StringIO
42 else:
43 from StringIO import StringIO
44
41 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
42 # Test functions begin
46 # Test functions begin
43 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
@@ -18,13 +18,18 b' import tempfile'
18 import shutil
18 import shutil
19 import random
19 import random
20 import time
20 import time
21 from io import StringIO
22
21
23 import nose.tools as nt
22 import nose.tools as nt
24 import IPython.testing.tools as tt
23 import IPython.testing.tools as tt
25
24
26 from IPython.extensions.autoreload import AutoreloadMagics
25 from IPython.extensions.autoreload import AutoreloadMagics
27 from IPython.core.hooks import TryNext
26 from IPython.core.hooks import TryNext
27 from IPython.utils.py3compat import PY3
28
29 if PY3:
30 from io import StringIO
31 else:
32 from StringIO import StringIO
28
33
29 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
30 # Test fixture
35 # Test fixture
@@ -1,11 +1,15 b''
1 from io import StringIO
2
3 import numpy as np
1 import numpy as np
4 from IPython.testing.decorators import skip_without
2 from IPython.testing.decorators import skip_without
5 from IPython.extensions import rmagic
3 from IPython.extensions import rmagic
4 from IPython.utils.py3compat import PY3
6 from rpy2 import rinterface
5 from rpy2 import rinterface
7 import nose.tools as nt
6 import nose.tools as nt
8
7
8 if PY3:
9 from io import StringIO
10 else:
11 from StringIO import StringIO
12
9 ip = get_ipython()
13 ip = get_ipython()
10 ip.magic('load_ext rmagic')
14 ip.magic('load_ext rmagic')
11
15
@@ -11,7 +11,6 b''
11 from __future__ import print_function
11 from __future__ import print_function
12
12
13 # Standard library imports
13 # Standard library imports
14 from io import StringIO
15 import sys
14 import sys
16 import unittest
15 import unittest
17
16
@@ -23,6 +22,11 b' from IPython.testing.decorators import skipif_not_matplotlib'
23 from IPython.utils.io import capture_output
22 from IPython.utils.io import capture_output
24 from IPython.utils import py3compat
23 from IPython.utils import py3compat
25
24
25 if py3compat.PY3:
26 from io import StringIO
27 else:
28 from StringIO import StringIO
29
26 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
27 # Test case
31 # Test case
28 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
@@ -109,9 +109,15 b' import sys'
109 import types
109 import types
110 import re
110 import re
111 import datetime
111 import datetime
112 from io import StringIO
113 from collections import deque
112 from collections import deque
114
113
114 from IPython.utils.py3compat import PY3
115
116 if PY3:
117 from io import StringIO
118 else:
119 from StringIO import StringIO
120
115
121
116 __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
122 __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
117 'for_type', 'for_type_by_name']
123 'for_type', 'for_type_by_name']
@@ -8,19 +8,23 b' from __future__ import print_function'
8 VERBOSE = True
8 VERBOSE = True
9
9
10 # stdlib imports
10 # stdlib imports
11 import io
12 import sys
11 import sys
13 import unittest
12 import unittest
14
13
15 # IPython imports
14 # IPython imports
16 from IPython.lib import irunner
15 from IPython.lib import irunner
17 from IPython.utils.py3compat import doctest_refactor_print
16 from IPython.utils.py3compat import doctest_refactor_print, PY3
17
18 if PY3:
19 from io import StringIO
20 else:
21 from StringIO import StringIO
18
22
19 # Testing code begins
23 # Testing code begins
20 class RunnerTestCase(unittest.TestCase):
24 class RunnerTestCase(unittest.TestCase):
21
25
22 def setUp(self):
26 def setUp(self):
23 self.out = io.StringIO()
27 self.out = StringIO()
24 #self.out = sys.stdout
28 #self.out = sys.stdout
25
29
26 def _test_runner(self,runner,source,output):
30 def _test_runner(self,runner,source,output):
@@ -7,7 +7,6 b' from __future__ import print_function'
7 VERBOSE = True
7 VERBOSE = True
8
8
9 # stdlib imports
9 # stdlib imports
10 import io
11 import sys
10 import sys
12 import unittest
11 import unittest
13 import re
12 import re
@@ -15,6 +14,12 b' import re'
15 # IPython imports
14 # IPython imports
16 from IPython.lib import irunner
15 from IPython.lib import irunner
17 from IPython.testing import decorators
16 from IPython.testing import decorators
17 from IPython.utils.py3compat import PY3
18
19 if PY3:
20 from io import StringIO
21 else:
22 from StringIO import StringIO
18
23
19 def pylab_not_importable():
24 def pylab_not_importable():
20 """Test if importing pylab fails. (For example, when having no display)"""
25 """Test if importing pylab fails. (For example, when having no display)"""
@@ -28,7 +33,7 b' def pylab_not_importable():'
28 class RunnerTestCase(unittest.TestCase):
33 class RunnerTestCase(unittest.TestCase):
29
34
30 def setUp(self):
35 def setUp(self):
31 self.out = io.StringIO()
36 self.out = StringIO()
32 #self.out = sys.stdout
37 #self.out = sys.stdout
33
38
34 def _test_runner(self,runner,source,output):
39 def _test_runner(self,runner,source,output):
@@ -15,10 +15,15 b' Module with tests for debug'
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import sys
17 import sys
18 from io import StringIO
19
18
20 from ...tests.base import TestsBase
19 from ...tests.base import TestsBase
21 from ..debug import DebugWriter
20 from ..debug import DebugWriter
21 from IPython.utils.py3compat import PY3
22
23 if PY3:
24 from io import StringIO
25 else:
26 from StringIO import StringIO
22
27
23
28
24 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
@@ -16,10 +16,15 b' Module with tests for files'
16
16
17 import sys
17 import sys
18 import os
18 import os
19 from io import StringIO
20
19
21 from ...tests.base import TestsBase
20 from ...tests.base import TestsBase
22 from ..files import FilesWriter
21 from ..files import FilesWriter
22 from IPython.utils.py3compat import PY3
23
24 if PY3:
25 from io import StringIO
26 else:
27 from StringIO import StringIO
23
28
24
29
25 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
@@ -15,10 +15,15 b' Module with tests for stdout'
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import sys
17 import sys
18 from io import StringIO
19
18
20 from ...tests.base import TestsBase
19 from ...tests.base import TestsBase
21 from ..stdout import StdoutWriter
20 from ..stdout import StdoutWriter
21 from IPython.utils.py3compat import PY3
22
23 if PY3:
24 from io import StringIO
25 else:
26 from StringIO import StringIO
22
27
23
28
24 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
@@ -16,7 +16,6 b' from __future__ import print_function'
16 import sys
16 import sys
17 import tempfile
17 import tempfile
18 import time
18 import time
19 from io import StringIO
20
19
21 from nose import SkipTest
20 from nose import SkipTest
22
21
@@ -58,7 +58,6 b' from __future__ import print_function'
58 #-----------------------------------------------------------------------------
58 #-----------------------------------------------------------------------------
59
59
60 # Stdlib
60 # Stdlib
61 import io
62 import os
61 import os
63 import re
62 import re
64 import sys
63 import sys
@@ -84,6 +83,12 b" matplotlib.use('Agg')"
84 from IPython import Config, InteractiveShell
83 from IPython import Config, InteractiveShell
85 from IPython.core.profiledir import ProfileDir
84 from IPython.core.profiledir import ProfileDir
86 from IPython.utils import io
85 from IPython.utils import io
86 from IPython.utils.py3compat import PY3
87
88 if PY3:
89 from io import StringIO
90 else:
91 from StringIO import StringIO
87
92
88 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
89 # Globals
94 # Globals
@@ -193,7 +198,7 b' class EmbeddedSphinxShell(object):'
193
198
194 def __init__(self):
199 def __init__(self):
195
200
196 self.cout = io.StringIO()
201 self.cout = StringIO()
197
202
198
203
199 # Create config object for IPython
204 # Create config object for IPython
@@ -29,7 +29,6 b' import traceback'
29 import unittest
29 import unittest
30
30
31 from inspect import getmodule
31 from inspect import getmodule
32 from io import StringIO
33
32
34 # We are overriding the default doctest runner, so we need to import a few
33 # We are overriding the default doctest runner, so we need to import a few
35 # things from doctest directly
34 # things from doctest directly
@@ -46,7 +45,12 b' from nose.plugins import doctests, Plugin'
46 from nose.util import anyp, getpackage, test_address, resolve_name, tolist
45 from nose.util import anyp, getpackage, test_address, resolve_name, tolist
47
46
48 # Our own imports
47 # Our own imports
49 from IPython.utils.py3compat import builtin_mod
48 from IPython.utils.py3compat import builtin_mod, PY3
49
50 if PY3:
51 from io import StringIO
52 else:
53 from StringIO import StringIO
50
54
51 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
52 # Module globals and other constants
56 # Module globals and other constants
@@ -38,7 +38,6 b" _scheme_default = 'Linux'"
38
38
39
39
40 # Imports
40 # Imports
41 import io
42 import keyword
41 import keyword
43 import os
42 import os
44 import sys
43 import sys
@@ -53,6 +52,12 b' except AttributeError:'
53 generate_tokens = tokenize._tokenize
52 generate_tokens = tokenize._tokenize
54
53
55 from IPython.utils.coloransi import *
54 from IPython.utils.coloransi import *
55 from IPython.utils.py3compat import PY3
56
57 if PY3:
58 from io import StringIO
59 else:
60 from StringIO import StringIO
56
61
57 #############################################################################
62 #############################################################################
58 ### Python Source Parser (does Hilighting)
63 ### Python Source Parser (does Hilighting)
@@ -143,13 +148,13 b' class Parser:'
143
148
144 string_output = 0
149 string_output = 0
145 if out == 'str' or self.out == 'str' or \
150 if out == 'str' or self.out == 'str' or \
146 isinstance(self.out,io.StringIO):
151 isinstance(self.out,StringIO):
147 # XXX - I don't really like this state handling logic, but at this
152 # XXX - I don't really like this state handling logic, but at this
148 # point I don't want to make major changes, so adding the
153 # point I don't want to make major changes, so adding the
149 # isinstance() check is the simplest I can do to ensure correct
154 # isinstance() check is the simplest I can do to ensure correct
150 # behavior.
155 # behavior.
151 out_old = self.out
156 out_old = self.out
152 self.out = io.StringIO()
157 self.out = StringIO()
153 string_output = 1
158 string_output = 1
154 elif out is not None:
159 elif out is not None:
155 self.out = out
160 self.out = out
@@ -183,7 +188,7 b' class Parser:'
183
188
184 # parse the source and write it
189 # parse the source and write it
185 self.pos = 0
190 self.pos = 0
186 text = io.StringIO(self.raw)
191 text = StringIO(self.raw)
187
192
188 error = False
193 error = False
189 try:
194 try:
@@ -16,7 +16,13 b' from __future__ import print_function, absolute_import'
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 import sys
18 import sys
19 from io import StringIO
19
20 from IPython.utils.py3compat import PY3
21
22 if PY3:
23 from io import StringIO
24 else:
25 from StringIO import StringIO
20
26
21 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
22 # Classes and functions
28 # Classes and functions
@@ -15,14 +15,18 b' from __future__ import print_function'
15
15
16 import sys
16 import sys
17
17
18 from io import StringIO
19 from subprocess import Popen, PIPE
18 from subprocess import Popen, PIPE
20 import unittest
19 import unittest
21
20
22 import nose.tools as nt
21 import nose.tools as nt
23
22
24 from IPython.utils.io import Tee, capture_output
23 from IPython.utils.io import Tee, capture_output
25 from IPython.utils.py3compat import doctest_refactor_print
24 from IPython.utils.py3compat import doctest_refactor_print, PY3
25
26 if PY3:
27 from io import StringIO
28 else:
29 from StringIO import StringIO
26
30
27 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
28 # Tests
32 # Tests
General Comments 0
You need to be logged in to leave comments. Login now