@@ -58,6 +58,8 @@ class newstr(unicode):
58
58
"""
59
59
A backport of the Python 3 str object to Py2
60
60
"""
61
+ no_convert_msg = "Can't convert '{}' object to str implicitly"
62
+
61
63
def __new__ (cls , * args , ** kwargs ):
62
64
"""
63
65
From the Py3 str docstring:
@@ -166,29 +168,32 @@ def encode(self, encoding='utf-8', errors='strict'):
166
168
def startswith (self , prefix , * args ):
167
169
if isinstance (prefix , Iterable ):
168
170
for thing in prefix :
169
- if isinstance (thing , bytes ):
170
- raise TypeError ("Can't convert 'bytes' object to str implicitly" )
171
+ if not isinstance (thing , unicode ):
172
+ raise TypeError (self . no_convert_msg . format ( type ( thing )) )
171
173
return super (newstr , self ).startswith (prefix , * args )
172
174
173
175
@no (bytes , 1 )
174
176
def endswith (self , prefix , * args ):
175
177
if isinstance (prefix , Iterable ):
176
178
for thing in prefix :
177
- if isinstance (thing , bytes ):
178
- raise TypeError ("Can't convert 'bytes' object to str implicitly" )
179
+ if not isinstance (thing , unicode ):
180
+ raise TypeError (self . no_convert_msg . format ( type ( thing )) )
179
181
return super (newstr , self ).endswith (prefix , * args )
180
182
181
- @no (bytes , 1 )
182
183
def split (self , sep = None , maxsplit = - 1 ):
183
184
# Py2 unicode.split() takes maxsplit as an optional parameter,
184
185
# not as a keyword argument as in Python 3 str.
186
+ if sep is not None and not isinstance (sep , unicode ):
187
+ raise TypeError (self .no_convert_msg .format (type (sep )))
185
188
parts = super (newstr , self ).split (sep , maxsplit )
186
189
return [newstr (part ) for part in parts ]
187
190
188
191
@no (bytes , 1 )
189
192
def rsplit (self , sep = None , maxsplit = - 1 ):
190
193
# Py2 unicode.rsplit() takes maxsplit as an optional parameter,
191
194
# not as a keyword argument as in Python 3 str.
195
+ if sep is not None and not isinstance (sep , unicode ):
196
+ raise TypeError (self .no_convert_msg .format (type (sep )))
192
197
parts = super (newstr , self ).rsplit (sep , maxsplit )
193
198
return [newstr (part ) for part in parts ]
194
199
@@ -213,15 +218,18 @@ def index(self, sub, *args):
213
218
raise ValueError ('substring not found' )
214
219
return pos
215
220
216
- # def find(self, sub, *args):
217
- # errmsg = "Can't convert '{}' object to newstr implicitly"
218
- # if not isinstance(sub, unicode):
219
- # raise TypeError(errmsg.format(type(sub)))
220
- # try:
221
- # return super(newstr, self).index(sub, *args)
222
- # except ValueError:
223
- # raise ValueError('substring not found')
221
+ def __eq__ (self , other ):
222
+ if isinstance (other , unicode ):
223
+ return super (newstr , self ).__eq__ (other )
224
+ else :
225
+ return False
226
+
227
+ def __ne__ (self , other ):
228
+ if isinstance (other , unicode ):
229
+ return super (newstr , self ).__ne__ (other )
230
+ else :
231
+ return True
224
232
225
233
226
- __all__ = ['newstr' ]
227
234
235
+ __all__ = ['newstr' ]
0 commit comments