Skip to content

Commit 139a6f3

Browse files
committed
[GR-40343] Various C API changes that improve opaqueness of the API.
PullRequest: graalpython/2394
2 parents cba0a36 + 6ce8bb1 commit 139a6f3

File tree

35 files changed

+1864
-247
lines changed

35 files changed

+1864
-247
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2017 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -20,11 +20,14 @@ PyAPI_DATA(PyTypeObject) PyBool_Type;
2020
Don't forget to apply Py_INCREF() when returning either!!! */
2121

2222
/* Don't use these directly */
23-
PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;
23+
PyAPI_DATA(struct _longobject*) _Py_FalseStructReference;
24+
PyAPI_DATA(struct _longobject*) _Py_TrueStructReference;
25+
#define _Py_TrueStruct (*_Py_TrueStructReference)
26+
#define _Py_FalseStruct (*_Py_FalseStructReference)
2427

2528
/* Use these macros */
26-
#define Py_False ((PyObject *) &_Py_FalseStruct)
27-
#define Py_True ((PyObject *) &_Py_TrueStruct)
29+
#define Py_False ((PyObject *) _Py_FalseStructReference)
30+
#define Py_True ((PyObject *) _Py_TrueStructReference)
2831

2932
/* Macros for returning Py_True or Py_False, respectively */
3033
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -24,7 +24,7 @@ typedef struct {
2424

2525
PyAPI_DATA(PyTypeObject) PyMethod_Type;
2626

27-
#define PyMethod_Check(op) ((op)->ob_type == &PyMethod_Type)
27+
#define PyMethod_Check(op) (Py_TYPE(op) == &PyMethod_Type)
2828

2929
PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *);
3030

@@ -47,7 +47,7 @@ typedef struct {
4747

4848
PyAPI_DATA(PyTypeObject) PyInstanceMethod_Type;
4949

50-
#define PyInstanceMethod_Check(op) ((op)->ob_type == &PyInstanceMethod_Type)
50+
#define PyInstanceMethod_Check(op) (Py_TYPE(op) == &PyInstanceMethod_Type)
5151

5252
PyAPI_FUNC(PyObject *) PyInstanceMethod_New(PyObject *);
5353
PyAPI_FUNC(PyObject *) PyInstanceMethod_Function(PyObject *);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -130,7 +130,7 @@ _PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
130130
return _PyObject_MakeTpCall(callable, args, nargs, kwnames);
131131
}
132132
res = func(callable, args, nargsf, kwnames);
133-
return _Py_CheckFunctionResult(callable, res, NULL);
133+
return res; // _Py_CheckFunctionResult(callable, res, NULL);
134134
}
135135

136136
/* Same as _PyObject_Vectorcall except that keyword arguments are passed as
@@ -197,8 +197,8 @@ PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
197197

198198
/* Return 1 if the getbuffer function is available, otherwise return 0. */
199199
#define PyObject_CheckBuffer(obj) \
200-
(((obj)->ob_type->tp_as_buffer != NULL) && \
201-
((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
200+
((Py_TYPE(obj)->tp_as_buffer != NULL) && \
201+
(Py_TYPE(obj)->tp_as_buffer->bf_getbuffer != NULL))
202202

203203
/* This is a C-API version of the getbuffer function call. It checks
204204
to make sure object has the required function pointer and issues the
@@ -266,14 +266,14 @@ PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
266266
/* ==== Iterators ================================================ */
267267

268268
#define PyIter_Check(obj) \
269-
((obj)->ob_type->tp_iternext != NULL && \
270-
(obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
269+
(Py_TYPE(obj)->tp_iternext != NULL && \
270+
Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
271271

272272
/* === Number Protocol ================================================== */
273273

274274
#define PyIndex_Check(obj) \
275-
((obj)->ob_type->tp_as_number != NULL && \
276-
(obj)->ob_type->tp_as_number->nb_index != NULL)
275+
(Py_TYPE(obj)->tp_as_number != NULL && \
276+
Py_TYPE(obj)->tp_as_number->nb_index != NULL)
277277

278278
/* === Sequence protocol ================================================ */
279279

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -123,9 +123,22 @@ typedef struct {
123123
/* Cast argument to PyVarObject* type. */
124124
#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
125125

126-
#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
127-
#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
128-
#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
126+
PyAPI_FUNC(Py_ssize_t) _Py_REFCNT(PyObject *);
127+
PyAPI_FUNC(void) _Py_SET_REFCNT(PyObject*, Py_ssize_t);
128+
PyAPI_FUNC(struct _typeobject*) _Py_TYPE(PyObject *);
129+
PyAPI_FUNC(void) _Py_SET_TYPE(PyObject *, struct _typeobject *);
130+
PyAPI_FUNC(Py_ssize_t) _Py_SIZE(PyVarObject *);
131+
PyAPI_FUNC(void) _Py_SET_SIZE(PyVarObject *, Py_ssize_t);
132+
133+
#define Py_REFCNT(ob) (_Py_REFCNT(_PyObject_CAST(ob)))
134+
#define Py_SET_REFCNT(ob, v) (_Py_SET_REFCNT(_PyObject_CAST(ob), v))
135+
#define Py_TYPE(ob) (_Py_TYPE(_PyObject_CAST(ob)))
136+
#define Py_SIZE(ob) (_Py_SIZE(_PyVarObject_CAST(ob)))
137+
138+
#define Py_SET_TYPE(ob, v) (_Py_SET_TYPE(_PyObject_CAST(ob), (struct _typeobject *) v))
139+
#define Py_SET_SIZE(ob, v) (_Py_SET_SIZE(_PyVarObject_CAST(ob), (Py_ssize_t) v))
140+
141+
#define Py_IS_TYPE(ob, type) (Py_TYPE(ob) == (type))
129142

130143
/*
131144
Type objects contain a string containing the type name (to help somewhat
@@ -445,7 +458,7 @@ static inline void _Py_NewReference(PyObject *op)
445458
}
446459
_Py_INC_TPALLOCS(op);
447460
_Py_INC_REFTOTAL;
448-
Py_REFCNT(op) = 1;
461+
Py_SET_REFCNT(op, 1);
449462
}
450463

451464
static inline void _Py_ForgetReference(PyObject *op)
@@ -562,8 +575,9 @@ where NULL (nil) is not suitable (since NULL often means 'error').
562575
563576
Don't forget to apply Py_INCREF() when returning this value!!!
564577
*/
565-
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
566-
#define Py_None (&_Py_NoneStruct)
578+
PyAPI_DATA(PyObject*) _Py_NoneStructReference; /* Don't use this directly */
579+
#define _Py_NoneStruct (*_Py_NoneStructReference)
580+
#define Py_None (_Py_NoneStructReference)
567581

568582
/* Macro for returning Py_None from a function */
569583
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
@@ -572,8 +586,9 @@ PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
572586
Py_NotImplemented is a singleton used to signal that an operation is
573587
not implemented for a given type combination.
574588
*/
575-
PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
576-
#define Py_NotImplemented (&_Py_NotImplementedStruct)
589+
PyAPI_DATA(PyObject*) _Py_NotImplementedStructReference; /* Don't use this directly */
590+
#define _Py_NotImplementedStruct (*_Py_NotImplementedStructReference)
591+
#define Py_NotImplemented (_Py_NotImplementedStructReference)
577592

578593
/* Macro for returning Py_NotImplemented from a function */
579594
#define Py_RETURN_NOTIMPLEMENTED \

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -142,7 +142,7 @@ static inline PyObject*
142142
_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
143143
{
144144
assert(op != NULL);
145-
Py_TYPE(op) = typeobj;
145+
Py_SET_TYPE(op, typeobj);
146146
if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
147147
Py_INCREF(typeobj);
148148
}
@@ -157,7 +157,7 @@ static inline PyVarObject*
157157
_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
158158
{
159159
assert(op != NULL);
160-
Py_SIZE(op) = size;
160+
Py_SET_SIZE(op, size);
161161
PyObject_INIT((PyObject *)op, typeobj);
162162
return op;
163163
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2017 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -22,7 +22,8 @@
2222
extern "C" {
2323
#endif
2424

25-
PyAPI_DATA(PyTypeObject) PyCapsule_Type;
25+
PyAPI_DATA(PyTypeObject*) PyCapsule_TypeReference;
26+
#define PyCapsule_Type (*PyCapsule_TypeReference)
2627

2728
typedef void (*PyCapsule_Destructor)(PyObject *);
2829

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -59,11 +59,11 @@ PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
5959
PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
6060

6161
#define PyExceptionInstance_Check(x) \
62-
PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
62+
PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)
6363

6464
PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
6565

66-
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
66+
#define PyExceptionInstance_Class(x) ((PyObject*)(Py_TYPE(x)))
6767

6868

6969
/* Predefined exceptions */

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -11,9 +11,10 @@ extern "C" {
1111

1212
/* The unique ellipsis object "..." */
1313

14-
PyAPI_DATA(PyObject) _Py_EllipsisObject; /* Don't use this directly */
14+
PyAPI_DATA(PyObject*) _Py_EllipsisObjectReference; /* Don't use this directly */
1515

16-
#define Py_Ellipsis (&_Py_EllipsisObject)
16+
#define _Py_EllipsisObject (*_Py_EllipsisObjectReference)
17+
#define Py_Ellipsis (_Py_EllipsisObjectReference)
1718

1819
/* Slice object interface */
1920

graalpython/com.oracle.graal.python.cext/modules/_cpython_struct.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -11,7 +11,7 @@
1111

1212
#define PY_SSIZE_T_CLEAN
1313

14-
#include "Python.h"
14+
#include "capi.h"
1515
#include "structmember.h"
1616
#include <ctype.h>
1717

@@ -1285,6 +1285,7 @@ align(Py_ssize_t size, char c, const formatdef *e)
12851285
static int
12861286
prepare_s(PyStructObject *self)
12871287
{
1288+
self = native_pointer_to_java(self);
12881289
const formatdef *f;
12891290
const formatdef *e;
12901291
formatcode *codes;
@@ -1294,9 +1295,10 @@ prepare_s(PyStructObject *self)
12941295
char c;
12951296
Py_ssize_t size, len, num, itemsize;
12961297
size_t ncodes;
1298+
PyObject* self_format = native_pointer_to_java(self->s_format);
12971299

1298-
fmt = PyBytes_AS_STRING(self->s_format);
1299-
if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self->s_format)) {
1300+
fmt = PyBytes_AS_STRING(self_format);
1301+
if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self_format)) {
13001302
PyErr_SetString(StructError, "embedded null character");
13011303
return -1;
13021304
}
@@ -1611,7 +1613,7 @@ Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
16111613
buffer->len);
16121614
return NULL;
16131615
}
1614-
return s_unpack_internal(self, (char*)buffer->buf + offset);
1616+
return s_unpack_internal(self, (char*)native_pointer_to_java(buffer->buf) + offset);
16151617
}
16161618

16171619

@@ -2084,7 +2086,10 @@ PyTypeObject PyStructType = {
20842086
/* ---- Standalone functions ---- */
20852087

20862088
#define MAXCACHE 100
2087-
static PyObject *cache = NULL;
2089+
/* Please note: the name of the cache needs to be prefixed because this file
2090+
includes 'capi.h' which defines a global variable with name 'cache'. */
2091+
#define STRUCT_CACHE _cpython_struct_cache
2092+
static PyObject *STRUCT_CACHE = NULL;
20882093

20892094
static int
20902095
cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
@@ -2097,13 +2102,13 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
20972102
return 1;
20982103
}
20992104

2100-
if (cache == NULL) {
2101-
cache = PyDict_New();
2102-
if (cache == NULL)
2105+
if (STRUCT_CACHE == NULL) {
2106+
STRUCT_CACHE = PyDict_New();
2107+
if (STRUCT_CACHE == NULL)
21032108
return 0;
21042109
}
21052110

2106-
s_object = PyDict_GetItemWithError(cache, fmt);
2111+
s_object = PyDict_GetItemWithError(STRUCT_CACHE, fmt);
21072112
if (s_object != NULL) {
21082113
Py_INCREF(s_object);
21092114
*ptr = (PyStructObject *)s_object;
@@ -2115,10 +2120,10 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
21152120

21162121
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
21172122
if (s_object != NULL) {
2118-
if (PyDict_GET_SIZE(cache) >= MAXCACHE)
2119-
PyDict_Clear(cache);
2123+
if (PyDict_GET_SIZE(STRUCT_CACHE) >= MAXCACHE)
2124+
PyDict_Clear(STRUCT_CACHE);
21202125
/* Attempt to cache the result */
2121-
if (PyDict_SetItem(cache, fmt, s_object) == -1)
2126+
if (PyDict_SetItem(STRUCT_CACHE, fmt, s_object) == -1)
21222127
PyErr_Clear();
21232128
*ptr = (PyStructObject *)s_object;
21242129
return Py_CLEANUP_SUPPORTED;
@@ -2136,7 +2141,7 @@ static PyObject *
21362141
_clearcache_impl(PyObject *module)
21372142
/*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
21382143
{
2139-
Py_CLEAR(cache);
2144+
Py_CLEAR(STRUCT_CACHE);
21402145
Py_RETURN_NONE;
21412146
}
21422147

@@ -2344,7 +2349,7 @@ PyInit__cpython_struct(void)
23442349
if (m == NULL)
23452350
return NULL;
23462351

2347-
Py_TYPE(&PyStructType) = &PyType_Type;
2352+
Py_SET_TYPE(&PyStructType, &PyType_Type);
23482353
if (PyType_Ready(&PyStructType) < 0)
23492354
return NULL;
23502355

graalpython/com.oracle.graal.python.cext/modules/_cpython_unicodedata.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -1460,7 +1460,7 @@ PyInit__cpython_unicodedata(void)
14601460
{
14611461
PyObject *m, *v;
14621462

1463-
Py_TYPE(&UCD_Type) = &PyType_Type;
1463+
Py_SET_TYPE(&UCD_Type, &PyType_Type);
14641464

14651465
m = PyModule_Create(&unicodedatamodule);
14661466
if (!m)

0 commit comments

Comments
 (0)