Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2380ac7

Browse files
committed
Merged revisions 59843-59863 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r59844 | raymond.hettinger | 2008-01-07 21:56:05 +0100 (Mon, 07 Jan 2008) | 1 line Use get() instead of pop() for the optimized version of _replace(). ........ r59847 | raymond.hettinger | 2008-01-07 22:33:51 +0100 (Mon, 07 Jan 2008) | 1 line Documentation nits. ........ r59849 | raymond.hettinger | 2008-01-08 03:02:05 +0100 (Tue, 08 Jan 2008) | 1 line Expand comment. ........ r59850 | raymond.hettinger | 2008-01-08 03:24:15 +0100 (Tue, 08 Jan 2008) | 1 line Docs on named tuple's naming conventions and limits of subclassing ........ r59851 | christian.heimes | 2008-01-08 04:40:04 +0100 (Tue, 08 Jan 2008) | 1 line It's verbose, not debug ........ r59852 | facundo.batista | 2008-01-08 13:25:20 +0100 (Tue, 08 Jan 2008) | 4 lines Issue #1757: The hash of a Decimal instance is no longer affected by the current context. Thanks Mark Dickinson. ........ r59853 | andrew.kuchling | 2008-01-08 15:30:55 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1137: allow assigning to .buffer_size attribute of PyExpat.parser objects ........ r59854 | andrew.kuchling | 2008-01-08 15:56:02 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1114: fix compilation of curses module on 64-bit AIX, and any other LP64 platforms where attr_t isn't a C long ........ r59856 | thomas.heller | 2008-01-08 16:15:09 +0100 (Tue, 08 Jan 2008) | 5 lines Use relative instead of absolute filenames in the C-level tracebacks. This prevents traceback prints pointing to files in this way: File "\loewis\25\python\Modules\_ctypes\callbacks.c", line 206, in 'calling callback function' ........ r59857 | christian.heimes | 2008-01-08 16:46:10 +0100 (Tue, 08 Jan 2008) | 2 lines Added __enter__ and __exit__ functions to HKEY object Added ExpandEnvironmentStrings to the _winreg module. ........ r59858 | georg.brandl | 2008-01-08 17:18:26 +0100 (Tue, 08 Jan 2008) | 2 lines Fix markup errors from r59857 and clarify key.__enter__/__exit__ docs ........ r59860 | georg.brandl | 2008-01-08 20:42:30 +0100 (Tue, 08 Jan 2008) | 2 lines Better method for associating .py files with the interpreter. ........ r59862 | facundo.batista | 2008-01-08 22:10:12 +0100 (Tue, 08 Jan 2008) | 9 lines Issue 846388. Adds a call to PyErr_CheckSignals to SRE_MATCH so that signal handlers can be invoked during long regular expression matches. It also adds a new error return value indicating that an exception occurred in a signal handler during the match, allowing exceptions in the signal handler to propagate up to the main loop. Thanks Josh Hoyt and Ralf Schmitt. ........
1 parent 790c823 commit 2380ac7

18 files changed

Lines changed: 436 additions & 72 deletions

Doc/library/_winreg.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ This module offers the following functions:
131131
+-------+--------------------------------------------+
132132

133133

134+
.. function:: ExpandEnvironmentStrings(unicode)
135+
136+
Expands environment strings %NAME% in unicode string like const:`REG_EXPAND_SZ`::
137+
138+
>>> ExpandEnvironmentStrings(u"%windir%")
139+
u"C:\\Windows"
140+
141+
.. versionadded:: 2.6
142+
143+
134144
.. function:: FlushKey(key)
135145

136146
Writes all the attributes of a key to the registry.
@@ -416,3 +426,16 @@ handle, and also disconnect the Windows handle from the handle object.
416426
handle is not closed. You would call this function when you need the
417427
underlying Win32 handle to exist beyond the lifetime of the handle object.
418428

429+
.. method:: PyHKEY.__enter__()
430+
PyHKEY.__exit__(\*exc_info)
431+
432+
The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
433+
supports the context protocol for the :keyword:`with` statement::
434+
435+
with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
436+
# ... work with key ...
437+
438+
will automatically close *key* when control leaves the :keyword:`with` block.
439+
440+
.. versionadded:: 2.6
441+

Doc/library/collections.rst

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ they add the ability to access fields by name instead of position index.
397397
method which lists the tuple contents in a ``name=value`` format.
398398

399399
The *fieldnames* are a single string with each fieldname separated by whitespace
400-
and/or commas (for example 'x y' or 'x, y'). Alternatively, the *fieldnames*
401-
can be specified with a sequence of strings (such as ['x', 'y']).
400+
and/or commas (for example 'x y' or 'x, y'). Alternatively, *fieldnames*
401+
can be a sequence of strings (such as ['x', 'y']).
402402

403403
Any valid Python identifier may be used for a fieldname except for names
404404
starting with an underscore. Valid identifiers consist of letters, digits,
@@ -477,7 +477,8 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
477477
print emp.name, emp.title
478478

479479
In addition to the methods inherited from tuples, named tuples support
480-
three additional methods and one attribute.
480+
three additional methods and one attribute. To prevent conflicts with
481+
field names, the method and attribute names start with an underscore.
481482

482483
.. method:: somenamedtuple._make(iterable)
483484

@@ -513,7 +514,7 @@ three additional methods and one attribute.
513514

514515
.. attribute:: somenamedtuple._fields
515516

516-
Tuple of strings listing the field names. This is useful for introspection
517+
Tuple of strings listing the field names. Useful for introspection
517518
and for creating new named tuple types from existing named tuples.
518519

519520
::
@@ -532,7 +533,7 @@ function::
532533
>>> getattr(p, 'x')
533534
11
534535

535-
When casting a dictionary to a named tuple, use the double-star-operator [#]_::
536+
To cast a dictionary to a named tuple, use the double-star-operator [#]_::
536537

537538
>>> d = {'x': 11, 'y': 22}
538539
>>> Point(**d)
@@ -557,14 +558,20 @@ a fixed-width print format::
557558
Point: x= 1.286 y= 6.000 hypot= 6.136
558559

559560
Another use for subclassing is to replace performance critcal methods with
560-
faster versions that bypass error-checking and localize variable access::
561+
faster versions that bypass error-checking and that localize variable access::
561562

562563
>>> class Point(namedtuple('Point', 'x y')):
563564
_make = classmethod(tuple.__new__)
564565
def _replace(self, _map=map, **kwds):
565-
return self._make(_map(kwds.pop, ('x', 'y'), self))
566+
return self._make(_map(kwds.get, ('x', 'y'), self))
566567

567-
Default values can be implemented by using :meth:`_replace`:: to
568+
569+
Subclassing is not useful for adding new, stored fields. Instead, simply
570+
create a new named tuple type from the :attr:`_fields` attribute::
571+
572+
>>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
573+
574+
Default values can be implemented by using :meth:`_replace` to
568575
customize a prototype instance::
569576

570577
>>> Account = namedtuple('Account', 'owner balance transaction_count')

Doc/library/pyexpat.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ XMLParser Objects
177177

178178
.. attribute:: xmlparser.buffer_size
179179

180-
The size of the buffer used when :attr:`buffer_text` is true. This value cannot
181-
be changed at this time.
180+
The size of the buffer used when :attr:`buffer_text` is true.
181+
A new buffer size can be set by assigning a new integer value
182+
to this attribute.
183+
When the size is changed, the buffer will be flushed.
184+
185+
.. versionchanged:: 2.6
186+
The buffer size can now be changed.
182187

183188

184189
.. attribute:: xmlparser.buffer_text

Doc/using/windows.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,17 @@ of your Python installation directory). This suppresses the terminal window on
188188
startup.
189189

190190
You can also make all ``.py`` scripts execute with :program:`pythonw.exe`,
191-
setting this through the usual facilites, for example (names might differ,
192-
depending on your version of Windows):
191+
setting this through the usual facilites, for example (might require
192+
administrative rights):
193193

194-
#. Open the context menu of a :file:`{*}.py` file.
195-
#. Click :menuselection:`Open with...`.
196-
#. Choose the interpreter of your choice (utilize :guilabel:`Other...` or
197-
:guilabel:`Choose Program...` if it is not in the list of default programs).
198-
#. Check :guilabel:`Always open files with this program`.
199-
#. Click :guilabel:`OK`.
194+
#. Launch a command prompt.
195+
#. Associate the correct file group with ``.py`` scripts::
196+
197+
assoc .py=Python.File
200198

199+
#. Redirect all Python files to the new executable::
200+
201+
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
201202

202203

203204
Additional modules

Doc/whatsnew/2.6.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,13 @@ complete list of changes, or look through the CVS logs for all the details.
875875
changed and :const:`UF_APPEND` to indicate that data can only be appended to the
876876
file. (Contributed by M. Levinson.)
877877

878+
* The :mod:`pyexpat` module's :class:`Parser` objects now allow setting
879+
their :attr:`buffer_size` attribute to change the size of the buffer
880+
used to hold character data.
881+
(Contributed by Achim Gaedke.)
882+
883+
.. Patch 1137
884+
878885
* The :mod:`random` module's :class:`Random` objects can
879886
now be pickled on a 32-bit system and unpickled on a 64-bit
880887
system, and vice versa. Unfortunately, this change also means

Lib/collections.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def namedtuple(typename, field_names, verbose=False):
3434
3535
"""
3636

37-
# Parse and validate the field names
37+
# Parse and validate the field names. Validation serves two purposes,
38+
# generating informative error messages and preventing template injection attacks.
3839
if isinstance(field_names, str):
3940
field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
4041
field_names = tuple(field_names)
@@ -129,7 +130,7 @@ class Point(namedtuple('Point', 'x y')):
129130
'Point class with optimized _make() and _replace() without error-checking'
130131
_make = classmethod(tuple.__new__)
131132
def _replace(self, _map=map, **kwds):
132-
return self._make(_map(kwds.pop, ('x', 'y'), self))
133+
return self._make(_map(kwds.get, ('x', 'y'), self))
133134

134135
print(Point(11, 22)._replace(x=100))
135136

Lib/decimal.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,10 @@ def compare(self, other, context=None):
809809
def __hash__(self):
810810
"""x.__hash__() <==> hash(x)"""
811811
# Decimal integers must hash the same as the ints
812-
# Non-integer decimals are normalized and hashed as strings
813-
# Normalization assures that hash(100E-1) == hash(10)
812+
#
813+
# The hash of a nonspecial noninteger Decimal must depend only
814+
# on the value of that Decimal, and not on its representation.
815+
# For example: hash(Decimal("100E-1")) == hash(Decimal("10")).
814816
if self._is_special:
815817
if self._isnan():
816818
raise TypeError('Cannot hash a NaN value.')
@@ -826,7 +828,13 @@ def __hash__(self):
826828
# 2**64-1. So we can replace hash((-1)**s*c*10**e) with
827829
# hash((-1)**s*c*pow(10, e, 2**64-1).
828830
return hash((-1)**op.sign*op.int*pow(10, op.exp, 2**64-1))
829-
return hash(str(self.normalize()))
831+
# The value of a nonzero nonspecial Decimal instance is
832+
# faithfully represented by the triple consisting of its sign,
833+
# its adjusted exponent, and its coefficient with trailing
834+
# zeros removed.
835+
return hash((self._sign,
836+
self._exp+len(self._int),
837+
self._int.rstrip('0')))
830838

831839
def as_tuple(self):
832840
"""Represents the number as a triple tuple.

Lib/test/test_decimal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,23 @@ def test_hash_method(self):
971971
self.assert_(hash(Decimal('Inf')))
972972
self.assert_(hash(Decimal('-Inf')))
973973

974+
# check that the value of the hash doesn't depend on the
975+
# current context (issue #1757)
976+
c = getcontext()
977+
old_precision = c.prec
978+
x = Decimal("123456789.1")
979+
980+
c.prec = 6
981+
h1 = hash(x)
982+
c.prec = 10
983+
h2 = hash(x)
984+
c.prec = 16
985+
h3 = hash(x)
986+
987+
self.assertEqual(h1, h2)
988+
self.assertEqual(h1, h3)
989+
c.prec = old_precision
990+
974991
def test_min_and_max_methods(self):
975992

976993
d1 = Decimal('15.32')

Lib/test/test_pyexpat.py

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# handler, are obscure and unhelpful.
33

44
from io import BytesIO
5+
import sys
56
import unittest
67

78
import pyexpat
@@ -385,6 +386,130 @@ def handler(text):
385386

386387
self.assertRaises(Exception, parser.Parse, xml)
387388

389+
class ChardataBufferTest(unittest.TestCase):
390+
"""
391+
test setting of chardata buffer size
392+
"""
393+
394+
def test_1025_bytes(self):
395+
self.assertEquals(self.small_buffer_test(1025), 2)
396+
397+
def test_1000_bytes(self):
398+
self.assertEquals(self.small_buffer_test(1000), 1)
399+
400+
def test_wrong_size(self):
401+
parser = expat.ParserCreate()
402+
parser.buffer_text = 1
403+
def f(size):
404+
parser.buffer_size = size
405+
406+
self.assertRaises(ValueError, f, -1)
407+
self.assertRaises(ValueError, f, 0)
408+
409+
def test_unchanged_size(self):
410+
xml1 = ("<?xml version='1.0' encoding='iso8859'?><s>%s" % ('a' * 512))
411+
xml2 = 'a'*512 + '</s>'
412+
parser = expat.ParserCreate()
413+
parser.CharacterDataHandler = self.counting_handler
414+
parser.buffer_size = 512
415+
parser.buffer_text = 1
416+
417+
# Feed 512 bytes of character data: the handler should be called
418+
# once.
419+
self.n = 0
420+
parser.Parse(xml1)
421+
self.assertEquals(self.n, 1)
422+
423+
# Reassign to buffer_size, but assign the same size.
424+
parser.buffer_size = parser.buffer_size
425+
self.assertEquals(self.n, 1)
426+
427+
# Try parsing rest of the document
428+
parser.Parse(xml2)
429+
self.assertEquals(self.n, 2)
430+
431+
432+
def test_disabling_buffer(self):
433+
xml1 = "<?xml version='1.0' encoding='iso8859'?><a>%s" % ('a' * 512)
434+
xml2 = ('b' * 1024)
435+
xml3 = "%s</a>" % ('c' * 1024)
436+
parser = expat.ParserCreate()
437+
parser.CharacterDataHandler = self.counting_handler
438+
parser.buffer_text = 1
439+
parser.buffer_size = 1024
440+
self.assertEquals(parser.buffer_size, 1024)
441+
442+
# Parse one chunk of XML
443+
self.n = 0
444+
parser.Parse(xml1, 0)
445+
self.assertEquals(parser.buffer_size, 1024)
446+
self.assertEquals(self.n, 1)
447+
448+
# Turn off buffering and parse the next chunk.
449+
parser.buffer_text = 0
450+
self.assertFalse(parser.buffer_text)
451+
self.assertEquals(parser.buffer_size, 1024)
452+
for i in range(10):
453+
parser.Parse(xml2, 0)
454+
self.assertEquals(self.n, 11)
455+
456+
parser.buffer_text = 1
457+
self.assertTrue(parser.buffer_text)
458+
self.assertEquals(parser.buffer_size, 1024)
459+
parser.Parse(xml3, 1)
460+
self.assertEquals(self.n, 12)
461+
462+
463+
464+
def make_document(self, bytes):
465+
return ("<?xml version='1.0'?><tag>" + bytes * 'a' + '</tag>')
466+
467+
def counting_handler(self, text):
468+
self.n += 1
469+
470+
def small_buffer_test(self, buffer_len):
471+
xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * buffer_len)
472+
parser = expat.ParserCreate()
473+
parser.CharacterDataHandler = self.counting_handler
474+
parser.buffer_size = 1024
475+
parser.buffer_text = 1
476+
477+
self.n = 0
478+
parser.Parse(xml)
479+
return self.n
480+
481+
def test_change_size_1(self):
482+
xml1 = "<?xml version='1.0' encoding='iso8859'?><a><s>%s" % ('a' * 1024)
483+
xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025)
484+
parser = expat.ParserCreate()
485+
parser.CharacterDataHandler = self.counting_handler
486+
parser.buffer_text = 1
487+
parser.buffer_size = 1024
488+
self.assertEquals(parser.buffer_size, 1024)
489+
490+
self.n = 0
491+
parser.Parse(xml1, 0)
492+
parser.buffer_size *= 2
493+
self.assertEquals(parser.buffer_size, 2048)
494+
parser.Parse(xml2, 1)
495+
self.assertEquals(self.n, 2)
496+
497+
def test_change_size_2(self):
498+
xml1 = "<?xml version='1.0' encoding='iso8859'?><a>a<s>%s" % ('a' * 1023)
499+
xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025)
500+
parser = expat.ParserCreate()
501+
parser.CharacterDataHandler = self.counting_handler
502+
parser.buffer_text = 1
503+
parser.buffer_size = 2048
504+
self.assertEquals(parser.buffer_size, 2048)
505+
506+
self.n=0
507+
parser.Parse(xml1, 0)
508+
parser.buffer_size = parser.buffer_size // 2
509+
self.assertEquals(parser.buffer_size, 1024)
510+
parser.Parse(xml2, 1)
511+
self.assertEquals(self.n, 4)
512+
388513

389514
def test_main():
390515
run_unittest(SetAttributeTest,
@@ -394,7 +519,8 @@ def test_main():
394519
BufferTextTest,
395520
HandlerExceptionTest,
396521
PositionTest,
397-
sf1296433Test)
522+
sf1296433Test,
523+
ChardataBufferTest)
398524

399525
if __name__ == "__main__":
400526
test_main()

Lib/test/test_socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ def isTipcAvailable():
11331133
for line in f:
11341134
if line.startswith("tipc "):
11351135
return True
1136-
if test_support.debug:
1136+
if test_support.verbose:
11371137
print("TIPC module is not loaded, please 'sudo modprobe tipc'")
11381138
return False
11391139

0 commit comments

Comments
 (0)