Skip to content

Commit 04049a5

Browse files
committed
Refactor: Use .equals() instead of equality operator (==) for comparing string constant marker.
As `NOT_SET` is a string constant marker that we set to mark not set values, e.g. for an escape char, it would be possible to compare for it via the equality operator. However, the python style check doesn't accept that and I could not find a specific `@SuppressWarnings` annotation for this use case, therefore I decided to switch the checks to `.equals()`.
1 parent 58f2b8e commit 04049a5

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVDialect.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public CSVDialect(Object cls, Shape instanceShape) {
6868
super(cls, instanceShape);
6969
}
7070

71+
7172
public CSVDialect(Object cls, Shape instanceShape, String delimiter, boolean doubleQuote, String escapeChar,
7273
String lineTerminator, String quoteChar, QuoteStyle quoting, boolean skipInitialSpace,
7374
boolean strict) {
@@ -81,9 +82,9 @@ public CSVDialect(Object cls, Shape instanceShape, String delimiter, boolean dou
8182
this.skipInitialSpace = skipInitialSpace;
8283
this.strict = strict;
8384

84-
this.delimiterCodePoint = this.delimiter == NOT_SET ? NOT_SET_CODEPOINT : this.delimiter.codePointAt(0);
85-
this.escapeCharCodePoint = this.escapeChar == NOT_SET ? NOT_SET_CODEPOINT : this.escapeChar.codePointAt(0);
86-
this.quoteCharCodePoint = quoteChar.codePointAt(0); // quote char cannot be NOT_SET
85+
this.delimiterCodePoint = this.delimiter.codePointAt(0); // delimiter cannot be NOT_SET
86+
this.escapeCharCodePoint = this.escapeChar.equals(NOT_SET) ? NOT_SET_CODEPOINT : this.escapeChar.codePointAt(0);
87+
this.quoteCharCodePoint = this.quoteChar.equals(NOT_SET) ? NOT_SET_CODEPOINT : this.quoteChar.codePointAt(0);
8788
this.lineTerminatorCodePoints = strToCodePointArray(this.lineTerminator);
8889
}
8990

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVDialectBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
6868
abstract static class DelimiterNode extends PythonUnaryBuiltinNode {
6969
@Specialization
7070
static Object doIt(CSVDialect self) {
71-
return self.delimiter == NOT_SET ? PNone.NONE : self.delimiter;
71+
return self.delimiter;
7272
}
7373
}
7474

@@ -86,7 +86,7 @@ static boolean doIt(CSVDialect self) {
8686
abstract static class EscapeCharNode extends PythonUnaryBuiltinNode {
8787
@Specialization
8888
static Object doIt(CSVDialect self) {
89-
return self.escapeChar == NOT_SET ? PNone.NONE : self.escapeChar;
89+
return self.escapeChar.equals(NOT_SET) ? PNone.NONE : self.escapeChar;
9090
}
9191
}
9292

@@ -104,7 +104,7 @@ static String doIt(CSVDialect self) {
104104
abstract static class QuoteCharNode extends PythonUnaryBuiltinNode {
105105
@Specialization
106106
static Object doIt(CSVDialect self) {
107-
return self.quoteChar == NOT_SET ? PNone.NONE : self.quoteChar;
107+
return self.quoteChar.equals(NOT_SET) ? PNone.NONE : self.quoteChar;
108108
}
109109
}
110110

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVModuleBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,15 @@ private Object createCSVDialect(VirtualFrame frame, PythonBuiltinClassType cls,
485485

486486
/* validate options */
487487

488-
if (delimiter == NOT_SET) {
488+
if (delimiter.equals(NOT_SET)) {
489489
throw raise(TypeError, ErrorMessages.DELIMITER_MUST_BE_ONE_CHAR_STRING);
490490
}
491491

492492
if (quotecharObj == PNone.NONE && quotingObj == PNone.NO_VALUE) {
493493
quoting = QUOTE_NONE;
494494
}
495495

496-
if (quoting != QUOTE_NONE && quotechar == NOT_SET) {
496+
if (quoting != QUOTE_NONE && quotechar.equals(NOT_SET)) {
497497
throw raise(TypeError, ErrorMessages.QUOTECHAR_MUST_BE_SET_IF_QUOTING_ENABLED);
498498
}
499499

@@ -543,7 +543,7 @@ private String getCharOrNone(String attribute, Object valueObj, String defaultVa
543543
if (valueObj == PNone.NO_VALUE) {
544544
return defaultValue;
545545
}
546-
if (valueObj == PNone.NONE || valueObj == NOT_SET) {
546+
if (valueObj == PNone.NONE || valueObj.equals(NOT_SET)) {
547547
return NOT_SET;
548548
}
549549

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void joinAppend(String field, boolean quoted) {
215215
}
216216
}
217217
if (wantEscape) {
218-
if (dialect.escapeChar == NOT_SET) {
218+
if (dialect.escapeChar.equals(NOT_SET)) {
219219
throw PRaiseNode.getUncached().raise(PythonBuiltinClassType.CSVError, ErrorMessages.ESCAPE_WITHOUT_ESCAPECHAR);
220220
}
221221
this.rec.append(dialect.escapeChar);
@@ -235,7 +235,7 @@ void joinAppendLineterminator() {
235235
this.rec.append(this.dialect.lineTerminator);
236236
}
237237

238-
boolean containsCodePoint(int[] codePoints, int codePoint) {
238+
private boolean containsCodePoint(int[] codePoints, int codePoint) {
239239
for (int cp : codePoints) {
240240
if (cp == codePoint) {
241241
return true;

0 commit comments

Comments
 (0)