##// END OF EJS Templates
Eliminate Str and CStr trait types except in IPython.parallel
Thomas Kluyver -
Show More
@@ -28,7 +28,7 b' from IPython.config.configurable import ('
28 28 )
29 29
30 30 from IPython.utils.traitlets import (
31 Int, Float, Str
31 Int, Float, Unicode
32 32 )
33 33
34 34 from IPython.config.loader import Config
@@ -42,7 +42,7 b' from IPython.config.loader import Config'
42 42 class MyConfigurable(Configurable):
43 43 a = Int(1, config=True, help="The integer a.")
44 44 b = Float(1.0, config=True, help="The integer b.")
45 c = Str('no config')
45 c = Unicode('no config')
46 46
47 47
48 48 mc_help=u"""MyConfigurable options
@@ -56,11 +56,11 b' MyConfigurable.b : Float'
56 56
57 57 class Foo(Configurable):
58 58 a = Int(0, config=True, help="The integer a.")
59 b = Str('nope', config=True)
59 b = Unicode('nope', config=True)
60 60
61 61
62 62 class Bar(Foo):
63 b = Str('gotit', config=False, help="The string b.")
63 b = Unicode('gotit', config=False, help="The string b.")
64 64 c = Float(config=True, help="The string c.")
65 65
66 66
@@ -28,7 +28,7 b' from StringIO import StringIO'
28 28 # Our own imports
29 29 from IPython.config.configurable import Configurable
30 30 from IPython.lib import pretty
31 from IPython.utils.traitlets import Bool, Dict, Int, Str, CStr
31 from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode
32 32
33 33
34 34 #-----------------------------------------------------------------------------
@@ -191,11 +191,11 b' class BaseFormatter(Configurable):'
191 191 returned and this format type is not used.
192 192 """
193 193
194 format_type = Str('text/plain')
194 format_type = Unicode('text/plain')
195 195
196 196 enabled = Bool(True, config=True)
197 197
198 print_method = Str('__repr__')
198 print_method = Unicode('__repr__')
199 199
200 200 # The singleton printers.
201 201 # Maps the IDs of the builtin singleton objects to the format functions.
@@ -337,14 +337,14 b' class PlainTextFormatter(BaseFormatter):'
337 337 """
338 338
339 339 # The format type of data returned.
340 format_type = Str('text/plain')
340 format_type = Unicode('text/plain')
341 341
342 342 # This subclass ignores this attribute as it always need to return
343 343 # something.
344 344 enabled = Bool(True, config=False)
345 345
346 346 # Look for a _repr_pretty_ methods to use for pretty printing.
347 print_method = Str('_repr_pretty_')
347 print_method = Unicode('_repr_pretty_')
348 348
349 349 # Whether to pretty-print or not.
350 350 pprint = Bool(True, config=True)
@@ -356,12 +356,12 b' class PlainTextFormatter(BaseFormatter):'
356 356 max_width = Int(79, config=True)
357 357
358 358 # The newline character.
359 newline = Str('\n', config=True)
359 newline = Unicode('\n', config=True)
360 360
361 361 # format-string for pprinting floats
362 float_format = Str('%r')
362 float_format = Unicode('%r')
363 363 # setter for float precision, either int or direct format-string
364 float_precision = CStr('', config=True)
364 float_precision = CUnicode('', config=True)
365 365
366 366 def _float_precision_changed(self, name, old, new):
367 367 """float_precision changed, set float_format accordingly.
@@ -454,9 +454,9 b' class HTMLFormatter(BaseFormatter):'
454 454 could be injected into an existing DOM. It should *not* include the
455 455 ```<html>`` or ```<body>`` tags.
456 456 """
457 format_type = Str('text/html')
457 format_type = Unicode('text/html')
458 458
459 print_method = Str('_repr_html_')
459 print_method = Unicode('_repr_html_')
460 460
461 461
462 462 class SVGFormatter(BaseFormatter):
@@ -471,9 +471,9 b' class SVGFormatter(BaseFormatter):'
471 471 ```<svg>``` tags, that could be injected into an existing DOM. It should
472 472 *not* include the ```<html>`` or ```<body>`` tags.
473 473 """
474 format_type = Str('image/svg+xml')
474 format_type = Unicode('image/svg+xml')
475 475
476 print_method = Str('_repr_svg_')
476 print_method = Unicode('_repr_svg_')
477 477
478 478
479 479 class PNGFormatter(BaseFormatter):
@@ -487,9 +487,9 b' class PNGFormatter(BaseFormatter):'
487 487 The return value of this formatter should be raw PNG data, *not*
488 488 base64 encoded.
489 489 """
490 format_type = Str('image/png')
490 format_type = Unicode('image/png')
491 491
492 print_method = Str('_repr_png_')
492 print_method = Unicode('_repr_png_')
493 493
494 494
495 495 class LatexFormatter(BaseFormatter):
@@ -503,9 +503,9 b' class LatexFormatter(BaseFormatter):'
503 503 The return value of this formatter should be a valid LaTeX equation,
504 504 enclosed in either ```$``` or ```$$```.
505 505 """
506 format_type = Str('text/latex')
506 format_type = Unicode('text/latex')
507 507
508 print_method = Str('_repr_latex_')
508 print_method = Unicode('_repr_latex_')
509 509
510 510
511 511 class JSONFormatter(BaseFormatter):
@@ -518,9 +518,9 b' class JSONFormatter(BaseFormatter):'
518 518
519 519 The return value of this formatter should be a valid JSON string.
520 520 """
521 format_type = Str('application/json')
521 format_type = Unicode('application/json')
522 522
523 print_method = Str('_repr_json_')
523 print_method = Unicode('_repr_json_')
524 524
525 525
526 526 class JavascriptFormatter(BaseFormatter):
@@ -534,9 +534,9 b' class JavascriptFormatter(BaseFormatter):'
534 534 The return value of this formatter should be valid Javascript code and
535 535 should *not* be enclosed in ```<script>``` tags.
536 536 """
537 format_type = Str('application/javascript')
537 format_type = Unicode('application/javascript')
538 538
539 print_method = Str('_repr_javascript_')
539 print_method = Unicode('_repr_javascript_')
540 540
541 541 FormatterABC.register(BaseFormatter)
542 542 FormatterABC.register(PlainTextFormatter)
@@ -70,7 +70,7 b' from IPython.utils.process import system, getoutput'
70 70 from IPython.utils.strdispatch import StrDispatch
71 71 from IPython.utils.syspathcontext import prepended_to_syspath
72 72 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
73 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
73 from IPython.utils.traitlets import (Int, CBool, CaselessStrEnum, Enum,
74 74 List, Unicode, Instance, Type)
75 75 from IPython.utils.warn import warn, error, fatal
76 76 import IPython.core.hooks
@@ -122,16 +122,16 b' def get_default_colors():'
122 122 return 'Linux'
123 123
124 124
125 class SeparateStr(Str):
126 """A Str subclass to validate separate_in, separate_out, etc.
125 class SeparateUnicode(Unicode):
126 """A Unicode subclass to validate separate_in, separate_out, etc.
127 127
128 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
128 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
129 129 """
130 130
131 131 def validate(self, obj, value):
132 132 if value == '0': value = ''
133 133 value = value.replace('\\n','\n')
134 return super(SeparateStr, self).validate(obj, value)
134 return super(SeparateUnicode, self).validate(obj, value)
135 135
136 136
137 137 class ReadlineNoRecord(object):
@@ -294,9 +294,9 b' class InteractiveShell(SingletonConfigurable, Magic):'
294 294 """
295 295 )
296 296
297 prompt_in1 = Str('In [\\#]: ', config=True)
298 prompt_in2 = Str(' .\\D.: ', config=True)
299 prompt_out = Str('Out[\\#]: ', config=True)
297 prompt_in1 = Unicode('In [\\#]: ', config=True)
298 prompt_in2 = Unicode(' .\\D.: ', config=True)
299 prompt_out = Unicode('Out[\\#]: ', config=True)
300 300 prompts_pad_left = CBool(True, config=True)
301 301 quiet = CBool(False, config=True)
302 302
@@ -307,7 +307,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
307 307 readline_use = CBool(True, config=True)
308 308 readline_merge_completions = CBool(True, config=True)
309 309 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
310 readline_remove_delims = Str('-/~', config=True)
310 readline_remove_delims = Unicode('-/~', config=True)
311 311 # don't use \M- bindings by default, because they
312 312 # conflict with 8-bit encodings. See gh-58,gh-88
313 313 readline_parse_and_bind = List([
@@ -327,9 +327,9 b' class InteractiveShell(SingletonConfigurable, Magic):'
327 327
328 328 # TODO: this part of prompt management should be moved to the frontends.
329 329 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
330 separate_in = SeparateStr('\n', config=True)
331 separate_out = SeparateStr('', config=True)
332 separate_out2 = SeparateStr('', config=True)
330 separate_in = SeparateUnicode('\n', config=True)
331 separate_out = SeparateUnicode('', config=True)
332 separate_out2 = SeparateUnicode('', config=True)
333 333 wildcards_case_sensitive = CBool(True, config=True)
334 334 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
335 335 default_value='Context', config=True)
@@ -1670,7 +1670,8 b' class InteractiveShell(SingletonConfigurable, Magic):'
1670 1670 # Remove some chars from the delimiters list. If we encounter
1671 1671 # unicode chars, discard them.
1672 1672 delims = readline.get_completer_delims().encode("ascii", "ignore")
1673 delims = delims.translate(None, self.readline_remove_delims)
1673 for d in self.readline_remove_delims:
1674 delims = delims.replace(d, "")
1674 1675 delims = delims.replace(ESC_MAGIC, '')
1675 1676 readline.set_completer_delims(delims)
1676 1677 # otherwise we end up with a monster history after a while:
@@ -3472,25 +3472,25 b' Defaulting color scheme to \'NoColor\'"""'
3472 3472 In [1]: from math import pi
3473 3473
3474 3474 In [2]: %precision 3
3475 Out[2]: '%.3f'
3475 Out[2]: u'%.3f'
3476 3476
3477 3477 In [3]: pi
3478 3478 Out[3]: 3.142
3479 3479
3480 3480 In [4]: %precision %i
3481 Out[4]: '%i'
3481 Out[4]: u'%i'
3482 3482
3483 3483 In [5]: pi
3484 3484 Out[5]: 3
3485 3485
3486 3486 In [6]: %precision %e
3487 Out[6]: '%e'
3487 Out[6]: u'%e'
3488 3488
3489 3489 In [7]: pi**10
3490 3490 Out[7]: 9.364805e+04
3491 3491
3492 3492 In [8]: %precision
3493 Out[8]: '%r'
3493 Out[8]: u'%r'
3494 3494
3495 3495 In [9]: pi**10
3496 3496 Out[9]: 93648.047476082982
@@ -36,7 +36,7 b' from IPython.core.macro import Macro'
36 36 from IPython.core.splitinput import split_user_input
37 37 from IPython.core import page
38 38
39 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance
39 from IPython.utils.traitlets import List, Int, Any, Unicode, CBool, Bool, Instance
40 40 from IPython.utils.text import make_quoted_expr
41 41 from IPython.utils.autoattr import auto_attr
42 42
@@ -756,7 +756,7 b' class AutocallChecker(PrefilterChecker):'
756 756
757 757 class PrefilterHandler(Configurable):
758 758
759 handler_name = Str('normal')
759 handler_name = Unicode('normal')
760 760 esc_strings = List([])
761 761 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
762 762 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
@@ -797,7 +797,7 b' class PrefilterHandler(Configurable):'
797 797
798 798 class AliasHandler(PrefilterHandler):
799 799
800 handler_name = Str('alias')
800 handler_name = Unicode('alias')
801 801
802 802 def handle(self, line_info):
803 803 """Handle alias input lines. """
@@ -812,7 +812,7 b' class AliasHandler(PrefilterHandler):'
812 812
813 813 class ShellEscapeHandler(PrefilterHandler):
814 814
815 handler_name = Str('shell')
815 handler_name = Unicode('shell')
816 816 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
817 817
818 818 def handle(self, line_info):
@@ -839,7 +839,7 b' class ShellEscapeHandler(PrefilterHandler):'
839 839
840 840
841 841 class MacroHandler(PrefilterHandler):
842 handler_name = Str("macro")
842 handler_name = Unicode("macro")
843 843
844 844 def handle(self, line_info):
845 845 obj = self.shell.user_ns.get(line_info.ifun)
@@ -850,7 +850,7 b' class MacroHandler(PrefilterHandler):'
850 850
851 851 class MagicHandler(PrefilterHandler):
852 852
853 handler_name = Str('magic')
853 handler_name = Unicode('magic')
854 854 esc_strings = List([ESC_MAGIC])
855 855
856 856 def handle(self, line_info):
@@ -864,7 +864,7 b' class MagicHandler(PrefilterHandler):'
864 864
865 865 class AutoHandler(PrefilterHandler):
866 866
867 handler_name = Str('auto')
867 handler_name = Unicode('auto')
868 868 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
869 869
870 870 def handle(self, line_info):
@@ -924,7 +924,7 b' class AutoHandler(PrefilterHandler):'
924 924
925 925 class HelpHandler(PrefilterHandler):
926 926
927 handler_name = Str('help')
927 handler_name = Unicode('help')
928 928 esc_strings = List([ESC_HELP])
929 929
930 930 def handle(self, line_info):
@@ -962,7 +962,7 b' class HelpHandler(PrefilterHandler):'
962 962
963 963 class EmacsHandler(PrefilterHandler):
964 964
965 handler_name = Str('emacs')
965 handler_name = Unicode('emacs')
966 966 esc_strings = List([])
967 967
968 968 def handle(self, line_info):
@@ -448,15 +448,15 b' def doctest_precision():'
448 448 In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
449 449
450 450 In [2]: %precision 5
451 Out[2]: '%.5f'
451 Out[2]: u'%.5f'
452 452
453 453 In [3]: f.float_format
454 Out[3]: '%.5f'
454 Out[3]: u'%.5f'
455 455
456 456 In [4]: %precision %e
457 Out[4]: '%e'
457 Out[4]: u'%e'
458 458
459 459 In [5]: f(3.1415927)
460 Out[5]: '3.141593e+00'
460 Out[5]: u'3.141593e+00'
461 461 """
462 462
@@ -21,7 +21,7 b' from IPython.external.qt import QtCore, QtGui'
21 21 from IPython.core.inputsplitter import IPythonInputSplitter, \
22 22 transform_ipy_prompt
23 23 from IPython.core.usage import default_gui_banner
24 from IPython.utils.traitlets import Bool, Str, Unicode
24 from IPython.utils.traitlets import Bool, Unicode
25 25 from frontend_widget import FrontendWidget
26 26 import styles
27 27
@@ -82,19 +82,18 b' class IPythonWidget(FrontendWidget):'
82 82 3. IPython: .error, .in-prompt, .out-prompt, etc
83 83 """)
84 84
85
86 syntax_style = Str(config=True,
85 syntax_style = Unicode(config=True,
87 86 help="""
88 87 If not empty, use this Pygments style for syntax highlighting. Otherwise,
89 88 the style sheet is queried for Pygments style information.
90 89 """)
91 90
92 91 # Prompts.
93 in_prompt = Str(default_in_prompt, config=True)
94 out_prompt = Str(default_out_prompt, config=True)
95 input_sep = Str(default_input_sep, config=True)
96 output_sep = Str(default_output_sep, config=True)
97 output_sep2 = Str(default_output_sep2, config=True)
92 in_prompt = Unicode(default_in_prompt, config=True)
93 out_prompt = Unicode(default_out_prompt, config=True)
94 input_sep = Unicode(default_input_sep, config=True)
95 output_sep = Unicode(default_output_sep, config=True)
96 output_sep2 = Unicode(default_output_sep2, config=True)
98 97
99 98 # FrontendWidget protected class variables.
100 99 _input_splitter_class = IPythonInputSplitter
@@ -33,7 +33,7 b' from IPython.core import ultratb'
33 33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
34 34 from IPython.frontend.terminal.ipapp import load_default_config
35 35
36 from IPython.utils.traitlets import Bool, Str, CBool, Unicode
36 from IPython.utils.traitlets import Bool, CBool, Unicode
37 37 from IPython.utils.io import ask_yes_no
38 38
39 39
@@ -31,7 +31,7 b' from IPython.utils.terminal import toggle_set_term_title, set_term_title'
31 31 from IPython.utils.process import abbrev_cwd
32 32 from IPython.utils.warn import warn
33 33 from IPython.utils.text import num_ini_spaces
34 from IPython.utils.traitlets import Int, Str, CBool, Unicode
34 from IPython.utils.traitlets import Int, CBool, Unicode
35 35
36 36 #-----------------------------------------------------------------------------
37 37 # Utilities
@@ -25,8 +25,8 b' Authors:'
25 25 from unittest import TestCase
26 26
27 27 from IPython.utils.traitlets import (
28 HasTraits, MetaHasTraits, TraitType, Any, CStr,
29 Int, Long, Float, Complex, Str, Unicode, TraitError,
28 HasTraits, MetaHasTraits, TraitType, Any, CBytes,
29 Int, Long, Float, Complex, Bytes, Unicode, TraitError,
30 30 Undefined, Type, This, Instance, TCPAddress, List, Tuple
31 31 )
32 32
@@ -700,13 +700,13 b' class TestComplex(TraitTestBase):'
700 700 _bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None]
701 701
702 702
703 class StringTrait(HasTraits):
703 class BytesTrait(HasTraits):
704 704
705 value = Str('string')
705 value = Bytes('string')
706 706
707 class TestString(TraitTestBase):
707 class TestBytes(TraitTestBase):
708 708
709 obj = StringTrait()
709 obj = BytesTrait()
710 710
711 711 _default_value = 'string'
712 712 _good_values = ['10', '-10', '10L',
@@ -781,7 +781,7 b' class TestTupleTrait(TraitTestBase):'
781 781 def test_invalid_args(self):
782 782 self.assertRaises(TypeError, Tuple, 5)
783 783 self.assertRaises(TypeError, Tuple, default_value='hello')
784 t = Tuple(Int, CStr, default_value=(1,5))
784 t = Tuple(Int, CBytes, default_value=(1,5))
785 785
786 786 class LooseTupleTrait(HasTraits):
787 787
@@ -798,12 +798,12 b' class TestLooseTupleTrait(TraitTestBase):'
798 798 def test_invalid_args(self):
799 799 self.assertRaises(TypeError, Tuple, 5)
800 800 self.assertRaises(TypeError, Tuple, default_value='hello')
801 t = Tuple(Int, CStr, default_value=(1,5))
801 t = Tuple(Int, CBytes, default_value=(1,5))
802 802
803 803
804 804 class MultiTupleTrait(HasTraits):
805 805
806 value = Tuple(Int, Str, default_value=[99,'bottles'])
806 value = Tuple(Int, Bytes, default_value=[99,'bottles'])
807 807
808 808 class TestMultiTuple(TraitTestBase):
809 809
@@ -951,7 +951,9 b' class CComplex(Complex):'
951 951 except:
952 952 self.error(obj, value)
953 953
954
954 # We should always be explicit about whether we're using bytes or unicode, both
955 # for Python 3 conversion and for reliable unicode behaviour on Python 2. So
956 # we don't have a Str type.
955 957 class Bytes(TraitType):
956 958 """A trait for strings."""
957 959
@@ -997,11 +999,6 b' class CUnicode(Unicode):'
997 999 except:
998 1000 self.error(obj, value)
999 1001
1000 if sys.version_info[0] < 3:
1001 Str, CStr = Bytes, CBytes
1002 else:
1003 Str, CStr = Unicode, CUnicode
1004
1005 1002
1006 1003 class Bool(TraitType):
1007 1004 """A boolean (True, False) trait."""
General Comments 0
You need to be logged in to leave comments. Login now