Skip to content

gh-132987: Support __index__() in the lzma module #133099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/test/test_lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,12 @@ def test_init_bad_preset(self):
LZMAFile(BytesIO(), "w", preset=10)
with self.assertRaises(LZMAError):
LZMAFile(BytesIO(), "w", preset=23)
with self.assertRaises(OverflowError):
with self.assertRaises(ValueError):
LZMAFile(BytesIO(), "w", preset=-1)
with self.assertRaises(OverflowError):
with self.assertRaises(ValueError):
LZMAFile(BytesIO(), "w", preset=-7)
with self.assertRaises(OverflowError):
LZMAFile(BytesIO(), "w", preset=2**1000)
with self.assertRaises(TypeError):
LZMAFile(BytesIO(), "w", preset="foo")
# Cannot specify a preset with mode="r".
Expand Down
48 changes: 26 additions & 22 deletions Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,28 @@ PyLzma_Free(void *opaque, void *ptr)
to be strictly correct, we need to define two separate converters.
*/

#define INT_TYPE_CONVERTER_FUNC(TYPE, FUNCNAME) \
static int \
FUNCNAME(PyObject *obj, void *ptr) \
{ \
unsigned long long val; \
\
val = PyLong_AsUnsignedLongLong(obj); \
if (PyErr_Occurred()) \
return 0; \
if ((unsigned long long)(TYPE)val != val) { \
PyErr_SetString(PyExc_OverflowError, \
"Value too large for " #TYPE " type"); \
return 0; \
} \
*(TYPE *)ptr = (TYPE)val; \
return 1; \
}
#define INT_TYPE_CONVERTER_FUNC(TYPE, FUNCNAME) \
static int \
FUNCNAME(PyObject *obj, void *ptr) \
{ \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, ptr, sizeof(TYPE), \
Py_ASNATIVEBYTES_NATIVE_ENDIAN | \
Py_ASNATIVEBYTES_ALLOW_INDEX | \
Py_ASNATIVEBYTES_REJECT_NEGATIVE | \
Py_ASNATIVEBYTES_UNSIGNED_BUFFER); \
if (bytes < 0) { \
return 0; \
} \
if ((size_t)bytes > sizeof(TYPE)) { \
PyErr_SetString(PyExc_OverflowError, \
"Python int too large for C "#TYPE); \
return 0; \
} \
return 1; \
}

INT_TYPE_CONVERTER_FUNC(uint32_t, uint32_converter)
INT_TYPE_CONVERTER_FUNC(uint64_t, uint64_converter)
INT_TYPE_CONVERTER_FUNC(lzma_vli, lzma_vli_converter)
INT_TYPE_CONVERTER_FUNC(lzma_mode, lzma_mode_converter)
INT_TYPE_CONVERTER_FUNC(lzma_match_finder, lzma_mf_converter)
Expand Down Expand Up @@ -355,11 +358,13 @@ lzma_filter_converter(_lzma_state *state, PyObject *spec, void *ptr)
"Filter specifier must have an \"id\" entry");
return 0;
}
f->id = PyLong_AsUnsignedLongLong(id_obj);
Py_DECREF(id_obj);
if (PyErr_Occurred()) {
lzma_vli id;
if (!lzma_vli_converter(id_obj, &id)) {
Py_DECREF(id_obj);
return 0;
}
Py_DECREF(id_obj);
f->id = id;

switch (f->id) {
case LZMA_FILTER_LZMA1:
Expand Down Expand Up @@ -1221,8 +1226,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
"Cannot specify memory limit with FORMAT_RAW");
return NULL;
}
memlimit_ = PyLong_AsUnsignedLongLong(memlimit);
if (PyErr_Occurred()) {
if (!uint64_converter(memlimit, &memlimit_)) {
return NULL;
}
}
Expand Down
Loading