Skip to content

gh-63891: Rename Py_SAFE_DOWNCAST to _Py_DOWNCAST #15090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Include/pyport.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ extern "C" {
#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
#endif

/* Py_SAFE_DOWNCAST is not a safe macro, so adding an alias to replace it.
* Pls use the _Py_DOWNCAST inside.
* https://bugs.python.org/issue19692
*/
#define _Py_DOWNCAST Py_SAFE_DOWNCAST

/* Py_SET_ERRNO_ON_MATH_ERROR(x)
* If a libm function did not set errno, but it looks like the result
* overflowed or not-a-number, set errno to ERANGE or EDOM. Set errno
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :meth:`_Py_DOWNCAST` which is an alias of :meth:`Py_SAFE_DOWNCAST`
due to :meth:`Py_SAFE_DOWNCAST` is not a safe macro.
10 changes: 5 additions & 5 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,7 @@ unique_key(CDataObject *target, Py_ssize_t index)
size_t bytes_left;

Py_BUILD_ASSERT(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
cp += sprintf(cp, "%x", _Py_DOWNCAST(index, Py_ssize_t, int));
while (target->b_base) {
bytes_left = sizeof(string) - (cp - string) - 1;
/* Hex format needs 2 characters per byte */
Expand All @@ -2589,7 +2589,7 @@ unique_key(CDataObject *target, Py_ssize_t index)
"ctypes object structure too deep");
return NULL;
}
cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, int));
cp += sprintf(cp, ":%x", _Py_DOWNCAST(target->b_index, Py_ssize_t, int));
target = target->b_base;
}
return PyUnicode_FromStringAndSize(string, cp-string);
Expand Down Expand Up @@ -3333,7 +3333,7 @@ _check_outarg_type(PyObject *arg, Py_ssize_t index)

PyErr_Format(PyExc_TypeError,
"'out' parameter %d must be a pointer type, not %s",
Py_SAFE_DOWNCAST(index, Py_ssize_t, int),
_Py_DOWNCAST(index, Py_ssize_t, int),
PyType_Check(arg) ?
((PyTypeObject *)arg)->tp_name :
Py_TYPE(arg)->tp_name);
Expand Down Expand Up @@ -4058,9 +4058,9 @@ PyCFuncPtr_call(PyCFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
return NULL;

if (converters) {
int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters),
int required = _Py_DOWNCAST(PyTuple_GET_SIZE(converters),
Py_ssize_t, int);
int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs),
int actual = _Py_DOWNCAST(PyTuple_GET_SIZE(callargs),
Py_ssize_t, int);

if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
cc = FFI_STDCALL;
#endif
result = ffi_prep_cif(&p->cif, cc,
Py_SAFE_DOWNCAST(nArgs, Py_ssize_t, int),
_Py_DOWNCAST(nArgs, Py_ssize_t, int),
_ctypes_get_ffi_type(restype),
&p->atypes[0]);
if (result != FFI_OK) {
Expand Down
4 changes: 2 additions & 2 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
}
PyErr_Format(PyExc_TypeError,
"Don't know how to convert parameter %d",
Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
_Py_DOWNCAST(index, Py_ssize_t, int));
return -1;
}
}
Expand Down Expand Up @@ -1178,7 +1178,7 @@ PyObject *_ctypes_callproc(PPROC pProc,

if (-1 == _call_function_pointer(flags, pProc, avalues, atypes,
rtype, resbuf,
Py_SAFE_DOWNCAST(argcount,
_Py_DOWNCAST(argcount,
Py_ssize_t,
int)))
goto cleanup;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
/* Adjust the size according to the alignment requirements */
size = ((size + total_align - 1) / total_align) * total_align;

stgdict->ffi_type_pointer.alignment = Py_SAFE_DOWNCAST(total_align,
stgdict->ffi_type_pointer.alignment = _Py_DOWNCAST(total_align,
Py_ssize_t,
unsigned short);
stgdict->ffi_type_pointer.size = size;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
if (len > (Py_ssize_t)MUNCH_SIZE)
process = MUNCH_SIZE;
else
process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
process = _Py_DOWNCAST(len, Py_ssize_t, unsigned int);
if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
_setException(PyExc_ValueError);
break;
Expand Down
26 changes: 13 additions & 13 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ _io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
/* Return up to n bytes. If at least one byte is buffered, we
only return buffered bytes. Otherwise, we do one raw read. */

have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
have = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (have > 0) {
n = Py_MIN(have, n);
res = _bufferedreader_read_fast(self, n);
Expand Down Expand Up @@ -966,7 +966,7 @@ _buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1)

CHECK_INITIALIZED(self)

n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
n = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (n > 0) {
if (n >= buffer->len) {
memcpy(buffer->buf, self->buffer + self->pos, buffer->len);
Expand Down Expand Up @@ -1080,7 +1080,7 @@ _buffered_readline(buffered *self, Py_ssize_t limit)
/* First, try to find a line in the buffer. This can run unlocked because
the calls to the C API are simple enough that they can't trigger
any thread switch. */
n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
n = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (limit >= 0 && n > limit)
n = limit;
start = self->buffer + self->pos;
Expand Down Expand Up @@ -1495,7 +1495,7 @@ _bufferedreader_fill_buffer(buffered *self)
{
Py_ssize_t start, len, n;
if (VALID_READ_BUFFER(self))
start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
start = _Py_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
else
start = 0;
len = self->buffer_size - start;
Expand All @@ -1514,7 +1514,7 @@ _bufferedreader_read_all(buffered *self)
PyObject *res = NULL, *data = NULL, *tmp = NULL, *chunks = NULL, *readall;

/* First copy what we have in the current buffer. */
current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
current_size = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (current_size) {
data = PyBytes_FromStringAndSize(
self->buffer + self->pos, current_size);
Expand Down Expand Up @@ -1604,7 +1604,7 @@ _bufferedreader_read_fast(buffered *self, Py_ssize_t n)
{
Py_ssize_t current_size;

current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
current_size = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (n <= current_size) {
/* Fast path: the data to read is fully buffered. */
PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
Expand All @@ -1625,7 +1625,7 @@ _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
Py_ssize_t current_size, remaining, written;
char *out;

current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
current_size = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (n <= current_size)
return _bufferedreader_read_fast(self, n);

Expand Down Expand Up @@ -1720,7 +1720,7 @@ _bufferedreader_peek_unlocked(buffered *self)
{
Py_ssize_t have, r;

have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
have = _Py_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
/* Constraints:
1. we don't want to advance the file position.
2. we don't want to lose block alignment, so we can't shift the buffer
Expand Down Expand Up @@ -1862,7 +1862,7 @@ _bufferedwriter_flush_unlocked(buffered *self)
while (self->write_pos < self->write_end) {
n = _bufferedwriter_raw_write(self,
self->buffer + self->write_pos,
Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
_Py_DOWNCAST(self->write_end - self->write_pos,
Py_off_t, Py_ssize_t));
if (n == -1) {
goto error;
Expand All @@ -1874,7 +1874,7 @@ _bufferedwriter_flush_unlocked(buffered *self)
}
self->write_pos += n;
self->raw_pos = self->write_pos;
written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
written += _Py_DOWNCAST(n, Py_off_t, Py_ssize_t);
/* Partial writes can return successfully when interrupted by a
signal (see write(2)). We must run signal handlers before
blocking another time, possibly indefinitely. */
Expand Down Expand Up @@ -1931,7 +1931,7 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
self->pos = 0;
self->raw_pos = 0;
}
avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
avail = _Py_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
if (buffer->len <= avail) {
memcpy(self->buffer + self->pos, buffer->buf, buffer->len);
if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
Expand All @@ -1955,13 +1955,13 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
/* Make some place by shifting the buffer. */
assert(VALID_WRITE_BUFFER(self));
memmove(self->buffer, self->buffer + self->write_pos,
Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
_Py_DOWNCAST(self->write_end - self->write_pos,
Py_off_t, Py_ssize_t));
self->write_end -= self->write_pos;
self->raw_pos -= self->write_pos;
self->pos -= self->write_pos;
self->write_pos = 0;
avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
avail = _Py_DOWNCAST(self->buffer_size - self->write_end,
Py_off_t, Py_ssize_t);
if (buffer->len <= avail) {
/* Everything can be buffered */
Expand Down
4 changes: 2 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,7 @@ _io_TextIOWrapper_tell_impl(textio *self)

/* Note our initial start point. */
cookie.start_pos += skip_bytes;
cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
cookie.chars_to_skip = _Py_DOWNCAST(chars_to_skip, Py_ssize_t, int);
if (chars_to_skip == 0)
goto finally;

Expand Down Expand Up @@ -2841,7 +2841,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
Py_DECREF(res);

/* The returned cookie corresponds to the last safe start point. */
cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
cookie.chars_to_skip = _Py_DOWNCAST(chars_to_skip, Py_ssize_t, int);
return textiowrapper_build_cookie(&cookie);

fail:
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ _pysqlite_long_from_int64(sqlite_int64 value)
if (value > LONG_MAX || value < LONG_MIN)
return PyLong_FromLongLong(value);
# endif
return PyLong_FromLong(Py_SAFE_DOWNCAST(value, sqlite_int64, long));
return PyLong_FromLong(_Py_DOWNCAST(value, sqlite_int64, long));
}

sqlite_int64
Expand Down
4 changes: 2 additions & 2 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
}
SSL_set_app_data(self->ssl, self);
if (sock) {
SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int));
SSL_set_fd(self->ssl, _Py_DOWNCAST(sock->sock_fd, SOCKET_T, int));
} else {
/* BIOs are reference counted and SSL_set_bio borrows our reference.
* To prevent a double free in memory_bio_dealloc() we need to take an
Expand Down Expand Up @@ -2293,7 +2293,7 @@ PySSL_select(PySocketSockObject *s, int writing, _PyTime_t timeout)

/* Wait until the socket becomes ready */
PySSL_BEGIN_ALLOW_THREADS
nfds = Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int);
nfds = _Py_DOWNCAST(s->sock_fd+1, SOCKET_T, int);
if (writing)
rc = select(nfds, NULL, &fds, NULL, &tv);
else
Expand Down
2 changes: 1 addition & 1 deletion Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset, char*
memcpy(res + 1, p, n);
if (n > 255)
n = 255;
*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
*res = _Py_DOWNCAST(n, Py_ssize_t, unsigned char);
} else {
if (e->pack(res, v, e) < 0) {
if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
Expand Down
2 changes: 1 addition & 1 deletion Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
Py_INCREF(tdo->values[i]);
}
/* len <= LINKCELLS < INT_MAX */
tdo->numread = Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
tdo->numread = _Py_DOWNCAST(len, Py_ssize_t, int);

if (len == LINKCELLS) {
if (next != Py_None) {
Expand Down
4 changes: 2 additions & 2 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3958,7 +3958,7 @@ os__getvolumepathname_impl(PyObject *module, path_t *path)

Py_BEGIN_ALLOW_THREADS
ret = GetVolumePathNameW(path->wide, mountpath,
Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
_Py_DOWNCAST(buflen, size_t, DWORD));
Py_END_ALLOW_THREADS

if (!ret) {
Expand Down Expand Up @@ -6189,7 +6189,7 @@ convert_sched_param(PyObject *param, struct sched_param *res)
PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
return 0;
}
res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
res->sched_priority = _Py_DOWNCAST(priority, long, int);
return 1;
}
#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Expand Down
4 changes: 2 additions & 2 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ trip_signal(int sig_num)
*/

#ifdef MS_WINDOWS
fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
fd = _Py_DOWNCAST(wakeup.fd, SOCKET_T, int);
#else
fd = wakeup.fd;
#endif
Expand Down Expand Up @@ -792,7 +792,7 @@ PySignal_SetWakeupFd(int fd)
fd = -1;

#ifdef MS_WINDOWS
old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
old_fd = _Py_DOWNCAST(wakeup.fd, SOCKET_T, int);
#else
old_fd = wakeup.fd;
#endif
Expand Down
4 changes: 2 additions & 2 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,10 @@ internal_select(PySocketSockObject *s, int writing, _PyTime_t interval,
/* See if the socket is ready */
Py_BEGIN_ALLOW_THREADS;
if (writing)
n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
n = select(_Py_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
NULL, &fds, &efds, tvp);
else
n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
n = select(_Py_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
&fds, NULL, &efds, tvp);
Py_END_ALLOW_THREADS;
#endif
Expand Down
2 changes: 1 addition & 1 deletion Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ find_syllable(const char *str, int *len, int *pos, int count, int column)
*len = -1;
for (i = 0; i < count; i++) {
const char *s = hangul_syllables[i][column];
len1 = Py_SAFE_DOWNCAST(strlen(s), size_t, int);
len1 = _Py_DOWNCAST(strlen(s), size_t, int);
if (len1 <= *len)
continue;
if (strncmp(str, s, len1) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,7 @@ PySequence_Contains(PyObject *seq, PyObject *ob)
if (sqm != NULL && sqm->sq_contains != NULL)
return (*sqm->sq_contains)(seq, ob);
result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
return _Py_DOWNCAST(result, Py_ssize_t, int);
}

/* Backwards compatibility */
Expand Down
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2443,7 +2443,7 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
i = -1;
Return:
Py_DECREF(it);
return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
return _Py_DOWNCAST(i, Py_ssize_t, int);
}

static int
Expand Down
Loading