Skip to content

Commit 0a964e6

Browse files
authored
Merge pull request #472 from enthought/fix/str-rfind
Fix failure of str_rfind for Unicode inputs
2 parents bc89672 + bfa9741 commit 0a964e6

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

CHANGES.rst

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Traits CHANGELOG
22
================
33

4+
Release 5.1.1
5+
-------------
6+
7+
Released: 2019-04-18
8+
9+
Fixes
10+
11+
* Revert a change (#449) which accidentally broke external uses of
12+
``_py2to3.str_find`` and ``_py2to3.str_rfind``. (#472)
13+
414
Release 5.1.0
515
-------------
616

traits/_py2to3.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@
1717
# use from traitsui.
1818
# Once that PR is merged and a new release of traitsui is available, we can
1919
# remove these aliases for good.
20-
str_find = str.find
21-
str_rfind = str.rfind
20+
if six.PY2:
21+
import string
22+
23+
str_find = string.find
24+
str_rfind = string.rfind
25+
else:
26+
str_find = str.find
27+
str_rfind = str.rfind
28+
2229

2330
if six.PY2:
2431
from types import InstanceType, ClassType
@@ -80,10 +87,10 @@ def nested_context_mgrs(*args):
8087

8188
class nested_context_mgrs(ExitStack):
8289
""" Emulation of python 2's :py:class:`contextlib.nested`.
83-
90+
8491
It has gone from python 3 due to it's deprecation status
8592
in python 2.
86-
93+
8794
Note that :py:class:`contextlib.nested` was deprecated for
8895
a reason: It has issues with context managers that fail
8996
during init. The same caveats also apply here.

traits/tests/test__py2to3.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2007, Enthought, Inc.
2+
# All rights reserved.
3+
#
4+
# This software is provided without warranty under the terms of the BSD
5+
# license included in /LICENSE.txt and may be redistributed only
6+
# under the conditions described in the aforementioned license. The license
7+
# is also available online at http://www.enthought.com/licenses/BSD.txt
8+
9+
from __future__ import absolute_import, print_function, unicode_literals
10+
11+
import unittest
12+
13+
import six
14+
15+
from traits._py2to3 import str_find, str_rfind
16+
17+
18+
class TestPy2to3(unittest.TestCase):
19+
def test_str_find(self):
20+
# Note: inputs are Unicode strings.
21+
self.assertEqual(str_find("abcabc", "c"), 2)
22+
self.assertEqual(str_find("abcabc", "d"), -1)
23+
24+
def test_str_rfind(self):
25+
# Note: inputs are Unicode strings.
26+
self.assertEqual(str_rfind("abcabc", "c"), 5)
27+
self.assertEqual(str_rfind("abcabc", "d"), -1)
28+
29+
if six.PY2:
30+
def test_str_find_with_bytestrings(self):
31+
# Try all possible mixes of Unicode with bytes.
32+
self.assertEqual(str_find("abcabc", "b"), 1)
33+
self.assertEqual(str_find(u"abcabc", "b"), 1)
34+
self.assertEqual(str_find("abcabc", u"b"), 1)
35+
self.assertEqual(str_find(u"abcabc", u"b"), 1)
36+
37+
def test_str_rfind_with_bytestrings(self):
38+
# Try all possible mixes of Unicode with bytes.
39+
self.assertEqual(str_rfind("abcabc", "b"), 4)
40+
self.assertEqual(str_rfind(u"abcabc", "b"), 4)
41+
self.assertEqual(str_rfind("abcabc", u"b"), 4)
42+
self.assertEqual(str_rfind(u"abcabc", u"b"), 4)

0 commit comments

Comments
 (0)