Skip to content

Commit ccabfdc

Browse files
author
georg.brandl
committed
Add a test for Py_ssize_t. Correct typo in getargs.c.
git-svn-id: http://svn.python.org/projects/python/trunk@45349 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c676e51 commit ccabfdc

3 files changed

Lines changed: 41 additions & 14 deletions

File tree

Lib/test/test_getargs2.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
VERY_LARGE = 0xFF0000121212121212121242L
4949

5050
from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \
51-
INT_MIN, LONG_MIN, LONG_MAX
51+
INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
5252

5353
# fake, they are not defined in Python's header files
5454
LLONG_MAX = 2**63-1
@@ -182,6 +182,23 @@ def test_l(self):
182182
self.failUnlessEqual(42, getargs_l(42L))
183183
self.assertRaises(OverflowError, getargs_l, VERY_LARGE)
184184

185+
def test_n(self):
186+
from _testcapi import getargs_n
187+
# n returns 'Py_ssize_t', and does range checking
188+
# (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX)
189+
self.failUnlessEqual(3, getargs_n(3.14))
190+
self.failUnlessEqual(99, getargs_n(Long()))
191+
self.failUnlessEqual(99, getargs_n(Int()))
192+
193+
self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)
194+
self.failUnlessEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN))
195+
self.failUnlessEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX))
196+
self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1)
197+
198+
self.failUnlessEqual(42, getargs_n(42))
199+
self.failUnlessEqual(42, getargs_n(42L))
200+
self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
201+
185202

186203
class LongLong_TestCase(unittest.TestCase):
187204
def test_L(self):

Modules/_testcapimodule.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ getargs_l(PyObject *self, PyObject *args)
360360
return PyLong_FromLong(value);
361361
}
362362

363+
static PyObject *
364+
getargs_n(PyObject *self, PyObject *args)
365+
{
366+
Py_ssize_t value;
367+
if (!PyArg_ParseTuple(args, "n", &value))
368+
return NULL;
369+
return PyInt_FromSsize_t(value);
370+
}
371+
363372
#ifdef HAVE_LONG_LONG
364373
static PyObject *
365374
getargs_L(PyObject *self, PyObject *args)
@@ -661,17 +670,18 @@ static PyMethodDef TestMethods[] = {
661670
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
662671
{"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
663672

664-
{"getargs_b", (PyCFunction)getargs_b, METH_VARARGS},
665-
{"getargs_B", (PyCFunction)getargs_B, METH_VARARGS},
666-
{"getargs_H", (PyCFunction)getargs_H, METH_VARARGS},
667-
{"getargs_I", (PyCFunction)getargs_I, METH_VARARGS},
668-
{"getargs_k", (PyCFunction)getargs_k, METH_VARARGS},
669-
{"getargs_i", (PyCFunction)getargs_i, METH_VARARGS},
670-
{"getargs_l", (PyCFunction)getargs_l, METH_VARARGS},
673+
{"getargs_b", getargs_b, METH_VARARGS},
674+
{"getargs_B", getargs_B, METH_VARARGS},
675+
{"getargs_H", getargs_H, METH_VARARGS},
676+
{"getargs_I", getargs_I, METH_VARARGS},
677+
{"getargs_k", getargs_k, METH_VARARGS},
678+
{"getargs_i", getargs_i, METH_VARARGS},
679+
{"getargs_l", getargs_l, METH_VARARGS},
680+
{"getargs_n", getargs_n, METH_VARARGS},
671681
#ifdef HAVE_LONG_LONG
672-
{"getargs_L", (PyCFunction)getargs_L, METH_VARARGS},
673-
{"getargs_K", (PyCFunction)getargs_K, METH_VARARGS},
674-
{"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS},
682+
{"getargs_L", getargs_L, METH_VARARGS},
683+
{"getargs_K", getargs_K, METH_VARARGS},
684+
{"test_longlong_api", test_longlong_api, METH_NOARGS},
675685
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
676686
{"codec_incrementalencoder",
677687
(PyCFunction)codec_incrementalencoder, METH_VARARGS},
@@ -682,7 +692,7 @@ static PyMethodDef TestMethods[] = {
682692
{"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
683693
#endif
684694
#ifdef WITH_THREAD
685-
{"_test_thread_state", (PyCFunction)test_thread_state, METH_VARARGS},
695+
{"_test_thread_state", test_thread_state, METH_VARARGS},
686696
#endif
687697
{NULL, NULL} /* sentinel */
688698
};

Python/getargs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
647647
Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
648648
Py_ssize_t ival;
649649
if (float_argument_error(arg))
650-
return converterr("integer<i>", arg, msgbuf, bufsize);
650+
return converterr("integer<n>", arg, msgbuf, bufsize);
651651
ival = PyInt_AsSsize_t(arg);
652652
if (ival == -1 && PyErr_Occurred())
653-
return converterr("integer<i>", arg, msgbuf, bufsize);
653+
return converterr("integer<n>", arg, msgbuf, bufsize);
654654
*p = ival;
655655
break;
656656
}

0 commit comments

Comments
 (0)