@@ -252,36 +252,36 @@ def test_integer_casting():
252
252
253
253
254
254
def test_int_convert ():
255
- class DeepThought (object ):
255
+ class Int (object ):
256
256
def __int__ (self ):
257
257
return 42
258
258
259
- class DoubleThought (object ):
260
- def __int__ (self ):
261
- return 42
262
-
263
- def __index__ (self ):
264
- return 0
265
-
266
- class ShallowThought (object ):
259
+ class NotInt (object ):
267
260
pass
268
261
269
- class FuzzyThought (object ):
262
+ class Float (object ):
270
263
def __float__ (self ):
271
264
return 41.99999
272
265
273
- class IndexedThought (object ):
266
+ class Index (object ):
274
267
def __index__ (self ):
275
268
return 42
276
269
277
- class TypeErrorThought (object ):
270
+ class IntAndIndex (object ):
271
+ def __int__ (self ):
272
+ return 42
273
+
274
+ def __index__ (self ):
275
+ return 0
276
+
277
+ class RaisingTypeErrorOnIndex (object ):
278
278
def __index__ (self ):
279
279
raise TypeError
280
280
281
281
def __int__ (self ):
282
282
return 42
283
283
284
- class RaisingThought (object ):
284
+ class RaisingValueErrorOnIndex (object ):
285
285
def __index__ (self ):
286
286
raise ValueError
287
287
@@ -299,21 +299,21 @@ def cant_convert(v):
299
299
assert convert (7 ) == 7
300
300
assert noconvert (7 ) == 7
301
301
cant_convert (3.14159 )
302
- assert convert (DeepThought ()) == 42
303
- requires_conversion (DeepThought ())
304
- assert convert (DoubleThought ()) == 0 # Fishy; `int(DoubleThought)` == 42
305
- assert noconvert (DoubleThought ()) == 0
306
- cant_convert (ShallowThought ())
307
- cant_convert (FuzzyThought ())
302
+ assert convert (Int ()) == 42
303
+ requires_conversion (Int ())
304
+ cant_convert (NotInt ())
305
+ cant_convert (Float ())
308
306
309
307
# Before Python 3.8, `PyLong_AsLong` does not pick up on `obj.__index__`,
310
308
# but pybind11 "backports" this behavior.
311
- assert convert (IndexedThought ()) == 42
312
- assert noconvert (IndexedThought ()) == 42
313
- assert convert (TypeErrorThought ()) == 42
314
- requires_conversion (TypeErrorThought ())
315
- assert convert (RaisingThought ()) == 42
316
- requires_conversion (RaisingThought ())
309
+ assert convert (Index ()) == 42
310
+ assert noconvert (Index ()) == 42
311
+ assert convert (IntAndIndex ()) == 0 # Fishy; `int(DoubleThought)` == 42
312
+ assert noconvert (IntAndIndex ()) == 0
313
+ assert convert (RaisingTypeErrorOnIndex ()) == 42
314
+ requires_conversion (RaisingTypeErrorOnIndex ())
315
+ assert convert (RaisingValueErrorOnIndex ()) == 42
316
+ requires_conversion (RaisingValueErrorOnIndex ())
317
317
318
318
319
319
def test_numpy_int_convert ():
0 commit comments