Skip to content

Commit 399399a

Browse files
authored
Refactor setattrbehavior calls (#227)
* Refactor setattrbehavior calls * Make sure all setattr handler results get checked
1 parent 7030a07 commit 399399a

File tree

1 file changed

+22
-69
lines changed

1 file changed

+22
-69
lines changed

atom/src/setattrbehavior.cpp

Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,7 @@ _mangled_property_handler( Member* member, CAtom* atom, PyObject* value )
268268
cppy::ptr name( PyUnicode_FromFormat( "_set_%s", suffix ) );
269269
if( !name )
270270
return -1;
271-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( atom ), name.get() ) );
272-
if( !callable )
273-
{
274-
if( PyErr_ExceptionMatches( PyExc_AttributeError ) )
275-
PyErr_SetString( PyExc_AttributeError, "can't set attribute" );
276-
return -1;
277-
}
278-
cppy::ptr argsptr( PyTuple_New( 1 ) );
279-
if( !argsptr )
280-
return -1;
281-
PyTuple_SET_ITEM( argsptr.get(), 0, cppy::incref( value ) );
282-
cppy::ptr ok( PyObject_Call( callable.get(), argsptr.get(), 0 ) );
271+
cppy::ptr ok( PyObject_CallMethodOneArg( pyobject_cast( atom ), name.get(), value ) );
283272
if( !ok )
284273
return -1;
285274
return 0;
@@ -291,12 +280,8 @@ property_handler( Member* member, CAtom* atom, PyObject* value )
291280
{
292281
if( member->setattr_context != Py_None )
293282
{
294-
cppy::ptr argsptr( PyTuple_New( 2 ) );
295-
if( !argsptr )
296-
return -1;
297-
PyTuple_SET_ITEM( argsptr.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
298-
PyTuple_SET_ITEM( argsptr.get(), 1, cppy::incref( pyobject_cast( value ) ) );
299-
cppy::ptr ok( PyObject_Call( member->setattr_context, argsptr.get(), 0 ) );
283+
PyObject* args[] = { pyobject_cast( atom ), value };
284+
cppy::ptr ok( PyObject_Vectorcall( member->setattr_context, args, 2, 0 ) );
300285
if( !ok )
301286
return -1;
302287
return 0;
@@ -308,17 +293,12 @@ property_handler( Member* member, CAtom* atom, PyObject* value )
308293
int
309294
call_object_object_value_handler( Member* member, CAtom* atom, PyObject* value )
310295
{
311-
cppy::ptr valueptr( cppy::incref( value ) );
312-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
296+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
313297
if( !valueptr )
314298
return -1;
315-
cppy::ptr callable( cppy::incref( member->setattr_context ) );
316-
cppy::ptr argsptr( PyTuple_New( 2 ) );
317-
if( !argsptr )
318-
return -1;
319-
PyTuple_SET_ITEM( argsptr.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
320-
PyTuple_SET_ITEM( argsptr.get(), 1, valueptr.release() );
321-
if( !callable.call( argsptr ) )
299+
PyObject* args[] = { pyobject_cast( atom ), valueptr.get() };
300+
cppy::ptr ok( PyObject_Vectorcall( member->setattr_context, args, 2, 0 ) );
301+
if( !ok )
322302
return -1;
323303
return 0;
324304
}
@@ -327,18 +307,12 @@ call_object_object_value_handler( Member* member, CAtom* atom, PyObject* value )
327307
int
328308
call_object_object_name_value_handler( Member* member, CAtom* atom, PyObject* value )
329309
{
330-
cppy::ptr valueptr( cppy::incref( value ) );
331-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
310+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
332311
if( !valueptr )
333312
return -1;
334-
cppy::ptr callable( cppy::incref( member->setattr_context ) );
335-
cppy::ptr argsptr( PyTuple_New( 3 ) );
336-
if( !argsptr )
337-
return -1;
338-
PyTuple_SET_ITEM( argsptr.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
339-
PyTuple_SET_ITEM( argsptr.get(), 1, cppy::incref( member->name ) );
340-
PyTuple_SET_ITEM( argsptr.get(), 2, valueptr.release() );
341-
if( !callable.call( argsptr ) )
313+
PyObject* args[] = { pyobject_cast( atom ), member->name, valueptr.get() };
314+
cppy::ptr ok( PyObject_Vectorcall( member->setattr_context, args, 3, 0 ) );
315+
if( !ok )
342316
return -1;
343317
return 0;
344318
}
@@ -347,18 +321,11 @@ call_object_object_name_value_handler( Member* member, CAtom* atom, PyObject* va
347321
int
348322
object_method_value_handler( Member* member, CAtom* atom, PyObject* value )
349323
{
350-
cppy::ptr valueptr( cppy::incref( value ) );
351-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
324+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
352325
if( !valueptr )
353326
return -1;
354-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( atom ), member->setattr_context ) );
355-
if( !callable )
356-
return -1;
357-
cppy::ptr argsptr( PyTuple_New( 1 ) );
358-
if( !argsptr )
359-
return -1;
360-
PyTuple_SET_ITEM( argsptr.get(), 0, valueptr.release() );
361-
if( !callable.call( argsptr ) )
327+
cppy::ptr ok( PyObject_CallMethodOneArg( pyobject_cast( atom ), member->setattr_context, valueptr.get() ) );
328+
if ( !ok )
362329
return -1;
363330
return 0;
364331
}
@@ -367,19 +334,12 @@ object_method_value_handler( Member* member, CAtom* atom, PyObject* value )
367334
int
368335
object_method_name_value_handler( Member* member, CAtom* atom, PyObject* value )
369336
{
370-
cppy::ptr valueptr( cppy::incref( value ) );
371-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
337+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
372338
if( !valueptr )
373339
return -1;
374-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( atom ), member->setattr_context ) );
375-
if( !callable )
376-
return -1;
377-
cppy::ptr argsptr( PyTuple_New( 2 ) );
378-
if( !argsptr )
379-
return -1;
380-
PyTuple_SET_ITEM( argsptr.get(), 0, cppy::incref( member->name ) );
381-
PyTuple_SET_ITEM( argsptr.get(), 1, valueptr.release() );
382-
if( !callable.call( argsptr ) )
340+
PyObject* args[] = { pyobject_cast( atom ), member->name, valueptr.get() };
341+
cppy::ptr ok( PyObject_VectorcallMethod( member->setattr_context, args, 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, 0 ) );
342+
if( !ok )
383343
return -1;
384344
return 0;
385345
}
@@ -388,19 +348,12 @@ object_method_name_value_handler( Member* member, CAtom* atom, PyObject* value )
388348
int
389349
member_method_object_value_handler( Member* member, CAtom* atom, PyObject* value )
390350
{
391-
cppy::ptr valueptr( cppy::incref( value ) );
392-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
351+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
393352
if( !valueptr )
394353
return -1;
395-
cppy::ptr callable( PyObject_GetAttr( pyobject_cast( member ), member->setattr_context ) );
396-
if( !callable )
397-
return -1;
398-
cppy::ptr argsptr( PyTuple_New( 2 ) );
399-
if( !argsptr )
400-
return -1;
401-
PyTuple_SET_ITEM( argsptr.get(), 0, cppy::incref( pyobject_cast( atom ) ) );
402-
PyTuple_SET_ITEM( argsptr.get(), 1, valueptr.release() );
403-
if( !callable.call( argsptr ) )
354+
PyObject* args[] = { pyobject_cast( member ), pyobject_cast( atom ), valueptr.get() };
355+
cppy::ptr ok( PyObject_VectorcallMethod( member->setattr_context, args, 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, 0 ) );
356+
if( !ok )
404357
return -1;
405358
return 0;
406359
}

0 commit comments

Comments
 (0)