Skip to content

Commit 27efdc5

Browse files
cyyeverpytorchmergebot
authored andcommitted
fix writable-strings warnings (pytorch#93246)
clang reports "ISO C++11 does not allow conversion from string literal to 'char *'" Pull Request resolved: pytorch#93246 Approved by: https://github.com/malfet
1 parent 59a81b6 commit 27efdc5

10 files changed

+36
-34
lines changed

torch/csrc/StorageMethods.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,16 @@ static PyObject* THPStorage_fromBuffer(
173173
PyObject* dtype_obj = nullptr;
174174
c10::ScalarType scalar_type = at::kByte;
175175
Py_buffer buffer = {};
176-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,clang-diagnostic-writable-strings)
177-
static char* kwlist[] = {
176+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
177+
constexpr char* kwlist[] = {
178178
"buffer", "byte_order", "count", "offset", "dtype", nullptr};
179-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
180-
const char* argtypes;
181-
argtypes = "O|snnO";
179+
constexpr char* argtypes = "O|snnO";
182180

183181
if (!PyArg_ParseTupleAndKeywords(
184182
args,
185183
keywds,
186184
argtypes,
187-
kwlist,
185+
const_cast<char**>(kwlist),
188186
&obj,
189187
&byte_order_str,
190188
&count,
@@ -337,10 +335,16 @@ static PyObject* THPStorage_fromFile(
337335
const char* filename;
338336
Py_ssize_t nbytes = 0;
339337
int shared = 0;
340-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,clang-diagnostic-writable-strings)
341-
static char* kwlist[] = {"filename", "shared", "nbytes", nullptr};
338+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
339+
constexpr char* kwlist[] = {"filename", "shared", "nbytes", nullptr};
342340
if (!PyArg_ParseTupleAndKeywords(
343-
args, keywds, "s|in", kwlist, &filename, &shared, &nbytes)) {
341+
args,
342+
keywds,
343+
"s|in",
344+
const_cast<char**>(kwlist),
345+
&filename,
346+
&shared,
347+
&nbytes)) {
344348
return nullptr;
345349
}
346350
if (shared)

torch/csrc/Stream.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ static PyObject* THPStream_pynew(
1616
int64_t stream_id = 0;
1717
int64_t device_index = 0;
1818
int64_t device_type = 0;
19-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,clang-diagnostic-writable-strings)
20-
static char* kwlist[] = {"stream_id", "device_index", "device_type", nullptr};
19+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
20+
constexpr char* kwlist[] = {
21+
"stream_id", "device_index", "device_type", nullptr};
2122
if (!PyArg_ParseTupleAndKeywords(
2223
args,
2324
kwargs,
2425
"|LLL",
25-
kwlist,
26+
const_cast<char**>(kwlist),
2627
&stream_id,
2728
&device_index,
2829
&device_type)) {

torch/csrc/autograd/python_anomaly_mode.h

-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ namespace torch {
1010
namespace autograd {
1111

1212
struct PyAnomalyMetadata : public AnomalyMetadata {
13-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,clang-diagnostic-writable-strings)
1413
static constexpr const char* ANOMALY_TRACE_KEY = "traceback_";
15-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,clang-diagnostic-writable-strings)
1614
static constexpr const char* ANOMALY_PARENT_KEY = "parent_";
1715

1816
PyAnomalyMetadata() {

torch/csrc/autograd/python_engine.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,20 @@ PyObject* THPEngine_run_backward(
179179
unsigned char allow_unreachable = 0;
180180
unsigned char accumulate_grad =
181181
0; // Indicate whether to accumulate grad into leaf Tensors or capture
182-
const char* accepted_kwargs[] = {// NOLINT
183-
"tensors",
184-
"grad_tensors",
185-
"keep_graph",
186-
"create_graph",
187-
"inputs",
188-
"allow_unreachable",
189-
"accumulate_grad",
190-
nullptr};
182+
constexpr char* accepted_kwargs[] = {// NOLINT
183+
"tensors",
184+
"grad_tensors",
185+
"keep_graph",
186+
"create_graph",
187+
"inputs",
188+
"allow_unreachable",
189+
"accumulate_grad",
190+
nullptr};
191191
if (!PyArg_ParseTupleAndKeywords(
192192
args,
193193
kwargs,
194194
"OObb|Obb",
195-
(char**)accepted_kwargs,
195+
const_cast<char**>(accepted_kwargs),
196196
&tensors,
197197
&grad_tensors,
198198
&keep_graph,

torch/csrc/autograd/python_legacy_variable.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ static PyObject* THPVariable_pynew(
2626
const char* name = nullptr;
2727

2828
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
29-
const char* accepted_args[] = {
29+
constexpr char* accepted_args[] = {
3030
"data", "requires_grad", "volatile", "_grad_fn", "name", nullptr};
3131
if (!PyArg_ParseTupleAndKeywords(
3232
args,
3333
kwds,
3434
"|ObbOz",
35-
(char**)accepted_args,
35+
const_cast<char**>(accepted_args),
3636
&data,
3737
&requires_grad,
3838
&is_volatile,

torch/csrc/cuda/Event.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ static PyObject* THCPEvent_pynew(
2525
unsigned char interprocess = 0;
2626

2727
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
28-
static char* kwlist[] = {
28+
constexpr char* kwlist[] = {
2929
"enable_timing", "blocking", "interprocess", nullptr};
3030
if (!PyArg_ParseTupleAndKeywords(
3131
args,
3232
kwargs,
3333
"|bbb",
34-
kwlist,
34+
const_cast<char**>(kwlist),
3535
&enable_timing,
3636
&blocking,
3737
&interprocess)) {

torch/csrc/cuda/Module.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,13 @@ PyObject* THCPModule_setStream_wrap(
230230
int64_t device_type = 0;
231231

232232
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
233-
static char* kwlist[] = {"stream_id", "device_index", "device_type", nullptr};
233+
constexpr char* kwlist[] = {
234+
"stream_id", "device_index", "device_type", nullptr};
234235
if (!PyArg_ParseTupleAndKeywords(
235236
args,
236237
kwargs,
237238
"|LLL",
238-
kwlist,
239+
const_cast<char**>(kwlist),
239240
&stream_id,
240241
&device_index,
241242
&device_type)) {

torch/csrc/cuda/Stream.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static PyObject* THCPStream_pynew(
2828
uint64_t stream_ptr = 0;
2929

3030
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
31-
static char* kwlist[] = {
31+
constexpr char* kwlist[] = {
3232
"priority",
3333
"stream_id",
3434
"device_index",
@@ -39,7 +39,7 @@ static PyObject* THCPStream_pynew(
3939
args,
4040
kwargs,
4141
"|iLLLK",
42-
kwlist,
42+
const_cast<char**>(kwlist),
4343
&priority,
4444
&stream_id,
4545
&device_index,

torch/csrc/utils/disable_torch_function.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ static bool is_basic_python_type(PyTypeObject* tp) {
297297
}
298298

299299
inline bool has_torch_function_attr(PyObject* obj) {
300-
// NOLINTNEXTLINE(clang-diagnostic-writable-strings)
301300
auto attr = PyObject_FastGetAttrString(obj, "__torch_function__");
302301
return (
303302
attr.ptr() != nullptr && attr.ptr() != torch::disabled_torch_function);

torch/csrc/utils/python_arg_parser.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ auto handle_torch_function_no_python_arg_parser(
350350
}
351351
if (ret.ptr() == nullptr || ret.ptr() == Py_NotImplemented) {
352352
for (auto& arg : overloaded_args) {
353-
// NOLINTNEXTLINE(clang-diagnostic-writable-strings)
354353
py::object torch_function =
355354
PyObject_FastGetAttrString(arg.ptr(), torch_function_name_str);
356355
if (!torch_function) {

0 commit comments

Comments
 (0)