Skip to content

Commit 94347d6

Browse files
committed
Fix Rect contains error message
This error shouldn't exist at all imo, but for backwards compatibility lets keep it for now. And the error should at least be accurate, so this commit fixes that. Previously it would say "int" even on FRects, where it meant float. Also strengthens the tests to explicitly test the TypeError behavior of this mismatched number type scenario.
1 parent 0c2020e commit 94347d6

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src_c/rect.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
138138
#define RectExport_getsize pg_rect_getsize
139139
#define RectExport_setsize pg_rect_setsize
140140
#define RectImport_primitiveType int
141+
#define RectImport_PrimitiveTypeName "int"
141142
#define RectImport_RectCheck pgRect_Check
142143
#define RectImport_OtherRectCheck pgFRect_Check
143144
#define RectImport_OtherRectCheckExact pgFRect_CheckExact
@@ -257,6 +258,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
257258
#define RectExport_getsize pg_frect_getsize
258259
#define RectExport_setsize pg_frect_setsize
259260
#define RectImport_primitiveType float
261+
#define RectImport_PrimitiveTypeName "float"
260262
#define RectImport_RectCheck pgFRect_Check
261263
#define RectImport_OtherRectCheck pgRect_Check
262264
#define RectImport_OtherRectCheckExact pgRect_CheckExact

src_c/rect_impl.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,17 @@
324324
#error RectImport_OtherRectCheck needs to be defined
325325
#endif
326326
#ifndef RectImport_RectCheckExact
327-
#error RectImport_RectCheckExact needs to be Defined
327+
#error RectImport_RectCheckExact needs to be defined
328328
#endif
329329
#ifndef RectImport_OtherRectCheckExact
330330
#error RectImport_OtherRectCheckExact needs to be Defined
331331
#endif
332332
#ifndef RectImport_primitiveType
333333
#error RectImport_primitiveType needs to be defined
334334
#endif
335+
#ifndef RectImport_PrimitiveTypeName
336+
#error RectImport_PrimitiveTypeName needs to be defined
337+
#endif
335338
#ifndef RectImport_innerRectStruct
336339
#error RectImport_innerRectStruct needs to be defined
337340
#endif
@@ -2010,7 +2013,8 @@ RectExport_containsSeq(RectObject *self, PyObject *arg)
20102013
if (ret < 0) {
20112014
PyErr_SetString(PyExc_TypeError, "'in <" ObjectName
20122015
">' requires rect style object"
2013-
" or int as left operand");
2016+
" or " RectImport_PrimitiveTypeName
2017+
" as left operand");
20142018
}
20152019
return ret;
20162020
}
@@ -3027,6 +3031,7 @@ RectExport_iterator(RectObject *self)
30273031
#undef RectImport_PythonNumberAsPrimitiveType
30283032
#undef RectImport_PrimitiveTypeAsPythonNumber
30293033
#undef RectImport_primitiveType
3034+
#undef RectImport_PrimitiveTypeName
30303035
#undef RectImport_RectCheck
30313036
#undef RectImport_OtherRectCheck
30323037
#undef RectImport_RectCheckExact

test/rect_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ def test_contains(self):
808808
self.assertTrue(2 in Rect(0, 0, 1, 2), "r does not contain 2")
809809
self.assertFalse(3 in Rect(0, 0, 1, 2), "r contains 3")
810810

811+
self.assertRaises(TypeError, lambda: 1.0 in Rect(0, 0, 1, 2))
811812
self.assertRaises(TypeError, lambda: "string" in Rect(0, 0, 1, 2))
812813
self.assertRaises(TypeError, lambda: 4 + 3j in Rect(0, 0, 1, 2))
813814

@@ -3104,6 +3105,7 @@ def test_contains(self):
31043105
# self.assertTrue(2 in FRect(0, 0, 1, 2), "r does not contain 2")
31053106
# self.assertFalse(3 in FRect(0, 0, 1, 2), "r contains 3")
31063107

3108+
self.assertRaises(TypeError, lambda: 1 in FRect(0, 0, 1, 2))
31073109
self.assertRaises(TypeError, lambda: "string" in FRect(0, 0, 1, 2))
31083110
self.assertRaises(TypeError, lambda: 4 + 3j in FRect(0, 0, 1, 2))
31093111

0 commit comments

Comments
 (0)