Skip to content

Commit 10b3cde

Browse files
committed
[GR-36132] Fix failing panda tests after CSV intrinsification.
PullRequest: graalpython/2105
2 parents bbe31dd + b6b80a9 commit 10b3cde

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_csv.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -41,7 +41,7 @@
4141
import csv
4242
from tempfile import TemporaryFile
4343

44-
class CsvTest(unittest.TestCase):
44+
class TestUnicodeCharacters(unittest.TestCase):
4545
def test_read_utf_32_delimiter(self):
4646
test_data = ['a\U0001F642b']
4747
reader = csv.reader(test_data, delimiter="\U0001F642")
@@ -74,6 +74,32 @@ def test_write_utf32_field_with_utf32_delimiter(self):
7474
self.assertEqual(fileobj.read(),
7575
expected + writer.dialect.lineterminator)
7676

77+
class TestDialectValidity(unittest.TestCase):
78+
# CPython supports empty quotechars and escapechars until inclusive 3.10.
79+
def test_quoting(self):
80+
class MyDialect(csv.Dialect):
81+
delimiter = ";"
82+
escapechar = '\\'
83+
doublequote = False
84+
skipinitialspace = True
85+
lineterminator = '\r\n'
86+
quoting = csv.QUOTE_NONE
87+
quotechar = ""
88+
d = MyDialect()
89+
self.assertEqual(d.quotechar, "")
90+
91+
def test_escapechar(self):
92+
class MyDialect(csv.Dialect):
93+
delimiter = ";"
94+
doublequote = False
95+
skipinitialspace = True
96+
lineterminator = '\r\n'
97+
quoting = csv.QUOTE_NONE
98+
escapechar = ""
99+
d = MyDialect()
100+
self.assertEqual(d.escapechar, "")
101+
102+
77103

78104

79105

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -48,6 +48,7 @@
4848

4949
import java.util.List;
5050

51+
import com.oracle.graal.python.PythonLanguage;
5152
import com.oracle.graal.python.builtins.Builtin;
5253
import com.oracle.graal.python.builtins.CoreFunctions;
5354
import com.oracle.graal.python.builtins.Python3Core;
@@ -93,6 +94,11 @@
9394
@CoreFunctions(defineModule = "_csv")
9495
public final class CSVModuleBuiltins extends PythonBuiltins {
9596

97+
static {
98+
// See comment about Python 3.10 in #getChar() and graalpython/test/test_csv.py
99+
assert PythonLanguage.MINOR <= 10;
100+
}
101+
96102
static final String WRITE = "write";
97103
static final String NOT_SET = "NOT_SET";
98104
static final int NOT_SET_CODEPOINT = -1;
@@ -531,10 +537,15 @@ private String getChar(String name, Object valueObj, String defaultValue,
531537
throw raise(TypeError, ErrorMessages.S_MUST_BE_STRING_NOT_S, name, getType.execute(valueObj));
532538
}
533539

534-
if (charValue.length() != 1 && charValue.codePointCount(0, charValue.length()) != 1) {
540+
if (charValue.length() > 1 && charValue.codePointCount(0, charValue.length()) > 1) {
535541
throw raise(TypeError, ErrorMessages.MUST_BE_ONE_CHARACTER_STRING, name);
536542
}
537543

544+
// CPython supports empty quotechars and escapechars until inclusive 3.10.
545+
if (charValue.length() == 0) {
546+
return NOT_SET;
547+
}
548+
538549
return charValue;
539550
}
540551

@@ -592,7 +603,7 @@ private QuoteStyle getQuotingValue(VirtualFrame frame, String name, Object value
592603
}
593604

594605
if (!pyLongCheckExactNode.execute(valueObj)) {
595-
throw raise(TypeError, ErrorMessages.MUST_BE_INTEGER, name);
606+
throw raise(TypeError, ErrorMessages.MUST_BE_INTEGER_QUOTED_ATTR, name);
596607
}
597608

598609
int value = pyLongAsIntNode.execute(frame, valueObj);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -449,6 +449,7 @@ public abstract class ErrorMessages {
449449
public static final String MUST_BE_BYTE_STRING_LEGTH1_NOT_P = "must be a byte string of length 1, not %p";
450450
public static final String MUST_BE_EITHER_OR = "%s: '%s' must be either %s or %s";
451451
public static final String MUST_BE_INTEGER = "%s must be an integer";
452+
public static final String MUST_BE_INTEGER_QUOTED_ATTR = "\"%s\" must be an integer";
452453
public static final String MUST_BE_INTEGER_NOT_P = "%s must be an integer, not %p";
453454
public static final String MUST_BE_NON_NEGATIVE = "%s must be non-negative";
454455
public static final String MUST_BE_NON_NEGATIVE_INTEGER = "%s must be non-negative integer";

0 commit comments

Comments
 (0)