Skip to content

Commit 98fa0e2

Browse files
committed
Consistent __str__ usage. Also helps Py3 support.
1 parent 643fc98 commit 98fa0e2

16 files changed

Lines changed: 47 additions & 25 deletions

src/robot/conf/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from robot.output import LOGGER, loggerhelper
2222
from robot.result.keywordremover import KeywordRemover
2323
from robot.result.flattenkeywordmatcher import validate_flatten_keyword
24-
from robot.utils import (abspath, escape, format_time, get_link_path,
24+
from robot.utils import (__str__, abspath, escape, format_time, get_link_path,
2525
html_escape, is_list_like,
2626
split_args_from_name_or_path)
2727

@@ -307,6 +307,9 @@ def __unicode__(self):
307307
return '\n'.join('%s: %s' % (name, self._opts[name])
308308
for name in sorted(self._opts))
309309

310+
def __str__(self):
311+
return __str__(self)
312+
310313
@property
311314
def output_directory(self):
312315
return self['OutputDir']

src/robot/model/itemlist.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from robot.utils import __str__
16+
1517

1618
class ItemList(object):
1719
__slots__ = ['_item_class', '_common_attrs', '_items']
@@ -81,5 +83,4 @@ def __len__(self):
8183
def __unicode__(self):
8284
return u'[%s]' % ', '.join(unicode(item) for item in self)
8385

84-
def __str__(self):
85-
return unicode(self).encode('ASCII', 'replace')
86+
__str__ = __str__

src/robot/model/message.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from robot.utils import html_escape, setter
15+
from robot.utils import __str__, html_escape, setter
1616

1717
from .itemlist import ItemList
1818
from .modelobject import ModelObject
@@ -58,6 +58,8 @@ def visit(self, visitor):
5858
def __unicode__(self):
5959
return self.message
6060

61+
__str__ = __str__
62+
6163

6264
class Messages(ItemList):
6365
__slots__ = []

src/robot/model/metadata.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from robot.utils import NormalizedDict
15+
from robot.utils import NormalizedDict, __str__
1616

1717

1818
class Metadata(NormalizedDict):
@@ -23,5 +23,4 @@ def __init__(self, initial=None):
2323
def __unicode__(self):
2424
return u'{%s}' % ', '.join('%s: %s' % (k, self[k]) for k in self)
2525

26-
def __str__(self):
27-
return unicode(self).encode('ASCII', 'replace')
26+
__str__ = __str__

src/robot/model/modelobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from robot.utils.setter import SetterAwareType
15+
from robot.utils import SetterAwareType, __str__
1616

1717

1818
class ModelObject(object):
@@ -23,7 +23,7 @@ def __unicode__(self):
2323
return self.name
2424

2525
def __str__(self):
26-
return unicode(self).encode('ASCII', 'replace')
26+
return __str__(self)
2727

2828
def __repr__(self):
2929
return repr(str(self))

src/robot/model/tags.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from robot.utils import Matcher, NormalizedDict, is_string, setter, unic
15+
from robot.utils import (Matcher, NormalizedDict, is_string, setter, unic,
16+
__str__)
1617

1718

1819
class Tags(object):
@@ -57,12 +58,12 @@ def __iter__(self):
5758
def __unicode__(self):
5859
return u'[%s]' % ', '.join(self)
5960

61+
def __str__(self):
62+
return __str__(self)
63+
6064
def __repr__(self):
6165
return repr(list(self))
6266

63-
def __str__(self):
64-
return unicode(self).encode('UTF-8')
65-
6667
def __getitem__(self, index):
6768
item = self._tags[index]
6869
return item if not isinstance(index, slice) else Tags(item)
@@ -111,9 +112,14 @@ def __init__(self, pattern):
111112
def match(self, tags):
112113
return self._matcher.match_any(tags)
113114

115+
# FIXME: Why only this class methods below??
116+
114117
def __unicode__(self):
115118
return self._matcher.pattern
116119

120+
def __str__(self):
121+
return __str__(self)
122+
117123
def __nonzero__(self):
118124
return bool(self._matcher)
119125

src/robot/utils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
from .markupwriters import HtmlWriter, XmlWriter, NullMarkupWriter
4848
from .importer import Importer
4949
from .match import eq, Matcher, MultiMatcher
50-
from .misc import (isatty, getdoc, plural_or_not, printable_name,
51-
seq2str, seq2str2)
50+
from .misc import (getdoc, isatty, plural_or_not, printable_name, seq2str,
51+
seq2str2, __str__)
5252
from .normalizing import lower, normalize, NormalizedDict
5353
from .platform import (IRONPYTHON, JYTHON, PYTHON, UNIXY, WINDOWS,
5454
RERAISED_EXCEPTIONS)
@@ -64,7 +64,7 @@
6464
from .robottypes import (is_bytes, is_dict_like, is_falsy, is_integer,
6565
is_list_like, is_number, is_string, is_truthy,
6666
is_unicode, type_name)
67-
from .setter import setter
67+
from .setter import setter, SetterAwareType
6868
from .text import (cut_long_message, format_assign_message,
6969
pad_console_length, get_console_length, split_tags_from_doc,
7070
split_args_from_name_or_path)

src/robot/utils/dotdict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __eq__(self, other):
4242
return dict.__eq__(self, other)
4343

4444
def __str__(self):
45-
return '{%s}' % ', '.join('%r: %r' % item for item in self.iteritems())
45+
return '{%s}' % ', '.join('%r: %r' % (key, self[key]) for key in self)
4646

4747
# Must use original dict.__repr__ to allow customising PrettyPrinter.
4848
__repr__ = dict.__repr__

src/robot/utils/misc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def getdoc(item):
107107
return unic(doc)
108108

109109

110+
def __str__(instance):
111+
return instance.__unicode__().encode('UTF-8')
112+
113+
110114
# On IronPython sys.stdxxx.isatty() always returns True
111115
if not IRONPYTHON:
112116

src/robot/utils/normalizing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __len__(self):
9090
return len(self._data)
9191

9292
def __str__(self):
93-
return str(dict(self.items()))
93+
return '{%s}' % ', '.join('%r: %r' % (key, self[key]) for key in self)
9494

9595
def __eq__(self, other):
9696
if not is_dict_like(other):

0 commit comments

Comments
 (0)