Skip to content

Commit 1b9f786

Browse files
committed
Make error messages easier to understand for Python users
1 parent 685a3d1 commit 1b9f786

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Modules/_zstd/compressor.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ _PyZstd_set_c_parameters(ZstdCompressor *self, PyObject *level_or_options,
3737
if (PyLong_Check(level_or_options)) {
3838
const int level = PyLong_AsInt(level_or_options);
3939
if (level == -1 && PyErr_Occurred()) {
40-
PyErr_SetString(PyExc_ValueError,
41-
"Compression level should be 32-bit signed int value.");
40+
PyErr_Format(PyExc_ValueError,
41+
"Compression level should be an int value between %d and %d.",
42+
ZSTD_minCLevel(), ZSTD_maxCLevel());
4243
return -1;
4344
}
4445

@@ -72,18 +73,19 @@ _PyZstd_set_c_parameters(ZstdCompressor *self, PyObject *level_or_options,
7273
return -1;
7374
}
7475

75-
/* Both key & value should be 32-bit signed int */
7676
const int key_v = PyLong_AsInt(key);
7777
if (key_v == -1 && PyErr_Occurred()) {
7878
PyErr_SetString(PyExc_ValueError,
79-
"Key of option dict should be 32-bit signed int value.");
79+
"Key of options dict should be a CParameter attribute.");
8080
return -1;
8181
}
8282

83+
// TODO(emmatyping): check bounds when there is a value error here for better
84+
// error message?
8385
const int value_v = PyLong_AsInt(value);
8486
if (value_v == -1 && PyErr_Occurred()) {
8587
PyErr_SetString(PyExc_ValueError,
86-
"Value of option dict should be 32-bit signed int value.");
88+
"Value of option dict should be an int.");
8789
return -1;
8890
}
8991

Modules/_zstd/decompressor.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,16 @@ _PyZstd_set_d_parameters(ZstdDecompressor *self, PyObject *options)
8888
const int key_v = PyLong_AsInt(key);
8989
if (key_v == -1 && PyErr_Occurred()) {
9090
PyErr_SetString(PyExc_ValueError,
91-
"Key of options dict should be 32-bit signed integer value.");
91+
"Key of options dict should be a DParameter attribute.");
9292
return -1;
9393
}
9494

95+
// TODO(emmatyping): check bounds when there is a value error here for better
96+
// error message?
9597
const int value_v = PyLong_AsInt(value);
9698
if (value_v == -1 && PyErr_Occurred()) {
9799
PyErr_SetString(PyExc_ValueError,
98-
"Value of options dict should be 32-bit signed integer value.");
100+
"Value of options dict should be an int.");
99101
return -1;
100102
}
101103

0 commit comments

Comments
 (0)