Skip to content

Commit 6d642a6

Browse files
dreissfacebook-github-bot
authored andcommitted
Remove (most) Python 2 support from C++ code (pytorch#35614)
Summary: Pull Request resolved: pytorch#35614 Python 2 has reached end-of-life and is no longer supported by PyTorch. Now we can clean up a lot of cruft that we put in place to support it. These changes were all done manually, and I skipped anything that seemed like it would take more than a few seconds, so I think it makes sense to review it manually as well. Test Plan: CI Differential Revision: D20842876 Pulled By: dreiss fbshipit-source-id: 18abf0d324ed2185ec6d27c864e935d856dcc6ad
1 parent 1b973aa commit 6d642a6

15 files changed

+3
-157
lines changed

tools/autograd/templates/python_nn_functions.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ static PyMethodDef nn_functions[] = {
6666
static PyObject* THPNNVariableFunctionsModule = NULL;
6767

6868
void initNNFunctions(PyObject* module) {
69-
#if PY_MAJOR_VERSION == 2
70-
PyObject* nn = Py_InitModule("torch._C._nn", nn_functions);
71-
Py_XINCREF(nn); // Py_InitModule returns "borrowed" reference
72-
#else
7369
static struct PyModuleDef def = {
7470
PyModuleDef_HEAD_INIT,
7571
"torch._C._nn",
@@ -78,7 +74,6 @@ void initNNFunctions(PyObject* module) {
7874
nn_functions
7975
};
8076
PyObject* nn = PyModule_Create(&def);
81-
#endif
8277
THPNNVariableFunctionsModule = nn;
8378
if (!nn) {
8479
throw python_error();

torch/csrc/Dtype.h

-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ inline bool THPDtype_Check(PyObject *obj) {
2121
inline bool THPPythonScalarType_Check(PyObject *obj) {
2222
return obj == (PyObject*)(&PyFloat_Type) ||
2323
obj == (PyObject*)(&PyBool_Type) ||
24-
#if PY_MAJOR_VERSION == 2
25-
obj == (PyObject*)(&PyInt_Type) ||
26-
#endif
2724
obj == (PyObject*)(&PyLong_Type);
2825
}
2926

torch/csrc/Module.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,6 @@ PyObject* initModule() {
650650
#endif
651651
#endif
652652

653-
#if PY_MAJOR_VERSION == 2
654-
ASSERT_TRUE(module = Py_InitModule("torch._C", methods.data()));
655-
#else
656653
static struct PyModuleDef torchmodule = {
657654
PyModuleDef_HEAD_INIT,
658655
"torch._C",
@@ -661,7 +658,6 @@ PyObject* initModule() {
661658
methods.data()
662659
};
663660
ASSERT_TRUE(module = PyModule_Create(&torchmodule));
664-
#endif
665661
ASSERT_TRUE(THPWrapper_init(module));
666662
ASSERT_TRUE(THPGenerator_init(module));
667663
ASSERT_TRUE(THPException_init(module));

torch/csrc/Size.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ static PyObject* wrap_tuple_fn(Args ... args)
109109
namespace {
110110
auto sq_concat = PyTuple_Type.tp_as_sequence->sq_concat;
111111
auto sq_repeat = PyTuple_Type.tp_as_sequence->sq_repeat;
112-
#if PY_MAJOR_VERSION == 2
113-
auto sq_slice = PyTuple_Type.tp_as_sequence->sq_slice;
114-
#endif
115112
binaryfunc mp_subscript = PyTuple_Type.tp_as_mapping->mp_subscript;
116113
}
117114

@@ -121,11 +118,7 @@ static PySequenceMethods THPSize_as_sequence = {
121118
wrap_tuple_fn<decltype(&sq_concat), &sq_concat>,
122119
wrap_tuple_fn<decltype(&sq_repeat), &sq_repeat>,
123120
nullptr, /* sq_item */
124-
#if PY_MAJOR_VERSION == 2
125-
wrap_tuple_fn<decltype(&sq_slice), &sq_slice>,
126-
#else
127121
nullptr, /* sq_slice */
128-
#endif
129122
nullptr, /* sq_ass_item */
130123
nullptr, /* sq_ass_slice */
131124
nullptr /* sq_contains */

torch/csrc/api/include/torch/python.h

-5
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ void bind_cpp_module_wrapper(
6767

6868
// `type()` always needs a `str`, but pybind11's `str()` method always creates
6969
// a `unicode` object.
70-
#if PY_MAJOR_VERSION < 3
71-
py::object name_str =
72-
py::reinterpret_steal<py::object>(PyString_FromString(name));
73-
#else
7470
py::object name_str = py::str(name);
75-
#endif
7671

7772
// Dynamically create the subclass of `ModuleWrapper`, which is a subclass of
7873
// `torch.nn.Module`, and will delegate all calls to the C++ module we're

torch/csrc/dl.c

-17
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,25 @@ static PyMethodDef TorchDlMethods[] = {
77
{NULL, NULL, 0, NULL}
88
};
99

10-
#if PY_MAJOR_VERSION != 2
1110
static struct PyModuleDef torchdlmodule = {
1211
PyModuleDef_HEAD_INIT,
1312
"torch._dl",
1413
NULL,
1514
-1,
1615
TorchDlMethods
1716
};
18-
#endif
1917

20-
#if PY_MAJOR_VERSION == 2
21-
PyMODINIT_FUNC init_dl(void)
22-
#else
2318
PyMODINIT_FUNC PyInit__dl(void)
24-
#endif
2519
{
2620

27-
#if PY_MAJOR_VERSION == 2
28-
#define ASSERT_TRUE(cmd) if (!(cmd)) {PyErr_SetString(PyExc_ImportError, "initialization error"); return;}
29-
#else
3021
#define ASSERT_TRUE(cmd) if (!(cmd)) return NULL
31-
#endif
3222

33-
#if PY_MAJOR_VERSION == 2
34-
ASSERT_TRUE(module = Py_InitModule("torch._dl", TorchDlMethods));
35-
#else
3623
ASSERT_TRUE(module = PyModule_Create(&torchdlmodule));
37-
#endif
3824
ASSERT_TRUE(PyModule_AddIntConstant(module, "RTLD_GLOBAL", (int64_t) RTLD_GLOBAL) == 0);
3925
ASSERT_TRUE(PyModule_AddIntConstant(module, "RTLD_NOW", (int64_t) RTLD_NOW) == 0);
4026
ASSERT_TRUE(PyModule_AddIntConstant(module, "RTLD_LAZY", (int64_t) RTLD_LAZY) == 0);
4127

42-
#if PY_MAJOR_VERSION == 2
43-
#else
4428
return module;
45-
#endif
4629

4730
#undef ASSERT_TRUE
4831
}

torch/csrc/jit/python/init.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,8 @@ void initJITBindings(PyObject* module) {
673673
}
674674

675675
THPObjectPtr getMemview(void* buf, size_t n) const {
676-
#if PY_MAJOR_VERSION >= 3
677676
THPObjectPtr memview(PyMemoryView_FromMemory(
678677
reinterpret_cast<char*>(buf), n, PyBUF_WRITE));
679-
#else
680-
THPObjectPtr memview(PyBuffer_FromReadWriteMemory(buf, n));
681-
#endif
682678
if (!memview) {
683679
throw python_error();
684680
}

torch/csrc/jit/python/python_sugared_value.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,7 @@ std::shared_ptr<SugaredValue> toSugaredValue(
701701
return SpecialFormValue::create(prim::annotate);
702702
#ifdef USE_DISTRIBUTED
703703
} else if (
704-
// RPC module is only avaialble for Python3
705-
// when build flag "USE_DISTRIBUTED" is on.
706-
!py::module::import("torch._six").attr("PY2").cast<bool>() &&
704+
// RPC module is only avaialble when build flag "USE_DISTRIBUTED" is on.
707705
obj.ptr() ==
708706
py::module::import("torch.distributed.rpc").attr("rpc_async").ptr()) {
709707
return SpecialFormValue::create(prim::rpc_async);

torch/csrc/python_headers.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
// workaround for Python 2 issue: https://bugs.python.org/issue17120
4+
// NOTE: It looks like this affects Python 3 as well.
45
#pragma push_macro("_XOPEN_SOURCE")
56
#pragma push_macro("_POSIX_C_SOURCE")
67
#undef _XOPEN_SOURCE

torch/csrc/serialization.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,8 @@ static inline ssize_t doPartialPythonReadBuffered(PyObject* fildes, void* buf, s
6161
THPObjectPtr r(PyObject_CallMethod(fildes, "read", "i", nbytes));
6262
if (!r) throw python_error();
6363

64-
// read output is String (Python 2) / Bytes (Python 3)
65-
#if PY_MAJOR_VERSION >= 3
6664
auto size = PyBytes_GET_SIZE(r.get());
6765
const void* py_buf = PyBytes_AsString(r.get());
68-
#else
69-
auto size = PyString_GET_SIZE(r.get());
70-
const void* py_buf = PyString_AsString(r.get());
71-
#endif
7266

7367
// we read EOF
7468
if (size == 0) {
@@ -83,13 +77,9 @@ static inline ssize_t doPartialPythonReadBuffered(PyObject* fildes, void* buf, s
8377

8478
// Either does fildes.readinto(buf) or fildes.write(buf)
8579
static inline ssize_t doPartialPythonIO(PyObject* fildes, void* buf, size_t nbytes, bool is_read) {
86-
#if PY_MAJOR_VERSION >= 3
8780
auto rw_flag = is_read ? PyBUF_WRITE : PyBUF_READ;
8881
THPObjectPtr memview(PyMemoryView_FromMemory(
8982
reinterpret_cast<char*>(buf), nbytes, rw_flag));
90-
#else
91-
THPObjectPtr memview(PyBuffer_FromReadWriteMemory(buf, nbytes));
92-
#endif
9383
if (!memview) throw python_error();
9484

9585
char* method = "write";

torch/csrc/stub.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ __declspec(dllimport)
55
#endif
66
extern PyObject* initModule();
77

8-
#if PY_MAJOR_VERSION == 2
9-
PyMODINIT_FUNC init_C()
10-
{
11-
initModule();
12-
}
13-
#else
148
PyMODINIT_FUNC PyInit__C()
159
{
1610
return initModule();
1711
}
18-
#endif

torch/csrc/utils.h

-20
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,6 @@
2424
#define THP_EXPECT(x, y) (x)
2525
#endif
2626

27-
#if PY_MAJOR_VERSION == 2
28-
#define THPUtils_checkReal_FLOAT(object) \
29-
(PyFloat_Check(object) || PyLong_Check(object) || PyInt_Check(object))
30-
31-
#define THPUtils_unpackReal_FLOAT(object) \
32-
(PyFloat_Check(object) ? PyFloat_AsDouble(object) : \
33-
PyLong_Check(object) ? PyLong_AsLongLong(object) : \
34-
PyInt_Check(object) ? PyInt_AsLong(object) : \
35-
(throw std::runtime_error("Could not parse real"), 0))
36-
37-
#define THPUtils_checkReal_INT(object) \
38-
(PyLong_Check(object) || PyInt_Check(object))
39-
40-
#define THPUtils_unpackReal_INT(object) \
41-
(PyLong_Check(object) ? PyLong_AsLongLong(object) : \
42-
PyInt_Check(object) ? PyInt_AsLong(object) : \
43-
(throw std::runtime_error("Could not parse real"), 0))
44-
#else /* PY_MAJOR_VERSION == 2 */
4527
#define THPUtils_checkReal_FLOAT(object) \
4628
(PyFloat_Check(object) || PyLong_Check(object))
4729

@@ -56,7 +38,6 @@
5638
#define THPUtils_unpackReal_INT(object) \
5739
(PyLong_Check(object) ? PyLong_AsLongLong(object) : \
5840
(throw std::runtime_error("Could not parse real"), 0))
59-
#endif
6041

6142
#define THPUtils_unpackReal_BOOL(object) \
6243
(PyBool_Check(object) ? object : \
@@ -76,7 +57,6 @@
7657
PyComplex_Check(object) || PyFloat_Check(object) || PyLong_Check(object) || PyInt_Check(object)
7758

7859
#define THPUtils_newReal_FLOAT(value) PyFloat_FromDouble(value)
79-
// TODO: handle int overflows for py2
8060
#define THPUtils_newReal_INT(value) PyInt_FromLong(value)
8161

8262
#define THPUtils_newReal_BOOL(value) PyBool_FromLong(value)

torch/csrc/utils/python_arg_parser.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,7 @@ inline at::ScalarType PythonArgs::scalartype(int i) {
355355
if (obj == (PyObject*)&PyBool_Type) {
356356
return at::ScalarType::Bool;
357357
}
358-
if (obj == (PyObject*)&PyLong_Type
359-
#if PY_MAJOR_VERSION == 2
360-
|| obj == (PyObject*)&PyInt_Type
361-
#endif
362-
) {
358+
if (obj == (PyObject*)&PyLong_Type) {
363359
return at::ScalarType::Long;
364360
}
365361
return reinterpret_cast<THPDtype*>(obj)->scalar_type;
@@ -624,10 +620,6 @@ static bool _is_basic_python_type(PyTypeObject *tp)
624620
tp == &PyUnicode_Type ||
625621
tp == &PyBytes_Type ||
626622

627-
#if PY_MAJOR_VERSION == 2
628-
tp == &PyString_Type ||
629-
#endif
630-
631623
/* other builtins */
632624
tp == &PySlice_Type ||
633625
tp == Py_TYPE(Py_None) ||

torch/csrc/utils/python_numbers.h

-34
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,14 @@
1212
const int64_t DOUBLE_INT_MAX = 9007199254740992;
1313

1414
inline PyObject* THPUtils_packInt64(int64_t value) {
15-
#if PY_MAJOR_VERSION == 2
16-
if (sizeof(long) == sizeof(int64_t)) {
17-
return PyInt_FromLong(static_cast<long>(value));
18-
} else if (value <= INT32_MAX && value >= INT32_MIN) {
19-
return PyInt_FromLong(static_cast<long>(value));
20-
}
21-
#endif
2215
return PyLong_FromLongLong(value);
2316
}
2417

2518
inline PyObject* THPUtils_packUInt64(uint64_t value) {
26-
#if PY_MAJOR_VERSION == 2
27-
if (value <= INT32_MAX) {
28-
return PyInt_FromLong(static_cast<long>(value));
29-
}
30-
#endif
3119
return PyLong_FromUnsignedLongLong(value);
3220
}
3321

3422
inline PyObject* THPUtils_packDoubleAsInt(double value) {
35-
#if PY_MAJOR_VERSION == 2
36-
if (value <= INT32_MAX && value >= INT32_MIN) {
37-
return PyInt_FromLong(static_cast<long>(value));
38-
}
39-
#endif
4023
return PyLong_FromDouble(value);
4124
}
4225

@@ -47,11 +30,7 @@ inline bool THPUtils_checkLong(PyObject* obj) {
4730
}
4831
#endif
4932

50-
#if PY_MAJOR_VERSION == 2
51-
return (PyLong_Check(obj) || PyInt_Check(obj)) && !PyBool_Check(obj);
52-
#else
5333
return PyLong_Check(obj) && !PyBool_Check(obj);
54-
#endif
5534
}
5635

5736
inline int64_t THPUtils_unpackLong(PyObject* obj) {
@@ -111,11 +90,7 @@ inline bool THPUtils_checkDouble(PyObject* obj) {
11190
return true;
11291
}
11392
#endif
114-
#if PY_MAJOR_VERSION == 2
115-
return PyFloat_Check(obj) || PyLong_Check(obj) || PyInt_Check(obj);
116-
#else
11793
return PyFloat_Check(obj) || PyLong_Check(obj);
118-
#endif
11994
}
12095

12196
inline bool THPUtils_checkScalar(PyObject* obj) {
@@ -124,11 +99,7 @@ inline bool THPUtils_checkScalar(PyObject* obj) {
12499
return true;
125100
}
126101
#endif
127-
#if PY_MAJOR_VERSION == 2
128-
return PyFloat_Check(obj) || PyLong_Check(obj) || PyInt_Check(obj) || PyComplex_Check(obj);
129-
#else
130102
return PyFloat_Check(obj) || PyLong_Check(obj) || PyComplex_Check(obj);
131-
#endif
132103
}
133104

134105
inline double THPUtils_unpackDouble(PyObject* obj) {
@@ -146,11 +117,6 @@ inline double THPUtils_unpackDouble(PyObject* obj) {
146117
}
147118
return (double)value;
148119
}
149-
#if PY_MAJOR_VERSION == 2
150-
if (PyInt_Check(obj)) {
151-
return (double)PyInt_AS_LONG(obj);
152-
}
153-
#endif
154120
double value = PyFloat_AsDouble(obj);
155121
if (value == -1 && PyErr_Occurred()) {
156122
throw python_error();

0 commit comments

Comments
 (0)