@@ -1144,14 +1144,10 @@ def test_range_necessary_list_calls(self):
1144
1144
"""
1145
1145
self .convert_check (before , after )
1146
1146
1147
-
1148
- class TestConservativeFuturize (CodeHandler ):
1149
- @unittest .expectedFailure
1150
1147
def test_basestring (self ):
1151
1148
"""
1152
- In conservative mode, futurize would not modify "basestring"
1153
- but merely import it, and the following code would still run on
1154
- both Py2 and Py3.
1149
+ The 2to3 basestring fixer breaks working Py2 code that uses basestring.
1150
+ This tests whether something sensible is done instead.
1155
1151
"""
1156
1152
before = """
1157
1153
assert isinstance('hello', basestring)
@@ -1164,41 +1160,7 @@ def test_basestring(self):
1164
1160
assert isinstance(u'hello', basestring)
1165
1161
assert isinstance(b'hello', basestring)
1166
1162
"""
1167
- self .convert_check (before , after , conservative = True )
1168
-
1169
- @unittest .expectedFailure
1170
- def test_open (self ):
1171
- """
1172
- In conservative mode, futurize would not import io.open because
1173
- this changes the default return type from bytes to text.
1174
- """
1175
- before = """
1176
- filename = 'temp_file_open.test'
1177
- contents = 'Temporary file contents. Delete me.'
1178
- with open(filename, 'w') as f:
1179
- f.write(contents)
1180
-
1181
- with open(filename, 'r') as f:
1182
- data = f.read()
1183
- assert isinstance(data, str)
1184
- assert data == contents
1185
- """
1186
- after = """
1187
- from past.builtins import open, str as oldbytes, unicode
1188
- filename = oldbytes(b'temp_file_open.test')
1189
- contents = oldbytes(b'Temporary file contents. Delete me.')
1190
- with open(filename, oldbytes(b'w')) as f:
1191
- f.write(contents)
1192
-
1193
- with open(filename, oldbytes(b'r')) as f:
1194
- data = f.read()
1195
- assert isinstance(data, oldbytes)
1196
- assert data == contents
1197
- assert isinstance(oldbytes(b'hello'), basestring)
1198
- assert isinstance(unicode(u'hello'), basestring)
1199
- assert isinstance(oldbytes(b'hello'), basestring)
1200
- """
1201
- self .convert_check (before , after , conservative = True )
1163
+ self .convert_check (before , after )
1202
1164
1203
1165
def test_safe_division (self ):
1204
1166
"""
@@ -1255,6 +1217,80 @@ def __truediv__(self, other):
1255
1217
"""
1256
1218
self .convert_check (before , after )
1257
1219
1220
+ def test_basestring_issue_156 (self ):
1221
+ before = """
1222
+ x = str(3)
1223
+ allowed_types = basestring, int
1224
+ assert isinstance('', allowed_types)
1225
+ assert isinstance(u'', allowed_types)
1226
+ assert isinstance(u'foo', basestring)
1227
+ """
1228
+ after = """
1229
+ from builtins import str
1230
+ from past.builtins import basestring
1231
+ x = str(3)
1232
+ allowed_types = basestring, int
1233
+ assert isinstance('', allowed_types)
1234
+ assert isinstance(u'', allowed_types)
1235
+ assert isinstance(u'foo', basestring)
1236
+ """
1237
+ self .convert_check (before , after )
1238
+
1239
+
1240
+ class TestConservativeFuturize (CodeHandler ):
1241
+ @unittest .expectedFailure
1242
+ def test_basestring (self ):
1243
+ """
1244
+ In conservative mode, futurize would not modify "basestring"
1245
+ but merely import it from ``past``, and the following code would still
1246
+ run on both Py2 and Py3.
1247
+ """
1248
+ before = """
1249
+ assert isinstance('hello', basestring)
1250
+ assert isinstance(u'hello', basestring)
1251
+ assert isinstance(b'hello', basestring)
1252
+ """
1253
+ after = """
1254
+ from past.builtins import basestring
1255
+ assert isinstance('hello', basestring)
1256
+ assert isinstance(u'hello', basestring)
1257
+ assert isinstance(b'hello', basestring)
1258
+ """
1259
+ self .convert_check (before , after , conservative = True )
1260
+
1261
+ @unittest .expectedFailure
1262
+ def test_open (self ):
1263
+ """
1264
+ In conservative mode, futurize would not import io.open because
1265
+ this changes the default return type from bytes to text.
1266
+ """
1267
+ before = """
1268
+ filename = 'temp_file_open.test'
1269
+ contents = 'Temporary file contents. Delete me.'
1270
+ with open(filename, 'w') as f:
1271
+ f.write(contents)
1272
+
1273
+ with open(filename, 'r') as f:
1274
+ data = f.read()
1275
+ assert isinstance(data, str)
1276
+ assert data == contents
1277
+ """
1278
+ after = """
1279
+ from past.builtins import open, str as oldbytes, unicode
1280
+ filename = oldbytes(b'temp_file_open.test')
1281
+ contents = oldbytes(b'Temporary file contents. Delete me.')
1282
+ with open(filename, oldbytes(b'w')) as f:
1283
+ f.write(contents)
1284
+
1285
+ with open(filename, oldbytes(b'r')) as f:
1286
+ data = f.read()
1287
+ assert isinstance(data, oldbytes)
1288
+ assert data == contents
1289
+ assert isinstance(oldbytes(b'hello'), basestring)
1290
+ assert isinstance(unicode(u'hello'), basestring)
1291
+ assert isinstance(oldbytes(b'hello'), basestring)
1292
+ """
1293
+ self .convert_check (before , after , conservative = True )
1258
1294
1259
1295
class TestFuturizeAllImports (CodeHandler ):
1260
1296
"""
0 commit comments