diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/distutils/util.py | 8 | ||||
-rw-r--r-- | Lib/inspect.py | 5 | ||||
-rwxr-xr-x | Lib/pydoc.py | 57 | ||||
-rw-r--r-- | Lib/re.py | 6 | ||||
-rw-r--r-- | Lib/test/test_deque.py | 21 | ||||
-rw-r--r-- | Lib/test/test_dict.py | 14 | ||||
-rw-r--r-- | Lib/test/test_file.py | 13 | ||||
-rw-r--r-- | Lib/test/test_fileio.py | 1 | ||||
-rw-r--r-- | Lib/test/test_set.py | 17 | ||||
-rw-r--r-- | Lib/test/test_struct.py | 7 | ||||
-rw-r--r-- | Lib/textwrap.py | 26 |
11 files changed, 155 insertions, 20 deletions
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py index 48cc17f624..e9d29ff49c 100644 --- a/Lib/distutils/util.py +++ b/Lib/distutils/util.py @@ -140,9 +140,13 @@ def get_platform (): # 'universal' instead of 'fat'. machine = 'fat' + cflags = get_config_vars().get('CFLAGS') - if '-arch x86_64' in get_config_vars().get('CFLAGS'): - machine = 'universal' + if '-arch x86_64' in cflags: + if '-arch i386' in cflags: + machine = 'universal' + else: + machine = 'fat64' elif machine in ('PowerPC', 'Power_Macintosh'): # Pick a sane name for the PPC architecture. diff --git a/Lib/inspect.py b/Lib/inspect.py index d3d946d525..1685bfc98d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -158,9 +158,8 @@ def isgeneratorfunction(object): Generator function objects provides same attributes as functions. See isfunction.__doc__ for attributes listing.""" - if (isfunction(object) or ismethod(object)) and \ - object.func_code.co_flags & CO_GENERATOR: - return True + return bool((isfunction(object) or ismethod(object)) and + object.func_code.co_flags & CO_GENERATOR) def isgenerator(object): """Return true if the object is a generator. diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 179026b051..dda0208edf 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1574,6 +1574,42 @@ class Helper: 'with': ('with', 'CONTEXTMANAGERS EXCEPTIONS yield'), 'yield': ('yield', ''), } + # Either add symbols to this dictionary or to the symbols dictionary + # directly: Whichever is easier. They are merged later. + _symbols_inverse = { + 'STRINGS' : ("'", "'''", "r'", "u'", '"""', '"', 'r"', 'u"'), + 'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&', + '|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'), + 'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'), + 'UNARY' : ('-', '~'), + 'AUGMENTEDASSIGNMENT' : ('+=', '-=', '*=', '/=', '%=', '&=', '|=', + '^=', '<<=', '>>=', '**=', '//='), + 'BITWISE' : ('<<', '>>', '&', '|', '^', '~'), + 'COMPLEX' : ('j', 'J') + } + symbols = { + '%': 'OPERATORS FORMATTING', + '**': 'POWER', + ',': 'TUPLES LISTS FUNCTIONS', + '.': 'ATTRIBUTES FLOAT MODULES OBJECTS', + '...': 'ELLIPSIS', + ':': 'SLICINGS DICTIONARYLITERALS', + '@': 'def class', + '\\': 'STRINGS', + '_': 'PRIVATENAMES', + '__': 'PRIVATENAMES SPECIALMETHODS', + '`': 'BACKQUOTES', + '(': 'TUPLES FUNCTIONS CALLS', + ')': 'TUPLES FUNCTIONS CALLS', + '[': 'LISTS SUBSCRIPTS SLICINGS', + ']': 'LISTS SUBSCRIPTS SLICINGS' + } + for topic, symbols_ in _symbols_inverse.iteritems(): + for symbol in symbols_: + topics = symbols.get(symbol, topic) + if topic not in topics: + topics = topics + ' ' + topic + symbols[symbol] = topics topics = { 'TYPES': ('types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS ' @@ -1717,10 +1753,12 @@ has the same effect as typing a particular string at the help> prompt. if type(request) is type(''): if request == 'help': self.intro() elif request == 'keywords': self.listkeywords() + elif request == 'symbols': self.listsymbols() elif request == 'topics': self.listtopics() elif request == 'modules': self.listmodules() elif request[:8] == 'modules ': self.listmodules(split(request)[1]) + elif request in self.symbols: self.showsymbol(request) elif request in self.keywords: self.showtopic(request) elif request in self.topics: self.showtopic(request) elif request: doc(request, 'Help on %s:') @@ -1766,6 +1804,14 @@ Here is a list of the Python keywords. Enter any keyword to get more help. ''') self.list(self.keywords.keys()) + def listsymbols(self): + self.output.write(''' +Here is a list of the punctuation symbols which Python assigns special meaning +to. Enter any symbol to get more help. + +''') + self.list(self.symbols.keys()) + def listtopics(self): self.output.write(''' Here is a list of available topics. Enter any topic name to get more help. @@ -1773,7 +1819,7 @@ Here is a list of available topics. Enter any topic name to get more help. ''') self.list(self.topics.keys()) - def showtopic(self, topic): + def showtopic(self, topic, more_xrefs=''): try: import pydoc_topics except ImportError: @@ -1787,7 +1833,7 @@ module "pydoc_topics" could not be found. self.output.write('no documentation found for %s\n' % repr(topic)) return if type(target) is type(''): - return self.showtopic(target) + return self.showtopic(target, more_xrefs) label, xrefs = target try: @@ -1796,6 +1842,8 @@ module "pydoc_topics" could not be found. self.output.write('no documentation found for %s\n' % repr(topic)) return pager(strip(doc) + '\n') + if more_xrefs: + xrefs = (xrefs or '') + ' ' + more_xrefs if xrefs: import StringIO, formatter buffer = StringIO.StringIO() @@ -1803,6 +1851,11 @@ module "pydoc_topics" could not be found. 'Related help topics: ' + join(split(xrefs), ', ') + '\n') self.output.write('\n%s\n' % buffer.getvalue()) + def showsymbol(self, symbol): + target = self.symbols[symbol] + topic, _, xrefs = target.partition(' ') + self.showtopic(topic, xrefs) + def listmodules(self, key=''): if key: self.output.write(''' @@ -145,7 +145,8 @@ def sub(pattern, repl, string, count=0): """Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; - if a callable, it's passed the match object and must return + if a string, backslash escapes in it are processed. If it is + a callable, it's passed the match object and must return a replacement string to be used.""" return _compile(pattern, 0).sub(repl, string, count) @@ -155,7 +156,8 @@ def subn(pattern, repl, string, count=0): non-overlapping occurrences of the pattern in the source string by the replacement repl. number is the number of substitutions that were made. repl can be either a string or a - callable; if a callable, it's passed the match object and must + callable; if a string, backslash escapes in it are processed. + If it is a callable, it's passed the match object and must return a replacement string to be used.""" return _compile(pattern, 0).subn(repl, string, count) diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index 0f0d09847e..4e2de3d005 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -1,7 +1,8 @@ from collections import deque import unittest from test import test_support, seq_tests -from weakref import proxy +import gc +import weakref import copy import cPickle as pickle import random @@ -418,6 +419,22 @@ class TestBasic(unittest.TestCase): d.append(1) gc.collect() + def test_container_iterator(self): + # Bug # XXX: tp_traverse was not implemented for deque iterator objects + class C(object): + pass + for i in range(2): + obj = C() + ref = weakref.ref(obj) + if i == 0: + container = deque([obj, 1]) + else: + container = reversed(deque([obj, 1])) + obj.x = iter(container) + del obj, container + gc.collect() + self.assert_(ref() is None, "Cycle was not collected") + class TestVariousIteratorArgs(unittest.TestCase): def test_constructor(self): @@ -528,7 +545,7 @@ class TestSubclass(unittest.TestCase): def test_weakref(self): d = deque('gallahad') - p = proxy(d) + p = weakref.proxy(d) self.assertEqual(str(p), str(d)) d = None self.assertRaises(ReferenceError, str, p) diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index f715657d36..14d62f5024 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -2,6 +2,7 @@ import unittest from test import test_support import UserDict, random, string +import gc, weakref class DictTest(unittest.TestCase): @@ -554,6 +555,19 @@ class DictTest(unittest.TestCase): pass d = {} + def test_container_iterator(self): + # Bug # XXX: tp_traverse was not implemented for dictiter objects + class C(object): + pass + iterators = (dict.iteritems, dict.itervalues, dict.iterkeys) + for i in iterators: + obj = C() + ref = weakref.ref(obj) + container = {obj: 1} + obj.x = i(container) + del obj, container + gc.collect() + self.assert_(ref() is None, "Cycle was not collected") from test import mapping_tests diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index 96f6da2023..b4f494ba96 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -125,6 +125,19 @@ class AutoFileTests(unittest.TestCase): class OtherFileTests(unittest.TestCase): + def testOpenDir(self): + this_dir = os.path.dirname(__file__) + for mode in (None, "w"): + try: + if mode: + f = open(this_dir, mode) + else: + f = open(this_dir) + except IOError as e: + self.assertEqual(e.filename, this_dir) + else: + self.fail("opening a directory didn't raise an IOError") + def testModeStrings(self): # check invalid mode strings for mode in ("", "aU", "wU+"): diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index c9787795e3..d8cf415ed8 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -109,6 +109,7 @@ class AutoFileTests(unittest.TestCase): _fileio._FileIO('.', 'r') except IOError as e: self.assertNotEqual(e.errno, 0) + self.assertEqual(e.filename, ".") else: self.fail("Should have raised IOError") diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index d38a675907..8d05712365 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -1,6 +1,7 @@ import unittest from test import test_support -from weakref import proxy +import gc +import weakref import operator import copy import pickle @@ -322,6 +323,18 @@ class TestJointOps(unittest.TestCase): self.assertEqual(sum(elem.hash_count for elem in d), n) self.assertEqual(d3, dict.fromkeys(d, 123)) + def test_container_iterator(self): + # Bug # XXX: tp_traverse was not implemented for set iterator object + class C(object): + pass + obj = C() + ref = weakref.ref(obj) + container = set([obj, 1]) + obj.x = iter(container) + del obj, container + gc.collect() + self.assert_(ref() is None, "Cycle was not collected") + class TestSet(TestJointOps): thetype = set @@ -538,7 +551,7 @@ class TestSet(TestJointOps): def test_weakref(self): s = self.thetype('gallahad') - p = proxy(s) + p = weakref.proxy(s) self.assertEqual(str(p), str(s)) s = None self.assertRaises(ReferenceError, str, p) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 232bffc275..7f5f08b4c6 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -2,6 +2,8 @@ import array import unittest import struct import warnings +warnings.filterwarnings("ignore", "struct integer overflow masking is deprecated", + DeprecationWarning) from functools import wraps from test.test_support import TestFailed, verbose, run_unittest @@ -461,6 +463,11 @@ class StructTest(unittest.TestCase): self.check_float_coerce(endian + fmt, 1.0) self.check_float_coerce(endian + fmt, 1.5) + def test_issue4228(self): + # Packing a long may yield either 32 or 64 bits + x = struct.pack('L', -1)[:4] + self.assertEqual(x, '\xff'*4) + def test_unpack_from(self): test_string = 'abcd01234' fmt = '4s' diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 192b43b1df..64a5b97c92 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -84,7 +84,7 @@ class TextWrapper: # splits into # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option! # (after stripping out empty strings). - wordsep_re = ( + wordsep_re = re.compile( r'(\s+|' # any whitespace r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash @@ -93,7 +93,7 @@ class TextWrapper: # "Hello there -- you goof-ball, use the -b option!" # splits into # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/ - wordsep_simple_re = r'(\s+)' + wordsep_simple_re = re.compile(r'(\s+)') # XXX this is not locale- or charset-aware -- string.lowercase # is US-ASCII only (and therefore English-only) @@ -124,6 +124,13 @@ class TextWrapper: self.drop_whitespace = drop_whitespace self.break_on_hyphens = break_on_hyphens + # recompile the regexes for Unicode mode -- done in this clumsy way for + # backwards compatibility because it's rather common to monkey-patch + # the TextWrapper class' wordsep_re attribute. + self.wordsep_re_uni = re.compile(self.wordsep_re.pattern, re.U) + self.wordsep_simple_re_uni = re.compile( + self.wordsep_simple_re.pattern, re.U) + # -- Private methods ----------------------------------------------- # (possibly useful for subclasses to override) @@ -160,12 +167,17 @@ class TextWrapper: 'use', ' ', 'the', ' ', '-b', ' ', option!' otherwise. """ - flags = re.UNICODE if isinstance(text, unicode) else 0 - if self.break_on_hyphens: - pat = self.wordsep_re + if isinstance(text, unicode): + if self.break_on_hyphens: + pat = self.wordsep_re_uni + else: + pat = self.wordsep_simple_re_uni else: - pat = self.wordsep_simple_re - chunks = re.compile(pat, flags).split(text) + if self.break_on_hyphens: + pat = self.wordsep_re + else: + pat = self.wordsep_simple_re + chunks = pat.split(text) chunks = filter(None, chunks) # remove empty chunks return chunks |