Skip to content

Commit 866e6ae

Browse files
committed
intrinsified PyWarn
1 parent 4b55a5c commit 866e6ae

File tree

4 files changed

+104
-11
lines changed

4 files changed

+104
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
import com.oracle.graal.python.builtins.modules.cext.PythonCextSliceBuiltins;
132132
import com.oracle.graal.python.builtins.modules.cext.PythonCextSysBuiltins;
133133
import com.oracle.graal.python.builtins.modules.cext.PythonCextTupleBuiltins;
134+
import com.oracle.graal.python.builtins.modules.cext.PythonCextWarnBuiltins;
134135
import com.oracle.graal.python.builtins.modules.csv.CSVDialectBuiltins;
135136
import com.oracle.graal.python.builtins.modules.csv.CSVModuleBuiltins;
136137
import com.oracle.graal.python.builtins.modules.csv.CSVReaderBuiltins;
@@ -507,6 +508,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
507508
new PythonCextSysBuiltins(),
508509
new PythonCextTupleBuiltins(),
509510
new PythonCextUnicodeBuiltins(),
511+
new PythonCextWarnBuiltins(),
510512
new WeakRefModuleBuiltins(),
511513
new ReferenceTypeBuiltins(),
512514
new WarningsModuleBuiltins(),

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

Lines changed: 10 additions & 2 deletions
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
@@ -52,6 +52,7 @@
5252
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5353
import com.oracle.graal.python.builtins.PythonBuiltins;
5454
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltinsClinicProviders.WarnBuiltinNodeClinicProviderGen;
55+
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltinsFactory.WarnBuiltinNodeFactory;
5556
import com.oracle.graal.python.builtins.objects.PNone;
5657
import com.oracle.graal.python.builtins.objects.code.PCode;
5758
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
@@ -953,19 +954,26 @@ public Assumption needNotPassExceptionAssumption() {
953954
@ArgumentClinic(name = "stacklevel", conversion = ClinicConversion.Int, defaultValue = "1")
954955
@ArgumentClinic(name = "source", defaultValue = "PNone.NONE")
955956
@GenerateNodeFactory
956-
abstract static class WarnBuiltinNode extends PythonClinicBuiltinNode {
957+
public abstract static class WarnBuiltinNode extends PythonClinicBuiltinNode {
957958
@Override
958959
protected ArgumentClinicProvider getArgumentClinic() {
959960
return WarnBuiltinNodeClinicProviderGen.INSTANCE;
960961
}
961962

963+
public abstract Object execute(VirtualFrame frame, PythonModule mod, Object message, Object category, int stacklevel, Object source);
964+
962965
@Specialization
963966
Object doWarn(VirtualFrame frame, PythonModule mod, Object message, Object category, int stacklevel, Object source,
964967
@Cached WarningsModuleNode moduleFunctionsNode) {
965968
// warnings_warn_impl
966969
moduleFunctionsNode.doWarn(frame, mod, message, moduleFunctionsNode.getCategory(frame, message, category), stacklevel, source);
967970
return PNone.NONE;
968971
}
972+
973+
public static WarnBuiltinNode create() {
974+
return WarnBuiltinNodeFactory.create(null);
975+
}
976+
969977
}
970978

971979
@ReportPolymorphism
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules.cext;
42+
43+
import com.oracle.graal.python.builtins.Builtin;
44+
import java.util.List;
45+
import com.oracle.graal.python.builtins.CoreFunctions;
46+
import com.oracle.graal.python.builtins.Python3Core;
47+
import com.oracle.graal.python.builtins.PythonBuiltins;
48+
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnBuiltinNode;
49+
import com.oracle.graal.python.builtins.objects.PNone;
50+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes;
51+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
52+
import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryBuiltinNode;
53+
import com.oracle.graal.python.runtime.exception.PException;
54+
import com.oracle.truffle.api.dsl.Cached;
55+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
56+
import com.oracle.truffle.api.dsl.NodeFactory;
57+
import com.oracle.truffle.api.dsl.Specialization;
58+
import com.oracle.truffle.api.frame.VirtualFrame;
59+
60+
@CoreFunctions(extendsModule = PythonCextBuiltins.PYTHON_CEXT)
61+
@GenerateNodeFactory
62+
public final class PythonCextWarnBuiltins extends PythonBuiltins {
63+
64+
@Override
65+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
66+
return PythonCextWarnBuiltinsFactory.getFactories();
67+
}
68+
69+
@Override
70+
public void initialize(Python3Core core) {
71+
super.initialize(core);
72+
}
73+
74+
@Builtin(name = "_PyErr_Warn", minNumOfPositionalArgs = 4)
75+
@GenerateNodeFactory
76+
abstract static class PyErrWarnNode extends PythonQuaternaryBuiltinNode {
77+
@Specialization
78+
@SuppressWarnings("unused")
79+
Object warn(VirtualFrame frame, Object message, Object category, long stackLevel, Object source,
80+
@Cached WarnBuiltinNode warnNode,
81+
@Cached CExtNodes.TransformExceptionToNativeNode transformExceptionToNativeNode) {
82+
try {
83+
// TODO: pass source again once we update to newer lib-python
84+
warnNode.execute(frame, getCore().lookupBuiltinModule("_warnings"), message, category, (int) stackLevel, PNone.NONE);
85+
return PNone.NONE;
86+
} catch (PException e) {
87+
transformExceptionToNativeNode.execute(frame, e);
88+
return getContext().getNativeNull();
89+
}
90+
}
91+
}
92+
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,6 @@ def _PyErr_NormalizeExceptionEx(exc, val, tb, recursion_depth):
257257
def PyErr_NormalizeException(exc, val, tb):
258258
return _PyErr_NormalizeExceptionEx(exc, val, tb, 0)
259259

260-
261-
@may_raise
262-
def _PyErr_Warn(message, category, stack_level, source):
263-
import warnings
264-
# TODO: pass source again once we update to newer lib-python
265-
warnings.warn(message, category, stack_level)
266-
return None
267-
268-
269260
@may_raise
270261
def PyException_SetCause(exc, cause):
271262
exc.__cause__ = cause

0 commit comments

Comments
 (0)