Skip to content

Commit 969fe57

Browse files
committed
Merged revisions 60245-60277 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r60246 | guido.van.rossum | 2008-01-24 18:58:05 +0100 (Thu, 24 Jan 2008) | 2 lines Fix test67.py from issue #1303614. ........ r60248 | raymond.hettinger | 2008-01-24 19:05:54 +0100 (Thu, 24 Jan 2008) | 1 line Clean-up and speed-up code by accessing numerator/denominator directly. There's no reason to enforce readonliness ........ r60249 | raymond.hettinger | 2008-01-24 19:12:23 +0100 (Thu, 24 Jan 2008) | 1 line Revert 60189 and restore performance. ........ r60250 | guido.van.rossum | 2008-01-24 19:21:02 +0100 (Thu, 24 Jan 2008) | 5 lines News about recently fixed crashers: - A few crashers fixed: weakref_in_del.py (issue #1377858); loosing_dict_ref.py (issue #1303614, test67.py); borrowed_ref_[34].py (not in tracker). ........ r60252 | thomas.heller | 2008-01-24 19:36:27 +0100 (Thu, 24 Jan 2008) | 7 lines Use a PyDictObject again for the array type cache; retrieving items from the WeakValueDictionary was slower by nearly a factor of 3. To avoid leaks, weakref proxies for the array types are put into the cache dict, with weakref callbacks that removes the entries when the type goes away. ........ r60253 | thomas.heller | 2008-01-24 19:54:12 +0100 (Thu, 24 Jan 2008) | 2 lines Replace Py_BuildValue with PyTuple_Pack because it is faster. Also add a missing DECREF. ........ r60254 | raymond.hettinger | 2008-01-24 20:05:29 +0100 (Thu, 24 Jan 2008) | 1 line Add support for trunc(). ........ r60255 | thomas.heller | 2008-01-24 20:15:02 +0100 (Thu, 24 Jan 2008) | 5 lines Invert the checks in get_[u]long and get_[u]longlong. The intent was to not accept float types; the result was that integer-like objects were not accepted. Ported from release25-maint. ........ r60256 | raymond.hettinger | 2008-01-24 20:30:19 +0100 (Thu, 24 Jan 2008) | 1 line Add support for int(r) just like the other numeric classes. ........ r60263 | raymond.hettinger | 2008-01-24 22:23:58 +0100 (Thu, 24 Jan 2008) | 1 line Expand tests to include nested graph structures. ........ r60264 | raymond.hettinger | 2008-01-24 22:47:56 +0100 (Thu, 24 Jan 2008) | 1 line Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures. ........ r60265 | amaury.forgeotdarc | 2008-01-24 23:51:18 +0100 (Thu, 24 Jan 2008) | 14 lines #1920: when considering a block starting by "while 0", the compiler optimized the whole construct away, even when an 'else' clause is present:: while 0: print("no") else: print("yes") did not generate any code at all. Now the compiler emits the 'else' block, like it already does for 'if' statements. Will backport. ........ r60266 | amaury.forgeotdarc | 2008-01-24 23:59:25 +0100 (Thu, 24 Jan 2008) | 2 lines News entry for r60265 (Issue 1920). ........ r60269 | raymond.hettinger | 2008-01-25 00:50:26 +0100 (Fri, 25 Jan 2008) | 1 line More code cleanup. Remove unnecessary indirection to useless class methods. ........ r60270 | raymond.hettinger | 2008-01-25 01:21:54 +0100 (Fri, 25 Jan 2008) | 1 line Add support for copy, deepcopy, and pickle. ........ r60271 | raymond.hettinger | 2008-01-25 01:33:45 +0100 (Fri, 25 Jan 2008) | 1 line Mark todos and review comments. ........ r60272 | raymond.hettinger | 2008-01-25 02:13:12 +0100 (Fri, 25 Jan 2008) | 1 line Add one other review comment. ........ r60273 | raymond.hettinger | 2008-01-25 02:23:38 +0100 (Fri, 25 Jan 2008) | 1 line Fix-up signature for approximation. ........ r60274 | raymond.hettinger | 2008-01-25 02:46:33 +0100 (Fri, 25 Jan 2008) | 1 line More design notes ........ r60276 | neal.norwitz | 2008-01-25 07:37:23 +0100 (Fri, 25 Jan 2008) | 6 lines Make the test more robust by trying to reconnect up to 3 times in case there were transient failures. This will hopefully silence the buildbots for this test. As we find other tests that have a problem, we can fix with a similar strategy assuming it is successful. It worked on my box in a loop for 10+ runs where it would have an exception otherwise. ........ r60277 | neal.norwitz | 2008-01-25 09:04:16 +0100 (Fri, 25 Jan 2008) | 4 lines Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64) and eliminate a compiler warning in floatobject.c. There might be a better way to go about this, but it should be good enough for now. ........
1 parent bad17e7 commit 969fe57

File tree

19 files changed

+675
-122
lines changed

19 files changed

+675
-122
lines changed

Lib/ctypes/test/test_numbers.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,31 @@ def test_byref(self):
105105
def test_floats(self):
106106
# c_float and c_double can be created from
107107
# Python int, long and float
108+
class FloatLike(object):
109+
def __float__(self):
110+
return 2.0
111+
f = FloatLike()
108112
for t in float_types:
109113
self.failUnlessEqual(t(2.0).value, 2.0)
110114
self.failUnlessEqual(t(2).value, 2.0)
111115
self.failUnlessEqual(t(2).value, 2.0)
116+
self.failUnlessEqual(t(f).value, 2.0)
112117

113118
def test_integers(self):
114-
# integers cannot be constructed from floats
119+
class FloatLike(object):
120+
def __float__(self):
121+
return 2.0
122+
f = FloatLike()
123+
class IntLike(object):
124+
def __int__(self):
125+
return 2
126+
i = IntLike()
127+
# integers cannot be constructed from floats,
128+
# but from integer-like objects
115129
for t in signed_types + unsigned_types:
116130
self.assertRaises(TypeError, t, 3.14)
131+
self.assertRaises(TypeError, t, f)
132+
self.failUnlessEqual(t(i).value, 2)
117133

118134
def test_sizes(self):
119135
for t in signed_types + unsigned_types + float_types + bool_types:

Lib/decimal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,8 @@ def __int__(self):
14511451
else:
14521452
return s*int(self._int[:self._exp] or '0')
14531453

1454+
__trunc__ = __int__
1455+
14541456
def _fix_nan(self, context):
14551457
"""Decapitate the payload of a NaN to fit the context"""
14561458
payload = self._int

Lib/pprint.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,31 @@ def _format(self, object, stream, indent, allowance, context, level):
164164
(issubclass(typ, set) and r is set.__repr__) or
165165
(issubclass(typ, frozenset) and r is frozenset.__repr__)
166166
):
167+
length = _len(object)
167168
if issubclass(typ, list):
168169
write('[')
169170
endchar = ']'
170171
elif issubclass(typ, set):
172+
if not length:
173+
write('set()')
174+
return
171175
write('{')
172176
endchar = '}'
173177
object = sorted(object)
174178
indent += 4
175179
elif issubclass(typ, frozenset):
180+
if not length:
181+
write('frozenset()')
182+
return
176183
write('frozenset([')
177184
endchar = '])'
178185
object = sorted(object)
179-
indent += 9
186+
indent += 10
180187
else:
181188
write('(')
182189
endchar = ')'
183190
if self._indent_per_level > 1:
184191
write((self._indent_per_level - 1) * ' ')
185-
length = _len(object)
186192
if length:
187193
context[objid] = 1
188194
indent = indent + self._indent_per_level

Lib/rational.py

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
RationalAbc = numbers.Rational
1414

1515

16-
def _gcd(a, b):
16+
def _gcd(a, b): # XXX This is a useful function. Consider making it public.
1717
"""Calculate the Greatest Common Divisor.
1818
1919
Unless b==0, the result will have the same sign as b (so that when
@@ -39,6 +39,8 @@ def _binary_float_to_ratio(x):
3939
>>> _binary_float_to_ratio(-.25)
4040
(-1, 4)
4141
"""
42+
# XXX Consider moving this to to floatobject.c
43+
# with a name like float.as_intger_ratio()
4244

4345
if x == 0:
4446
return 0, 1
@@ -79,6 +81,10 @@ def _binary_float_to_ratio(x):
7981
_RATIONAL_FORMAT = re.compile(
8082
r'^\s*(?P<sign>[-+]?)(?P<num>\d+)(?:/(?P<denom>\d+))?\s*$')
8183

84+
# XXX Consider accepting decimal strings as input since they are exact.
85+
# Rational("2.01") --> s="2.01" ; Rational.from_decimal(Decimal(s)) --> Rational(201, 100)"
86+
# If you want to avoid going through the decimal module, just parse the string directly:
87+
# s.partition('.') --> ('2', '.', '01') --> Rational(int('2'+'01'), 10**len('01')) --> Rational(201, 100)
8288

8389
class Rational(RationalAbc):
8490
"""This class implements rational numbers.
@@ -93,7 +99,7 @@ class Rational(RationalAbc):
9399
94100
"""
95101

96-
__slots__ = ('_numerator', '_denominator')
102+
__slots__ = ('numerator', 'denominator')
97103

98104
# We're immutable, so use __new__ not __init__
99105
def __new__(cls, numerator=0, denominator=1):
@@ -133,8 +139,8 @@ def __new__(cls, numerator=0, denominator=1):
133139
raise ZeroDivisionError('Rational(%s, 0)' % numerator)
134140

135141
g = _gcd(numerator, denominator)
136-
self._numerator = int(numerator // g)
137-
self._denominator = int(denominator // g)
142+
self.numerator = int(numerator // g)
143+
self.denominator = int(denominator // g)
138144
return self
139145

140146
@classmethod
@@ -192,29 +198,22 @@ def as_continued_fraction(self):
192198
n, d = d, n
193199
return cf
194200

195-
@classmethod
196-
def approximate_from_float(cls, f, max_denominator):
197-
'Best rational approximation to f with a denominator <= max_denominator'
201+
def approximate(self, max_denominator):
202+
'Best rational approximation with a denominator <= max_denominator'
198203
# XXX First cut at algorithm
199204
# Still needs rounding rules as specified at
200205
# http://en.wikipedia.org/wiki/Continued_fraction
201-
cf = cls.from_float(f).as_continued_fraction()
206+
if self.denominator <= max_denominator:
207+
return self
208+
cf = self.as_continued_fraction()
202209
result = Rational(0)
203210
for i in range(1, len(cf)):
204-
new = cls.from_continued_fraction(cf[:i])
211+
new = self.from_continued_fraction(cf[:i])
205212
if new.denominator > max_denominator:
206213
break
207214
result = new
208215
return result
209216

210-
@property
211-
def numerator(a):
212-
return a._numerator
213-
214-
@property
215-
def denominator(a):
216-
return a._denominator
217-
218217
def __repr__(self):
219218
"""repr(self)"""
220219
return ('Rational(%r,%r)' % (self.numerator, self.denominator))
@@ -226,6 +225,16 @@ def __str__(self):
226225
else:
227226
return '%s/%s' % (self.numerator, self.denominator)
228227

228+
""" XXX This section needs a lot more commentary
229+
230+
* Explain the typical sequence of checks, calls, and fallbacks.
231+
* Explain the subtle reasons why this logic was needed.
232+
* It is not clear how common cases are handled (for example, how
233+
does the ratio of two huge integers get converted to a float
234+
without overflowing the long-->float conversion.
235+
236+
"""
237+
229238
def _operator_fallbacks(monomorphic_operator, fallback_operator):
230239
"""Generates forward and reverse operators given a purely-rational
231240
operator and a function from the operator module.
@@ -299,18 +308,15 @@ def __rfloordiv__(b, a):
299308
"""a // b"""
300309
return math.floor(a / b)
301310

302-
@classmethod
303-
def _mod(cls, a, b):
304-
div = a // b
305-
return a - b * div
306-
307311
def __mod__(a, b):
308312
"""a % b"""
309-
return a._mod(a, b)
313+
div = a // b
314+
return a - b * div
310315

311316
def __rmod__(b, a):
312317
"""a % b"""
313-
return b._mod(a, b)
318+
div = a // b
319+
return a - b * div
314320

315321
def __pow__(a, b):
316322
"""a ** b
@@ -369,6 +375,8 @@ def __trunc__(a):
369375
else:
370376
return a.numerator // a.denominator
371377

378+
__int__ = __trunc__
379+
372380
def __floor__(a):
373381
"""Will be math.floor(a) in 3.0."""
374382
return a.numerator // a.denominator
@@ -410,6 +418,7 @@ def __hash__(self):
410418
float must have the same hash as that float.
411419
412420
"""
421+
# XXX since this method is expensive, consider caching the result
413422
if self.denominator == 1:
414423
# Get integers right.
415424
return hash(self.numerator)
@@ -481,3 +490,18 @@ def __ge__(a, b):
481490
def __bool__(a):
482491
"""a != 0"""
483492
return a.numerator != 0
493+
494+
# support for pickling, copy, and deepcopy
495+
496+
def __reduce__(self):
497+
return (self.__class__, (str(self),))
498+
499+
def __copy__(self):
500+
if type(self) == Rational:
501+
return self # I'm immutable; therefore I am my own clone
502+
return self.__class__(self.numerator, self.denominator)
503+
504+
def __deepcopy__(self, memo):
505+
if type(self) == Rational:
506+
return self # My components are also immutable
507+
return self.__class__(self.numerator, self.denominator)

Lib/test/crashers/loosing_dict_ref.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

Lib/test/test_decimal.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ def checkSameDec(operation, useOther=False):
11411141
checkSameDec("__floordiv__", True)
11421142
checkSameDec("__hash__")
11431143
checkSameDec("__int__")
1144+
checkSameDec("__trunc__")
11441145
checkSameDec("__mod__", True)
11451146
checkSameDec("__mul__", True)
11461147
checkSameDec("__neg__")
@@ -1204,6 +1205,16 @@ def test_int(self):
12041205
r = d.to_integral(ROUND_DOWN)
12051206
self.assertEqual(Decimal(int(d)), r)
12061207

1208+
def test_trunc(self):
1209+
for x in range(-250, 250):
1210+
s = '%0.2f' % (x / 100.0)
1211+
# should work the same as for floats
1212+
self.assertEqual(int(Decimal(s)), int(float(s)))
1213+
# should work the same as to_integral in the ROUND_DOWN mode
1214+
d = Decimal(s)
1215+
r = d.to_integral(ROUND_DOWN)
1216+
self.assertEqual(Decimal(trunc(d)), r)
1217+
12071218
class ContextAPItests(unittest.TestCase):
12081219

12091220
def test_pickle(self):

Lib/test/test_descr.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4248,6 +4248,29 @@ def __call__(self, *args):
42484248
finally:
42494249
builtins.__import__ = orig_import
42504250

4251+
def test_losing_dict_ref_segfault():
4252+
# This used to segfault;
4253+
# derived from issue #1303614, test67.py
4254+
if verbose:
4255+
print("Testing losing dict ref segfault...")
4256+
4257+
class Strange(object):
4258+
def __hash__(self):
4259+
return hash('hello')
4260+
4261+
def __eq__(self, other):
4262+
x.__dict__ = {} # the old x.__dict__ is deallocated
4263+
return False
4264+
4265+
class X(object):
4266+
pass
4267+
4268+
v = 123
4269+
x = X()
4270+
x.__dict__ = {Strange(): 42, 'hello': v+456}
4271+
x.hello
4272+
4273+
42514274
def test_main():
42524275
weakref_segfault() # Must be first, somehow
42534276
wrapper_segfault() # NB This one is slow
@@ -4348,6 +4371,7 @@ def test_main():
43484371
test_weakref_in_del_segfault()
43494372
test_borrowed_ref_3_segfault()
43504373
test_borrowed_ref_4_segfault()
4374+
test_losing_dict_ref_segfault()
43514375

43524376
if verbose: print("All OK")
43534377

Lib/test/test_grammar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,15 @@ def testWhile(self):
500500
while 0: pass
501501
else: pass
502502

503+
# Issue1920: "while 0" is optimized away,
504+
# ensure that the "else" clause is still present.
505+
x = 0
506+
while 0:
507+
x = 1
508+
else:
509+
x = 2
510+
self.assertEquals(x, 2)
511+
503512
def testFor(self):
504513
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
505514
for i in 1, 2, 3: pass

0 commit comments

Comments
 (0)