mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	gh-95605: Fix float(s) error message when s contains only whitespace (GH-95665)
				
					
				
			This PR fixes the error message from float(s) in the case where s contains only whitespace.
This commit is contained in:
		
							parent
							
								
									37c0f9ccc0
								
							
						
					
					
						commit
						97e9cfa75a
					
				
					 3 changed files with 14 additions and 1 deletions
				
			
		| 
						 | 
					@ -137,6 +137,10 @@ def check(s):
 | 
				
			||||||
        check('123\xbd')
 | 
					        check('123\xbd')
 | 
				
			||||||
        check('  123 456  ')
 | 
					        check('  123 456  ')
 | 
				
			||||||
        check(b'  123 456  ')
 | 
					        check(b'  123 456  ')
 | 
				
			||||||
 | 
					        # all whitespace (cf. https://github.com/python/cpython/issues/95605)
 | 
				
			||||||
 | 
					        check('')
 | 
				
			||||||
 | 
					        check(' ')
 | 
				
			||||||
 | 
					        check('\t \n')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # non-ascii digits (error came from non-digit '!')
 | 
					        # non-ascii digits (error came from non-digit '!')
 | 
				
			||||||
        check('\u0663\u0661\u0664!')
 | 
					        check('\u0663\u0661\u0664!')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Fix misleading contents of error message when converting an all-whitespace
 | 
				
			||||||
 | 
					string to :class:`float`.
 | 
				
			||||||
| 
						 | 
					@ -162,11 +162,18 @@ float_from_string_inner(const char *s, Py_ssize_t len, void *obj)
 | 
				
			||||||
    double x;
 | 
					    double x;
 | 
				
			||||||
    const char *end;
 | 
					    const char *end;
 | 
				
			||||||
    const char *last = s + len;
 | 
					    const char *last = s + len;
 | 
				
			||||||
    /* strip space */
 | 
					    /* strip leading whitespace */
 | 
				
			||||||
    while (s < last && Py_ISSPACE(*s)) {
 | 
					    while (s < last && Py_ISSPACE(*s)) {
 | 
				
			||||||
        s++;
 | 
					        s++;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    if (s == last) {
 | 
				
			||||||
 | 
					        PyErr_Format(PyExc_ValueError,
 | 
				
			||||||
 | 
					                     "could not convert string to float: "
 | 
				
			||||||
 | 
					                     "%R", obj);
 | 
				
			||||||
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* strip trailing whitespace */
 | 
				
			||||||
    while (s < last - 1 && Py_ISSPACE(last[-1])) {
 | 
					    while (s < last - 1 && Py_ISSPACE(last[-1])) {
 | 
				
			||||||
        last--;
 | 
					        last--;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue