mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-133583: Add support for fixed size unsigned integers in argument parsing (GH-133584)
* Add Argument Clinic converters: uint8, uint16, uint32, uint64. * Add private C API: _PyLong_UInt8_Converter(), _PyLong_UInt16_Converter(), _PyLong_UInt32_Converter(), _PyLong_UInt64_Converter().
This commit is contained in:
parent
3224b99872
commit
4c914e7a36
7 changed files with 87 additions and 93 deletions
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include <lzma.h>
|
||||
|
||||
#include "pycore_long.h" // _PyLong_UInt32_Converter()
|
||||
// Blocks output buffer wrappers
|
||||
#include "pycore_blocks_output_buffer.h"
|
||||
|
||||
|
@ -223,8 +224,6 @@ FUNCNAME(PyObject *obj, void *ptr) \
|
|||
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)
|
||||
|
@ -254,7 +253,7 @@ parse_filter_spec_lzma(_lzma_state *state, PyObject *spec)
|
|||
return NULL;
|
||||
}
|
||||
if (preset_obj != NULL) {
|
||||
int ok = uint32_converter(preset_obj, &preset);
|
||||
int ok = _PyLong_UInt32_Converter(preset_obj, &preset);
|
||||
Py_DECREF(preset_obj);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
|
@ -275,14 +274,14 @@ parse_filter_spec_lzma(_lzma_state *state, PyObject *spec)
|
|||
if (!PyArg_ParseTupleAndKeywords(state->empty_tuple, spec,
|
||||
"|OOO&O&O&O&O&O&O&O&", optnames,
|
||||
&id, &preset_obj,
|
||||
uint32_converter, &options->dict_size,
|
||||
uint32_converter, &options->lc,
|
||||
uint32_converter, &options->lp,
|
||||
uint32_converter, &options->pb,
|
||||
_PyLong_UInt32_Converter, &options->dict_size,
|
||||
_PyLong_UInt32_Converter, &options->lc,
|
||||
_PyLong_UInt32_Converter, &options->lp,
|
||||
_PyLong_UInt32_Converter, &options->pb,
|
||||
lzma_mode_converter, &options->mode,
|
||||
uint32_converter, &options->nice_len,
|
||||
_PyLong_UInt32_Converter, &options->nice_len,
|
||||
lzma_mf_converter, &options->mf,
|
||||
uint32_converter, &options->depth)) {
|
||||
_PyLong_UInt32_Converter, &options->depth)) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"Invalid filter specifier for LZMA filter");
|
||||
PyMem_Free(options);
|
||||
|
@ -301,7 +300,7 @@ parse_filter_spec_delta(_lzma_state *state, PyObject *spec)
|
|||
lzma_options_delta *options;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(state->empty_tuple, spec, "|OO&", optnames,
|
||||
&id, uint32_converter, &dist)) {
|
||||
&id, _PyLong_UInt32_Converter, &dist)) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"Invalid filter specifier for delta filter");
|
||||
return NULL;
|
||||
|
@ -325,7 +324,7 @@ parse_filter_spec_bcj(_lzma_state *state, PyObject *spec)
|
|||
lzma_options_bcj *options;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(state->empty_tuple, spec, "|OO&", optnames,
|
||||
&id, uint32_converter, &start_offset)) {
|
||||
&id, _PyLong_UInt32_Converter, &start_offset)) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"Invalid filter specifier for BCJ filter");
|
||||
return NULL;
|
||||
|
@ -806,7 +805,7 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (preset_obj != Py_None && !uint32_converter(preset_obj, &preset)) {
|
||||
if (preset_obj != Py_None && !_PyLong_UInt32_Converter(preset_obj, &preset)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -1226,7 +1225,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
|
|||
"Cannot specify memory limit with FORMAT_RAW");
|
||||
return NULL;
|
||||
}
|
||||
if (!uint64_converter(memlimit, &memlimit_)) {
|
||||
if (!_PyLong_UInt64_Converter(memlimit, &memlimit_)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
|
3
Modules/clinic/socketmodule.c.h
generated
3
Modules/clinic/socketmodule.c.h
generated
|
@ -6,6 +6,7 @@ preserve
|
|||
# include "pycore_gc.h" // PyGC_Head
|
||||
# include "pycore_runtime.h" // _Py_ID()
|
||||
#endif
|
||||
#include "pycore_long.h" // _PyLong_UInt16_Converter()
|
||||
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
|
||||
|
||||
PyDoc_STRVAR(_socket_socket_close__doc__,
|
||||
|
@ -369,4 +370,4 @@ exit:
|
|||
#ifndef _SOCKET_IF_INDEXTONAME_METHODDEF
|
||||
#define _SOCKET_IF_INDEXTONAME_METHODDEF
|
||||
#endif /* !defined(_SOCKET_IF_INDEXTONAME_METHODDEF) */
|
||||
/*[clinic end generated code: output=c971b79d2193b426 input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=07776dd21d1e3b56 input=a9049054013a1b77]*/
|
||||
|
|
|
@ -638,33 +638,22 @@ _PyLong_##NAME##_Converter(PyObject *obj, void *ptr) \
|
|||
return 1; \
|
||||
}
|
||||
|
||||
UNSIGNED_INT_CONVERTER(UInt16, uint16_t)
|
||||
UNSIGNED_INT_CONVERTER(UInt32, uint32_t)
|
||||
|
||||
#if defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)
|
||||
# ifdef MS_WINDOWS
|
||||
UNSIGNED_INT_CONVERTER(NetIfindex, NET_IFINDEX)
|
||||
# else
|
||||
UNSIGNED_INT_CONVERTER(NetIfindex, unsigned int)
|
||||
# define _PyLong_NetIfindex_Converter _PyLong_UnsignedInt_Converter
|
||||
# define NET_IFINDEX unsigned int
|
||||
# endif
|
||||
#endif // defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)
|
||||
|
||||
/*[python input]
|
||||
class uint16_converter(CConverter):
|
||||
type = "uint16_t"
|
||||
converter = '_PyLong_UInt16_Converter'
|
||||
|
||||
class uint32_converter(CConverter):
|
||||
type = "uint32_t"
|
||||
converter = '_PyLong_UInt32_Converter'
|
||||
|
||||
class NET_IFINDEX_converter(CConverter):
|
||||
type = "NET_IFINDEX"
|
||||
converter = '_PyLong_NetIfindex_Converter'
|
||||
|
||||
[python start generated code]*/
|
||||
/*[python end generated code: output=da39a3ee5e6b4b0d input=3de2e4a03fbf83b8]*/
|
||||
/*[python end generated code: output=da39a3ee5e6b4b0d input=1cf809c40a407c34]*/
|
||||
|
||||
/*[clinic input]
|
||||
module _socket
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue