Skip to content

Commit 5008d15

Browse files
cyyeverpytorchmergebot
authored andcommitted
[2/N] Remove usage of C array (pytorch#139589)
Follows pytorch#139567 Pull Request resolved: pytorch#139589 Approved by: https://github.com/ezyang
1 parent c92de3b commit 5008d15

File tree

5 files changed

+37
-48
lines changed

5 files changed

+37
-48
lines changed

torch/csrc/Device.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,12 @@ typedef PyObject* (*getter)(PyObject*, void*);
219219

220220
// NB: If you edit these properties/methods, update torch/_C/__init__.pyi.in
221221

222-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,modernize-avoid-c-arrays)
223-
static struct PyGetSetDef THPDevice_properties[] = {
222+
static const std::initializer_list<PyGetSetDef> THPDevice_properties = {
224223
{"type", (getter)THPDevice_type, nullptr, nullptr, nullptr},
225224
{"index", (getter)THPDevice_index, nullptr, nullptr, nullptr},
226225
{nullptr}};
227226

228-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,modernize-avoid-c-arrays)
229-
static PyMethodDef THPDevice_methods[] = {
227+
static const std::initializer_list<PyMethodDef> THPDevice_methods = {
230228
{"__reduce__", THPDevice_reduce, METH_NOARGS, nullptr},
231229
{"__enter__", THPDevice_enter, METH_NOARGS, nullptr},
232230
{"__exit__", THPDevice_exit, METH_VARARGS, nullptr},
@@ -266,9 +264,11 @@ PyTypeObject THPDeviceType = {
266264
0, /* tp_weaklistoffset */
267265
nullptr, /* tp_iter */
268266
nullptr, /* tp_iternext */
269-
THPDevice_methods, /* tp_methods */
267+
// NOLINTNEXTLINE(*const-cast)
268+
const_cast<PyMethodDef*>(std::data(THPDevice_methods)), /* tp_methods */
270269
nullptr, /* tp_members */
271-
THPDevice_properties, /* tp_getset */
270+
// NOLINTNEXTLINE(*const-cast)
271+
const_cast<PyGetSetDef*>(std::data(THPDevice_properties)), /* tp_getset */
272272
nullptr, /* tp_base */
273273
nullptr, /* tp_dict */
274274
nullptr, /* tp_descr_get */

torch/csrc/Dtype.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ static PyObject* THPDtype_to_complex(PyObject* _self, PyObject* noargs) {
9898

9999
typedef PyObject* (*getter)(PyObject*, void*);
100100

101-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,modernize-avoid-c-arrays)
102-
static struct PyGetSetDef THPDtype_properties[] = {
101+
static const std::initializer_list<PyGetSetDef> THPDtype_properties = {
103102
{"is_floating_point",
104103
(getter)THPDtype_is_floating_point,
105104
nullptr,
@@ -110,8 +109,7 @@ static struct PyGetSetDef THPDtype_properties[] = {
110109
{"itemsize", (getter)THPDtype_itemsize, nullptr, nullptr, nullptr},
111110
{nullptr}};
112111

113-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,modernize-avoid-c-arrays)
114-
static PyMethodDef THPDtype_methods[] = {
112+
static const std::initializer_list<PyMethodDef> THPDtype_methods = {
115113
{"__reduce__", THPDtype_reduce, METH_NOARGS, nullptr},
116114
{"to_real", THPDtype_to_real, METH_NOARGS, nullptr},
117115
{"to_complex", THPDtype_to_complex, METH_NOARGS, nullptr},
@@ -150,9 +148,11 @@ PyTypeObject THPDtypeType = {
150148
0, /* tp_weaklistoffset */
151149
nullptr, /* tp_iter */
152150
nullptr, /* tp_iternext */
153-
THPDtype_methods, /* tp_methods */
151+
// NOLINTNEXTLINE(*const-cast)
152+
const_cast<PyMethodDef*>(std::data(THPDtype_methods)), /* tp_methods */
154153
nullptr, /* tp_members */
155-
THPDtype_properties, /* tp_getset */
154+
// NOLINTNEXTLINE(*const-cast)
155+
const_cast<PyGetSetDef*>(std::data(THPDtype_properties)), /* tp_getset */
156156
nullptr, /* tp_base */
157157
nullptr, /* tp_dict */
158158
nullptr, /* tp_descr_get */

torch/csrc/Exceptions.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,14 @@ std::string processErrorMsg(std::string str) {
204204
}
205205

206206
static std::string formatMessage(const char* format, va_list fmt_args) {
207-
static const size_t ERROR_BUF_SIZE = 1024;
208-
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
209-
char error_buf[ERROR_BUF_SIZE];
210-
vsnprintf(error_buf, ERROR_BUF_SIZE, format, fmt_args);
211-
212-
// Ensure that the string is null terminated
213-
error_buf[sizeof(error_buf) / sizeof(*error_buf) - 1] = 0;
214-
215-
return std::string(error_buf);
207+
constexpr size_t ERROR_BUF_SIZE = 1024;
208+
std::string error_buf(ERROR_BUF_SIZE, '\0');
209+
auto res = vsnprintf(error_buf.data(), ERROR_BUF_SIZE, format, fmt_args);
210+
if (res < 0) {
211+
res = 0;
212+
}
213+
error_buf.resize(res);
214+
return error_buf;
216215
}
217216

218217
void translate_exception_to_python(const std::exception_ptr& e_ptr) {

torch/csrc/Stream.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ static PyObject* THPStream_richcompare(
289289
return result;
290290
}
291291

292-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables)
293-
static struct PyMemberDef THPStream_members[] = {
292+
static const std::initializer_list<PyMemberDef> THPStream_members = {
294293
{"stream_id",
295294
T_LONGLONG,
296295
offsetof(THPStream, stream_id),
@@ -308,13 +307,11 @@ static struct PyMemberDef THPStream_members[] = {
308307
nullptr},
309308
{nullptr}};
310309

311-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables)
312-
static struct PyGetSetDef THPStream_properties[] = {
310+
static const std::initializer_list<PyGetSetDef> THPStream_properties = {
313311
{"device", (getter)THPStream_get_device, nullptr, nullptr, nullptr},
314312
{nullptr}};
315313

316-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables)
317-
static PyMethodDef THPStream_methods[] = {
314+
static const std::initializer_list<PyMethodDef> THPStream_methods = {
318315
{"query", THPStream_query, METH_NOARGS, nullptr},
319316
{"synchronize", THPStream_synchronize, METH_NOARGS, nullptr},
320317
{"wait_event", THPStream_wait_event, METH_O, nullptr},
@@ -355,9 +352,12 @@ static PyTypeObject THPStreamType = {
355352
0, /* tp_weaklistoffset */
356353
nullptr, /* tp_iter */
357354
nullptr, /* tp_iternext */
358-
THPStream_methods, /* tp_methods */
359-
THPStream_members, /* tp_members */
360-
THPStream_properties, /* tp_getset */
355+
// NOLINTNEXTLINE(*const-cast)
356+
const_cast<PyMethodDef*>(std::data(THPStream_methods)), /* tp_methods */
357+
// NOLINTNEXTLINE(*const-cast)
358+
const_cast<PyMemberDef*>(std::data(THPStream_members)), /* tp_members */
359+
// NOLINTNEXTLINE(*const-cast)
360+
const_cast<PyGetSetDef*>(std::data(THPStream_properties)), /* tp_getset */
361361
nullptr, /* tp_base */
362362
nullptr, /* tp_dict */
363363
nullptr, /* tp_descr_get */

torch/csrc/TypeInfo.cpp

+8-18
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ static PyObject* THPIInfo_str(THPIInfo* self) {
273273
return !PyErr_Occurred() ? THPUtils_packString(oss.str().c_str()) : nullptr;
274274
}
275275

276-
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-c-arrays)
277-
static struct PyGetSetDef THPFInfo_properties[] = {
276+
static const std::initializer_list<PyGetSetDef> THPFInfo_properties = {
278277
{"bits", (getter)THPDTypeInfo_bits, nullptr, nullptr, nullptr},
279278
{"eps", (getter)THPFInfo_eps, nullptr, nullptr, nullptr},
280279
{"max", (getter)THPFInfo_max, nullptr, nullptr, nullptr},
@@ -289,11 +288,6 @@ static struct PyGetSetDef THPFInfo_properties[] = {
289288
{"dtype", (getter)THPFInfo_dtype, nullptr, nullptr, nullptr},
290289
{nullptr}};
291290

292-
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-c-arrays)
293-
static PyMethodDef THPFInfo_methods[] = {
294-
{nullptr} /* Sentinel */
295-
};
296-
297291
PyTypeObject THPFInfoType = {
298292
PyVarObject_HEAD_INIT(nullptr, 0)
299293
"torch.finfo", /* tp_name */
@@ -322,9 +316,10 @@ PyTypeObject THPFInfoType = {
322316
0, /* tp_weaklistoffset */
323317
nullptr, /* tp_iter */
324318
nullptr, /* tp_iternext */
325-
THPFInfo_methods, /* tp_methods */
319+
nullptr, /* tp_methods */
326320
nullptr, /* tp_members */
327-
THPFInfo_properties, /* tp_getset */
321+
// NOLINTNEXTLINE(*const-cast)
322+
const_cast<PyGetSetDef*>(std::data(THPFInfo_properties)), /* tp_getset */
328323
nullptr, /* tp_base */
329324
nullptr, /* tp_dict */
330325
nullptr, /* tp_descr_get */
@@ -335,19 +330,13 @@ PyTypeObject THPFInfoType = {
335330
THPFInfo_pynew, /* tp_new */
336331
};
337332

338-
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-c-arrays)
339-
static struct PyGetSetDef THPIInfo_properties[] = {
333+
static const std::initializer_list<PyGetSetDef> THPIInfo_properties = {
340334
{"bits", (getter)THPDTypeInfo_bits, nullptr, nullptr, nullptr},
341335
{"max", (getter)THPIInfo_max, nullptr, nullptr, nullptr},
342336
{"min", (getter)THPIInfo_min, nullptr, nullptr, nullptr},
343337
{"dtype", (getter)THPIInfo_dtype, nullptr, nullptr, nullptr},
344338
{nullptr}};
345339

346-
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-c-arrays)
347-
static PyMethodDef THPIInfo_methods[] = {
348-
{nullptr} /* Sentinel */
349-
};
350-
351340
PyTypeObject THPIInfoType = {
352341
PyVarObject_HEAD_INIT(nullptr, 0)
353342
"torch.iinfo", /* tp_name */
@@ -376,9 +365,10 @@ PyTypeObject THPIInfoType = {
376365
0, /* tp_weaklistoffset */
377366
nullptr, /* tp_iter */
378367
nullptr, /* tp_iternext */
379-
THPIInfo_methods, /* tp_methods */
368+
nullptr, /* tp_methods */
380369
nullptr, /* tp_members */
381-
THPIInfo_properties, /* tp_getset */
370+
// NOLINTNEXTLINE(*const-cast)
371+
const_cast<PyGetSetDef*>(std::data(THPIInfo_properties)), /* tp_getset */
382372
nullptr, /* tp_base */
383373
nullptr, /* tp_dict */
384374
nullptr, /* tp_descr_get */

0 commit comments

Comments
 (0)