Skip to content

Commit ca1b818

Browse files
committed
Handling of invalid values in the Search, Search, Replace and Replace methods
1 parent e2cb537 commit ca1b818

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
import unittest
3+
from Npp import notepad, editor
4+
5+
class SearchReplaceInvalidArgumentTestCase(unittest.TestCase):
6+
def setUp(self):
7+
notepad.new()
8+
9+
def tearDown(self):
10+
notepad.close()
11+
12+
def test_invalid_search_argument(self):
13+
for invalid_arg in ["", None]:
14+
for method in [editor.research, editor.search, editor.replace, editor.rereplace]:
15+
self.assertRaises(TypeError, method, invalid_arg, None)
16+
17+
def test_invalid_replace_argument(self):
18+
for method in [editor.replace, editor.rereplace]:
19+
self.assertRaises(TypeError, method, "abc", None)
20+
21+
22+
suite = unittest.TestLoader().loadTestsFromTestCase(SearchReplaceInvalidArgumentTestCase)

PythonScript/src/ScintillaWrapper.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,15 @@ void ScintillaWrapper::replaceImpl(boost::python::object searchStr, boost::pytho
808808
intptr_t currentDocumentCodePage = this->GetCodePage();
809809

810810
std::string searchChars = extractEncodedString(searchStr, currentDocumentCodePage);
811+
812+
if (searchStr.is_none() || searchChars.empty()) {
813+
throw NppPythonScript::ArgumentException("search parameter must not be none or an empty string");
814+
}
815+
816+
if (replaceStr.is_none()) {
817+
throw NppPythonScript::ArgumentException("repalce parameter must not be none");
818+
}
819+
811820
std::string replaceChars;
812821
bool isPythonReplaceFunction = true;
813822

@@ -953,6 +962,11 @@ void ScintillaWrapper::searchImpl(boost::python::object searchStr,
953962

954963
std::string searchChars = extractEncodedString(searchStr, currentDocumentCodePage);
955964

965+
if (searchStr.is_none() || searchChars.empty())
966+
{
967+
throw NppPythonScript::ArgumentException("search parameter must not be none or an empty string");
968+
}
969+
956970
if (!PyCallable_Check(matchFunction.ptr()))
957971
{
958972
throw NppPythonScript::ArgumentException("match parameter must be callable, i.e. either a function or a lambda expression");

docs/source/scintilla.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5197,6 +5197,9 @@ Helper Methods
51975197
See :meth:`editor.rereplace`, as this method is identical, with the exception that the search string is treated literally,
51985198
and not as a regular expression.
51995199

5200+
The search argument triggers an exception if either an empty string or None is specified.
5201+
The replace argument triggers an exception if None is specified.
5202+
52005203
If you use a function as the replace argument, the function will still receive a ``re.MatchObject`` like object as the parameter,
52015204
``group(0)`` will therefore always contain the string searched for (possibly in a different case if ``re.IGNORECASE`` was passed in the flags)
52025205

@@ -5233,6 +5236,8 @@ Helper Methods
52335236
The replace function provided to editor.rereplace should not invoke additional calls to editor.rereplace or other replacement functions
52345237
that modify the same text being processed. Doing so may result in unpredictable behavior.
52355238

5239+
The search argument triggers an exception if either an empty string or None is specified.
5240+
The replace argument triggers an exception if None is specified.
52365241

52375242
``flags`` are from the re module (e.g. ``re.IGNORECASE``), so ``import re`` if you use the flags.
52385243

0 commit comments

Comments
 (0)