itom
Loading...
Searching...
No Matches
ito::PythonQtConversion Class Reference

Conversion class with mainly static methods, which convert values between Qt and standard C++ data types and PyObject* values. More...

Public Types

enum  UnicodeEncodings {
  utf_8 , latin_1 , iso_8859_1 , mbcs ,
  ascii , utf_16 , utf_16_LE , utf_16_BE ,
  utf_32 , utf_32_BE , utf_32_LE , other
}
 

Static Public Member Functions

static PyObject * QStringToPyObject (const QString &str)
 converts QString to Python string (unicode!)
 
static PyObject * QStringListToPyObject (const QStringList &list)
 converts QStringList to Python tuple
 
static PyObject * QStringListToPyList (const QStringList &list)
 converts QStringList to Python list
 
static PyObject * QDateToPyDate (const QDate &date)
 converts QDate to Python datetime.date object
 
static PyObject * QDateTimeToPyDateTime (const QDateTime &datetime)
 converts QDateTime to Python datetime.datetime object
 
static PyObject * QTimeToPyTime (const QTime &time)
 converts QTime to Python datetime.time object
 
static QString PyObjGetRepresentation (PyObject *val)
 get string representation of py object
 
static QString PyObjGetString (PyObject *val)
 get string value from py object
 
static QString PyObjGetString (PyObject *val, bool strict, bool &ok)
 get string value from py object
 
static std::string PyObjGetStdStringAsLatin1 (PyObject *val, bool strict, bool &ok)
 get std::string value from py object. std::string has latin1 encoding
 
static QByteArray PyObjGetBytes (PyObject *val, bool strict, bool &ok)
 get bytes from py object
 
static QSharedPointer< char > PyObjGetBytesShared (PyObject *val, bool strict, bool &ok)
 get bytes from py object
 
static int PyObjGetInt (PyObject *val, bool strict, bool &ok)
 get int from py object
 
static unsigned int PyObjGetUInt (PyObject *val, bool strict, bool &ok)
 get unsigned int from py object
 
static qint64 PyObjGetLongLong (PyObject *val, bool strict, bool &ok)
 get int64 from py object
 
static quint64 PyObjGetULongLong (PyObject *val, bool strict, bool &ok)
 get int64 from py object
 
static double PyObjGetDouble (PyObject *val, bool strict, bool &ok)
 get double from py object
 
static QVector< double > PyObjGetDoubleArray (PyObject *val, bool strict, bool &ok)
 get double-array from py object
 
static complex128 PyObjGetComplex (PyObject *val, bool strict, bool &ok)
 get complex from py object
 
static QVector< complex128 > PyObjGetComplexArray (PyObject *val, bool strict, bool &ok)
 get complex-array from py object
 
static QVector< int > PyObjGetIntArray (PyObject *val, bool strict, bool &ok)
 get int-array from py object
 
static bool PyObjGetBool (PyObject *val, bool strict, bool &ok)
 get bool from py object
 
static QVector< ito::ShapePyObjGetShapeVector (PyObject *val, bool &ok)
 get shape vector from py object
 
static QVector< ito::ByteArrayPyObjGetByteArrayList (PyObject *val, bool strict, bool &ok)
 get ito::ByteArray list from py object
 
static ito::DataObjectPyObjGetDataObjectNewPtr (PyObject *val, bool strict, bool &ok, ito::RetVal *retVal=nullptr, bool addNumpyOrgTags=false)
 
static QSharedPointer< ito::DataObjectPyObjGetSharedDataObject (PyObject *val, bool strict, bool &ok, ito::RetVal *retVal=nullptr)
 
static QStringList PyObjToStringList (PyObject *val, bool strict, bool &ok)
 create a string list from python sequence
 
static QVariant PyObjToQVariant (PyObject *val, int type=-1)
 conversion from PyObject* to QVariant
 
static bool PyObjToVoidPtr (PyObject *val, void **retPtr, int *retType, int type=-1, bool strict=false)
 tries to convert PyObject* to known data type and returns deep-copy of the value, given as char*
 
static PyObject * GetPyBool (bool val)
 convert QVariant from PyObject
 
static PyObject * QVariantToPyObject (const QVariant &v)
 conversion from given QVariant to appropriate PyObject*
 
static PyObject * QVariantMapToPyObject (const QVariantMap &m)
 conversion from given QVariantMap to python-dictionary.
 
static PyObject * QVariantListToPyObject (const QVariantList &l)
 conversion from given QVariantList to python-tuple.
 
static PyObject * DataObjectToPyObject (const ito::DataObject &dObj)
 
static PyObject * AddInBaseToPyObject (ito::AddInBase *aib)
 
static PyObject * ConvertQtValueToPythonInternal (int type, const void *data)
 method internally used for conversion from given type-id (QMetaType) and corresponding char*-pointer to PyObject*
 
static PyObject * QByteArrayToPyUnicode (const QByteArray &ba, const char *errors="replace")
 
static PyObject * QByteArrayToPyUnicodeSecure (const QByteArray &ba, const char *errors="replace")
 
static PyObject * QByteArrayUtf8ToPyUnicode (const QByteArray &ba, const char *errors="replace")
 
static PyObject * QByteArrayUtf8ToPyUnicodeSecure (const QByteArray &ba, const char *errors="replace")
 
static PyObject * ByteArrayToPyUnicode (const char *byteArray, Py_ssize_t len=0, const char *errors="replace")
 
static PyObject * PyUnicodeToPyByteObject (PyObject *unicode, const char *errors="replace")
 

Static Private Member Functions

static int guessQMetaTypeFromPyObject (PyObject *val)
 
static void baseObjectDeleterDataObject (ito::DataObject *sharedObject)
 

Static Private Attributes

static UnicodeEncodings textEncoding = PythonQtConversion::latin_1
 
static QByteArray textEncodingName = "latin_1"
 
static QHash< char *, PyObject * > m_pyBaseObjectStorage = QHash<char*, PyObject*>()
 

Friends

class PythonEngine
 

Detailed Description

Conversion class with mainly static methods, which convert values between Qt and standard C++ data types and PyObject* values.

Parts of this class are taken from the project PythonQt (http://pythonqt.sourceforge.net/)

Member Function Documentation

◆ ConvertQtValueToPythonInternal()

PyObject * ito::PythonQtConversion::ConvertQtValueToPythonInternal ( int type,
const void * data )
static

method internally used for conversion from given type-id (QMetaType) and corresponding char*-pointer to PyObject*

This method is the opposite from PyObjToVoidPtr and converts a pair given by type-id (see QMetaType) and corresponding char*-pointer, which to the variable's content to the appropriate python type.

A python error is returned if conversion failed.

Parameters
typeis given type-id (QMetaType)
datais the content, casted to char*
Returns
is the resulting PyObject* (new reference)

◆ GetPyBool()

PyObject * ito::PythonQtConversion::GetPyBool ( bool val)
static

convert QVariant from PyObject

conversion from bool to python boolean type

Returns new reference to Py_True or Py_False depending on input val.

Parameters
valis the given boolean input
Returns
resulting PyObject* (new reference)

◆ PyObjGetBool()

bool ito::PythonQtConversion::PyObjGetBool ( PyObject * val,
bool strict,
bool & ok )
static

get bool from py object

conversion from PyObject* to bool

tries to convert the given PyObject* val as boolean variable and returns its value. If val is no boolean object the output depends on the param strict. If strict==false, the output is true, if the integer conversion (strict==false) of val is unequal to zero.

Parameters
valis the given python object
strictindicates if only real boolean types should be converted
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting bool

◆ PyObjGetByteArrayList()

QVector< ito::ByteArray > ito::PythonQtConversion::PyObjGetByteArrayList ( PyObject * val,
bool strict,
bool & ok )
static

get ito::ByteArray list from py object

conversion from PyObject* to vector of ito::ByteArray

tries to interpret given PyObject* as list of strings and converts it to QVector<ito::ByteArray>. If strict is true, we do not want to convert a string to a stringlist, since single strings in python are also detected to be sequences.

Strings are converted to the byte array using the latin1 coded, if possible.

Parameters
valis the given python object
strictindicates if any object fitting to the sequence protocol is interpreted as string list, too [true]
ok(ByRef) is set to true if conversion was successful, else false
Returns
resulting QVector<ito::ByteArray>

◆ PyObjGetBytes()

QByteArray ito::PythonQtConversion::PyObjGetBytes ( PyObject * val,
bool strict,
bool & ok )
static

get bytes from py object

conversion from PyObject* to QString

If val is a byte-object, it is directly converted into a QString. If val is an unicode-object, its value is converted using the current encoding and returned. In any other case the string-like-representation is only returned if strict is set to false.

Parameters
valis the given python object
strictindicates if only real byte or unicode objects can be converted to string
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting QString

◆ PyObjGetDouble()

double ito::PythonQtConversion::PyObjGetDouble ( PyObject * val,
bool strict,
bool & ok )
static

get double from py object

conversion from PyObject* to double

If val is a floating-point object, it is directly converted into a double variable. Otherwise, the output depends on strict. If strict is equal to false, any fixed-point object is interpreted as double and the result is returned. Additionally boolean variables are returned as 0.0 (false) or 1.0 (true) if strict is false, too.

Parameters
valis the given python object
strictindicates if only real floating point numbers should be converted
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting double value

◆ PyObjGetInt()

int ito::PythonQtConversion::PyObjGetInt ( PyObject * val,
bool strict,
bool & ok )
static

get int from py object

conversion from PyObject* to integer

If val is a fixed-point object, it is directly converted into an integer variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.

Parameters
valis the given python object
strictindicates if only real integer or long types should be converted
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting integer

◆ PyObjGetLongLong()

qint64 ito::PythonQtConversion::PyObjGetLongLong ( PyObject * val,
bool strict,
bool & ok )
static

get int64 from py object

conversion from PyObject* to long long (64bit, if possible)

If val is a fixed-point object, it is directly converted into an qint64 variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.

Parameters
valis the given python object
strictindicates if only real integer or long types should be converted
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting qint64

◆ PyObjGetRepresentation()

QString ito::PythonQtConversion::PyObjGetRepresentation ( PyObject * val)
static

get string representation of py object

string representation of PyObject*

returns a string or a string-like interpretation of the given PyObject*. If this object is no byte- or unicode-object, its string representation is returned, obtained by calling the tp_repr-slot of the corresponding Type-struct.

Parameters
valis the given python object
Returns
resulting QString

◆ PyObjGetShapeVector()

QVector< ito::Shape > ito::PythonQtConversion::PyObjGetShapeVector ( PyObject * val,
bool & ok )
static

get shape vector from py object

get shape vector from pyObj

◆ PyObjGetSharedDataObject()

QSharedPointer< ito::DataObject > ito::PythonQtConversion::PyObjGetSharedDataObject ( PyObject * val,
bool strict,
bool & ok,
ito::RetVal * retVal = nullptr )
static

converts the python object to a DataObject. If the returned value is destroyed, possible base objects will be safely removed, too. If strict is true, the returned DataObject is exactly the same object, than wrapped by the given python object. Else, numpy-like arrays can also be accepted and will be converted (shallow or deep copy, whatever is possible) to a dataObject.

◆ PyObjGetStdStringAsLatin1()

std::string ito::PythonQtConversion::PyObjGetStdStringAsLatin1 ( PyObject * val,
bool strict,
bool & ok )
static

get std::string value from py object. std::string has latin1 encoding

conversion from PyObject* to std::string

If val is a byte-object, it is directly converted into a std::string. If val is an unicode-object, its value is converted using the current encoding and returned. In any other case the string-like-representation is only returned if strict is set to false.

Parameters
valis the given python object
strictindicates if only real byte or unicode objects can be converted to string
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting QString

◆ PyObjGetString()

QString ito::PythonQtConversion::PyObjGetString ( PyObject * val,
bool strict,
bool & ok )
static

get string value from py object

conversion from PyObject* to QString

If val is a byte-object, it is directly converted into a QString. If val is an unicode-object, its value is converted using the current encoding and returned. In any other case the string-like-representation is only returned if strict is set to false.

Parameters
valis the given python object
strictindicates if only real byte or unicode objects can be converted to string
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting QString

◆ PyObjGetUInt()

unsigned int ito::PythonQtConversion::PyObjGetUInt ( PyObject * val,
bool strict,
bool & ok )
static

get unsigned int from py object

conversion from PyObject* to unsigned integer

If val is a fixed-point object, it is directly converted into an integer variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.

Parameters
valis the given python object
strictindicates if only real integer or long types should be converted
ok(ByRef) is set to true if conversion succeeded.
Returns
resulting integer

◆ PyObjGetULongLong()

quint64 ito::PythonQtConversion::PyObjGetULongLong ( PyObject * val,
bool strict,
bool & ok )
static

get int64 from py object

conversion from PyObject* to unsigned integer 64bit

If val is a fixed-point object, it is directly converted into an quint64 variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.

Parameters
valis the given python object
strictindicates if only real integer or long types should be converted
ok(ByRef) is set to true if conversion succeeded. Conversion fails if number is smaller than zero.
Returns
resulting quint64

◆ PyObjToQVariant()

QVariant ito::PythonQtConversion::PyObjToQVariant ( PyObject * val,
int type = -1 )
static

conversion from PyObject* to QVariant

convert python object to qvariant, if type is given it will try to create a qvariant of that type, otherwise it will guess from the python type

methods tries to convert PyObject* to QVariant. Type indicates the desired type of QVariant, given by the type-number of QMetaType. If type==-1, the right type is guessed by checking the PyObject*-type. If conversion failed, QVariant() is returned.

Parameters
valis the given python object
typeis the desired Qt-type (with respect to QMetaType) or -1, if resulting type should be guessed by input
Returns
resulting QVariant or empty QVariant(), if conversion failed

◆ PyObjToStringList()

QStringList ito::PythonQtConversion::PyObjToStringList ( PyObject * val,
bool strict,
bool & ok )
static

create a string list from python sequence

conversion from PyObject* to QStringList

tries to interpret given PyObject* as list of strings and converts it to QStringList. If strict is true, we do not want to convert a string to a stringlist, since single strings in python are also detected to be sequences.

Parameters
valis the given python object
strictindicates if any object fitting to the sequence protocol is interpreted as string list, too [true]
ok(ByRef) is set to true if conversion was successful, else false
Returns
resulting QStringList

◆ PyObjToVoidPtr()

bool ito::PythonQtConversion::PyObjToVoidPtr ( PyObject * val,
void ** retPtr,
int * retType,
int type = -1,
bool strict = false )
static

tries to convert PyObject* to known data type and returns deep-copy of the value, given as char*

convert python object to char* using QMetaType. if type is given it will try to create a char* of that type, otherwise it will guess from the python type. If fails, NULL is returned

methods tries to convert PyObject* to QVariant. Type indicates the desired type of QVariant, given by the type-number of QMetaType. If type==-1, the right type is guessed by checking the PyObject*-type. If conversion failed, QVariant() is returned.

The deep-copy is created using QMetaType::create(...)

Parameters
valis the given python object
retPtris the resulting pointer to type char*, containing the converted deep copy or NULL, if error
retTypeis an integer-pointer, containing the type-id of the converted result (with respect to QMetaType) or -1, if failure
typeis the desired QMetaType-type-id or -1, if conversion-type should be guessed considering the type of val.
strictdecides whether the given PyObject should strictly be converted into the C-type or a cast like int->string is allowed, too.
Returns
true if conversion succeeded or false otherwise
See also
PyObjToQVariant

◆ QStringListToPyList()

PyObject * ito::PythonQtConversion::QStringListToPyList ( const QStringList & list)
static

converts QStringList to Python list

conversion from given QStringList to list of python-unicode objects

returns new reference to list of Python-unicode objects, containing the content of given QStringList. Every single string is converted using QStringToPyObject.

Parameters
listis reference to QStringList
Returns
is the resulting PyObject-list (new reference)

◆ QStringListToPyObject()

PyObject * ito::PythonQtConversion::QStringListToPyObject ( const QStringList & list)
static

converts QStringList to Python tuple

conversion from given QStringList to tuple of python-unicode objects

returns new reference to tuple of Python-unicode objects, containing the content of given QStringList. Every single string is converted using QStringToPyObject.

Parameters
listis reference to QStringList
Returns
is the resulting PyObject-tuple (new reference)

◆ QStringToPyObject()

PyObject * ito::PythonQtConversion::QStringToPyObject ( const QString & str)
static

converts QString to Python string (unicode!)

conversion from given QString to PyObject*

returns new reference to Python-unicode object, containing the content of given QString. If str is empty or null, the resulting string is empty string ("")

Parameters
stris reference to QString
Returns
is the resulting PyUnicode-Object (new reference)

◆ QVariantListToPyObject()

PyObject * ito::PythonQtConversion::QVariantListToPyObject ( const QVariantList & l)
static

conversion from given QVariantList to python-tuple.

returns new reference to python-tuple type. Each item of QVariantList is one element in tuple. The values are converted using QVariantToPyObject.

Parameters
mis reference to QVariantMap
Returns
is the resulting PyObject*
See also
QVariantToPyObject

◆ QVariantMapToPyObject()

PyObject * ito::PythonQtConversion::QVariantMapToPyObject ( const QVariantMap & m)
static

conversion from given QVariantMap to python-dictionary.

returns new reference to python-dictionary type. Each key of QVariantMap is one element in dictionary. The values are converted using QVariantToPyObject.

Parameters
mis reference to QVariantMap
Returns
is the resulting PyObject*
See also
QVariantToPyObject

◆ QVariantToPyObject()

PyObject * ito::PythonQtConversion::QVariantToPyObject ( const QVariant & v)
static

conversion from given QVariant to appropriate PyObject*

returns new reference to PyObject*, which contains the conversion from given QVariant-variable.

Parameters
vis reference to QVariant
Returns
is the resulting PyObject*
See also
ConvertQtValueToPytonInternal

Member Data Documentation

◆ m_pyBaseObjectStorage

QHash< char *, PyObject * > ito::PythonQtConversion::m_pyBaseObjectStorage = QHash<char*, PyObject*>()
staticprivate

if any PyObject is converted into a QVariant-object, dataObject or any other class, and if this PyObject has a base-pointer unequal to None, this base pointer is incremented during the lifetime of the dataObject, passed to QVariant. If this dataObject is destroyed, the baseObjectDeleterDataObject deleter method is called and decrements the base PyObject.

Be careful: For decrementing the refcount, the GIL must be hold by this deleter!


The documentation for this class was generated from the following files: