Skip to content

Commit c340c8f

Browse files
Merge branch 'python:main' into pythongh-87452
2 parents ecbe497 + 3e6019c commit c340c8f

145 files changed

Lines changed: 1480 additions & 1416 deletions

File tree

Some content is hidden

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

‎Include/cpython/abstract.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod(
103103
static inline PyObject *
104104
PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
105105
{
106-
return PyObject_VectorcallMethod(name, &self,
107-
1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
106+
size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
107+
return PyObject_VectorcallMethod(name, &self, nargsf, _Py_NULL);
108108
}
109109

110110
static inline PyObject *
@@ -113,8 +113,8 @@ PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
113113
PyObject *args[2] = {self, arg};
114114

115115
assert(arg != NULL);
116-
return PyObject_VectorcallMethod(name, args,
117-
2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
116+
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
117+
return PyObject_VectorcallMethod(name, args, nargsf, _Py_NULL);
118118
}
119119

120120
PyAPI_FUNC(PyObject *) _PyObject_CallMethod(PyObject *obj,
@@ -144,16 +144,16 @@ _PyObject_VectorcallMethodId(
144144
{
145145
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
146146
if (!oname) {
147-
return NULL;
147+
return _Py_NULL;
148148
}
149149
return PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
150150
}
151151

152152
static inline PyObject *
153153
_PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
154154
{
155-
return _PyObject_VectorcallMethodId(name, &self,
156-
1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
155+
size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
156+
return _PyObject_VectorcallMethodId(name, &self, nargsf, _Py_NULL);
157157
}
158158

159159
static inline PyObject *
@@ -162,8 +162,8 @@ _PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg
162162
PyObject *args[2] = {self, arg};
163163

164164
assert(arg != NULL);
165-
return _PyObject_VectorcallMethodId(name, args,
166-
2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
165+
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
166+
return _PyObject_VectorcallMethodId(name, args, nargsf, _Py_NULL);
167167
}
168168

169169
PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);

‎Include/cpython/unicodeobject.h‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,15 @@ enum PyUnicode_Kind {
314314

315315
/* Return one of the PyUnicode_*_KIND values defined above. */
316316
#define PyUnicode_KIND(op) \
317-
(assert(PyUnicode_Check(op)), \
318-
assert(PyUnicode_IS_READY(op)), \
319-
((PyASCIIObject *)(op))->state.kind)
317+
(assert(PyUnicode_IS_READY(op)), \
318+
_PyASCIIObject_CAST(op)->state.kind)
320319

321320
/* Return a void pointer to the raw unicode buffer. */
322321
static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
323322
if (PyUnicode_IS_ASCII(op)) {
324-
return (void*)(_PyASCIIObject_CAST(op) + 1);
323+
return _Py_STATIC_CAST(void*, (_PyASCIIObject_CAST(op) + 1));
325324
}
326-
return (void*)(_PyCompactUnicodeObject_CAST(op) + 1);
325+
return _Py_STATIC_CAST(void*, (_PyCompactUnicodeObject_CAST(op) + 1));
327326
}
328327

329328
static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
@@ -348,9 +347,9 @@ static inline void* PyUnicode_DATA(PyObject *op) {
348347
No checks are performed, use PyUnicode_KIND() before to ensure
349348
these will work correctly. */
350349

351-
#define PyUnicode_1BYTE_DATA(op) ((Py_UCS1*)PyUnicode_DATA(op))
352-
#define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
353-
#define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
350+
#define PyUnicode_1BYTE_DATA(op) _Py_STATIC_CAST(Py_UCS1*, PyUnicode_DATA(op))
351+
#define PyUnicode_2BYTE_DATA(op) _Py_STATIC_CAST(Py_UCS2*, PyUnicode_DATA(op))
352+
#define PyUnicode_4BYTE_DATA(op) _Py_STATIC_CAST(Py_UCS4*, PyUnicode_DATA(op))
354353

355354
/* Returns the length of the unicode string. The caller has to make sure that
356355
the string has it's canonical representation set before calling
@@ -373,16 +372,16 @@ static inline void PyUnicode_WRITE(unsigned int kind, void *data,
373372
{
374373
if (kind == PyUnicode_1BYTE_KIND) {
375374
assert(value <= 0xffU);
376-
((Py_UCS1 *)data)[index] = (Py_UCS1)value;
375+
_Py_STATIC_CAST(Py_UCS1*, data)[index] = _Py_STATIC_CAST(Py_UCS1, value);
377376
}
378377
else if (kind == PyUnicode_2BYTE_KIND) {
379378
assert(value <= 0xffffU);
380-
((Py_UCS2 *)data)[index] = (Py_UCS2)value;
379+
_Py_STATIC_CAST(Py_UCS2*, data)[index] = _Py_STATIC_CAST(Py_UCS2, value);
381380
}
382381
else {
383382
assert(kind == PyUnicode_4BYTE_KIND);
384383
assert(value <= 0x10ffffU);
385-
((Py_UCS4 *)data)[index] = value;
384+
_Py_STATIC_CAST(Py_UCS4*, data)[index] = value;
386385
}
387386
}
388387
#define PyUnicode_WRITE(kind, data, index, value) \
@@ -394,13 +393,13 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
394393
const void *data, Py_ssize_t index)
395394
{
396395
if (kind == PyUnicode_1BYTE_KIND) {
397-
return ((const Py_UCS1 *)data)[index];
396+
return _Py_STATIC_CAST(const Py_UCS1*, data)[index];
398397
}
399398
if (kind == PyUnicode_2BYTE_KIND) {
400-
return ((const Py_UCS2 *)data)[index];
399+
return _Py_STATIC_CAST(const Py_UCS2*, data)[index];
401400
}
402401
assert(kind == PyUnicode_4BYTE_KIND);
403-
return ((const Py_UCS4 *)data)[index];
402+
return _Py_STATIC_CAST(const Py_UCS4*, data)[index];
404403
}
405404
#define PyUnicode_READ(kind, data, index) \
406405
PyUnicode_READ((unsigned int)(kind), (const void*)(data), (index))
@@ -643,9 +642,9 @@ static inline Py_ssize_t PyUnicode_GET_SIZE(PyObject *op)
643642
{
644643
_Py_COMP_DIAG_PUSH
645644
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
646-
if (_PyASCIIObject_CAST(op)->wstr == NULL) {
645+
if (_PyASCIIObject_CAST(op)->wstr == _Py_NULL) {
647646
(void)PyUnicode_AsUnicode(op);
648-
assert(_PyASCIIObject_CAST(op)->wstr != NULL);
647+
assert(_PyASCIIObject_CAST(op)->wstr != _Py_NULL);
649648
}
650649
return PyUnicode_WSTR_LENGTH(op);
651650
_Py_COMP_DIAG_POP
@@ -675,7 +674,7 @@ Py_DEPRECATED(3.3)
675674
static inline Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *op)
676675
{
677676
wchar_t *wstr = _PyASCIIObject_CAST(op)->wstr;
678-
if (wstr != NULL) {
677+
if (wstr != _Py_NULL) {
679678
return wstr;
680679
}
681680

@@ -693,7 +692,9 @@ static inline const char* PyUnicode_AS_DATA(PyObject *op)
693692
{
694693
_Py_COMP_DIAG_PUSH
695694
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
696-
return (const char *)PyUnicode_AS_UNICODE(op);
695+
Py_UNICODE *data = PyUnicode_AS_UNICODE(op);
696+
// In C++, casting directly PyUnicode* to const char* is not valid
697+
return _Py_STATIC_CAST(const char*, _Py_STATIC_CAST(const void*, data));
697698
_Py_COMP_DIAG_POP
698699
}
699700
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000

‎Include/cpython/weakrefobject.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
3838

3939
static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
4040
assert(PyWeakref_Check(ref_obj));
41-
PyWeakReference *ref = (PyWeakReference *)ref_obj;
41+
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
4242
PyObject *obj = ref->wr_object;
4343
// Explanation for the Py_REFCNT() check: when a weakref's target is part
4444
// of a long chain of deallocations which triggers the trashcan mechanism,

‎Include/object.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ static inline void Py_DECREF(PyObject *op)
587587
/* Function to use in case the object pointer can be NULL: */
588588
static inline void Py_XINCREF(PyObject *op)
589589
{
590-
if (op != NULL) {
590+
if (op != _Py_NULL) {
591591
Py_INCREF(op);
592592
}
593593
}
@@ -597,7 +597,7 @@ static inline void Py_XINCREF(PyObject *op)
597597

598598
static inline void Py_XDECREF(PyObject *op)
599599
{
600-
if (op != NULL) {
600+
if (op != _Py_NULL) {
601601
Py_DECREF(op);
602602
}
603603
}

‎Include/pyport.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
# define _Py_CAST(type, expr) ((type)(expr))
3434
#endif
3535

36+
// Static inline functions should use _Py_NULL rather than using directly NULL
37+
// to prevent C++ compiler warnings. In C++, _Py_NULL uses nullptr.
38+
#ifdef __cplusplus
39+
# define _Py_NULL nullptr
40+
#else
41+
# define _Py_NULL NULL
42+
#endif
43+
3644

3745
/* Defines to build Python and its standard library:
3846
*

‎Lib/test/_testcppext.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
5252

5353
static PyMethodDef _testcppext_methods[] = {
5454
{"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
55-
{"test_api_casts", test_api_casts, METH_NOARGS, NULL},
55+
{"test_api_casts", test_api_casts, METH_NOARGS, nullptr},
5656
{nullptr, nullptr, 0, nullptr} /* sentinel */
5757
};
5858

@@ -68,7 +68,7 @@ _testcppext_exec(PyObject *module)
6868

6969
static PyModuleDef_Slot _testcppext_slots[] = {
7070
{Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)},
71-
{0, NULL}
71+
{0, nullptr}
7272
};
7373

7474

@@ -81,8 +81,8 @@ static struct PyModuleDef _testcppext_module = {
8181
0, // m_size
8282
_testcppext_methods, // m_methods
8383
_testcppext_slots, // m_slots
84-
NULL, // m_traverse
85-
NULL, // m_clear
84+
nullptr, // m_traverse
85+
nullptr, // m_clear
8686
nullptr, // m_free
8787
};
8888

0 commit comments

Comments
 (0)