1
1
#include "geometry.h"
2
+ #include "doc/geometry_doc.h"
2
3
3
4
static PyObject *
4
5
_pg_circle_subtype_new (PyTypeObject * type , pgCircleBase * circle )
@@ -34,13 +35,14 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
34
35
return 1 ;
35
36
}
36
37
38
+ /* Paths for sequences */
37
39
if (pgSequenceFast_Check (obj )) {
38
40
PyObject * * f_arr = PySequence_Fast_ITEMS (obj );
39
41
length = PySequence_Fast_GET_SIZE (obj );
40
42
41
43
if (length == 3 ) {
42
- if (!pg_DoubleFromObj (f_arr [0 ], & ( out -> x ) ) ||
43
- !pg_DoubleFromObj (f_arr [1 ], & ( out -> y ) ) ||
44
+ if (!pg_DoubleFromObj (f_arr [0 ], & out -> x ) ||
45
+ !pg_DoubleFromObj (f_arr [1 ], & out -> y ) ||
44
46
!_pg_circle_set_radius (f_arr [2 ], out )) {
45
47
return 0 ;
46
48
}
@@ -53,7 +55,7 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
53
55
return 1 ;
54
56
}
55
57
else if (length == 2 ) {
56
- if (!pg_TwoDoublesFromObj (f_arr [0 ], & ( out -> x ) , & ( out -> y ) ) ||
58
+ if (!pg_TwoDoublesFromObj (f_arr [0 ], & out -> x , & out -> y ) ||
57
59
!_pg_circle_set_radius (f_arr [1 ], out )) {
58
60
return 0 ;
59
61
}
@@ -66,21 +68,20 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
66
68
}
67
69
}
68
70
else if (PySequence_Check (obj )) {
69
- /* Path for other sequences or Types that count as sequences*/
70
71
PyObject * tmp = NULL ;
71
72
length = PySequence_Length (obj );
72
73
if (length == 3 ) {
73
74
/*These are to be substituted with better pg_DoubleFromSeqIndex()
74
75
* implementations*/
75
76
tmp = PySequence_ITEM (obj , 0 );
76
- if (!pg_DoubleFromObj (tmp , & ( out -> x ) )) {
77
+ if (!pg_DoubleFromObj (tmp , & out -> x )) {
77
78
Py_DECREF (tmp );
78
79
return 0 ;
79
80
}
80
81
Py_DECREF (tmp );
81
82
82
83
tmp = PySequence_ITEM (obj , 1 );
83
- if (!pg_DoubleFromObj (tmp , & ( out -> y ) )) {
84
+ if (!pg_DoubleFromObj (tmp , & out -> y )) {
84
85
Py_DECREF (tmp );
85
86
return 0 ;
86
87
}
@@ -97,7 +98,7 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
97
98
}
98
99
else if (length == 2 ) {
99
100
tmp = PySequence_ITEM (obj , 0 );
100
- if (!pg_TwoDoublesFromObj (tmp , & ( out -> x ) , & ( out -> y ) )) {
101
+ if (!pg_TwoDoublesFromObj (tmp , & out -> x , & out -> y )) {
101
102
Py_DECREF (tmp );
102
103
return 0 ;
103
104
}
@@ -128,41 +129,41 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
128
129
}
129
130
}
130
131
131
- if (PyObject_HasAttrString (obj , "circle" )) {
132
- PyObject * circleattr ;
133
- circleattr = PyObject_GetAttrString (obj , "circle" );
134
- if (!circleattr ) {
135
- PyErr_Clear ();
136
- return 0 ;
137
- }
138
- if (PyCallable_Check (circleattr )) /*call if it's a method*/
139
- {
140
- PyObject * circleresult = PyObject_CallObject (circleattr , NULL );
141
- Py_DECREF (circleattr );
142
- if (!circleresult ) {
143
- PyErr_Clear ();
144
- return 0 ;
145
- }
146
- circleattr = circleresult ;
147
- }
148
- if (!pgCircle_FromObject (circleattr , out )) {
132
+ /* Path for objects that have a circle attribute */
133
+ PyObject * circleattr ;
134
+ if (!(circleattr = PyObject_GetAttrString (obj , "circle" ))) {
135
+ PyErr_Clear ();
136
+ return 0 ;
137
+ }
138
+
139
+ if (PyCallable_Check (circleattr )) /*call if it's a method*/
140
+ {
141
+ PyObject * circleresult = PyObject_CallObject (circleattr , NULL );
142
+ Py_DECREF (circleattr );
143
+ if (!circleresult ) {
149
144
PyErr_Clear ();
150
- Py_DECREF (circleattr );
151
145
return 0 ;
152
146
}
153
- Py_DECREF (circleattr );
147
+ circleattr = circleresult ;
148
+ }
154
149
155
- return 1 ;
150
+ if (!pgCircle_FromObject (circleattr , out )) {
151
+ PyErr_Clear ();
152
+ Py_DECREF (circleattr );
153
+ return 0 ;
156
154
}
157
- return 0 ;
155
+
156
+ Py_DECREF (circleattr );
157
+
158
+ return 1 ;
158
159
}
159
160
160
161
static PyObject *
161
162
pg_circle_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
162
163
{
163
164
pgCircleObject * self = (pgCircleObject * )type -> tp_alloc (type , 0 );
164
165
165
- if (self != NULL ) {
166
+ if (self ) {
166
167
self -> circle .x = self -> circle .y = 0 ;
167
168
self -> circle .r = 1 ;
168
169
self -> weakreflist = NULL ;
@@ -174,18 +175,19 @@ static int
174
175
pg_circle_init (pgCircleObject * self , PyObject * args , PyObject * kwds )
175
176
{
176
177
if (!pgCircle_FromObject (args , & self -> circle )) {
177
- PyErr_SetString (PyExc_TypeError ,
178
- "Argument must be Circle style object" );
178
+ PyErr_SetString (
179
+ PyExc_TypeError ,
180
+ "Arguments must be a Circle, a sequence of length 3 or 2, or an "
181
+ "object with an attribute called 'circle'" );
179
182
return -1 ;
180
183
}
181
-
182
184
return 0 ;
183
185
}
184
186
185
187
static void
186
188
pg_circle_dealloc (pgCircleObject * self )
187
189
{
188
- if (self -> weakreflist != NULL ) {
190
+ if (self -> weakreflist ) {
189
191
PyObject_ClearWeakRefs ((PyObject * )self );
190
192
}
191
193
@@ -235,11 +237,11 @@ pg_circle_str(pgCircleObject *self)
235
237
}
236
238
237
239
static struct PyMethodDef pg_circle_methods [] = {
238
- {"__copy__" , (PyCFunction )pg_circle_copy , METH_NOARGS , NULL },
239
- {"copy" , (PyCFunction )pg_circle_copy , METH_NOARGS , NULL },
240
+ {"__copy__" , (PyCFunction )pg_circle_copy , METH_NOARGS , DOC_CIRCLE_COPY },
241
+ {"copy" , (PyCFunction )pg_circle_copy , METH_NOARGS , DOC_CIRCLE_COPY },
240
242
{NULL , NULL , 0 , NULL }};
241
243
242
- #define GETSET_FOR_SIMPLE (name ) \
244
+ #define GETTER_SETTER (name ) \
243
245
static PyObject *pg_circle_get##name(pgCircleObject *self, void *closure) \
244
246
{ \
245
247
return PyFloat_FromDouble(self->circle.name); \
@@ -249,19 +251,19 @@ static struct PyMethodDef pg_circle_methods[] = {
249
251
{ \
250
252
double val; \
251
253
DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); \
252
- if (pg_DoubleFromObj(value, &val)) { \
253
- self->circle.name = val; \
254
- return 0; \
254
+ if (!pg_DoubleFromObj(value, &val)) { \
255
+ PyErr_Format(PyExc_TypeError, "Expected a number, got '%s'", \
256
+ Py_TYPE(value)->tp_name); \
257
+ return -1; \
255
258
} \
256
- PyErr_SetString(PyExc_TypeError, "Expected a number"); \
257
- return -1; \
259
+ self->circle.name = val; \
260
+ return 0; \
258
261
}
259
262
260
- // they are repetitive enough that we can abstract them like this
261
- GETSET_FOR_SIMPLE (x )
262
- GETSET_FOR_SIMPLE (y )
263
+ GETTER_SETTER (x )
264
+ GETTER_SETTER (y )
263
265
264
- #undef GETSET_FOR_SIMPLE
266
+ #undef GETTER_SETTER
265
267
266
268
static PyObject *
267
269
pg_circle_getr (pgCircleObject * self , void * closure )
@@ -277,13 +279,13 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure)
277
279
DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME (value );
278
280
279
281
if (!pg_DoubleFromObj (value , & radius )) {
280
- PyErr_SetString (PyExc_TypeError ,
281
- "Invalid type for radius, must be numeric" );
282
+ PyErr_Format (PyExc_TypeError , "Expected a number, got '%s'" ,
283
+ Py_TYPE ( value ) -> tp_name );
282
284
return -1 ;
283
285
}
284
286
285
287
if (radius <= 0 ) {
286
- PyErr_SetString (PyExc_ValueError , "Invalid radius value, must be > 0 " );
288
+ PyErr_SetString (PyExc_ValueError , "Radius must be positive " );
287
289
return -1 ;
288
290
}
289
291
@@ -293,9 +295,9 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure)
293
295
}
294
296
295
297
static PyGetSetDef pg_circle_getsets [] = {
296
- {"x" , (getter )pg_circle_getx , (setter )pg_circle_setx , NULL , NULL },
297
- {"y" , (getter )pg_circle_gety , (setter )pg_circle_sety , NULL , NULL },
298
- {"r" , (getter )pg_circle_getr , (setter )pg_circle_setr , NULL , NULL },
298
+ {"x" , (getter )pg_circle_getx , (setter )pg_circle_setx , DOC_CIRCLE_X , NULL },
299
+ {"y" , (getter )pg_circle_gety , (setter )pg_circle_sety , DOC_CIRCLE_Y , NULL },
300
+ {"r" , (getter )pg_circle_getr , (setter )pg_circle_setr , DOC_CIRCLE_R , NULL },
299
301
{NULL , 0 , NULL , NULL , NULL }};
300
302
301
303
static PyTypeObject pgCircle_Type = {
@@ -305,7 +307,7 @@ static PyTypeObject pgCircle_Type = {
305
307
.tp_repr = (reprfunc )pg_circle_repr ,
306
308
.tp_str = (reprfunc )pg_circle_str ,
307
309
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE ,
308
- .tp_doc = NULL ,
310
+ .tp_doc = DOC_CIRCLE ,
309
311
.tp_weaklistoffset = offsetof(pgCircleObject , weakreflist ),
310
312
.tp_methods = pg_circle_methods ,
311
313
.tp_getset = pg_circle_getsets ,
0 commit comments