PY-23625 Fixed: Incorrect default argument value in print() function

Escape non-printable characters in parameter's default value.
This commit is contained in:
Semyon Proshev
2017-06-09 17:02:28 +03:00
parent 3e447142db
commit 45321db0ac
3 changed files with 26 additions and 7 deletions
@@ -20,6 +20,7 @@ import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
@@ -187,14 +188,20 @@ public class PyNamedParameterImpl extends PyBaseElementImpl<PyNamedParameterStub
final PyExpression defaultValue = getDefaultValue();
if (defaultValueShouldBeIncluded(includeDefaultValue, defaultValue, argumentType)) {
String representation = PyUtil.getReadableRepr(defaultValue, true);
if (defaultValue instanceof PyStringLiteralExpression) {
final Pair<String, String> quotes = PyStringLiteralUtil.getQuotes(defaultValue.getText());
if (quotes != null) {
representation = quotes.getFirst() + PyStringLiteralUtil.getStringValue(defaultValue) + quotes.getSecond();
}
final Pair<String, String> quotes = defaultValue instanceof PyStringLiteralExpression
? PyStringLiteralUtil.getQuotes(defaultValue.getText())
: null;
sb.append("=");
if (quotes != null) {
final String value = ((PyStringLiteralExpression)defaultValue).getStringValue();
sb.append(quotes.getFirst());
StringUtil.escapeStringCharacters(value.length(), value, sb);
sb.append(quotes.getSecond());
}
else {
sb.append(PyUtil.getReadableRepr(defaultValue, true));
}
sb.append("=").append(representation);
}
return sb.toString();
@@ -0,0 +1,5 @@
def foo(p="\n", t="\t", r="\r"):
pass
foo(<arg1>)
@@ -606,6 +606,13 @@ public class PyParameterInfoTest extends LightMarkedTestCase {
);
}
// PY-23625
public void testEscapingInDefaultValue() {
final int offset = loadTest(1).get("<arg1>").getTextOffset();
feignCtrlP(offset).check("p: str=\"\\n\", t: str=\"\\t\", r: str=\"\\r\"", new String[]{"p: str=\"\\n\", "});
}
/**
* Imitates pressing of Ctrl+P; fails if results are not as expected.
* @param offset offset of 'cursor' where Ctrl+P is pressed.