@@ -1144,14 +1144,10 @@ def test_range_necessary_list_calls(self):
11441144 """
11451145 self .convert_check (before , after )
11461146
1147-
1148- class TestConservativeFuturize (CodeHandler ):
1149- @unittest .expectedFailure
11501147 def test_basestring (self ):
11511148 """
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.
11551151 """
11561152 before = """
11571153 assert isinstance('hello', basestring)
@@ -1164,41 +1160,7 @@ def test_basestring(self):
11641160 assert isinstance(u'hello', basestring)
11651161 assert isinstance(b'hello', basestring)
11661162 """
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 )
12021164
12031165 def test_safe_division (self ):
12041166 """
@@ -1255,6 +1217,80 @@ def __truediv__(self, other):
12551217 """
12561218 self .convert_check (before , after )
12571219
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 )
12581294
12591295class TestFuturizeAllImports (CodeHandler ):
12601296 """
0 commit comments