Skip to content

Commit 6d0174e

Browse files
committed
Rename GraalPy-speficic PyCFunction functions
1 parent f73eb11 commit 6d0174e

File tree

11 files changed

+48
-61
lines changed

11 files changed

+48
-61
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2022, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2022 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -39,6 +39,14 @@ PyAPI_DATA(PyTypeObject) PyCMethod_Type;
3939
#define PyCMethod_CheckExact(op) Py_IS_TYPE((op), &PyCMethod_Type)
4040
#define PyCMethod_Check(op) PyObject_TypeCheck((op), &PyCMethod_Type)
4141

42+
// GraalPy public API functions to replace struct access. Return borrowed references
43+
PyAPI_FUNC(PyTypeObject *) GraalPyCMethod_GetClass(PyObject *func);
44+
PyAPI_FUNC(const char*) GraalPyCFunction_GetDoc(PyObject *func);
45+
PyAPI_FUNC(void) GraalPyCFunction_SetDoc(PyObject *func, const char *doc);
46+
PyAPI_FUNC(PyObject*) GraalPyCFunction_GetModule(PyObject* a);
47+
PyAPI_FUNC(PyMethodDef*) GraalPyCFunction_GetMethodDef(PyObject* a);
48+
PyAPI_FUNC(void) GraalPyCFunction_SetModule(PyObject* a, PyObject* b);
49+
PyAPI_FUNC(void) GraalPyCFunction_SetMethodDef(PyObject* a, PyMethodDef *b);
4250

4351
/* Static inline functions for direct access to these values.
4452
Type checks are *not* done, so use with care. */
@@ -58,15 +66,6 @@ static inline int PyCFunction_GET_FLAGS(PyObject *func) {
5866
#define PyCFunction_GET_FLAGS(func) PyCFunction_GET_FLAGS(_PyObject_CAST(func))
5967

6068
static inline PyTypeObject* PyCFunction_GET_CLASS(PyObject *func_obj) {
61-
return PyCMethod_GetClass(func_obj);
69+
return GraalPyCMethod_GetClass(func_obj);
6270
}
6371
#define PyCFunction_GET_CLASS(func) PyCFunction_GET_CLASS(_PyObject_CAST(func))
64-
65-
/*
66-
* XXX These functions are GraalPy-only. We need them to replace field access.
67-
* Currently inserted by our autopatch_capi.py
68-
*/
69-
PyAPI_FUNC(PyObject*) _PyCFunction_GetModule(PyObject* a);
70-
PyAPI_FUNC(PyMethodDef*) _PyCFunction_GetMethodDef(PyObject* a);
71-
PyAPI_FUNC(void) _PyCFunction_SetModule(PyObject* a, PyObject* b);
72-
PyAPI_FUNC(void) _PyCFunction_SetMethodDef(PyObject* a, PyMethodDef *b);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -53,7 +53,6 @@ typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
5353
PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
5454
PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
5555
PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
56-
PyAPI_FUNC(PyTypeObject *) PyCMethod_GetClass(PyObject *func);
5756

5857
Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
5958

@@ -125,10 +124,6 @@ PyAPI_FUNC(PyObject *) PyCMethod_New(PyMethodDef *, PyObject *,
125124
#define METH_METHOD 0x0200
126125
#endif
127126

128-
// GraalPy public API functions
129-
PyAPI_FUNC(const char*) GraalPyCFunction_GetDoc(PyObject *func);
130-
PyAPI_FUNC(void) GraalPyCFunction_SetDoc(PyObject *func, const char *doc);
131-
132127

133128
#ifndef Py_LIMITED_API
134129
# define Py_CPYTHON_METHODOBJECT_H

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,27 @@ int PyCFunction_GetFlags(PyObject *func) {
8282
return def->ml_flags;
8383
}
8484

85-
PyTypeObject * PyCMethod_GetClass(PyObject *func) {
85+
PyTypeObject * GraalPyCMethod_GetClass(PyObject *func) {
8686
PyMethodDef* def = GraalPyPrivate_GET_PyCFunctionObject_m_ml(func);
8787
return def->ml_flags & METH_METHOD ? GraalPyPrivate_GET_PyCMethodObject_mm_class(func) : NULL;
8888
}
8989

90-
PyObject* _PyCFunction_GetModule(PyObject *func) {
90+
PyObject* GraalPyCFunction_GetModule(PyObject *func) {
9191
return GraalPyPrivate_GET_PyCFunctionObject_m_module(func);
9292
}
9393

94-
PyMethodDef* _PyCFunction_GetMethodDef(PyObject *func) {
94+
PyMethodDef* GraalPyCFunction_GetMethodDef(PyObject *func) {
9595
return GraalPyPrivate_GET_PyCFunctionObject_m_ml(func);
9696
}
9797

98-
void _PyCFunction_SetModule(PyObject *func, PyObject *mod) {
98+
void GraalPyCFunction_SetModule(PyObject *func, PyObject *mod) {
9999
GraalPyPrivate_SET_PyCFunctionObject_m_module(func, mod);
100100
}
101101

102-
void _PyCFunction_SetMethodDef(PyObject *func, PyMethodDef *def) {
102+
void GraalPyCFunction_SetMethodDef(PyObject *func, PyMethodDef *def) {
103103
GraalPyPrivate_SET_PyCFunctionObject_m_ml(func, def);
104104
}
105105

106-
// GraalPy additions
107106
const char *
108107
GraalPyCFunction_GetDoc(PyObject *func) {
109108
return GraalPyPrivate_GET_PyCFunctionObject_m_ml(func)->ml_doc;

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/CApiBuiltinsProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,9 @@ private static Path resolvePath(Path path) {
750750
* removing
751751
*/
752752
"PySlice_Start", "PySlice_Step", "PySlice_Stop",
753-
"PyCMethod_GetClass", "PyDescrObject_GetName", "PyDescrObject_GetType", "PyInterpreterState_GetIDFromThreadState",
753+
"PyDescrObject_GetName", "PyDescrObject_GetType", "PyInterpreterState_GetIDFromThreadState",
754754
"PyMethodDescrObject_GetMethod", "PyObject_GetDoc", "PyObject_SetDoc", "_PyFrame_SetLineNumber",
755-
"_PyCFunction_GetModule", "_PyCFunction_GetMethodDef", "PyCode_GetName",
756-
"_PyCFunction_SetModule", "_PyCFunction_SetMethodDef",
755+
"PyCode_GetName",
757756
"PyCode_GetFileName", "_PyArray_Resize", "_PyArray_Data",
758757
"_PyErr_Occurred", "_PyNamespace_New", "_Py_GetErrorHandler",
759758
// Not actually additional, only defined on Windows.

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_functions.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
class CallableIter:
5050
def __init__(self, start):
5151
self.idx = start
52-
52+
5353
def __call__(self, *args):
5454
cur = self.idx
5555
self.idx += 1
@@ -532,17 +532,17 @@ def _ref_hash_not_implemented(args):
532532
),
533533
code="""
534534
#include <string.h>
535-
535+
536536
// CPython and other don't have this function; so define it
537537
#ifndef GRAALVM_PYTHON
538-
#define _PyCFunction_GetMethodDef(OBJ) (((PyCFunctionObject*) (OBJ))->m_ml)
538+
#define GraalPyCFunction_GetMethodDef(OBJ) (((PyCFunctionObject*) (OBJ))->m_ml)
539539
#endif
540-
541-
static PyObject* wrap_PyCFunction_GetMethodDef(PyObject* func) {
540+
541+
static PyObject* wrapGraalPyCFunction_GetMethodDef(PyObject* func) {
542542
PyMethodDef *src;
543543
PyMethodDef dst;
544544
if (PyCFunction_Check(func)) {
545-
src = _PyCFunction_GetMethodDef(func);
545+
src = GraalPyCFunction_GetMethodDef(func);
546546
dst = *src;
547547
return PyUnicode_FromString(dst.ml_name);
548548
}
@@ -555,7 +555,7 @@ def _ref_hash_not_implemented(args):
555555
resultspec="O",
556556
argspec="O",
557557
arguments=["PyObject* func"],
558-
callfunction="wrap_PyCFunction_GetMethodDef",
558+
callfunction="wrapGraalPyCFunction_GetMethodDef",
559559
cmpfunc=unhandled_error_compare
560560
)
561561
# create function from PyMethodDef
@@ -571,10 +571,10 @@ def test_PyMethodDef(self):
571571
572572
// CPython and other don't have this function; so define it
573573
#ifndef GRAALVM_PYTHON
574-
#define _PyCFunction_GetMethodDef(OBJ) (((PyCFunctionObject*) (OBJ))->m_ml)
575-
#define _PyCFunction_SetMethodDef(OBJ, VAL) (((PyCFunctionObject*) (OBJ))->m_ml = (VAL))
576-
#define _PyCFunction_GetModule(OBJ) (((PyCFunctionObject*) (OBJ))->m_module)
577-
#define _PyCFunction_SetModule(OBJ, VAL) (((PyCFunctionObject*) (OBJ))->m_module = (VAL))
574+
#define GraalPyCFunction_GetMethodDef(OBJ) (((PyCFunctionObject*) (OBJ))->m_ml)
575+
#define GraalPyCFunction_SetMethodDef(OBJ, VAL) (((PyCFunctionObject*) (OBJ))->m_ml = (VAL))
576+
#define GraalPyCFunction_GetModule(OBJ) (((PyCFunctionObject*) (OBJ))->m_module)
577+
#define GraalPyCFunction_SetModule(OBJ, VAL) (((PyCFunctionObject*) (OBJ))->m_module = (VAL))
578578
#define PyMethodDescrObject_GetMethod(OBJ) (((PyMethodDescrObject *) (OBJ))->d_method)
579579
#endif
580580
@@ -606,7 +606,7 @@ def test_PyMethodDef(self):
606606
PyErr_SetString(PyExc_TypeError, "<callable> is not a PyCFunction (i.e. builtin_method_or_function)");
607607
return NULL;
608608
}
609-
PyMethodDef *def = _PyCFunction_GetMethodDef(arg);
609+
PyMethodDef *def = GraalPyCFunction_GetMethodDef(arg);
610610
return PyUnicode_FromString(def->ml_name);
611611
}
612612
@@ -615,7 +615,7 @@ def test_PyMethodDef(self):
615615
PyErr_SetString(PyExc_TypeError, "<callable> is not a PyCFunction (i.e. builtin_method_or_function)");
616616
return NULL;
617617
}
618-
PyMethodDef *def = _PyCFunction_GetMethodDef(arg);
618+
PyMethodDef *def = GraalPyCFunction_GetMethodDef(arg);
619619
return PyLong_FromLong(def->ml_flags);
620620
}
621621
@@ -675,7 +675,7 @@ def test_PyMethodDef(self):
675675
return NULL;
676676
}
677677
}
678-
678+
679679
static PyObject *call_meth(PyObject *self, PyObject *args) {
680680
PyObject *callable, *callable_args, *callable_kwargs = NULL;
681681
if (!PyArg_ParseTuple(args, "OO|O:call_meth",
@@ -687,7 +687,7 @@ def test_PyMethodDef(self):
687687
PyErr_SetString(PyExc_TypeError, "<callable> is not a PyCFunction (i.e. builtin_method_or_function) ");
688688
return NULL;
689689
}
690-
PyMethodDef *def = _PyCFunction_GetMethodDef(callable);
690+
PyMethodDef *def = GraalPyCFunction_GetMethodDef(callable);
691691
// returns a borrowed ref
692692
PyObject *callable_self = PyCFunction_GetSelf(callable);
693693
return _call_PyCFunction(def, callable_self, callable_args, callable_kwargs);
@@ -714,7 +714,7 @@ def test_PyMethodDef(self):
714714
PyErr_SetString(PyExc_TypeError, "<callable> is not a PyCFunction (i.e. builtin_method_or_function)");
715715
return NULL;
716716
}
717-
PyMethodDef *def = _PyCFunction_GetMethodDef(arg);
717+
PyMethodDef *def = GraalPyCFunction_GetMethodDef(arg);
718718
return PyUnicode_FromString(def->ml_doc);
719719
}
720720
@@ -723,20 +723,20 @@ def test_PyMethodDef(self):
723723
PyErr_SetString(PyExc_TypeError, "<callable> is not a PyCFunction (i.e. builtin_method_or_function)");
724724
return NULL;
725725
}
726-
PyMethodDef *def = _PyCFunction_GetMethodDef(arg);
727-
_PyCFunction_SetMethodDef(arg, def);
728-
return PyUnicode_FromString(_PyCFunction_GetMethodDef(arg)->ml_doc);
726+
PyMethodDef *def = GraalPyCFunction_GetMethodDef(arg);
727+
GraalPyCFunction_SetMethodDef(arg, def);
728+
return PyUnicode_FromString(GraalPyCFunction_GetMethodDef(arg)->ml_doc);
729729
}
730730
731731
static PyObject *get_set_module(PyObject *self, PyObject *arg) {
732732
if (!PyCFunction_Check(arg)) {
733733
PyErr_SetString(PyExc_TypeError, "<callable> is not a PyCFunction (i.e. builtin_method_or_function)");
734734
return NULL;
735735
}
736-
PyObject *module = _PyCFunction_GetModule(arg);
736+
PyObject *module = GraalPyCFunction_GetModule(arg);
737737
Py_XINCREF(self);
738-
_PyCFunction_SetModule(arg, self);
739-
if (_PyCFunction_GetModule(arg) != self) {
738+
GraalPyCFunction_SetModule(arg, self);
739+
if (GraalPyCFunction_GetModule(arg) != self) {
740740
PyErr_SetString(PyExc_TypeError, "module of function is not self");
741741
return NULL;
742742
}
@@ -754,7 +754,7 @@ def test_PyMethodDef(self):
754754
}
755755
PyMethodDef *def;
756756
if (PyCFunction_Check(callable)) {
757-
def = _PyCFunction_GetMethodDef(callable);
757+
def = GraalPyCFunction_GetMethodDef(callable);
758758
} else if (PyObject_TypeCheck(callable, &PyMethodDescr_Type)) {
759759
def = PyMethodDescrObject_GetMethod(callable);
760760
} else {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiFunction.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ public final class CApiFunction {
233233
@CApiBuiltin(name = "PyCFunction_GetSelf", ret = PyObject, args = {PyObject}, call = CImpl)
234234
@CApiBuiltin(name = "PyCFunction_New", ret = PyObject, args = {PyMethodDef, PyObject}, call = CImpl)
235235
@CApiBuiltin(name = "PyCFunction_NewEx", ret = PyObject, args = {PyMethodDef, PyObject, PyObject}, call = CImpl)
236-
@CApiBuiltin(name = "PyCMethod_GetClass", ret = PyTypeObject, args = {PyObject}, call = CImpl)
237236
@CApiBuiltin(name = "PyCMethod_New", ret = PyObject, args = {PyMethodDef, PyObject, PyObject, PyTypeObject}, call = CImpl)
238237
@CApiBuiltin(name = "PyUnstable_Code_New", ret = PyCodeObject, args = {Int, Int, Int, Int, Int, PyObject, PyObject, PyObject, PyObject, PyObject, PyObject, PyObject, PyObject, PyObject, Int,
239238
PyObject, PyObject}, call = CImpl)
@@ -590,10 +589,6 @@ public final class CApiFunction {
590589
@CApiBuiltin(name = "_PyBytesWriter_Resize", ret = Pointer, args = {_PYBYTESWRITER_PTR, Pointer, Py_ssize_t}, call = CImpl)
591590
@CApiBuiltin(name = "_PyBytesWriter_WriteBytes", ret = Pointer, args = {_PYBYTESWRITER_PTR, Pointer, CONST_VOID_PTR, Py_ssize_t}, call = CImpl)
592591
@CApiBuiltin(name = "_PyBytes_Resize", ret = Int, args = {PyObjectPtr, Py_ssize_t}, call = CImpl)
593-
@CApiBuiltin(name = "_PyCFunction_GetMethodDef", ret = PyMethodDef, args = {PyObject}, call = CImpl)
594-
@CApiBuiltin(name = "_PyCFunction_GetModule", ret = PyObject, args = {PyObject}, call = CImpl)
595-
@CApiBuiltin(name = "_PyCFunction_SetMethodDef", ret = Void, args = {PyObject, PyMethodDef}, call = CImpl)
596-
@CApiBuiltin(name = "_PyCFunction_SetModule", ret = Void, args = {PyObject, PyObject}, call = CImpl)
597592
@CApiBuiltin(name = "_PyDict_ContainsId", ret = Int, args = {PyObject, _PY_IDENTIFIER_PTR}, call = CImpl)
598593
@CApiBuiltin(name = "_PyDict_GetItemIdWithError", ret = PyObject, args = {PyObject, _PY_IDENTIFIER_PTR}, call = CImpl)
599594
@CApiBuiltin(name = "_PyDict_GetItemStringWithError", ret = PyObject, args = {PyObject, ConstCharPtrAsTruffleString}, call = CImpl)

graalpython/lib-graalpython/modules/autopatch_capi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022, 2025, 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
@@ -173,8 +173,8 @@ def consume_identifier_backwards(idx):
173173
r'\W(m_ml\s*->\s*ml_doc)\W': (replace_field_access, 'GraalPyCFunction_GetDoc((PyObject*)(%receiver))', 'GraalPyCFunction_SetDoc((PyObject*)(%receiver), %value)'),
174174
# Py_CLEAR/Py_VISIT on a function's module is skipped for us, Java GC takes care of it
175175
r'(Py_(?:CLEAR|VISIT)\((?:(?:\(?\([a-zA-Z0-9_]|[a-zA-Z0-9_])(?:[a-zA-Z0-9_]|\)|\*\)|->)*)->m_module\);)': (simple_replace, ''),
176-
r'\W(m_ml)\W': (replace_field_access, '_PyCFunction_GetMethodDef((PyObject*)(%receiver))', '_PyCFunction_SetMethodDef((PyObject*)(%receiver), %value)'),
177-
r'\W(m_module)\W': (replace_field_access, '_PyCFunction_GetModule((PyObject*)(%receiver))', '_PyCFunction_SetModule((PyObject*)(%receiver), %value)'),
176+
r'\W(m_ml)\W': (replace_field_access, 'GraalPyCFunction_GetMethodDef((PyObject*)(%receiver))', 'GraalPyCFunction_SetMethodDef((PyObject*)(%receiver), %value)'),
177+
r'\W(m_module)\W': (replace_field_access, 'GraalPyCFunction_GetModule((PyObject*)(%receiver))', 'GraalPyCFunction_SetModule((PyObject*)(%receiver), %value)'),
178178
r'(&PyTuple_GET_ITEM\(([\(\w](?:\w|->|\.|\(|\))*), 0\))': (simple_replace, r'PySequence_Fast_ITEMS(\2)'),
179179
# already defined by GraalPy:
180180
r'^\s*()#\s*define\s+Py_SET_TYPE\W': (simple_replace, '//'),

graalpython/lib-graalpython/patches/torch-1.13.1.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ index 81e98428..df857f31 100644
324324
- return PyErr_Format(
325325
- PyExc_RuntimeError,
326326
- "function '%s' already has a docstring",
327-
- _PyCFunction_GetMethodDef((PyObject*)(f))->ml_name);
327+
- GraalPyCFunction_GetMethodDef((PyObject*)(f))->ml_name);
328328
- }
329329
- GraalPyCFunction_SetDoc((PyObject*)(f), doc_str);
330330
- } else if (strcmp(Py_TYPE(obj)->tp_name, "method_descriptor") == 0) {

graalpython/lib-graalpython/patches/torch-2.2.1.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ index bbf99ce4..0168b101 100644
299299
- return PyErr_Format(
300300
- PyExc_RuntimeError,
301301
- "function '%s' already has a docstring",
302-
- _PyCFunction_GetMethodDef((PyObject*)(f))->ml_name);
302+
- GraalPyCFunction_GetMethodDef((PyObject*)(f))->ml_name);
303303
- }
304304
- GraalPyCFunction_SetDoc((PyObject*)(f), doc_str);
305305
- } else if (strcmp(Py_TYPE(obj)->tp_name, "method_descriptor") == 0) {

graalpython/lib-graalpython/patches/torch-2.4.1.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ index da74bc9d3..4dfbc7316 100644
470470
- return PyErr_Format(
471471
- PyExc_RuntimeError,
472472
- "function '%s' already has a docstring",
473-
- _PyCFunction_GetMethodDef((PyObject*)(f))->ml_name);
473+
- GraalPyCFunction_GetMethodDef((PyObject*)(f))->ml_name);
474474
- }
475475
- GraalPyCFunction_SetDoc((PyObject*)(f), doc_str);
476476
- } else if (strcmp(Py_TYPE(obj)->tp_name, "method_descriptor") == 0) {

0 commit comments

Comments
 (0)