Skip to content
Browse files

Eliminate Str and CStr trait types except in IPython.parallel

  • Loading branch information...
1 parent a00541c commit 0784a05ac8a663dcbde27968ff5643ac13c5f762 @takluyver committed May 24, 2011
View
8 IPython/config/tests/test_configurable.py
@@ -28,7 +28,7 @@
)
from IPython.utils.traitlets import (
- Int, Float, Str
+ Int, Float, Unicode
)
from IPython.config.loader import Config
@@ -42,7 +42,7 @@
class MyConfigurable(Configurable):
a = Int(1, config=True, help="The integer a.")
b = Float(1.0, config=True, help="The integer b.")
- c = Str('no config')
+ c = Unicode('no config')
mc_help=u"""MyConfigurable options
@@ -56,11 +56,11 @@ class MyConfigurable(Configurable):
class Foo(Configurable):
a = Int(0, config=True, help="The integer a.")
- b = Str('nope', config=True)
+ b = Unicode('nope', config=True)
class Bar(Foo):
- b = Str('gotit', config=False, help="The string b.")
+ b = Unicode('gotit', config=False, help="The string b.")
c = Float(config=True, help="The string c.")
View
40 IPython/core/formatters.py
@@ -28,7 +28,7 @@
# Our own imports
from IPython.config.configurable import Configurable
from IPython.lib import pretty
-from IPython.utils.traitlets import Bool, Dict, Int, Str, CStr
+from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode
#-----------------------------------------------------------------------------
@@ -191,11 +191,11 @@ class BaseFormatter(Configurable):
returned and this format type is not used.
"""
- format_type = Str('text/plain')
+ format_type = Unicode('text/plain')
enabled = Bool(True, config=True)
- print_method = Str('__repr__')
+ print_method = Unicode('__repr__')
# The singleton printers.
# Maps the IDs of the builtin singleton objects to the format functions.
@@ -337,14 +337,14 @@ def dtype_pprinter(obj, p, cycle):
"""
# The format type of data returned.
- format_type = Str('text/plain')
+ format_type = Unicode('text/plain')
# This subclass ignores this attribute as it always need to return
# something.
enabled = Bool(True, config=False)
# Look for a _repr_pretty_ methods to use for pretty printing.
- print_method = Str('_repr_pretty_')
+ print_method = Unicode('_repr_pretty_')
# Whether to pretty-print or not.
pprint = Bool(True, config=True)
@@ -356,12 +356,12 @@ def dtype_pprinter(obj, p, cycle):
max_width = Int(79, config=True)
# The newline character.
- newline = Str('\n', config=True)
+ newline = Unicode('\n', config=True)
# format-string for pprinting floats
- float_format = Str('%r')
+ float_format = Unicode('%r')
# setter for float precision, either int or direct format-string
- float_precision = CStr('', config=True)
+ float_precision = CUnicode('', config=True)
def _float_precision_changed(self, name, old, new):
"""float_precision changed, set float_format accordingly.
@@ -454,9 +454,9 @@ class HTMLFormatter(BaseFormatter):
could be injected into an existing DOM. It should *not* include the
```<html>`` or ```<body>`` tags.
"""
- format_type = Str('text/html')
+ format_type = Unicode('text/html')
- print_method = Str('_repr_html_')
+ print_method = Unicode('_repr_html_')
class SVGFormatter(BaseFormatter):
@@ -471,9 +471,9 @@ class SVGFormatter(BaseFormatter):
```<svg>``` tags, that could be injected into an existing DOM. It should
*not* include the ```<html>`` or ```<body>`` tags.
"""
- format_type = Str('image/svg+xml')
+ format_type = Unicode('image/svg+xml')
- print_method = Str('_repr_svg_')
+ print_method = Unicode('_repr_svg_')
class PNGFormatter(BaseFormatter):
@@ -487,9 +487,9 @@ class PNGFormatter(BaseFormatter):
The return value of this formatter should be raw PNG data, *not*
base64 encoded.
"""
- format_type = Str('image/png')
+ format_type = Unicode('image/png')
- print_method = Str('_repr_png_')
+ print_method = Unicode('_repr_png_')
class LatexFormatter(BaseFormatter):
@@ -503,9 +503,9 @@ class LatexFormatter(BaseFormatter):
The return value of this formatter should be a valid LaTeX equation,
enclosed in either ```$``` or ```$$```.
"""
- format_type = Str('text/latex')
+ format_type = Unicode('text/latex')
- print_method = Str('_repr_latex_')
+ print_method = Unicode('_repr_latex_')
class JSONFormatter(BaseFormatter):
@@ -518,9 +518,9 @@ class JSONFormatter(BaseFormatter):
The return value of this formatter should be a valid JSON string.
"""
- format_type = Str('application/json')
+ format_type = Unicode('application/json')
- print_method = Str('_repr_json_')
+ print_method = Unicode('_repr_json_')
class JavascriptFormatter(BaseFormatter):
@@ -534,9 +534,9 @@ class JavascriptFormatter(BaseFormatter):
The return value of this formatter should be valid Javascript code and
should *not* be enclosed in ```<script>``` tags.
"""
- format_type = Str('application/javascript')
+ format_type = Unicode('application/javascript')
- print_method = Str('_repr_javascript_')
+ print_method = Unicode('_repr_javascript_')
FormatterABC.register(BaseFormatter)
FormatterABC.register(PlainTextFormatter)
View
27 IPython/core/interactiveshell.py
@@ -70,7 +70,7 @@
from IPython.utils.strdispatch import StrDispatch
from IPython.utils.syspathcontext import prepended_to_syspath
from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
-from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
+from IPython.utils.traitlets import (Int, CBool, CaselessStrEnum, Enum,
List, Unicode, Instance, Type)
from IPython.utils.warn import warn, error, fatal
import IPython.core.hooks
@@ -122,16 +122,16 @@ def get_default_colors():
return 'Linux'
-class SeparateStr(Str):
- """A Str subclass to validate separate_in, separate_out, etc.
+class SeparateUnicode(Unicode):
+ """A Unicode subclass to validate separate_in, separate_out, etc.
- This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
+ This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
"""
def validate(self, obj, value):
if value == '0': value = ''
value = value.replace('\\n','\n')
- return super(SeparateStr, self).validate(obj, value)
+ return super(SeparateUnicode, self).validate(obj, value)
class ReadlineNoRecord(object):
@@ -294,9 +294,9 @@ def _exiter_default(self):
"""
)
- prompt_in1 = Str('In [\\#]: ', config=True)
- prompt_in2 = Str(' .\\D.: ', config=True)
- prompt_out = Str('Out[\\#]: ', config=True)
+ prompt_in1 = Unicode('In [\\#]: ', config=True)
+ prompt_in2 = Unicode(' .\\D.: ', config=True)
+ prompt_out = Unicode('Out[\\#]: ', config=True)
prompts_pad_left = CBool(True, config=True)
quiet = CBool(False, config=True)
@@ -307,7 +307,7 @@ def _exiter_default(self):
readline_use = CBool(True, config=True)
readline_merge_completions = CBool(True, config=True)
readline_omit__names = Enum((0,1,2), default_value=2, config=True)
- readline_remove_delims = Str('-/~', config=True)
+ readline_remove_delims = Unicode('-/~', config=True)
# don't use \M- bindings by default, because they
# conflict with 8-bit encodings. See gh-58,gh-88
readline_parse_and_bind = List([
@@ -327,9 +327,9 @@ def _exiter_default(self):
# TODO: this part of prompt management should be moved to the frontends.
# Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
- separate_in = SeparateStr('\n', config=True)
- separate_out = SeparateStr('', config=True)
- separate_out2 = SeparateStr('', config=True)
+ separate_in = SeparateUnicode('\n', config=True)
+ separate_out = SeparateUnicode('', config=True)
+ separate_out2 = SeparateUnicode('', config=True)
wildcards_case_sensitive = CBool(True, config=True)
xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
default_value='Context', config=True)
@@ -1670,7 +1670,8 @@ def init_readline(self):
# Remove some chars from the delimiters list. If we encounter
# unicode chars, discard them.
delims = readline.get_completer_delims().encode("ascii", "ignore")
- delims = delims.translate(None, self.readline_remove_delims)
+ for d in self.readline_remove_delims:
+ delims = delims.replace(d, "")
delims = delims.replace(ESC_MAGIC, '')
readline.set_completer_delims(delims)
# otherwise we end up with a monster history after a while:
View
8 IPython/core/magic.py
@@ -3472,25 +3472,25 @@ def magic_precision(self, s=''):
In [1]: from math import pi
In [2]: %precision 3
- Out[2]: '%.3f'
+ Out[2]: u'%.3f'
In [3]: pi
Out[3]: 3.142
In [4]: %precision %i
- Out[4]: '%i'
+ Out[4]: u'%i'
In [5]: pi
Out[5]: 3
In [6]: %precision %e
- Out[6]: '%e'
+ Out[6]: u'%e'
In [7]: pi**10
Out[7]: 9.364805e+04
In [8]: %precision
- Out[8]: '%r'
+ Out[8]: u'%r'
In [9]: pi**10
Out[9]: 93648.047476082982
View
18 IPython/core/prefilter.py
@@ -36,7 +36,7 @@
from IPython.core.splitinput import split_user_input
from IPython.core import page
-from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance
+from IPython.utils.traitlets import List, Int, Any, Unicode, CBool, Bool, Instance
from IPython.utils.text import make_quoted_expr
from IPython.utils.autoattr import auto_attr
@@ -756,7 +756,7 @@ def check(self, line_info):
class PrefilterHandler(Configurable):
- handler_name = Str('normal')
+ handler_name = Unicode('normal')
esc_strings = List([])
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
@@ -797,7 +797,7 @@ def __str__(self):
class AliasHandler(PrefilterHandler):
- handler_name = Str('alias')
+ handler_name = Unicode('alias')
def handle(self, line_info):
"""Handle alias input lines. """
@@ -812,7 +812,7 @@ def handle(self, line_info):
class ShellEscapeHandler(PrefilterHandler):
- handler_name = Str('shell')
+ handler_name = Unicode('shell')
esc_strings = List([ESC_SHELL, ESC_SH_CAP])
def handle(self, line_info):
@@ -839,7 +839,7 @@ def handle(self, line_info):
class MacroHandler(PrefilterHandler):
- handler_name = Str("macro")
+ handler_name = Unicode("macro")
def handle(self, line_info):
obj = self.shell.user_ns.get(line_info.ifun)
@@ -850,7 +850,7 @@ def handle(self, line_info):
class MagicHandler(PrefilterHandler):
- handler_name = Str('magic')
+ handler_name = Unicode('magic')
esc_strings = List([ESC_MAGIC])
def handle(self, line_info):
@@ -864,7 +864,7 @@ def handle(self, line_info):
class AutoHandler(PrefilterHandler):
- handler_name = Str('auto')
+ handler_name = Unicode('auto')
esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
def handle(self, line_info):
@@ -924,7 +924,7 @@ def handle(self, line_info):
class HelpHandler(PrefilterHandler):
- handler_name = Str('help')
+ handler_name = Unicode('help')
esc_strings = List([ESC_HELP])
def handle(self, line_info):
@@ -962,7 +962,7 @@ def handle(self, line_info):
class EmacsHandler(PrefilterHandler):
- handler_name = Str('emacs')
+ handler_name = Unicode('emacs')
esc_strings = List([])
def handle(self, line_info):
View
8 IPython/core/tests/test_magic.py
@@ -448,15 +448,15 @@ def doctest_precision():
In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
In [2]: %precision 5
- Out[2]: '%.5f'
+ Out[2]: u'%.5f'
In [3]: f.float_format
- Out[3]: '%.5f'
+ Out[3]: u'%.5f'
In [4]: %precision %e
- Out[4]: '%e'
+ Out[4]: u'%e'
In [5]: f(3.1415927)
- Out[5]: '3.141593e+00'
+ Out[5]: u'3.141593e+00'
"""
View
15 IPython/frontend/qt/console/ipython_widget.py
@@ -21,7 +21,7 @@
from IPython.core.inputsplitter import IPythonInputSplitter, \
transform_ipy_prompt
from IPython.core.usage import default_gui_banner
-from IPython.utils.traitlets import Bool, Str, Unicode
+from IPython.utils.traitlets import Bool, Unicode
from frontend_widget import FrontendWidget
import styles
@@ -82,19 +82,18 @@ class IPythonWidget(FrontendWidget):
3. IPython: .error, .in-prompt, .out-prompt, etc
""")
-
- syntax_style = Str(config=True,
+ syntax_style = Unicode(config=True,
help="""
If not empty, use this Pygments style for syntax highlighting. Otherwise,
the style sheet is queried for Pygments style information.
""")
# Prompts.
- in_prompt = Str(default_in_prompt, config=True)
- out_prompt = Str(default_out_prompt, config=True)
- input_sep = Str(default_input_sep, config=True)
- output_sep = Str(default_output_sep, config=True)
- output_sep2 = Str(default_output_sep2, config=True)
+ in_prompt = Unicode(default_in_prompt, config=True)
+ out_prompt = Unicode(default_out_prompt, config=True)
+ input_sep = Unicode(default_input_sep, config=True)
+ output_sep = Unicode(default_output_sep, config=True)
+ output_sep2 = Unicode(default_output_sep2, config=True)
# FrontendWidget protected class variables.
_input_splitter_class = IPythonInputSplitter
View
2 IPython/frontend/terminal/embed.py
@@ -33,7 +33,7 @@
from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
from IPython.frontend.terminal.ipapp import load_default_config
-from IPython.utils.traitlets import Bool, Str, CBool, Unicode
+from IPython.utils.traitlets import Bool, CBool, Unicode
from IPython.utils.io import ask_yes_no
View
2 IPython/frontend/terminal/interactiveshell.py
@@ -31,7 +31,7 @@
from IPython.utils.process import abbrev_cwd
from IPython.utils.warn import warn
from IPython.utils.text import num_ini_spaces
-from IPython.utils.traitlets import Int, Str, CBool, Unicode
+from IPython.utils.traitlets import Int, CBool, Unicode
#-----------------------------------------------------------------------------
# Utilities
View
18 IPython/utils/tests/test_traitlets.py
@@ -25,8 +25,8 @@
from unittest import TestCase
from IPython.utils.traitlets import (
- HasTraits, MetaHasTraits, TraitType, Any, CStr,
- Int, Long, Float, Complex, Str, Unicode, TraitError,
+ HasTraits, MetaHasTraits, TraitType, Any, CBytes,
+ Int, Long, Float, Complex, Bytes, Unicode, TraitError,
Undefined, Type, This, Instance, TCPAddress, List, Tuple
)
@@ -700,13 +700,13 @@ class TestComplex(TraitTestBase):
_bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None]
-class StringTrait(HasTraits):
+class BytesTrait(HasTraits):
- value = Str('string')
+ value = Bytes('string')
-class TestString(TraitTestBase):
+class TestBytes(TraitTestBase):
- obj = StringTrait()
+ obj = BytesTrait()
_default_value = 'string'
_good_values = ['10', '-10', '10L',
@@ -781,7 +781,7 @@ class TestTupleTrait(TraitTestBase):
def test_invalid_args(self):
self.assertRaises(TypeError, Tuple, 5)
self.assertRaises(TypeError, Tuple, default_value='hello')
- t = Tuple(Int, CStr, default_value=(1,5))
+ t = Tuple(Int, CBytes, default_value=(1,5))
class LooseTupleTrait(HasTraits):
@@ -798,12 +798,12 @@ class TestLooseTupleTrait(TraitTestBase):
def test_invalid_args(self):
self.assertRaises(TypeError, Tuple, 5)
self.assertRaises(TypeError, Tuple, default_value='hello')
- t = Tuple(Int, CStr, default_value=(1,5))
+ t = Tuple(Int, CBytes, default_value=(1,5))
class MultiTupleTrait(HasTraits):
- value = Tuple(Int, Str, default_value=[99,'bottles'])
+ value = Tuple(Int, Bytes, default_value=[99,'bottles'])
class TestMultiTuple(TraitTestBase):
View
9 IPython/utils/traitlets.py
@@ -951,7 +951,9 @@ def validate (self, obj, value):
except:
self.error(obj, value)
-
+# We should always be explicit about whether we're using bytes or unicode, both
+# for Python 3 conversion and for reliable unicode behaviour on Python 2. So
+# we don't have a Str type.
class Bytes(TraitType):
"""A trait for strings."""
@@ -996,11 +998,6 @@ def validate(self, obj, value):
return unicode(value)
except:
self.error(obj, value)
-
-if sys.version_info[0] < 3:
- Str, CStr = Bytes, CBytes
-else:
- Str, CStr = Unicode, CUnicode
class Bool(TraitType):

0 comments on commit 0784a05

Please sign in to comment.
Something went wrong with that request. Please try again.