Skip to content

Commit d993fae

Browse files
committed
[GR-34614][GR-44418] More fixes for PyTorch
PullRequest: graalpython/2825
2 parents 94ba18f + a90c9f1 commit d993fae

Some content is hidden

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

52 files changed

+2720
-1644
lines changed

graalpython/com.oracle.graal.python.cext/include/cpython/code.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2022 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -139,6 +139,10 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
139139
use PyFrame_GetLineNumber() instead. */
140140
PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
141141

142+
/* GraalPy specific to avoid direct field access. Used by (at least) pytorch */
143+
PyAPI_FUNC(PyObject*) PyCode_GetFileName(PyCodeObject* code);
144+
PyAPI_FUNC(PyObject*) PyCode_GetName(PyCodeObject* code);
145+
142146
/* for internal use only */
143147
struct _opaque {
144148
int computed_line;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2023, 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+
#ifndef Py_CPYTHON_PYFRAME_H
42+
# error "this header file must not be included directly"
43+
#endif
44+
45+
PyAPI_FUNC(PyFrameObject *) PyFrame_GetBack(PyFrameObject *frame);
46+
PyAPI_FUNC(PyObject *) PyFrame_GetLocals(PyFrameObject *frame);
47+
48+
PyAPI_FUNC(PyObject *) PyFrame_GetGlobals(PyFrameObject *frame);
49+
PyAPI_FUNC(PyObject *) PyFrame_GetBuiltins(PyFrameObject *frame);
50+
51+
PyAPI_FUNC(PyObject *) PyFrame_GetGenerator(PyFrameObject *frame);
52+
PyAPI_FUNC(int) PyFrame_GetLasti(PyFrameObject *frame);

graalpython/com.oracle.graal.python.cext/include/pyframe.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2022 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -21,6 +21,12 @@ PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
2121

2222
PyAPI_FUNC(PyCodeObject *) PyFrame_GetCode(PyFrameObject *frame);
2323

24+
#ifndef Py_LIMITED_API
25+
# define Py_CPYTHON_PYFRAME_H
26+
# include "cpython/pyframe.h"
27+
# undef Py_CPYTHON_PYFRAME_H
28+
#endif
29+
2430
#ifdef __cplusplus
2531
}
2632
#endif

graalpython/com.oracle.graal.python.cext/src/_warnings.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ MUST_INLINE static int warn_unicode(PyObject *category, PyObject *message, Py_ss
5454
return 0;
5555
}
5656

57+
MUST_INLINE static PyObject* warn_explicit(PyObject *category, PyObject *message,
58+
PyObject *filename, int lineno,
59+
PyObject *module, PyObject *registry) {
60+
return GraalPyTruffleErr_WarnExplicit(category, message, filename, lineno, module, registry);
61+
}
62+
5763
// taken from CPython "Python/_warnings.c"
5864
int PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) {
5965
PyObject *message = PyUnicode_FromString(text);
@@ -84,3 +90,90 @@ int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *
8490
return ret;
8591
}
8692

93+
// Adapted from CPython
94+
int PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
95+
PyObject *filename, int lineno,
96+
PyObject *module, PyObject *registry)
97+
{
98+
PyObject *res;
99+
if (category == NULL)
100+
category = PyExc_RuntimeWarning;
101+
res = warn_explicit(category, message, filename, lineno,
102+
module, registry);
103+
if (res == NULL)
104+
return -1;
105+
Py_DECREF(res);
106+
return 0;
107+
}
108+
109+
// Adapted from CPython
110+
int PyErr_WarnExplicit(PyObject *category, const char *text,
111+
const char *filename_str, int lineno,
112+
const char *module_str, PyObject *registry)
113+
{
114+
PyObject *message = PyUnicode_FromString(text);
115+
PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
116+
PyObject *module = NULL;
117+
int ret = -1;
118+
119+
if (message == NULL || filename == NULL)
120+
goto exit;
121+
if (module_str != NULL) {
122+
module = PyUnicode_FromString(module_str);
123+
if (module == NULL)
124+
goto exit;
125+
}
126+
127+
ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
128+
module, registry);
129+
130+
exit:
131+
Py_XDECREF(message);
132+
Py_XDECREF(module);
133+
Py_XDECREF(filename);
134+
return ret;
135+
}
136+
137+
// Adapted from CPython
138+
int PyErr_WarnExplicitFormat(PyObject *category,
139+
const char *filename_str, int lineno,
140+
const char *module_str, PyObject *registry,
141+
const char *format, ...)
142+
{
143+
PyObject *message;
144+
PyObject *module = NULL;
145+
PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
146+
int ret = -1;
147+
va_list vargs;
148+
149+
if (filename == NULL)
150+
goto exit;
151+
if (module_str != NULL) {
152+
module = PyUnicode_FromString(module_str);
153+
if (module == NULL)
154+
goto exit;
155+
}
156+
157+
#ifdef HAVE_STDARG_PROTOTYPES
158+
va_start(vargs, format);
159+
#else
160+
va_start(vargs);
161+
#endif
162+
message = PyUnicode_FromFormatV(format, vargs);
163+
if (message != NULL) {
164+
PyObject *res = warn_explicit(category, message, filename, lineno,
165+
module, registry);
166+
Py_DECREF(message);
167+
if (res != NULL) {
168+
Py_DECREF(res);
169+
ret = 0;
170+
}
171+
}
172+
va_end(vargs);
173+
exit:
174+
Py_XDECREF(module);
175+
Py_XDECREF(filename);
176+
return ret;
177+
}
178+
179+

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,18 @@ PyAPI_FUNC(int) PyCapsule_SetPointer(PyObject* a, void* b) {
15051505
PyAPI_FUNC(PyObject*) PyClassMethod_New(PyObject* a) {
15061506
return GraalPyClassMethod_New(a);
15071507
}
1508+
#undef PyCode_Addr2Line
1509+
PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject* a, int b) {
1510+
return GraalPyCode_Addr2Line(a, b);
1511+
}
1512+
#undef PyCode_GetFileName
1513+
PyAPI_FUNC(PyObject*) PyCode_GetFileName(PyCodeObject* a) {
1514+
return GraalPyCode_GetFileName(a);
1515+
}
1516+
#undef PyCode_GetName
1517+
PyAPI_FUNC(PyObject*) PyCode_GetName(PyCodeObject* a) {
1518+
return GraalPyCode_GetName(a);
1519+
}
15081520
#undef PyCode_New
15091521
PyAPI_FUNC(PyCodeObject*) PyCode_New(int a, int b, int c, int d, int e, PyObject* f, PyObject* g, PyObject* h, PyObject* i, PyObject* j, PyObject* k, PyObject* l, PyObject* m, int n, PyObject* o) {
15101522
return GraalPyCode_New(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
@@ -1633,14 +1645,14 @@ PyAPI_FUNC(void) PyErr_Restore(PyObject* a, PyObject* b, PyObject* c) {
16331645
PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject* a, PyObject* b, PyObject* c) {
16341646
GraalPyErr_SetExcInfo(a, b, c);
16351647
}
1636-
#undef PyErr_WarnExplicit
1637-
PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject* a, const char* b, const char* c, int d, const char* e, PyObject* f) {
1638-
return GraalPyErr_WarnExplicit(a, truffleString(b), truffleString(c), d, truffleString(e), f);
1639-
}
16401648
#undef PyEval_GetBuiltins
16411649
PyAPI_FUNC(PyObject*) PyEval_GetBuiltins() {
16421650
return GraalPyEval_GetBuiltins();
16431651
}
1652+
#undef PyEval_GetFrame
1653+
PyAPI_FUNC(PyFrameObject*) PyEval_GetFrame() {
1654+
return GraalPyEval_GetFrame();
1655+
}
16441656
#undef PyEval_RestoreThread
16451657
PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState* a) {
16461658
GraalPyEval_RestoreThread(a);
@@ -1677,6 +1689,34 @@ PyAPI_FUNC(int) PyFile_WriteObject(PyObject* a, PyObject* b, int c) {
16771689
PyAPI_FUNC(PyObject*) PyFloat_FromDouble(double a) {
16781690
return GraalPyFloat_FromDouble(a);
16791691
}
1692+
#undef PyFrame_GetBack
1693+
PyAPI_FUNC(PyFrameObject*) PyFrame_GetBack(PyFrameObject* a) {
1694+
return GraalPyFrame_GetBack(a);
1695+
}
1696+
#undef PyFrame_GetBuiltins
1697+
PyAPI_FUNC(PyObject*) PyFrame_GetBuiltins(PyFrameObject* a) {
1698+
return GraalPyFrame_GetBuiltins(a);
1699+
}
1700+
#undef PyFrame_GetCode
1701+
PyAPI_FUNC(PyCodeObject*) PyFrame_GetCode(PyFrameObject* a) {
1702+
return GraalPyFrame_GetCode(a);
1703+
}
1704+
#undef PyFrame_GetGlobals
1705+
PyAPI_FUNC(PyObject*) PyFrame_GetGlobals(PyFrameObject* a) {
1706+
return GraalPyFrame_GetGlobals(a);
1707+
}
1708+
#undef PyFrame_GetLasti
1709+
PyAPI_FUNC(int) PyFrame_GetLasti(PyFrameObject* a) {
1710+
return GraalPyFrame_GetLasti(a);
1711+
}
1712+
#undef PyFrame_GetLineNumber
1713+
PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject* a) {
1714+
return GraalPyFrame_GetLineNumber(a);
1715+
}
1716+
#undef PyFrame_GetLocals
1717+
PyAPI_FUNC(PyObject*) PyFrame_GetLocals(PyFrameObject* a) {
1718+
return GraalPyFrame_GetLocals(a);
1719+
}
16801720
#undef PyFrame_New
16811721
PyAPI_FUNC(PyFrameObject*) PyFrame_New(PyThreadState* a, PyCodeObject* b, PyObject* c, PyObject* d) {
16821722
return GraalPyFrame_New(a, b, c, d);

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ typedef struct {
114114
BUILTIN(PyCapsule_SetName, int, PyObject*, const char*) \
115115
BUILTIN(PyCapsule_SetPointer, int, PyObject*, void*) \
116116
BUILTIN(PyClassMethod_New, PyObject*, PyObject*) \
117+
BUILTIN(PyCode_Addr2Line, int, PyCodeObject*, int) \
118+
BUILTIN(PyCode_GetFileName, PyObject*, PyCodeObject*) \
119+
BUILTIN(PyCode_GetName, PyObject*, PyCodeObject*) \
117120
BUILTIN(PyCode_New, PyCodeObject*, int, int, int, int, int, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, int, PyObject*) \
118121
BUILTIN(PyCode_NewEmpty, PyCodeObject*, const char*, const char*, int) \
119122
BUILTIN(PyCode_NewWithPosOnlyArgs, PyCodeObject*, int, int, int, int, int, int, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, PyObject*, int, PyObject*) \
@@ -146,8 +149,8 @@ typedef struct {
146149
BUILTIN(PyErr_PrintEx, void, int) \
147150
BUILTIN(PyErr_Restore, void, PyObject*, PyObject*, PyObject*) \
148151
BUILTIN(PyErr_SetExcInfo, void, PyObject*, PyObject*, PyObject*) \
149-
BUILTIN(PyErr_WarnExplicit, int, PyObject*, const char*, const char*, int, const char*, PyObject*) \
150152
BUILTIN(PyEval_GetBuiltins, PyObject*) \
153+
BUILTIN(PyEval_GetFrame, PyFrameObject*) \
151154
BUILTIN(PyEval_RestoreThread, void, PyThreadState*) \
152155
BUILTIN(PyEval_SaveThread, PyThreadState*) \
153156
BUILTIN(PyException_GetCause, PyObject*, PyObject*) \
@@ -157,6 +160,13 @@ typedef struct {
157160
BUILTIN(PyException_SetTraceback, int, PyObject*, PyObject*) \
158161
BUILTIN(PyFile_WriteObject, int, PyObject*, PyObject*, int) \
159162
BUILTIN(PyFloat_FromDouble, PyObject*, double) \
163+
BUILTIN(PyFrame_GetBack, PyFrameObject*, PyFrameObject*) \
164+
BUILTIN(PyFrame_GetBuiltins, PyObject*, PyFrameObject*) \
165+
BUILTIN(PyFrame_GetCode, PyCodeObject*, PyFrameObject*) \
166+
BUILTIN(PyFrame_GetGlobals, PyObject*, PyFrameObject*) \
167+
BUILTIN(PyFrame_GetLasti, int, PyFrameObject*) \
168+
BUILTIN(PyFrame_GetLineNumber, int, PyFrameObject*) \
169+
BUILTIN(PyFrame_GetLocals, PyObject*, PyFrameObject*) \
160170
BUILTIN(PyFrame_New, PyFrameObject*, PyThreadState*, PyCodeObject*, PyObject*, PyObject*) \
161171
BUILTIN(PyFrozenSet_New, PyObject*, PyObject*) \
162172
BUILTIN(PyGILState_Check, int) \
@@ -302,6 +312,7 @@ typedef struct {
302312
BUILTIN(PyTruffleDict_Next, PyObject*, PyObject*, Py_ssize_t) \
303313
BUILTIN(PyTruffleErr_Fetch, PyObject*) \
304314
BUILTIN(PyTruffleErr_GetExcInfo, PyObject*) \
315+
BUILTIN(PyTruffleErr_WarnExplicit, PyObject*, PyObject*, PyObject*, PyObject*, int, PyObject*, PyObject*) \
305316
BUILTIN(PyTruffleFloat_AsDouble, double, PyObject*) \
306317
BUILTIN(PyTruffleHash_InitSecret, void, void*) \
307318
BUILTIN(PyTruffleLong_AsPrimitive, long, PyObject*, int, long) \
@@ -353,7 +364,7 @@ typedef struct {
353364
BUILTIN(PyTruffle_NoValue, PyObject*) \
354365
BUILTIN(PyTruffle_None, PyObject*) \
355366
BUILTIN(PyTruffle_NotImplemented, PyObject*) \
356-
BUILTIN(PyTruffle_Object_Free, int, void*) \
367+
BUILTIN(PyTruffle_Object_Free, void, void*) \
357368
BUILTIN(PyTruffle_Register_NULL, void, void*) \
358369
BUILTIN(PyTruffle_Set_Native_Slots, int, PyTypeObject*, void*, void*) \
359370
BUILTIN(PyTruffle_Set_SulongType, void*, PyTypeObject*, void*) \

graalpython/com.oracle.graal.python.cext/src/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ int _PyDict_Next(PyObject *d, Py_ssize_t *ppos, PyObject **pkey, PyObject **pval
7979
}
8080
return 0;
8181
}
82-
(*ppos)++;
8382
if (pkey != NULL) {
8483
*pkey = PyTuple_GetItem(tresult, 0);
8584
}
@@ -89,6 +88,7 @@ int _PyDict_Next(PyObject *d, Py_ssize_t *ppos, PyObject **pkey, PyObject **pval
8988
if (phash != NULL) {
9089
*phash = PyLong_AsSsize_t(PyTuple_GetItem(tresult, 2));
9190
}
91+
*ppos = PyLong_AsSsize_t(PyTuple_GetItem(tresult, 3));
9292
Py_DECREF(tresult);
9393
return 1;
9494

graalpython/com.oracle.graal.python.cext/src/object_shared.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ PyObject* PyObject_CallObject(PyObject* callable, PyObject* args) {
215215
return Graal_PyTruffleObject_Call1(callable, args, NULL, 0);
216216
}
217217

218+
PyObject* PyObject_CallNoArgs(PyObject* callable) {
219+
return Graal_PyTruffleObject_Call1(callable, NULL, NULL, 0);
220+
}
221+
218222
PyObject* PyObject_CallFunction(PyObject* callable, const char* fmt, ...) {
219223
if (fmt == NULL || fmt[0] == '\0') {
220224
return Graal_PyTruffleObject_Call1(callable, NULL, NULL, 0);

graalpython/com.oracle.graal.python.cext/src/obmalloc.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,12 @@ void _PyObject_Free(void* ptr) {
119119
return;
120120
}
121121
if(points_to_py_handle_space(ptr) || polyglot_is_value(ptr)) {
122-
if(GraalPyTruffle_Object_Free(ptr)) {
123-
/* If 1 is returned, the upcall function already took care of freeing */
124-
return;
125-
}
122+
GraalPyTruffle_Object_Free(ptr);
123+
} else {
124+
mem_head_t* ptr_with_head = AS_MEM_HEAD(ptr);
125+
PyTruffle_FreeMemory(ptr_with_head->size);
126+
free(ptr_with_head);
126127
}
127-
mem_head_t* ptr_with_head = AS_MEM_HEAD(ptr);
128-
PyTruffle_FreeMemory(ptr_with_head->size);
129-
free(ptr_with_head);
130128
}
131129

132130
void* PyObject_Malloc(size_t size) {

0 commit comments

Comments
 (0)