Skip to content

Commit

Permalink
Optimise conversion of QgsAttributes to Python objects
Browse files Browse the repository at this point in the history
Applies the same optimisations as are present in the QgsFeature
methods to avoid the overhead of sip's QVariant conversion logic
when attributes are of a basic type
  • Loading branch information
nyalldawson committed Feb 21, 2025
1 parent a51f4f6 commit 702acae
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 22 deletions.
62 changes: 54 additions & 8 deletions python/PyQt6/core/auto_generated/qgsattributes.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ typedef QMap<int, QString> QgsFieldNameMap;
typedef QMap<int, QgsField> QgsFieldMap;




typedef QVector<QVariant> QgsAttributes;

%MappedType QgsAttributes
Expand All @@ -39,14 +41,60 @@ typedef QVector<QVariant> QgsAttributes;
// Set the list elements.
for ( int i = 0; i < sipCpp->size(); ++i )
{
QVariant *v = new QVariant( sipCpp->at( i ) );
PyObject *tobj;

if ( ( tobj = sipConvertFromNewType( v, sipType_QVariant, Py_None ) ) == NULL )
const QVariant v = sipCpp->at( i );
PyObject *tobj = NULL;
// QByteArray null handling is "special"! See null_from_qvariant_converter in conversions.sip
if ( QgsVariantUtils::isNull( v, true ) && v.userType() != QMetaType::Type::QByteArray )
{
Py_INCREF( Py_None );
sipRes = Py_None;
}
else
{
switch ( v.userType() )
{
case QMetaType::Type::Int:
tobj = PyLong_FromLong( v.toInt() );
break;

case QMetaType::Type::UInt:
tobj = PyLong_FromUnsignedLong( v.toUInt() );
break;

case QMetaType::Type::Long:
case QMetaType::Type::LongLong:
tobj = PyLong_FromLongLong( v.toLongLong() );
break;

case QMetaType::Type::ULong:
case QMetaType::Type::ULongLong:
tobj = PyLong_FromUnsignedLongLong( v.toULongLong() );
break;

case QMetaType::Type::Bool:
tobj = PyBool_FromLong( v.toBool() ? 1 : 0 );
break;

case QMetaType::Type::Float:
case QMetaType::Type::Double:
tobj = PyFloat_FromDouble( v.toDouble() );
break;

case QMetaType::Type::QString:
tobj = PyUnicode_FromString( v.toString().toUtf8().constData() );
break;

default:
{
QVariant *newV = new QVariant( v );
tobj = sipConvertFromNewType( newV, sipType_QVariant, sipTransferObj );
break;
}
}
}
if ( tobj == NULL )
{
Py_DECREF( l );
delete v;

return NULL;
}

Expand Down Expand Up @@ -125,8 +173,6 @@ typedef QVector<QVariant> QgsAttributes;
return sipGetState( sipTransferObj );
%End
};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
69 changes: 62 additions & 7 deletions python/core/auto_generated/qgsattributes.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef QMap<int, QString> QgsFieldNameMap;
typedef QMap<int, QgsField> QgsFieldMap;



typedef QVector<QVariant> QgsAttributes;

%MappedType QgsAttributes
Expand All @@ -39,14 +40,69 @@ typedef QVector<QVariant> QgsAttributes;
// Set the list elements.
for ( int i = 0; i < sipCpp->size(); ++i )
{
QVariant *v = new QVariant( sipCpp->at( i ) );
PyObject *tobj;

if ( ( tobj = sipConvertFromNewType( v, sipType_QVariant, Py_None ) ) == NULL )
const QVariant v = sipCpp->at( i );
PyObject *tobj = NULL;
if ( !v.isValid() )
{
Py_INCREF( Py_None );
tobj = Py_None;
}
// QByteArray null handling is "special"! See null_from_qvariant_converter in conversions.sip
else if ( QgsVariantUtils::isNull( v, true ) && v.userType() != QMetaType::Type::QByteArray )
{
PyObject *vartype = sipConvertFromEnum( v.type(), sipType_QVariant_Type );
PyObject *args = PyTuple_Pack( 1, vartype );
PyTypeObject *typeObj = sipTypeAsPyTypeObject( sipType_QVariant );
tobj = PyObject_Call( ( PyObject * )typeObj, args, nullptr );
Py_DECREF( args );
Py_DECREF( vartype );
}
else
{
switch ( v.userType() )
{
case QMetaType::Type::Int:
tobj = PyLong_FromLong( v.toInt() );
break;

case QMetaType::Type::UInt:
tobj = PyLong_FromUnsignedLong( v.toUInt() );
break;

case QMetaType::Type::Long:
case QMetaType::Type::LongLong:
tobj = PyLong_FromLongLong( v.toLongLong() );
break;

case QMetaType::Type::ULong:
case QMetaType::Type::ULongLong:
tobj = PyLong_FromUnsignedLongLong( v.toULongLong() );
break;

case QMetaType::Type::Bool:
tobj = PyBool_FromLong( v.toBool() ? 1 : 0 );
break;

case QMetaType::Type::Float:
case QMetaType::Type::Double:
tobj = PyFloat_FromDouble( v.toDouble() );
break;

case QMetaType::Type::QString:
tobj = PyUnicode_FromString( v.toString().toUtf8().constData() );
break;

default:
{
QVariant *newV = new QVariant( v );
tobj = sipConvertFromNewType( newV, sipType_QVariant, sipTransferObj );
break;
}
}
}
if ( tobj == NULL )
{
Py_DECREF( l );
delete v;

return NULL;
}

Expand Down Expand Up @@ -126,7 +182,6 @@ typedef QVector<QVariant> QgsAttributes;
%End
};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
Loading

0 comments on commit 702acae

Please sign in to comment.