diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ctypes/test/test_funcptr.py | 6 | ||||
-rw-r--r-- | Lib/decimal.py | 17 | ||||
-rw-r--r-- | Lib/test/test_decimal.py | 8 | ||||
-rw-r--r-- | Lib/urlparse.py | 58 |
4 files changed, 34 insertions, 55 deletions
diff --git a/Lib/ctypes/test/test_funcptr.py b/Lib/ctypes/test/test_funcptr.py index 56099cc..7ddf319 100644 --- a/Lib/ctypes/test/test_funcptr.py +++ b/Lib/ctypes/test/test_funcptr.py @@ -123,5 +123,11 @@ class CFuncPtrTestCase(unittest.TestCase): self.failUnlessEqual(strtok(None, b"\n"), "c") self.failUnlessEqual(strtok(None, b"\n"), None) + def test_NULL_funcptr(self): + tp = CFUNCTYPE(c_int) + func = tp() # NULL function pointer + # raise a ValueError when we try to call it + self.assertRaises(ValueError, func) + if __name__ == '__main__': unittest.main() diff --git a/Lib/decimal.py b/Lib/decimal.py index 434930a..5e1b16b 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -524,6 +524,8 @@ class Decimal(_numbers.Real, _numbers.Inexact): Decimal("314") >>> Decimal(Decimal(314)) # another decimal instance Decimal("314") + >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay + Decimal("3.14") """ # Note that the coefficient, self._int, is actually stored as @@ -539,7 +541,7 @@ class Decimal(_numbers.Real, _numbers.Inexact): # From a string # REs insist on real strings, so we can too. if isinstance(value, str): - m = _parser(value) + m = _parser(value.strip()) if m is None: if context is None: context = getcontext() @@ -3542,7 +3544,16 @@ class Context(object): return rounding def create_decimal(self, num='0'): - """Creates a new Decimal instance but using self as context.""" + """Creates a new Decimal instance but using self as context. + + This method implements the to-number operation of the + IBM Decimal specification.""" + + if isinstance(num, str) and num != num.strip(): + return self._raise_error(ConversionSyntax, + "no trailing or leading whitespace is " + "permitted.") + d = Decimal(num, context=self) if d._isnan() and len(d._int) > self.prec - self._clamp: return self._raise_error(ConversionSyntax, @@ -5157,7 +5168,7 @@ _parser = re.compile(r""" # A numeric string consists of: (?P<diag>\d*) # with (possibly empty) diagnostic information. ) # \s* - $ + \Z """, re.VERBOSE | re.IGNORECASE).match _all_zeros = re.compile('0*$').match diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 0dd7f00..83fb337 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -429,6 +429,10 @@ class DecimalExplicitConstructionTest(unittest.TestCase): #just not a number self.assertEqual(str(Decimal('ugly')), 'NaN') + #leading and trailing whitespace permitted + self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4') + self.assertEqual(str(Decimal(' -7.89')), '-7.89') + def test_explicit_from_tuples(self): #zero @@ -517,6 +521,10 @@ class DecimalExplicitConstructionTest(unittest.TestCase): self.assertEqual(str(d), '456789') d = nc.create_decimal('456789') self.assertEqual(str(d), '4.57E+5') + # leading and trailing whitespace should result in a NaN; + # spaces are already checked in Cowlishaw's test-suite, so + # here we just check that a trailing newline results in a NaN + self.assertEqual(str(nc.create_decimal('3.14\n')), 'NaN') # from tuples d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) diff --git a/Lib/urlparse.py b/Lib/urlparse.py index 1f435b9..0c05dfe 100644 --- a/Lib/urlparse.py +++ b/Lib/urlparse.py @@ -37,46 +37,11 @@ _parse_cache = {} def clear_cache(): """Clear the parse cache.""" - global _parse_cache - _parse_cache = {} + _parse_cache.clear() -class BaseResult(tuple): - """Base class for the parsed result objects. - - This provides the attributes shared by the two derived result - objects as read-only properties. The derived classes are - responsible for checking the right number of arguments were - supplied to the constructor. - - """ - - __slots__ = () - - # Attributes that access the basic components of the URL: - - @property - def scheme(self): - return self[0] - - @property - def netloc(self): - return self[1] - - @property - def path(self): - return self[2] - - @property - def query(self): - return self[-2] - - @property - def fragment(self): - return self[-1] - - # Additional attributes that provide access to parsed-out portions - # of the netloc: +class ResultMixin(object): + """Shared methods for the parsed result objects.""" @property def username(self): @@ -116,31 +81,20 @@ class BaseResult(tuple): return int(port, 10) return None +from collections import namedtuple -class SplitResult(BaseResult): +class SplitResult(namedtuple('SplitResult', 'scheme netloc path query fragment'), ResultMixin): __slots__ = () - def __new__(cls, scheme, netloc, path, query, fragment): - return BaseResult.__new__( - cls, (scheme, netloc, path, query, fragment)) - def geturl(self): return urlunsplit(self) -class ParseResult(BaseResult): +class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin): __slots__ = () - def __new__(cls, scheme, netloc, path, params, query, fragment): - return BaseResult.__new__( - cls, (scheme, netloc, path, params, query, fragment)) - - @property - def params(self): - return self[3] - def geturl(self): return urlunparse(self) |