[Python-checkins] r63425 - in python/trunk: Lib/test/test_builtin.py Lib/test/test_py3kwarn.py Python/bltinmodule.c

Eric Smith eric+python-dev at trueblade.com
Sat May 17 21:43:21 CEST 2008


If I'm reading this change correctly, Guido previously said don't add 
this warning:
http://mail.python.org/pipermail/python-dev/2008-February/077158.html
"I think practicality says we should not warn about this."

I'm not sure about the PyNumber_ToBase change.  Why is that added to 
hex, and why isn't it added to oct?  That seems like a fairly 
significant change (and a gratuitous difference from oct).

Eric.

benjamin.peterson wrote:
> Author: benjamin.peterson
> Date: Sat May 17 21:21:20 2008
> New Revision: 63425
> 
> Log:
> add Py3k warnings to oct and hex. backport hex behavior (because it's not different)
> 
> 
> Modified:
>    python/trunk/Lib/test/test_builtin.py
>    python/trunk/Lib/test/test_py3kwarn.py
>    python/trunk/Python/bltinmodule.c
> 
> Modified: python/trunk/Lib/test/test_builtin.py
> ==============================================================================
> --- python/trunk/Lib/test/test_builtin.py	(original)
> +++ python/trunk/Lib/test/test_builtin.py	Sat May 17 21:21:20 2008
> @@ -632,6 +632,10 @@
>          self.assertEqual(hex(-16L), '-0x10L')
>          self.assertRaises(TypeError, hex, {})
>  
> +        class Spam(object):
> +            def __index__(self): return 23
> +        self.assertEqual(hex(Spam()), "0x17")
> +
>      def test_id(self):
>          id(None)
>          id(1)
> 
> Modified: python/trunk/Lib/test/test_py3kwarn.py
> ==============================================================================
> --- python/trunk/Lib/test/test_py3kwarn.py	(original)
> +++ python/trunk/Lib/test/test_py3kwarn.py	Sat May 17 21:21:20 2008
> @@ -123,6 +123,20 @@
>          with catch_warning() as w:
>              self.assertWarning(buffer('a'), w, expected)
>  
> +    def test_hex_and_oct(self):
> +        class Spam(object):
> +            def __hex__(self): return "0x17"
> +            def __oct__(self): return "07"
> +
> +        expected = 'In 3.x, oct() converts the result of __index__ to octal; ' \
> +                   'Use future_builtins.oct for this behavior. ' \
> +                   'Also, note the returned format is different.'
> +        with catch_warning() as w:
> +            self.assertWarning(oct(Spam()), w, expected)
> +        expected = 'In 3.x, hex() converts the result of __index__ to hexidecimal.'
> +        with catch_warning() as w:
> +            self.assertWarning(hex(Spam()), w, expected)
> +
>  
>  class TestStdlibRemovals(unittest.TestCase):
>  
> 
> Modified: python/trunk/Python/bltinmodule.c
> ==============================================================================
> --- python/trunk/Python/bltinmodule.c	(original)
> +++ python/trunk/Python/bltinmodule.c	Sat May 17 21:21:20 2008
> @@ -1181,22 +1181,29 @@
>  {
>  	PyNumberMethods *nb;
>  	PyObject *res;
> -
> -	if ((nb = v->ob_type->tp_as_number) == NULL ||
> -	    nb->nb_hex == NULL) {
> -		PyErr_SetString(PyExc_TypeError,
> -			   "hex() argument can't be converted to hex");
> -		return NULL;
> -	}
> -	res = (*nb->nb_hex)(v);
> -	if (res && !PyString_Check(res)) {
> -		PyErr_Format(PyExc_TypeError,
> -			     "__hex__ returned non-string (type %.200s)",
> -			     res->ob_type->tp_name);
> -		Py_DECREF(res);
> -		return NULL;
> +	
> +	nb = Py_TYPE(v)->tp_as_number;
> +	
> +	if (nb != NULL && nb->nb_hex != NULL) {
> +		if (PyErr_WarnPy3k("In 3.x, hex() converts "
> +				   "the result of __index__ to hexidecimal.",
> +				   1) < 0)
> +			return NULL;
> +		res = (*nb->nb_hex)(v);
> +		if (res && !PyString_Check(res)) {
> +			PyErr_Format(PyExc_TypeError,
> +				     "__hex__ returned non-string (type %.200s)",
> +				     res->ob_type->tp_name);
> +			Py_DECREF(res);
> +			return NULL;
> +		}
> +		return res;
>  	}
> -	return res;
> +	else if (PyIndex_Check(v))
> +		return PyNumber_ToBase(v, 16);
> +	PyErr_SetString(PyExc_TypeError,
> +			"hex() argument can't be converted to hex");
> +	return NULL;
>  }
>  
>  PyDoc_STRVAR(hex_doc,
> @@ -1456,6 +1463,11 @@
>  			   "oct() argument can't be converted to oct");
>  		return NULL;
>  	}
> +	if (PyErr_WarnPy3k("In 3.x, oct() converts the result of __index__ to octal; "
> +			   "Use future_builtins.oct for this behavior. "
> +                            "Also, note the returned format is different.",
> +                           1) < 0)
> +		return NULL;
>  	res = (*nb->nb_oct)(v);
>  	if (res && !PyString_Check(res)) {
>  		PyErr_Format(PyExc_TypeError,
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
> 



More information about the Python-checkins mailing list