Skip to content

Commit 19c081e

Browse files
frmdstryrMatthieuDartiailh
authored andcommitted
Refactor setattrbehavior calls
1 parent 7030a07 commit 19c081e

File tree

1 file changed

+17
-69
lines changed

1 file changed

+17
-69
lines changed

atom/src/setattrbehavior.cpp

Lines changed: 17 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,11 @@ 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+
if( !PyObject_Vectorcall( member->setattr_context, args, 2, 0 ) )
322301
return -1;
323302
return 0;
324303
}
@@ -327,18 +306,11 @@ call_object_object_value_handler( Member* member, CAtom* atom, PyObject* value )
327306
int
328307
call_object_object_name_value_handler( Member* member, CAtom* atom, PyObject* value )
329308
{
330-
cppy::ptr valueptr( cppy::incref( value ) );
331-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
309+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
332310
if( !valueptr )
333311
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 ) )
312+
PyObject* args[] = { pyobject_cast( atom ), member->name, valueptr.get() };
313+
if( !PyObject_Vectorcall( member->setattr_context, args, 3, 0 ) )
342314
return -1;
343315
return 0;
344316
}
@@ -347,18 +319,10 @@ call_object_object_name_value_handler( Member* member, CAtom* atom, PyObject* va
347319
int
348320
object_method_value_handler( Member* member, CAtom* atom, PyObject* value )
349321
{
350-
cppy::ptr valueptr( cppy::incref( value ) );
351-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
322+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
352323
if( !valueptr )
353324
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 ) )
325+
if( !PyObject_CallMethodOneArg( pyobject_cast( atom ), member->setattr_context, valueptr.get() ) )
362326
return -1;
363327
return 0;
364328
}
@@ -367,19 +331,11 @@ object_method_value_handler( Member* member, CAtom* atom, PyObject* value )
367331
int
368332
object_method_name_value_handler( Member* member, CAtom* atom, PyObject* value )
369333
{
370-
cppy::ptr valueptr( cppy::incref( value ) );
371-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
334+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
372335
if( !valueptr )
373336
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 ) )
337+
PyObject* args[] = { pyobject_cast( atom ), member->name, valueptr.get() };
338+
if( !PyObject_VectorcallMethod( member->setattr_context, args, 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, 0 ) )
383339
return -1;
384340
return 0;
385341
}
@@ -388,19 +344,11 @@ object_method_name_value_handler( Member* member, CAtom* atom, PyObject* value )
388344
int
389345
member_method_object_value_handler( Member* member, CAtom* atom, PyObject* value )
390346
{
391-
cppy::ptr valueptr( cppy::incref( value ) );
392-
valueptr = member->full_validate( atom, Py_None, valueptr.get() );
347+
cppy::ptr valueptr( member->full_validate( atom, Py_None, value ) );
393348
if( !valueptr )
394349
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 ) )
350+
PyObject* args[] = { pyobject_cast( member ), pyobject_cast( atom ), valueptr.get() };
351+
if( !PyObject_VectorcallMethod( member->setattr_context, args, 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, 0 ) )
404352
return -1;
405353
return 0;
406354
}

0 commit comments

Comments
 (0)