diff --git a/Include/pyport.h b/Include/pyport.h index 32d98c59a7c134..ef586a850c2db5 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -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 diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-08-03-07-46-38.bpo-19692.TowfC5.rst b/Misc/NEWS.d/next/Core and Builtins/2019-08-03-07-46-38.bpo-19692.TowfC5.rst new file mode 100644 index 00000000000000..ba6d1af40344e5 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-08-03-07-46-38.bpo-19692.TowfC5.rst @@ -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. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index c1941c16400e11..9272850c37cfd2 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -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 */ @@ -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); @@ -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); @@ -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) { diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index 97463b599bc004..e18feefab86d47 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -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) { diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index bd67c6eeaeed30..3efc2df88650fb 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -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; } } @@ -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; diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index 235a4d79ad2ca1..816dfcf2ced7c0 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -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; diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index a806241897fcbd..f8909ea34d6734 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -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; diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 86dd277f94c1bf..ceae54a967d235 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -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); @@ -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); @@ -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; @@ -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; @@ -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); @@ -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); @@ -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); @@ -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 @@ -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; @@ -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. */ @@ -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) { @@ -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 */ diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 05911d9222ff6e..7107a5dd54585a 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -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; @@ -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: diff --git a/Modules/_sqlite/util.c b/Modules/_sqlite/util.c index 3fa671d052b0d8..806157423c2384 100644 --- a/Modules/_sqlite/util.c +++ b/Modules/_sqlite/util.c @@ -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 diff --git a/Modules/_ssl.c b/Modules/_ssl.c index da30cbb758e27e..c9f20994e6322a 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -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 @@ -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 diff --git a/Modules/_struct.c b/Modules/_struct.c index ba8f9cd8e966ab..3506264bf58293 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -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)) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 781d0cca54196d..26219fe70509d2 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -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) { diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 4f8c074a671670..bc7f1a30d4679b 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -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) { @@ -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) */ diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 95569b931d6020..3a37c7fa5851b2 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -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 @@ -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 diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f220c263631928..afecbff229e0b5 100755 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -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 diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index ae0d4e46f9a409..0a974949025bbc 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -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) { diff --git a/Objects/abstract.c b/Objects/abstract.c index db1c3064db6f1f..ec3590d099e22c 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -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 */ diff --git a/Objects/dictobject.c b/Objects/dictobject.c index b6205d93ca1408..250c863053e958 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -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 diff --git a/Objects/longobject.c b/Objects/longobject.c index 3978f5c4a16730..b44b8715f0aead 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -333,7 +333,7 @@ PyLong_FromLong(long ival) v = _PyLong_New(1); if (v) { Py_SIZE(v) = sign; - v->ob_digit[0] = Py_SAFE_DOWNCAST( + v->ob_digit[0] = _Py_DOWNCAST( abs_ival, unsigned long, digit); } return (PyObject*)v; @@ -345,9 +345,9 @@ PyLong_FromLong(long ival) v = _PyLong_New(2); if (v) { Py_SIZE(v) = 2*sign; - v->ob_digit[0] = Py_SAFE_DOWNCAST( + v->ob_digit[0] = _Py_DOWNCAST( abs_ival & PyLong_MASK, unsigned long, digit); - v->ob_digit[1] = Py_SAFE_DOWNCAST( + v->ob_digit[1] = _Py_DOWNCAST( abs_ival >> PyLong_SHIFT, unsigned long, digit); } return (PyObject*)v; @@ -366,7 +366,7 @@ PyLong_FromLong(long ival) Py_SIZE(v) = ndigits*sign; t = abs_ival; while (t) { - *p++ = Py_SAFE_DOWNCAST( + *p++ = _Py_DOWNCAST( t & PyLong_MASK, unsigned long, digit); t >>= PyLong_SHIFT; } @@ -1524,7 +1524,7 @@ _PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr) return 0; } - *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); + *(unsigned short *)ptr = _Py_DOWNCAST(uval, unsigned long, unsigned short); return 1; } @@ -1546,7 +1546,7 @@ _PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr) return 0; } - *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int); + *(unsigned int *)ptr = _Py_DOWNCAST(uval, unsigned long, unsigned int); return 1; } diff --git a/Objects/object.c b/Objects/object.c index 7f2c23a9ff8c57..68540797ec7de4 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1460,7 +1460,7 @@ PyObject_IsTrue(PyObject *v) else return 1; /* if it is negative, it should be either -1 or -2 */ - return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); + return (res > 0) ? 1 : _Py_DOWNCAST(res, Py_ssize_t, int); } /* equivalent of 'not v' diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5545eae79505a3..5ae98b67912e1f 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7389,7 +7389,7 @@ decode_code_page_errors(UINT code_page, assert(out - *buf <= *bufsize); *bufsize = out - *buf; /* (in - startin) <= size and size is an int */ - ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int); + ret = _Py_DOWNCAST(in - startin, Py_ssize_t, int); error: Py_XDECREF(encoding_obj); diff --git a/PC/winreg.c b/PC/winreg.c index d0df7ef0ad47f4..40765e0843b55f 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -592,7 +592,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len); if (*retDataBuf == NULL) return FALSE; - *retDataSize = Py_SAFE_DOWNCAST( + *retDataSize = _Py_DOWNCAST( (len + 1) * sizeof(wchar_t), Py_ssize_t, DWORD); } @@ -631,7 +631,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) wstr = PyUnicode_AsUnicodeAndSize(t, &len); if (wstr == NULL) return FALSE; - size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t), + size += _Py_DOWNCAST((len + 1) * sizeof(wchar_t), size_t, DWORD); } @@ -688,7 +688,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) PyErr_NoMemory(); return FALSE; } - *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD); + *retDataSize = _Py_DOWNCAST(view.len, Py_ssize_t, DWORD); memcpy(*retDataBuf, view.buf, view.len); PyBuffer_Release(&view); } diff --git a/Parser/parsetok.c b/Parser/parsetok.c index a5d78974b871b3..836cfe3d6a852a 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -320,7 +320,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, lineno = type == STRING ? tok->first_lineno : tok->lineno; line_start = type == STRING ? tok->multi_line_start : tok->line_start; if (a != NULL && a >= line_start) { - col_offset = Py_SAFE_DOWNCAST(a - line_start, + col_offset = _Py_DOWNCAST(a - line_start, intptr_t, int); } else { @@ -328,7 +328,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, } if (b != NULL && b >= tok->line_start) { - end_col_offset = Py_SAFE_DOWNCAST(b - tok->line_start, + end_col_offset = _Py_DOWNCAST(b - tok->line_start, intptr_t, int); } else { diff --git a/Python/compile.c b/Python/compile.c index d2de7a72a811ff..1e9610a14e55ca 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1404,7 +1404,7 @@ compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) return 0; i = &c->u->u_curblock->b_instr[off]; i->i_opcode = opcode; - i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); + i->i_oparg = _Py_DOWNCAST(oparg, Py_ssize_t, int); compiler_set_lineno(c, off); return 1; } @@ -5846,7 +5846,7 @@ makecode(struct compiler *c, struct assembler *a) nlocals = PyDict_GET_SIZE(c->u->u_varnames); assert(nlocals < INT_MAX); - nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); + nlocals_int = _Py_DOWNCAST(nlocals, Py_ssize_t, int); flags = compute_code_flags(c); if (flags < 0) @@ -5865,9 +5865,9 @@ makecode(struct compiler *c, struct assembler *a) goto error; } - posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); - posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); - kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); + posonlyargcount = _Py_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); + posorkeywordargcount = _Py_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); + kwonlyargcount = _Py_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); maxdepth = stackdepth(c); if (maxdepth < 0) { goto error; diff --git a/Python/fileutils.c b/Python/fileutils.c index 55bc1940aeb886..143f02b6ce3e8b 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -832,7 +832,7 @@ FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) __int64 in; memcpy(&in, in_ptr, sizeof(in)); *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ - *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); + *time_out = _Py_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); } void diff --git a/Python/marshal.c b/Python/marshal.c index cb11c8c74ef3a6..b3a6d6de7518f9 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -203,7 +203,7 @@ w_pstring(const char *s, Py_ssize_t n, WFILE *p) static void w_short_pstring(const char *s, Py_ssize_t n, WFILE *p) { - w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p); + w_byte(_Py_DOWNCAST(n, Py_ssize_t, unsigned char), p); w_string(s, n, p); } diff --git a/Python/pystrtod.c b/Python/pystrtod.c index 4aa99d546caf9e..319ee91581531e 100644 --- a/Python/pystrtod.c +++ b/Python/pystrtod.c @@ -614,7 +614,7 @@ ensure_decimal_point(char* buffer, size_t buf_size, int precision) digits_start = p; while (*p && Py_ISDIGIT(*p)) ++p; - digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int); + digit_count = _Py_DOWNCAST(p - digits_start, Py_ssize_t, int); if (*p == '.') { if (Py_ISDIGIT(*(p+1))) { @@ -681,7 +681,7 @@ ensure_decimal_point(char* buffer, size_t buf_size, int precision) repr/str builtins and those never want an upper case 'E' */ written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1); if (!(0 <= written && - written < Py_SAFE_DOWNCAST(buf_avail, size_t, int))) + written < _Py_DOWNCAST(buf_avail, size_t, int))) /* output truncated, or something else bad happened */ return NULL; remove_trailing_zeros(buffer); diff --git a/Python/thread_nt.h b/Python/thread_nt.h index a5246dd0504dbb..650cc2d3326943 100644 --- a/Python/thread_nt.h +++ b/Python/thread_nt.h @@ -197,7 +197,7 @@ PyThread_start_new_thread(void (*func)(void *), void *arg) PyThreadState *tstate = _PyThreadState_GET(); size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0; hThread = (HANDLE)_beginthreadex(0, - Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int), + _Py_DOWNCAST(stacksize, Py_ssize_t, unsigned int), bootstrap, obj, 0, &threadID); if (hThread == 0) {