Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 15 additions & 1 deletion PythonScript/src/ScintillaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("replace parameter must not be none");
}

std::string replaceChars;
bool isPythonReplaceFunction = true;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1076,4 +1090,4 @@ std::string ScintillaWrapper::iso_latin_1_to_utf8(const std::string& ansi_input)
return output;
}

}
}
5 changes: 5 additions & 0 deletions docs/source/scintilla.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.

Expand Down