From ca1b8184f6b31372c4d22007a622df420ba42a8d Mon Sep 17 00:00:00 2001 From: Ekopalypse Date: Thu, 2 Jan 2025 11:32:17 +0100 Subject: [PATCH 1/2] Handling of invalid values in the Search, Search, Replace and Replace methods --- ...st_SearchReplaceInvalidArgumentTestCase.py | 22 +++++++++++++++++++ PythonScript/src/ScintillaWrapper.cpp | 14 ++++++++++++ docs/source/scintilla.rst | 5 +++++ 3 files changed, 41 insertions(+) create mode 100644 PythonScript/python_tests/tests/test_SearchReplaceInvalidArgumentTestCase.py diff --git a/PythonScript/python_tests/tests/test_SearchReplaceInvalidArgumentTestCase.py b/PythonScript/python_tests/tests/test_SearchReplaceInvalidArgumentTestCase.py new file mode 100644 index 00000000..654bbf0f --- /dev/null +++ b/PythonScript/python_tests/tests/test_SearchReplaceInvalidArgumentTestCase.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +import unittest +from Npp import notepad, editor + +class SearchReplaceInvalidArgumentTestCase(unittest.TestCase): + def setUp(self): + notepad.new() + + def tearDown(self): + notepad.close() + + def test_invalid_search_argument(self): + for invalid_arg in ["", None]: + for method in [editor.research, editor.search, editor.replace, editor.rereplace]: + self.assertRaises(TypeError, method, invalid_arg, None) + + def test_invalid_replace_argument(self): + for method in [editor.replace, editor.rereplace]: + self.assertRaises(TypeError, method, "abc", None) + + +suite = unittest.TestLoader().loadTestsFromTestCase(SearchReplaceInvalidArgumentTestCase) diff --git a/PythonScript/src/ScintillaWrapper.cpp b/PythonScript/src/ScintillaWrapper.cpp index 728cfa0f..00a2f754 100644 --- a/PythonScript/src/ScintillaWrapper.cpp +++ b/PythonScript/src/ScintillaWrapper.cpp @@ -808,6 +808,15 @@ void ScintillaWrapper::replaceImpl(boost::python::object searchStr, boost::pytho intptr_t currentDocumentCodePage = this->GetCodePage(); std::string searchChars = extractEncodedString(searchStr, currentDocumentCodePage); + + if (searchStr.is_none() || searchChars.empty()) { + throw NppPythonScript::ArgumentException("search parameter must not be none or an empty string"); + } + + if (replaceStr.is_none()) { + throw NppPythonScript::ArgumentException("repalce parameter must not be none"); + } + std::string replaceChars; bool isPythonReplaceFunction = true; @@ -953,6 +962,11 @@ void ScintillaWrapper::searchImpl(boost::python::object searchStr, std::string searchChars = extractEncodedString(searchStr, currentDocumentCodePage); + if (searchStr.is_none() || searchChars.empty()) + { + throw NppPythonScript::ArgumentException("search parameter must not be none or an empty string"); + } + if (!PyCallable_Check(matchFunction.ptr())) { throw NppPythonScript::ArgumentException("match parameter must be callable, i.e. either a function or a lambda expression"); diff --git a/docs/source/scintilla.rst b/docs/source/scintilla.rst index dba0c367..72f9d012 100644 --- a/docs/source/scintilla.rst +++ b/docs/source/scintilla.rst @@ -5197,6 +5197,9 @@ Helper Methods See :meth:`editor.rereplace`, as this method is identical, with the exception that the search string is treated literally, and not as a regular expression. + The search argument triggers an exception if either an empty string or None is specified. + The replace argument triggers an exception if None is specified. + If you use a function as the replace argument, the function will still receive a ``re.MatchObject`` like object as the parameter, ``group(0)`` will therefore always contain the string searched for (possibly in a different case if ``re.IGNORECASE`` was passed in the flags) @@ -5233,6 +5236,8 @@ Helper Methods The replace function provided to editor.rereplace should not invoke additional calls to editor.rereplace or other replacement functions that modify the same text being processed. Doing so may result in unpredictable behavior. + The search argument triggers an exception if either an empty string or None is specified. + The replace argument triggers an exception if None is specified. ``flags`` are from the re module (e.g. ``re.IGNORECASE``), so ``import re`` if you use the flags. From a41ef01a7e153a0f2dede6fd7e2a51f98de86858 Mon Sep 17 00:00:00 2001 From: Ekopalypse <47723516+Ekopalypse@users.noreply.github.com> Date: Sat, 4 Jan 2025 13:26:16 +0100 Subject: [PATCH 2/2] fix typo --- PythonScript/src/ScintillaWrapper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PythonScript/src/ScintillaWrapper.cpp b/PythonScript/src/ScintillaWrapper.cpp index 00a2f754..7a9b535b 100644 --- a/PythonScript/src/ScintillaWrapper.cpp +++ b/PythonScript/src/ScintillaWrapper.cpp @@ -814,7 +814,7 @@ void ScintillaWrapper::replaceImpl(boost::python::object searchStr, boost::pytho } if (replaceStr.is_none()) { - throw NppPythonScript::ArgumentException("repalce parameter must not be none"); + throw NppPythonScript::ArgumentException("replace parameter must not be none"); } std::string replaceChars; @@ -1090,4 +1090,4 @@ std::string ScintillaWrapper::iso_latin_1_to_utf8(const std::string& ansi_input) return output; } -} \ No newline at end of file +}