mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 11:49:12 +00:00 
			
		
		
		
	Issue19995: more informative error message; spelling corrections; use operator.mod instead of __mod__
This commit is contained in:
		
							parent
							
								
									a17d678098
								
							
						
					
					
						commit
						9ab748013b
					
				
					 2 changed files with 30 additions and 14 deletions
				
			
		| 
						 | 
					@ -8,6 +8,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
 | 
				
			||||||
import _string
 | 
					import _string
 | 
				
			||||||
import codecs
 | 
					import codecs
 | 
				
			||||||
import itertools
 | 
					import itertools
 | 
				
			||||||
 | 
					import operator
 | 
				
			||||||
import struct
 | 
					import struct
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import unittest
 | 
					import unittest
 | 
				
			||||||
| 
						 | 
					@ -1127,20 +1128,20 @@ class UnicodeTest(string_tests.CommonTest,
 | 
				
			||||||
        self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9')
 | 
					        self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        #issue 19995
 | 
					        #issue 19995
 | 
				
			||||||
        class PsuedoInt:
 | 
					        class PseudoInt:
 | 
				
			||||||
            def __init__(self, value):
 | 
					            def __init__(self, value):
 | 
				
			||||||
                self.value = int(value)
 | 
					                self.value = int(value)
 | 
				
			||||||
            def __int__(self):
 | 
					            def __int__(self):
 | 
				
			||||||
                return self.value
 | 
					                return self.value
 | 
				
			||||||
            def __index__(self):
 | 
					            def __index__(self):
 | 
				
			||||||
                return self.value
 | 
					                return self.value
 | 
				
			||||||
        class PsuedoFloat:
 | 
					        class PseudoFloat:
 | 
				
			||||||
            def __init__(self, value):
 | 
					            def __init__(self, value):
 | 
				
			||||||
                self.value = float(value)
 | 
					                self.value = float(value)
 | 
				
			||||||
            def __int__(self):
 | 
					            def __int__(self):
 | 
				
			||||||
                return int(self.value)
 | 
					                return int(self.value)
 | 
				
			||||||
        pi = PsuedoFloat(3.1415)
 | 
					        pi = PseudoFloat(3.1415)
 | 
				
			||||||
        letter_m = PsuedoInt(109)
 | 
					        letter_m = PseudoInt(109)
 | 
				
			||||||
        self.assertEqual('%x' % 42, '2a')
 | 
					        self.assertEqual('%x' % 42, '2a')
 | 
				
			||||||
        self.assertEqual('%X' % 15, 'F')
 | 
					        self.assertEqual('%X' % 15, 'F')
 | 
				
			||||||
        self.assertEqual('%o' % 9, '11')
 | 
					        self.assertEqual('%o' % 9, '11')
 | 
				
			||||||
| 
						 | 
					@ -1149,11 +1150,11 @@ class UnicodeTest(string_tests.CommonTest,
 | 
				
			||||||
        self.assertEqual('%X' % letter_m, '6D')
 | 
					        self.assertEqual('%X' % letter_m, '6D')
 | 
				
			||||||
        self.assertEqual('%o' % letter_m, '155')
 | 
					        self.assertEqual('%o' % letter_m, '155')
 | 
				
			||||||
        self.assertEqual('%c' % letter_m, 'm')
 | 
					        self.assertEqual('%c' % letter_m, 'm')
 | 
				
			||||||
        self.assertRaises(TypeError, '%x'.__mod__, pi),
 | 
					        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float', operator.mod, '%x', 3.14),
 | 
				
			||||||
        self.assertRaises(TypeError, '%x'.__mod__, 3.14),
 | 
					        self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float', operator.mod, '%X', 2.11),
 | 
				
			||||||
        self.assertRaises(TypeError, '%X'.__mod__, 2.11),
 | 
					        self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float', operator.mod, '%o', 1.79),
 | 
				
			||||||
        self.assertRaises(TypeError, '%o'.__mod__, 1.79),
 | 
					        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not PseudoFloat', operator.mod, '%x', pi),
 | 
				
			||||||
        self.assertRaises(TypeError, '%c'.__mod__, pi),
 | 
					        self.assertRaises(TypeError, operator.mod, '%c', pi),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_formatting_with_enum(self):
 | 
					    def test_formatting_with_enum(self):
 | 
				
			||||||
        # issue18780
 | 
					        # issue18780
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13986,11 +13986,13 @@ mainformatlong(PyObject *v,
 | 
				
			||||||
    if (!PyNumber_Check(v))
 | 
					    if (!PyNumber_Check(v))
 | 
				
			||||||
        goto wrongtype;
 | 
					        goto wrongtype;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* make sure number is a type of integer */
 | 
					    /* make sure number is a type of integer for o, x, and X */
 | 
				
			||||||
    if (!PyLong_Check(v)) {
 | 
					    if (!PyLong_Check(v)) {
 | 
				
			||||||
        if (type == 'o' || type == 'x' || type == 'X') {
 | 
					        if (type == 'o' || type == 'x' || type == 'X') {
 | 
				
			||||||
            iobj = PyNumber_Index(v);
 | 
					            iobj = PyNumber_Index(v);
 | 
				
			||||||
            if (iobj == NULL) {
 | 
					            if (iobj == NULL) {
 | 
				
			||||||
 | 
					                if (PyErr_ExceptionMatches(PyExc_TypeError))
 | 
				
			||||||
 | 
					                    goto wrongtype;
 | 
				
			||||||
                return -1;
 | 
					                return -1;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -14052,10 +14054,23 @@ mainformatlong(PyObject *v,
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
wrongtype:
 | 
					wrongtype:
 | 
				
			||||||
 | 
					    switch(type)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        case 'o':
 | 
				
			||||||
 | 
					        case 'x':
 | 
				
			||||||
 | 
					        case 'X':
 | 
				
			||||||
 | 
					            PyErr_Format(PyExc_TypeError,
 | 
				
			||||||
 | 
					                    "%%%c format: an integer is required, "
 | 
				
			||||||
 | 
					                    "not %.200s",
 | 
				
			||||||
 | 
					                    type, Py_TYPE(v)->tp_name);
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
            PyErr_Format(PyExc_TypeError,
 | 
					            PyErr_Format(PyExc_TypeError,
 | 
				
			||||||
                    "%%%c format: a number is required, "
 | 
					                    "%%%c format: a number is required, "
 | 
				
			||||||
                    "not %.200s",
 | 
					                    "not %.200s",
 | 
				
			||||||
                    type, Py_TYPE(v)->tp_name);
 | 
					                    type, Py_TYPE(v)->tp_name);
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    return -1;
 | 
					    return -1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue