Skip to content

Commit 039b87b

Browse files
committed
[GR-20057] Moved CoerceToString and CoerceToFileDescriptorNode functionality into PythonObjectLibrary.
PullRequest: graalpython/930
2 parents c31a801 + 9f4676d commit 039b87b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+718
-422
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ def test_key_info():
262262
assert not polyglot.__key_info__(builtinObj, "__len__", "removable")
263263
assert not polyglot.__key_info__(builtinObj, "__len__", "insertable")
264264

265+
def test_java_classpath():
266+
import java
267+
try:
268+
java.add_to_classpath(1)
269+
except TypeError as e:
270+
assert "classpath argument 1 must be string, not int" in str(e)
271+
272+
try:
273+
java.add_to_classpath('a', 1)
274+
except TypeError as e:
275+
assert "classpath argument 2 must be string, not int" in str(e)
276+
265277
def test_host_lookup():
266278
import java
267279
try:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
def test_select_raises_on_object_with_no_fileno_func():
41+
import select
42+
try :
43+
select.select('abc', [], [], 0)
44+
except TypeError:
45+
raised = True
46+
assert raised
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
def test_SemLock_raises_on_non_string_name():
41+
from _multiprocessing import SemLock
42+
try :
43+
SemLock(kind=1, value=1, name={1:2}, maxvalue=1, unlink=1)
44+
except TypeError:
45+
raised = True
46+
assert raised

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ def test_inet_aton_errs(self):
5757
self.assertRaises(OSError, lambda : socket.inet_aton('oracle.com'))
5858
self.assertRaises(OSError, lambda : socket.inet_aton('0x7000000000000000'))
5959
self.assertRaises(OSError, lambda : socket.inet_aton('255.255.256.1'))
60-
self.assertRaises(TypeError, lambda : socket.inet_aton(255))
60+
self.assertRaises(TypeError, lambda : socket.inet_aton(255))
61+
62+
def test_get_name_info():
63+
import socket
64+
try :
65+
socket.getnameinfo((1, 0, 0, 0), 0)
66+
except TypeError:
67+
raised = True
68+
assert raised

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@
177177
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
178178
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
179179
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
180+
import com.oracle.graal.python.nodes.util.CannotCastException;
180181
import com.oracle.graal.python.nodes.util.CastToByteNode;
181182
import com.oracle.graal.python.nodes.util.CastToJavaIntNode;
182183
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
183184
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
184185
import com.oracle.graal.python.nodes.util.CoerceToDoubleNode;
185-
import com.oracle.graal.python.nodes.util.CoerceToStringNode;
186186
import com.oracle.graal.python.nodes.util.SplitArgsNode;
187187
import com.oracle.graal.python.runtime.ExecutionContext.ForeignCallContext;
188188
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
@@ -1861,16 +1861,16 @@ Object strNoArgs(LazyPythonClass strClass, PNone arg, PNone encoding, PNone erro
18611861
}
18621862

18631863
@Specialization(guards = {"!isNativeClass(strClass)", "!isNoValue(obj)", "isNoValue(encoding)", "isNoValue(errors)"})
1864-
Object strOneArg(VirtualFrame frame, LazyPythonClass strClass, Object obj, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
1865-
@Cached CoerceToStringNode coerceToStringNode) {
1866-
Object result = coerceToStringNode.execute(frame, obj);
1864+
Object strOneArg(LazyPythonClass strClass, Object obj, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
1865+
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
1866+
Object result = lib.asPString(obj);
18671867

18681868
// try to return a primitive if possible
18691869
if (getIsStringProfile().profile(result instanceof String)) {
18701870
return asPString(strClass, (String) result);
18711871
}
18721872

1873-
// CoerceToStringNode guarantees that the returned object is an instanceof of 'str'
1873+
// PythonObjectLibrary guarantees that the returned object is an instanceof of 'str'
18741874
return result;
18751875
}
18761876

@@ -1906,11 +1906,12 @@ private Object decodeBytes(VirtualFrame frame, LazyPythonClass strClass, PBytes
19061906
* into a natively allocated subtype structure
19071907
*/
19081908
@Specialization(guards = {"isSubtypeOfString(frame, isSubtype, cls)", "isNoValue(encoding)", "isNoValue(errors)"}, limit = "1")
1909-
Object doNativeSubclass(VirtualFrame frame, PythonNativeClass cls, Object obj, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
1909+
Object doNativeSubclass(@SuppressWarnings("unused") VirtualFrame frame, PythonNativeClass cls, Object obj, @SuppressWarnings("unused") Object encoding,
1910+
@SuppressWarnings("unused") Object errors,
19101911
@Cached @SuppressWarnings("unused") IsSubtypeNode isSubtype,
1911-
@Cached CoerceToStringNode castToStringNode,
1912+
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
19121913
@Cached CExtNodes.StringSubtypeNew subtypeNew) {
1913-
return subtypeNew.call(cls, castToStringNode.execute(frame, obj));
1914+
return subtypeNew.call(cls, lib.asPString(obj));
19141915
}
19151916

19161917
protected static boolean isSubtypeOfString(VirtualFrame frame, IsSubtypeNode isSubtypeNode, PythonNativeClass cls) {
@@ -2197,7 +2198,12 @@ private String getModuleNameFromGlobals(PythonObject globals, HashingStorageLibr
21972198
CompilerDirectives.transferToInterpreterAndInvalidate();
21982199
throw new IllegalStateException("invalid globals object");
21992200
}
2200-
return ensureCastToStringNode().execute(nameAttr);
2201+
try {
2202+
return ensureCastToStringNode().execute(nameAttr);
2203+
} catch (CannotCastException e) {
2204+
CompilerDirectives.transferToInterpreterAndInvalidate();
2205+
throw new IllegalStateException();
2206+
}
22012207
}
22022208

22032209
@SuppressWarnings("try")

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@
153153
import com.oracle.graal.python.nodes.object.GetClassNode;
154154
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
155155
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
156+
import com.oracle.graal.python.nodes.util.CannotCastException;
156157
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
157-
import com.oracle.graal.python.nodes.util.CoerceToStringNode;
158-
import com.oracle.graal.python.nodes.util.CoerceToStringNodeGen;
159158
import com.oracle.graal.python.runtime.PythonContext;
160159
import com.oracle.graal.python.runtime.PythonCore;
161160
import com.oracle.graal.python.runtime.PythonOptions;
@@ -1466,28 +1465,29 @@ public abstract static class PrintNode extends PythonBuiltinNode {
14661465
@Child private ReadAttributeFromObjectNode readStdout;
14671466
@Child private GetAttributeNode getWrite = GetAttributeNode.create("write", null);
14681467
@Child private CallNode callWrite = CallNode.create();
1469-
@Child private CoerceToStringNode toString = CoerceToStringNodeGen.create();
14701468
@Child private LookupAndCallUnaryNode callFlushNode;
14711469
@CompilationFinal private Assumption singleContextAssumption;
14721470
@CompilationFinal private PythonModule cachedSys;
14731471

14741472
@Specialization
14751473
PNone printNoKeywords(VirtualFrame frame, Object[] values, @SuppressWarnings("unused") PNone sep, @SuppressWarnings("unused") PNone end, @SuppressWarnings("unused") PNone file,
1476-
@SuppressWarnings("unused") PNone flush) {
1474+
@SuppressWarnings("unused") PNone flush,
1475+
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
14771476
Object stdout = getStdout();
1478-
return printAllGiven(frame, values, DEFAULT_SEPARATOR, DEFAULT_END, stdout, false);
1477+
return printAllGiven(frame, values, DEFAULT_SEPARATOR, DEFAULT_END, stdout, false, lib);
14791478
}
14801479

14811480
@Specialization(guards = {"!isNone(file)", "!isNoValue(file)"})
1482-
PNone printAllGiven(VirtualFrame frame, Object[] values, String sep, String end, Object file, boolean flush) {
1481+
PNone printAllGiven(VirtualFrame frame, Object[] values, String sep, String end, Object file, boolean flush,
1482+
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
14831483
int lastValue = values.length - 1;
14841484
Object write = getWrite.executeObject(frame, file);
14851485
for (int i = 0; i < lastValue; i++) {
1486-
callWrite.execute(frame, write, toString.execute(frame, values[i]));
1486+
callWrite.execute(frame, write, lib.asPString(values[i]));
14871487
callWrite.execute(frame, write, sep);
14881488
}
14891489
if (lastValue >= 0) {
1490-
callWrite.execute(frame, write, toString.execute(frame, values[lastValue]));
1490+
callWrite.execute(frame, write, lib.asPString(values[lastValue]));
14911491
}
14921492
callWrite.execute(frame, write, end);
14931493
if (flush) {
@@ -1505,14 +1505,19 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
15051505
@Cached CastToJavaStringNode castSep,
15061506
@Cached CastToJavaStringNode castEnd,
15071507
@Cached("createIfTrueNode()") CoerceToBooleanNode castFlush,
1508-
@Cached PRaiseNode raiseNode) {
1509-
String sep = sepIn instanceof PNone ? DEFAULT_SEPARATOR : castSep.execute(sepIn);
1510-
if (sep == null) {
1508+
@Cached PRaiseNode raiseNode,
1509+
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
1510+
String sep;
1511+
try {
1512+
sep = sepIn instanceof PNone ? DEFAULT_SEPARATOR : castSep.execute(sepIn);
1513+
} catch (CannotCastException e) {
15111514
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "sep must be None or a string, not %p", sepIn);
15121515
}
15131516

1514-
String end = endIn instanceof PNone ? DEFAULT_END : castEnd.execute(endIn);
1515-
if (end == null) {
1517+
String end;
1518+
try {
1519+
end = endIn instanceof PNone ? DEFAULT_END : castEnd.execute(endIn);
1520+
} catch (CannotCastException e) {
15161521
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "end must be None or a string, not %p", sepIn);
15171522
}
15181523

@@ -1528,7 +1533,7 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
15281533
} else {
15291534
flush = castFlush.executeBoolean(frame, flushIn);
15301535
}
1531-
return printAllGiven(frame, values, sep, end, file, flush);
1536+
return printAllGiven(frame, values, sep, end, file, flush, lib);
15321537
}
15331538

15341539
private Object getStdout() {
@@ -1587,8 +1592,13 @@ public abstract static class AsciiNode extends PythonUnaryBuiltinNode {
15871592
@Specialization(guards = "isString(obj)")
15881593
public Object asciiString(Object obj,
15891594
@Cached CastToJavaStringNode castToJavaStringNode) {
1590-
String str = castToJavaStringNode.execute(obj);
1591-
return doAsciiString(str);
1595+
try {
1596+
String str = castToJavaStringNode.execute(obj);
1597+
return doAsciiString(str);
1598+
} catch (CannotCastException e) {
1599+
CompilerDirectives.transferToInterpreterAndInvalidate();
1600+
throw new IllegalStateException("should not be reached");
1601+
}
15921602
}
15931603

15941604
@TruffleBoundary

0 commit comments

Comments
 (0)