From 1554b18b5aad4547574f680761da047d356b30b4 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sun, 20 Dec 2009 16:03:30 +0000 Subject: [PATCH] Merged revisions 76930 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r76930 | mark.dickinson | 2009-12-20 15:57:56 +0000 (Sun, 20 Dec 2009) | 1 line Add missing tests for PyArg_Parse* with format 'h' ........ --- Lib/test/test_getargs2.py | 17 ++++++++++++++++- Modules/_testcapimodule.c | 10 ++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py index ee01018b3e3..108159cd65c 100644 --- a/Lib/test/test_getargs2.py +++ b/Lib/test/test_getargs2.py @@ -48,7 +48,8 @@ LARGE = 0x7FFFFFFF VERY_LARGE = 0xFF0000121212121212121242 from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ - INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX + INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ + SHRT_MIN, SHRT_MAX # fake, they are not defined in Python's header files LLONG_MAX = 2**63-1 @@ -135,6 +136,20 @@ class Unsigned_TestCase(unittest.TestCase): self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) class Signed_TestCase(unittest.TestCase): + def test_h(self): + from _testcapi import getargs_h + # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) + self.assertRaises(TypeError, getargs_h, 3.14) + self.assertEqual(99, getargs_h(Int())) + + self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) + self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) + self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) + self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) + + self.assertEqual(42, getargs_h(42)) + self.assertRaises(OverflowError, getargs_h, VERY_LARGE) + def test_i(self): from _testcapi import getargs_i # i returns 'int', and does range checking (INT_MIN ... INT_MAX) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 6244f3ff829..70da707d437 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -454,6 +454,15 @@ getargs_B(PyObject *self, PyObject *args) return PyLong_FromUnsignedLong((unsigned long)value); } +static PyObject * +getargs_h(PyObject *self, PyObject *args) +{ + short value; + if (!PyArg_ParseTuple(args, "h", &value)) + return NULL; + return PyLong_FromLong((long)value); +} + static PyObject * getargs_H(PyObject *self, PyObject *args) { @@ -1566,6 +1575,7 @@ static PyMethodDef TestMethods[] = { METH_VARARGS|METH_KEYWORDS}, {"getargs_b", getargs_b, METH_VARARGS}, {"getargs_B", getargs_B, METH_VARARGS}, + {"getargs_h", getargs_h, METH_VARARGS}, {"getargs_H", getargs_H, METH_VARARGS}, {"getargs_I", getargs_I, METH_VARARGS}, {"getargs_k", getargs_k, METH_VARARGS},