Skip to content

Commit 05bb9e1

Browse files
committed
Test and fix for issue PythonCharmers#15: os.path.join(p, s) with newstr
1 parent 37adad5 commit 05bb9e1

2 files changed

Lines changed: 30 additions & 26 deletions

File tree

‎future/builtins/backports/newstr.py‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,17 @@ def __contains__(self, key):
123123
raise TypeError(errmsg.format(type(key)))
124124
return issubset(list(newkey), list(self))
125125

126-
@no(bytes)
126+
@no(newbytes)
127127
def __add__(self, other):
128128
return newstr(super(newstr, self).__add__(other))
129129

130-
@no(bytes)
130+
@no(newbytes)
131131
def __radd__(self, left):
132132
" left + self "
133-
if isinstance(left, unicode):
134-
try:
135-
return newstr(left) + self
136-
except:
137-
raise NotImplemented
133+
try:
134+
return newstr(left) + self
135+
except:
136+
return NotImplemented
138137

139138
def __mul__(self, other):
140139
return newstr(super(newstr, self).__mul__(other))
@@ -149,15 +148,15 @@ def join(self, iterable):
149148
raise TypeError(errmsg.format(i))
150149
return newstr(super(newstr, self).join(iterable))
151150

152-
@no(bytes)
151+
@no(newbytes)
153152
def find(self, sub, *args):
154153
return super(newstr, self).find(sub, *args)
155154

156-
@no(bytes)
155+
@no(newbytes)
157156
def rfind(self, sub, *args):
158157
return super(newstr, self).rfind(sub, *args)
159158

160-
@no(bytes, (1, 2))
159+
@no(newbytes, (1, 2))
161160
def replace(self, old, new, *args):
162161
return newstr(super(newstr, self).replace(old, new, *args))
163162

@@ -179,19 +178,19 @@ def encode(self, encoding='utf-8', errors='strict'):
179178
# not keyword arguments as in Python 3 str.
180179
return newbytes(super(newstr, self).encode(encoding, errors))
181180

182-
@no(bytes, 1)
181+
@no(newbytes, 1)
183182
def startswith(self, prefix, *args):
184183
if isinstance(prefix, Iterable):
185184
for thing in prefix:
186-
if not isinstance(thing, unicode):
185+
if type(thing) == newbytes:
187186
raise TypeError(self.no_convert_msg.format(type(thing)))
188187
return super(newstr, self).startswith(prefix, *args)
189188

190-
@no(bytes, 1)
189+
@no(newbytes, 1)
191190
def endswith(self, prefix, *args):
192191
if isinstance(prefix, Iterable):
193192
for thing in prefix:
194-
if not isinstance(thing, unicode):
193+
if type(thing) == newbytes:
195194
raise TypeError(self.no_convert_msg.format(type(thing)))
196195
return super(newstr, self).endswith(prefix, *args)
197196

@@ -203,7 +202,7 @@ def split(self, sep=None, maxsplit=-1):
203202
parts = super(newstr, self).split(sep, maxsplit)
204203
return [newstr(part) for part in parts]
205204

206-
@no(bytes, 1)
205+
@no(newbytes, 1)
207206
def rsplit(self, sep=None, maxsplit=-1):
208207
# Py2 unicode.rsplit() takes maxsplit as an optional parameter,
209208
# not as a keyword argument as in Python 3 str.
@@ -212,17 +211,17 @@ def rsplit(self, sep=None, maxsplit=-1):
212211
parts = super(newstr, self).rsplit(sep, maxsplit)
213212
return [newstr(part) for part in parts]
214213

215-
@no(bytes, 1)
214+
@no(newbytes, 1)
216215
def partition(self, sep):
217216
parts = super(newstr, self).partition(sep)
218217
return tuple(newstr(part) for part in parts)
219218

220-
@no(bytes, 1)
219+
@no(newbytes, 1)
221220
def rpartition(self, sep):
222221
parts = super(newstr, self).rpartition(sep)
223222
return tuple(newstr(part) for part in parts)
224223

225-
@no(bytes, 1)
224+
@no(newbytes, 1)
226225
def index(self, sub, *args):
227226
"""
228227
Like newstr.find() but raise ValueError when the substring is not

‎future/tests/test_str.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_str(self):
2121

2222
def test_os_path_join(self):
2323
"""
24-
Issue #...: can't os.path.join(u'abc', str(u'def'))
24+
Issue #15: can't os.path.join(u'abc', str(u'def'))
2525
"""
2626
self.assertEqual(os.path.join(u'abc', str(u'def')),
2727
u'abc{}def'.format(os.sep))
@@ -123,8 +123,9 @@ def test_str_iteration(self):
123123
def test_str_plus_bytes(self):
124124
s = str(u'ABCD')
125125
b = b'EFGH'
126-
with self.assertRaises(TypeError):
127-
s + b
126+
# We allow this now:
127+
# with self.assertRaises(TypeError):
128+
# s + b
128129
# str objects don't have an __radd__ method, so the following
129130
# does not raise a TypeError. Is this a problem?
130131
# with self.assertRaises(TypeError):
@@ -171,9 +172,9 @@ def test_str_replace(self):
171172
self.assertTrue(isinstance(c, str))
172173

173174
with self.assertRaises(TypeError):
174-
s.replace(b'A', u'F')
175+
s.replace(bytes(b'A'), u'F')
175176
with self.assertRaises(TypeError):
176-
s.replace(u'A', b'F')
177+
s.replace(u'A', bytes(b'F'))
177178

178179
def test_str_partition(self):
179180
s1 = str('ABCD')
@@ -210,17 +211,21 @@ def test_str_index(self):
210211
with self.assertRaises(TypeError):
211212
s.index(67)
212213
with self.assertRaises(TypeError):
213-
s.index(b'C')
214+
s.index(bytes(b'C'))
214215

215216
def test_startswith(self):
216217
s = str('abcd')
217218
self.assertTrue(s.startswith('a'))
218219
self.assertTrue(s.startswith(('a', 'd')))
219220
self.assertTrue(s.startswith(str('ab')))
221+
# We allow this, because e.g. Python 2 os.path.join passes its arg a
222+
# byte-string '/' indiscriminately.
223+
self.assertFalse(s.startswith(b'A'))
224+
self.assertTrue(s.startswith(b'a'))
220225
with self.assertRaises(TypeError) as cm:
221-
s.startswith(b'A')
226+
self.assertFalse(s.startswith(bytes(b'A')))
222227
with self.assertRaises(TypeError) as cm:
223-
s.startswith((b'A', b'B'))
228+
s.startswith((bytes(b'A'), bytes(b'B')))
224229
with self.assertRaises(TypeError) as cm:
225230
s.startswith(65)
226231

0 commit comments

Comments
 (0)