Skip to content

Commit 42dc1c4

Browse files
authored
Replace unsafe PY_*_VERSION comparisons to fix for Python 4.0 (#14280)
`#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7` For example, this is true for Python 3.7-3.11 but also true for Python 4.7-4.11. Instead, `PY_VERSION_HEX` should be used. https://docs.python.org/3.11/c-api/apiabiversion.html ```c /* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2. Use this for numeric comparisons, e.g. #if PY_VERSION_HEX >= ... */ ``` https://github.com/python/cpython/blob/2e279e85fece187b6058718ac7e82d1692461e26/Include/patchlevel.h#L29-L30 Remove compatibility code that only applied to EOL and unsupported Python versions (<= 3.6)
1 parent b0003af commit 42dc1c4

File tree

5 files changed

+7
-51
lines changed

5 files changed

+7
-51
lines changed

mypy/stubtest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,9 +1533,6 @@ def get_typeshed_stdlib_modules(
15331533
stdlib_py_versions = mypy.modulefinder.load_stdlib_py_versions(custom_typeshed_dir)
15341534
if version_info is None:
15351535
version_info = sys.version_info[0:2]
1536-
# Typeshed's minimum supported Python 3 is Python 3.7
1537-
if sys.version_info < (3, 7):
1538-
version_info = (3, 7)
15391536

15401537
def exists_in_version(module: str) -> bool:
15411538
assert version_info is not None

mypyc/common.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@
4444

4545
PLATFORM_SIZE = 4 if IS_32_BIT_PLATFORM else 8
4646

47-
# Python 3.5 on macOS uses a hybrid 32/64-bit build that requires some workarounds.
48-
# The same generated C will be compiled in both 32 and 64 bit modes when building mypy
49-
# wheels (for an unknown reason).
50-
#
51-
# Note that we use "in ['darwin']" because of https://github.com/mypyc/mypyc/issues/761.
52-
IS_MIXED_32_64_BIT_BUILD: Final = sys.platform in ["darwin"] and sys.version_info < (3, 6)
53-
5447
# Maximum value for a short tagged integer.
5548
MAX_SHORT_INT: Final = 2 ** (8 * int(SIZEOF_SIZE_T) - 2) - 1
5649

@@ -59,9 +52,8 @@
5952

6053
# Maximum value for a short tagged integer represented as a C integer literal.
6154
#
62-
# Note: Assume that the compiled code uses the same bit width as mypyc, except for
63-
# Python 3.5 on macOS.
64-
MAX_LITERAL_SHORT_INT: Final = MAX_SHORT_INT if not IS_MIXED_32_64_BIT_BUILD else 2**30 - 1
55+
# Note: Assume that the compiled code uses the same bit width as mypyc
56+
MAX_LITERAL_SHORT_INT: Final = MAX_SHORT_INT
6557
MIN_LITERAL_SHORT_INT: Final = -MAX_LITERAL_SHORT_INT - 1
6658

6759
# Decription of the C type used to track the definedness of attributes and

mypyc/lib-rt/CPy.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,8 @@ static inline bool CPy_KeepPropagating(void) {
499499
}
500500
// We want to avoid the public PyErr_GetExcInfo API for these because
501501
// it requires a bunch of spurious refcount traffic on the parts of
502-
// the triple we don't care about. Unfortunately the layout of the
503-
// data structure changed in 3.7 so we need to handle that.
504-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
502+
// the triple we don't care about.
505503
#define CPy_ExcState() PyThreadState_GET()->exc_info
506-
#else
507-
#define CPy_ExcState() PyThreadState_GET()
508-
#endif
509504

510505
void CPy_Raise(PyObject *exc);
511506
void CPy_Reraise(void);
@@ -527,7 +522,7 @@ void CPy_AttributeError(const char *filename, const char *funcname, const char *
527522

528523
// Misc operations
529524

530-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8
525+
#if PY_VERSION_HEX >= 0x03080000
531526
#define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc)
532527
#define CPy_TRASHCAN_END(op) Py_TRASHCAN_END
533528
#else

mypyc/lib-rt/getargsfast.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include <Python.h>
1919
#include "CPy.h"
2020

21-
/* None of this is supported on Python 3.6 or earlier */
22-
#if PY_VERSION_HEX >= 0x03070000
23-
2421
#define PARSER_INITED(parser) ((parser)->kwtuple != NULL)
2522

2623
/* Forward */
@@ -570,5 +567,3 @@ skipitem_fast(const char **p_format, va_list *p_va)
570567

571568
*p_format = format;
572569
}
573-
574-
#endif

mypyc/lib-rt/pythonsupport.h

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ extern "C" {
2222

2323
/////////////////////////////////////////
2424
// Adapted from bltinmodule.c in Python 3.7.0
25-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
2625
_Py_IDENTIFIER(__mro_entries__);
2726
static PyObject*
2827
update_bases(PyObject *bases)
@@ -96,16 +95,8 @@ update_bases(PyObject *bases)
9695
Py_XDECREF(new_bases);
9796
return NULL;
9897
}
99-
#else
100-
static PyObject*
101-
update_bases(PyObject *bases)
102-
{
103-
return bases;
104-
}
105-
#endif
10698

10799
// From Python 3.7's typeobject.c
108-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 6
109100
_Py_IDENTIFIER(__init_subclass__);
110101
static int
111102
init_subclass(PyTypeObject *type, PyObject *kwds)
@@ -134,14 +125,6 @@ init_subclass(PyTypeObject *type, PyObject *kwds)
134125
return 0;
135126
}
136127

137-
#else
138-
static int
139-
init_subclass(PyTypeObject *type, PyObject *kwds)
140-
{
141-
return 0;
142-
}
143-
#endif
144-
145128
// Adapted from longobject.c in Python 3.7.0
146129

147130
/* This function adapted from PyLong_AsLongLongAndOverflow, but with
@@ -306,7 +289,7 @@ list_count(PyListObject *self, PyObject *value)
306289
return CPyTagged_ShortFromSsize_t(count);
307290
}
308291

309-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION < 8
292+
#if PY_VERSION_HEX < 0x03080000
310293
static PyObject *
311294
_PyDict_GetItemStringWithError(PyObject *v, const char *key)
312295
{
@@ -321,13 +304,7 @@ _PyDict_GetItemStringWithError(PyObject *v, const char *key)
321304
}
322305
#endif
323306

324-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION < 6
325-
/* _PyUnicode_EqualToASCIIString got added in 3.5.3 (argh!) so we can't actually know
326-
* whether it will be present at runtime, so we just assume we don't have it in 3.5. */
327-
#define CPyUnicode_EqualToASCIIString(x, y) (PyUnicode_CompareWithASCIIString((x), (y)) == 0)
328-
#elif PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 6
329307
#define CPyUnicode_EqualToASCIIString(x, y) _PyUnicode_EqualToASCIIString(x, y)
330-
#endif
331308

332309
// Adapted from genobject.c in Python 3.7.2
333310
// Copied because it wasn't in 3.5.2 and it is undocumented anyways.
@@ -390,7 +367,7 @@ _CPyDictView_New(PyObject *dict, PyTypeObject *type)
390367
}
391368
#endif
392369

393-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >=10
370+
#if PY_VERSION_HEX >= 0x030A0000 // 3.10
394371
static int
395372
_CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
396373
PyObject *tmp = NULL;
@@ -404,7 +381,7 @@ _CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
404381
#define _CPyObject_HasAttrId _PyObject_HasAttrId
405382
#endif
406383

407-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION < 9
384+
#if PY_VERSION_HEX < 0x03090000
408385
// OneArgs and NoArgs functions got added in 3.9
409386
#define _PyObject_CallMethodIdNoArgs(self, name) \
410387
_PyObject_CallMethodIdObjArgs((self), (name), NULL)

0 commit comments

Comments
 (0)