Warning, file /kdevelop/kdev-python/debugger/variable.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2012 Sven Brauch <svenbrauch@googlemail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "variable.h"
0008 #include "debugsession.h"
0009 #include <interfaces/icore.h>
0010 
0011 #include <QDebug>
0012 #include "debuggerdebug.h"
0013 
0014 namespace Python {
0015 
0016 Variable::Variable(KDevelop::TreeModel* model, KDevelop::TreeItem* parent, const QString& expression, const QString& display)
0017     : KDevelop::Variable(model, parent, expression, display)
0018     , m_notifyCreated(nullptr)
0019     , m_pythonPtr(0)
0020 {
0021 
0022 }
0023 
0024 void Variable::dataFetched(QByteArray rawData)
0025 {
0026     QList<QByteArray> data = rawData.split('\n');
0027     data.removeLast();
0028     QByteArray value;
0029     foreach ( const QByteArray& item, data ) {
0030         value.append(item);
0031     }
0032     setValue(value);
0033     setHasMore(true);
0034     qCDebug(KDEV_PYTHON_DEBUGGER) << "value set to" << value << ", calling update method";
0035     if ( m_notifyCreated ) {
0036         QMetaObject::invokeMethod(m_notifyCreated, m_notifyCreatedMethod, Qt::QueuedConnection, Q_ARG(bool, true));
0037         m_notifyCreated = nullptr;
0038     }
0039 }
0040 
0041 void Variable::attachMaybe(QObject* callback, const char* callbackMethod)
0042 {
0043     IDebugSession* is = ICore::self()->debugController()->currentSession();
0044     DebugSession* s = static_cast<DebugSession*>(is);
0045     s->createVariable(this, callback, callbackMethod);
0046 }
0047 
0048 // TODO: make it really fetch *more*, not just some
0049 void Variable::fetchMoreChildren()
0050 {
0051     QString cmd;
0052     if ( m_pythonPtr ) {
0053         cmd = "__kdevpython_debugger_utils.format_ptr_children("+QString::number(m_pythonPtr)+")\n";
0054     }
0055     else {
0056         cmd = "__kdevpython_debugger_utils.format_object_children("+expression()+")\n";
0057     }
0058     InternalPdbCommand* fetchChildrenScript = new InternalPdbCommand(this, "moreChildrenFetched", cmd);
0059     static_cast<DebugSession*>(ICore::self()->debugController()->currentSession())->addCommand(fetchChildrenScript);
0060 }
0061 
0062 void Variable::setId(unsigned long int id)
0063 {
0064     m_pythonPtr = id;
0065 }
0066 
0067 void Variable::moreChildrenFetched(QByteArray rawData)
0068 {
0069     deleteChildren();
0070 
0071     QList<QByteArray> data = rawData.split('\n');
0072     data.removeLast();
0073     int i = 0;
0074     int initialLength = data.length();
0075     QRegExp formatExtract("(ptr:<(\\d*)>\\s)?([\\[\\]\\.a-zA-Z0-9_]+) \\=\\> (.*)$");
0076     formatExtract.setPatternSyntax(QRegExp::RegExp2);
0077     formatExtract.setMinimal(true);
0078     while ( i < data.length() ) {
0079         QByteArray d = data.at(i);
0080         // sort magic functions at the end of the list, they're not too interesting usually
0081         if ( d.startsWith('_') && i < initialLength ) {
0082             data.append(d);
0083             i++;
0084             continue;
0085         }
0086 
0087         QString childName;
0088         QString realValue;
0089         QString prettyName;
0090         unsigned long int pythonId = 0;
0091         if ( formatExtract.exactMatch(d) ) {
0092             QString id = formatExtract.capturedTexts().at(2);
0093             if ( ! id.isEmpty() ) {
0094                 pythonId = id.toLong();
0095             }
0096             childName = expression() + formatExtract.capturedTexts().at(3);
0097             prettyName = formatExtract.capturedTexts().at(3);
0098             realValue = formatExtract.capturedTexts().at(4);
0099         }
0100         else {
0101             i++;
0102             continue;
0103         }
0104         Variable* v = new Variable(model_, this, childName, prettyName);
0105         appendChild(v);
0106         qCDebug(KDEV_PYTHON_DEBUGGER) << "adding child:" << expression() << i << d;
0107         v->setValue(realValue);
0108         v->setId(pythonId);
0109         v->setHasMoreInitial(true);
0110         i++;
0111     }
0112 }
0113 
0114 }
0115 
0116 #include "moc_variable.cpp"