Convert a Python integer to a C :c:expr:`unsigned long` without
overflow checking.
+ .. versionchanged:: next
+ Use :meth:`~object.__index__` if available.
+
``L`` (:class:`int`) [long long]
Convert a Python integer to a C :c:expr:`long long`.
Convert a Python integer to a C :c:expr:`unsigned long long`
without overflow checking.
+ .. versionchanged:: next
+ Use :meth:`~object.__index__` if available.
+
``n`` (:class:`int`) [:c:type:`Py_ssize_t`]
Convert a Python integer to a C :c:type:`Py_ssize_t`.
Adding ``?`` after any format unit makes ``None`` be accepted as a value.
(Contributed by Serhiy Storchaka in :gh:`112068`.)
+* The ``k`` and ``K`` formats in :c:func:`PyArg_ParseTuple` and
+ similar functions now use :meth:`~object.__index__` if available,
+ like all other integer formats.
+ (Contributed by Serhiy Storchaka in :gh:`112068`.)
+
* Add macros :c:func:`Py_PACK_VERSION` and :c:func:`Py_PACK_FULL_VERSION` for
bit-packing Python version numbers.
(Contributed by Petr Viktorin in :gh:`128629`.)
if (nargs < 3) {
goto skip_optional;
}
- if (!PyLong_Check(args[2])) {
+ if (!PyIndex_Check(args[2])) {
_PyArg_BadArgument("test_unsigned_long_converter", "argument 3", "int", args[2]);
goto exit;
}
static PyObject *
test_unsigned_long_converter_impl(PyObject *module, unsigned long a,
unsigned long b, unsigned long c)
-/*[clinic end generated code: output=540bb0ba2894e1fe input=f450d94cae1ef73b]*/
+/*[clinic end generated code: output=d74eed227d77a31b input=f450d94cae1ef73b]*/
/*[clinic input]
if (nargs < 3) {
goto skip_optional;
}
- if (!PyLong_Check(args[2])) {
+ if (!PyIndex_Check(args[2])) {
_PyArg_BadArgument("test_unsigned_long_long_converter", "argument 3", "int", args[2]);
goto exit;
}
unsigned long long a,
unsigned long long b,
unsigned long long c)
-/*[clinic end generated code: output=3d69994f618b46bb input=a15115dc41866ff4]*/
+/*[clinic end generated code: output=5ca4e4dfb3db644b input=a15115dc41866ff4]*/
/*[clinic input]
def test_k(self):
from _testcapi import getargs_k
# k returns 'unsigned long', no range checking
- # it does not accept float, or instances with __int__
self.assertRaises(TypeError, getargs_k, 3.14)
- self.assertRaises(TypeError, getargs_k, Index())
+ self.assertEqual(99, getargs_k(Index()))
self.assertEqual(0, getargs_k(IndexIntSubclass()))
self.assertRaises(TypeError, getargs_k, BadIndex())
- self.assertRaises(TypeError, getargs_k, BadIndex2())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_k(BadIndex2()))
self.assertEqual(0, getargs_k(BadIndex3()))
self.assertRaises(TypeError, getargs_k, Int())
self.assertEqual(0, getargs_k(IntSubclass()))
from _testcapi import getargs_K
# K return 'unsigned long long', no range checking
self.assertRaises(TypeError, getargs_K, 3.14)
- self.assertRaises(TypeError, getargs_K, Index())
+ self.assertEqual(99, getargs_K(Index()))
self.assertEqual(0, getargs_K(IndexIntSubclass()))
self.assertRaises(TypeError, getargs_K, BadIndex())
- self.assertRaises(TypeError, getargs_K, BadIndex2())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_K(BadIndex2()))
self.assertEqual(0, getargs_K(BadIndex3()))
self.assertRaises(TypeError, getargs_K, Int())
self.assertEqual(0, getargs_K(IntSubclass()))
self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))
self.assertEqual(0, getargs_K(0))
+ self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))
self.assertEqual(0, getargs_K(ULLONG_MAX+1))
self.assertEqual(42, getargs_K(42))
--- /dev/null
+The ``k`` and ``K`` formats in :c:func:`PyArg_Parse` now support the
+:meth:`~object.__index__` special method, like all other integer formats.
if (z == -1 && PyErr_Occurred()) {
goto exit;
}
- if (!PyLong_Check(args[4])) {
+ if (!PyIndex_Check(args[4])) {
_PyArg_BadArgument("ungetmouse", "argument 5", "int", args[4]);
goto exit;
}
PyObject *return_value = NULL;
unsigned long newmask;
- if (!PyLong_Check(arg)) {
+ if (!PyIndex_Check(arg)) {
_PyArg_BadArgument("mousemask", "argument", "int", arg);
goto exit;
}
#ifndef _CURSES_USE_DEFAULT_COLORS_METHODDEF
#define _CURSES_USE_DEFAULT_COLORS_METHODDEF
#endif /* !defined(_CURSES_USE_DEFAULT_COLORS_METHODDEF) */
-/*[clinic end generated code: output=acae2eb9cf75e76d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=dbbbe86a4171799a input=a9049054013a1b77]*/
if (nargs < 3) {
goto skip_optional;
}
- if (!PyLong_Check(args[2])) {
+ if (!PyIndex_Check(args[2])) {
_PyArg_BadArgument("unsigned_long_converter", "argument 3", "int", args[2]);
goto exit;
}
if (nargs < 3) {
goto skip_optional;
}
- if (!PyLong_Check(args[2])) {
+ if (!PyIndex_Check(args[2])) {
_PyArg_BadArgument("unsigned_long_long_converter", "argument 3", "int", args[2]);
goto exit;
}
exit:
return return_value;
}
-/*[clinic end generated code: output=b57b94aeba0882b4 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=84ffc31f27215baa input=a9049054013a1b77]*/
if (fd < 0) {
goto exit;
}
- if (!PyLong_Check(args[1])) {
+ if (!PyIndex_Check(args[1])) {
PyErr_Format(PyExc_TypeError, "ioctl() argument 2 must be int, not %T", args[1]);
goto exit;
}
exit:
return return_value;
}
-/*[clinic end generated code: output=30c01b46bfa840c1 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=65a16bc64c7b4de4 input=a9049054013a1b77]*/
if (!path_converter(args[0], &path)) {
goto exit;
}
- if (!PyLong_Check(args[1])) {
+ if (!PyIndex_Check(args[1])) {
_PyArg_BadArgument("chflags", "argument 'flags'", "int", args[1]);
goto exit;
}
if (!path_converter(args[0], &path)) {
goto exit;
}
- if (!PyLong_Check(args[1])) {
+ if (!PyIndex_Check(args[1])) {
_PyArg_BadArgument("lchflags", "argument 'flags'", "int", args[1]);
goto exit;
}
#ifndef OS__EMSCRIPTEN_DEBUGGER_METHODDEF
#define OS__EMSCRIPTEN_DEBUGGER_METHODDEF
#endif /* !defined(OS__EMSCRIPTEN_DEBUGGER_METHODDEF) */
-/*[clinic end generated code: output=35dd8edb53b50537 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a5ca2541f2af5462 input=a9049054013a1b77]*/
if (!_PyArg_CheckPositional("pthread_kill", nargs, 2, 2)) {
goto exit;
}
- if (!PyLong_Check(args[0])) {
+ if (!PyIndex_Check(args[0])) {
_PyArg_BadArgument("pthread_kill", "argument 1", "int", args[0]);
goto exit;
}
#ifndef SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
#define SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
#endif /* !defined(SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF) */
-/*[clinic end generated code: output=a8b1ac2fc44a007e input=a9049054013a1b77]*/
+/*[clinic end generated code: output=48bfaffeb25df5d2 input=a9049054013a1b77]*/
unsigned long *p = va_arg(*p_va, unsigned long *);
HANDLE_NULLABLE;
unsigned long ival;
- if (PyLong_Check(arg))
- ival = PyLong_AsUnsignedLongMask(arg);
- else
+ if (!PyIndex_Check(arg)) {
return converterr(nullable, "int", arg, msgbuf, bufsize);
+ }
+ ival = PyLong_AsUnsignedLongMask(arg);
+ if (ival == (unsigned long)(long)-1 && PyErr_Occurred()) {
+ RETURN_ERR_OCCURRED;
+ }
*p = ival;
break;
}
unsigned long long *p = va_arg(*p_va, unsigned long long *);
HANDLE_NULLABLE;
unsigned long long ival;
- if (PyLong_Check(arg))
- ival = PyLong_AsUnsignedLongLongMask(arg);
- else
+ if (!PyIndex_Check(arg)) {
return converterr(nullable, "int", arg, msgbuf, bufsize);
+ }
+ ival = PyLong_AsUnsignedLongLongMask(arg);
+ if (ival == (unsigned long long)(long long)-1 && PyErr_Occurred()) {
+ RETURN_ERR_OCCURRED;
+ }
*p = ival;
break;
}
def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
if self.format_unit == 'k':
return self.format_code("""
- if (!PyLong_Check({argname})) {{{{
+ if (!PyIndex_Check({argname})) {{{{
{bad_argument}
goto exit;
}}}}
def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
if self.format_unit == 'K':
return self.format_code("""
- if (!PyLong_Check({argname})) {{{{
+ if (!PyIndex_Check({argname})) {{{{
{bad_argument}
goto exit;
}}}}