Skip to content

Commit 3b73d50

Browse files
committed
[GR-15799] Fix isUpper/isLower to behave more like the python equivalent
PullRequest: graalpython/515
2 parents 4dc7b19 + 5768012 commit 3b73d50

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ def test_islower(self):
541541
self.checkequal(True, 'abc', 'islower')
542542
self.checkequal(False, 'aBc', 'islower')
543543
self.checkequal(True, 'abc\n', 'islower')
544+
self.checkequal(True, 'a_b!c\n', 'islower')
545+
self.checkequal(False, 'A_b!c\n', 'islower')
544546
self.checkraises(TypeError, 'abc', 'islower', 42)
545547
self.checkequalnofix(False, '\u1FFc', 'islower')
546548
self.assertFalse('\u2167'.islower())
@@ -563,6 +565,8 @@ def test_isupper(self):
563565
self.checkequal(True, 'ABC', 'isupper')
564566
self.checkequal(False, 'AbC', 'isupper')
565567
self.checkequal(True, 'ABC\n', 'isupper')
568+
self.checkequal(True, 'A_B!C\n', 'isupper')
569+
self.checkequal(False, 'a_B!C\n', 'isupper')
566570
self.checkraises(TypeError, 'abc', 'isupper', 42)
567571
if not sys.platform.startswith('java'):
568572
self.checkequalnofix(False, '\u1FFc', 'isupper')

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SocketModuleBuiltins.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@
5353
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
5454
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5555
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
56+
import com.oracle.graal.python.nodes.util.CastToIndexNode;
5657
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5758
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
59+
import com.oracle.truffle.api.dsl.Cached;
5860
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5961
import com.oracle.truffle.api.dsl.NodeFactory;
6062
import com.oracle.truffle.api.dsl.Specialization;
@@ -77,18 +79,21 @@ Object socket(LazyPythonClass cls, @SuppressWarnings("unused") PNone family, @Su
7779
}
7880

7981
@Specialization(guards = {"isNoValue(type)", "isNoValue(proto)", "isNoValue(fileno)"})
80-
Object socket(LazyPythonClass cls, int family, @SuppressWarnings("unused") PNone type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno) {
81-
return createSocketInternal(cls, family, PSocket.SOCK_STREAM, 0);
82+
Object socket(LazyPythonClass cls, Object family, @SuppressWarnings("unused") PNone type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno,
83+
@Cached CastToIndexNode cast) {
84+
return createSocketInternal(cls, cast.execute(family), PSocket.SOCK_STREAM, 0);
8285
}
8386

8487
@Specialization(guards = {"isNoValue(proto)", "isNoValue(fileno)"})
85-
Object socket(LazyPythonClass cls, int family, int type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno) {
86-
return createSocketInternal(cls, family, type, 0);
88+
Object socket(LazyPythonClass cls, Object family, Object type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno,
89+
@Cached CastToIndexNode cast) {
90+
return createSocketInternal(cls, cast.execute(family), cast.execute(type), 0);
8791
}
8892

8993
@Specialization(guards = {"isNoValue(fileno)"})
90-
Object socket(LazyPythonClass cls, int family, int type, int proto, @SuppressWarnings("unused") PNone fileno) {
91-
return createSocketInternal(cls, family, type, proto);
94+
Object socket(LazyPythonClass cls, Object family, Object type, Object proto, @SuppressWarnings("unused") PNone fileno,
95+
@Cached CastToIndexNode cast) {
96+
return createSocketInternal(cls, cast.execute(family), cast.execute(type), cast.execute(proto));
9297
}
9398

9499
private Object createSocketInternal(LazyPythonClass cls, int family, int type, int proto) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,21 +1713,21 @@ abstract static class IsLowerNode extends PythonUnaryBuiltinNode {
17131713
@Specialization
17141714
@TruffleBoundary
17151715
boolean doString(String self) {
1716-
int spaces = 0;
1716+
int uncased = 0;
17171717
if (self.length() == 0) {
17181718
return false;
17191719
}
17201720
for (int i = 0; i < self.length(); i++) {
17211721
int codePoint = self.codePointAt(i);
17221722
if (!Character.isLowerCase(codePoint)) {
1723-
if (Character.isWhitespace(codePoint)) {
1724-
spaces++;
1723+
if (Character.toLowerCase(codePoint) == Character.toUpperCase(codePoint)) {
1724+
uncased++;
17251725
} else {
17261726
return false;
17271727
}
17281728
}
17291729
}
1730-
return spaces == 0 || self.length() > spaces;
1730+
return uncased == 0 || self.length() > uncased;
17311731
}
17321732
}
17331733

@@ -1827,21 +1827,21 @@ abstract static class IsUpperNode extends PythonUnaryBuiltinNode {
18271827
@Specialization
18281828
@TruffleBoundary
18291829
boolean doString(String self) {
1830-
int spaces = 0;
1830+
int uncased = 0;
18311831
if (self.length() == 0) {
18321832
return false;
18331833
}
18341834
for (int i = 0; i < self.length(); i++) {
18351835
int codePoint = self.codePointAt(i);
18361836
if (!Character.isUpperCase(codePoint)) {
1837-
if (Character.isWhitespace(codePoint)) {
1838-
spaces++;
1837+
if (Character.toLowerCase(codePoint) == Character.toUpperCase(codePoint)) {
1838+
uncased++;
18391839
} else {
18401840
return false;
18411841
}
18421842
}
18431843
}
1844-
return spaces == 0 || self.length() > spaces;
1844+
return uncased == 0 || self.length() > uncased;
18451845

18461846
}
18471847
}

0 commit comments

Comments
 (0)