8000 Loosen up str type-checking to allow mixing with native Py2 types (bu… · shawnp/python-future@38a2cea · GitHub
[go: up one dir, main page]

Skip to content

Commit 38a2cea

Browse files
committed
Loosen up str type-checking to allow mixing with native Py2 types (but not newbytes)
* Is this a good idea in general? It helps with e.g. os.path.join() (issue PythonCharmers#15)
1 parent aecb525 commit 38a2cea

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

future/builtins/backports/newstr.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ 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 "
133133
try:
@@ -149,21 +149,22 @@ def join(self, iterable):
149149
raise TypeError(errmsg.format(i))
150150
return newstr(super(newstr, self).join(iterable))
151151

152-
@no(bytes)
152+
@no(newbytes)
153153
def find(self, sub, *args):
154154
return super(newstr, self).find(sub, *args)
155155

156-
@no(bytes)
156+
@no(newbytes)
157157
def rfind(self, sub, *args):
158158
return super(newstr, self).rfind(sub, *args)
159159

160-
@no(bytes, (1, 2))
160+
@no(newbytes, (1, 2))
161161
def replace(self, old, new, *args):
162162
return newstr(super(newstr, self).replace(old, new, *args))
163163

164164
def decode(self, *args):
165165
raise AttributeError("decode method has been disabled in newstr")
166166

167+
@no(newbytes, (1, 2))
167168
def encode(self, encoding='utf-8', errors='strict'):
168169
"""
169170
Returns bytes
@@ -179,22 +180,23 @@ def encode(self, encoding='utf-8', errors='strict'):
179180
# not keyword arguments as in Python 3 str.
180181
return newbytes(super(newstr, self).encode(encoding, errors))
181182

182-
@no(bytes, 1)
183+
@no(newbytes, 1)
183184
def startswith(self, prefix, *args):
184185
if isinstance(prefix, Iterable):
185186
for thing in prefix:
186187
if not isinstance(thing, unicode):
187188
raise TypeError(self.no_convert_msg.format(type(thing)))
188189
return super(newstr, self).startswith(prefix, *args)
189190

190-
@no(bytes, 1)
191+
@no(newbytes, 1)
191192
def endswith(self, prefix, *args):
192193
if isinstance(prefix, Iterable):
193194
for thing in prefix:
194195
if not isinstance(thing, unicode):
195196
raise TypeError(self.no_convert_msg.format(type(thing)))
196197
return super(newstr, self).endswith(prefix, *args)
197198

199+
@no(newbytes, 1)
198200
def split(self, sep=None, maxsplit=-1):
199201
# Py2 unicode.split() takes maxsplit as an optional parameter,
200202
# not as a keyword argument as in Python 3 str.
@@ -203,7 +205,7 @@ def split(self, sep=None, maxsplit=-1):
203205
parts = super(newstr, self).split(sep, maxsplit)
204206
return [newstr(part) for part in parts]
205207

206-
@no(bytes, 1)
208+
@no(newbytes, 1)
207209
def rsplit(self, sep=None, maxsplit=-1):
208210
# Py2 unicode.rsplit() takes maxsplit as an optional parameter,
209211
# not as a keyword argument as in Python 3 str.
@@ -212,17 +214,17 @@ def rsplit(self, sep=None, maxsplit=-1):
212214
parts = super(newstr, self).rsplit(sep, maxsplit)
213215
return [newstr(part) for part in parts]
214216

215-
@no(bytes, 1)
217+
@no(newbytes, 1)
216218
def partition(self, sep):
217219
parts = super(newstr, self).partition(sep)
218220
return tuple(newstr(part) for part in parts)
219221

220-
@no(bytes, 1)
222+
@no(newbytes, 1)
221223
def rpartition(self, sep):
222224
parts = super(newstr, self).rpartition(sep)
223225
return tuple(newstr(part) for part in parts)
224226

225-
@no(bytes, 1)
227+
@no(newbytes, 1)
226228
def index(self, sub, *args):
227229
"""
228230
Like newstr.find() but raise ValueError when the substring is not
@@ -248,22 +250,22 @@ def __ne__(self, other):
248250
unorderable_err = 'unorderable types: str() and {0}'
249251

250252
def __lt__(self, other):
251-
if not istext(other):
253+
if not isinstance(other, unicode):
252254
raise TypeError(self.unorderable_err.format(type(other)))
253255
return super(newbytes, self).__lt__(other)
254256

255257
def __le__(self, other):
256-
if not istext(other):
258+
if not isinstance(other, unicode):
257259
raise TypeError(self.unorderable_err.format(type(other)))
258260
return super(newbytes, self).__le__(other)
259261

260262
def __gt__(self, other):
261-
if not istext(other):
263+
if not isinstance(other, unicode):
262264
raise TypeError(self.unorderable_err.format(type(other)))
263265
return super(newbytes, self).__gt__(other)
264266

265267
def __ge__(self, other):
266-
if not istext(other):
268+
if not isinstance(other, unicode):
267269
raise TypeError(self.unorderable_err.format(type(other)))
268270
return super(newbytes, self).__ge__(other)
269271

future/tests/test_str.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ 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+
# with self.assertRaises(TypeError):
127+
# s + b
128128
# str objects don't have an __radd__ method, so the following
129129
# does not raise a TypeError. Is this a problem?
130130
# with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)
0