Skip to content

Commit 03e1d90

Browse files
committedMar 28, 2023
Immediately write out the console history file BEFORE running commands
This prevents loss of history when a user enters a Python command which results in a QGIS crash
1 parent b9b06b0 commit 03e1d90

File tree

8 files changed

+20
-6
lines changed

8 files changed

+20
-6
lines changed
 

‎python/console/console_sci.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ def promptForState(self, state):
137137
class ShellScintilla(QgsCodeEditorPython):
138138

139139
def __init__(self, parent=None):
140-
super().__init__(parent, [], QgsCodeEditor.Mode.CommandInput)
140+
# We set the ImmediatelyUpdateHistory flag here, as users can easily
141+
# crash QGIS by entering a Python command, and we don't want the
142+
# history leading to the crash lost..
143+
super().__init__(parent, [], QgsCodeEditor.Mode.CommandInput,
144+
flags=QgsCodeEditor.Flags(QgsCodeEditor.Flag.CodeFolding | QgsCodeEditor.Flag.ImmediatelyUpdateHistory))
141145

142146
self.parent = parent
143147
self._interpreter = PythonInterpreter()

‎python/gui/auto_additions/qgscodeeditor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
QgsCodeEditor.MarginRole.baseClass = QgsCodeEditor
2222
# monkey patching scoped based enum
2323
QgsCodeEditor.Flag.CodeFolding.__doc__ = "Indicates that code folding should be enabled for the editor"
24-
QgsCodeEditor.Flag.__doc__ = 'Flags controlling behavior of code editor\n\n.. versionadded:: 3.28\n\n' + '* ``CodeFolding``: ' + QgsCodeEditor.Flag.CodeFolding.__doc__
24+
QgsCodeEditor.Flag.ImmediatelyUpdateHistory.__doc__ = "Indicates that the history file should be immediately updated whenever a command is executed, instead of the default behavior of only writing the history on widget close. Since QGIS 3.32."
25+
QgsCodeEditor.Flag.__doc__ = 'Flags controlling behavior of code editor\n\n.. versionadded:: 3.28\n\n' + '* ``CodeFolding``: ' + QgsCodeEditor.Flag.CodeFolding.__doc__ + '\n' + '* ``ImmediatelyUpdateHistory``: ' + QgsCodeEditor.Flag.ImmediatelyUpdateHistory.__doc__
2526
# --
2627
QgsCodeEditor.Flag.baseClass = QgsCodeEditor
2728
QgsCodeEditor.Flags.baseClass = QgsCodeEditor

‎python/gui/auto_generated/codeeditors/qgscodeeditor.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ A text editor based on QScintilla2.
9999
enum class Flag
100100
{
101101
CodeFolding,
102+
ImmediatelyUpdateHistory,
102103
};
103104

104105
typedef QFlags<QgsCodeEditor::Flag> Flags;

‎python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ code autocompletion.
3131

3232

3333
QgsCodeEditorPython( QWidget *parent /TransferThis/ = 0, const QList<QString> &filenames = QList<QString>(),
34-
QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor );
34+
QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor, QgsCodeEditor::Flags flags = QgsCodeEditor::Flag::CodeFolding );
3535
%Docstring
3636
Construct a new Python editor.
3737

3838
:param parent: The parent QWidget
3939
:param filenames: The list of apis files to load for the Python lexer
4040
:param mode: code editor mode (since QGIS 3.30)
41+
:param flags: code editor flags (since QGIS 3.32)
4142

4243
.. versionadded:: 2.6
4344
%End

‎src/gui/codeeditors/qgscodeeditor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,11 @@ QStringList QgsCodeEditor::history() const
761761
void QgsCodeEditor::runCommand( const QString &command, bool skipHistory )
762762
{
763763
if ( !skipHistory )
764+
{
764765
updateHistory( { command } );
766+
if ( mFlags & QgsCodeEditor::Flag::ImmediatelyUpdateHistory )
767+
writeHistoryFile();
768+
}
765769

766770
if ( mInterpreter )
767771
mInterpreter->exec( command );

‎src/gui/codeeditors/qgscodeeditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla
139139
enum class Flag : int
140140
{
141141
CodeFolding = 1 << 0, //!< Indicates that code folding should be enabled for the editor
142+
ImmediatelyUpdateHistory = 1 << 1, //!< Indicates that the history file should be immediately updated whenever a command is executed, instead of the default behavior of only writing the history on widget close. Since QGIS 3.32.
142143
};
143144
Q_ENUM( Flag )
144145

‎src/gui/codeeditors/qgscodeeditorpython.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ const QgsSettingsEntryBool *QgsCodeEditorPython::settingBlackNormalizeQuotes = n
5151
///@endcond PRIVATE
5252

5353

54-
QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList<QString> &filenames, Mode mode )
54+
QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList<QString> &filenames, Mode mode, Flags flags )
5555
: QgsCodeEditor( parent,
5656
QString(),
5757
false,
5858
false,
59-
QgsCodeEditor::Flag::CodeFolding, mode )
59+
flags,
60+
mode )
6061
, mAPISFilesList( filenames )
6162
{
6263
if ( !parent )

‎src/gui/codeeditors/qgscodeeditorpython.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ class GUI_EXPORT QgsCodeEditorPython : public QgsCodeEditor
7171
* \param parent The parent QWidget
7272
* \param filenames The list of apis files to load for the Python lexer
7373
* \param mode code editor mode (since QGIS 3.30)
74+
* \param flags code editor flags (since QGIS 3.32)
7475
* \since QGIS 2.6
7576
*/
7677
QgsCodeEditorPython( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QList<QString> &filenames = QList<QString>(),
77-
QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor );
78+
QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor, QgsCodeEditor::Flags flags = QgsCodeEditor::Flag::CodeFolding );
7879

7980
Qgis::ScriptLanguage language() const override;
8081
Qgis::ScriptLanguageCapabilities languageCapabilities() const override;

0 commit comments

Comments
 (0)
Please sign in to comment.