diff options
Diffstat (limited to 'Lib')
51 files changed, 1250 insertions, 934 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py index 283bdcf..f29b5da 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -5,7 +5,6 @@ default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention). Use setfirstweekday() to set the first day of the week (0=Monday, 6=Sunday).""" -from __future__ import with_statement import sys import datetime import locale as _locale diff --git a/Lib/contextlib.py b/Lib/contextlib.py index aeec40e..647efdd 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -1,6 +1,7 @@ """Utilities for with-statement contexts. See PEP 343.""" import sys +from functools import wraps __all__ = ["contextmanager", "nested", "closing"] @@ -77,14 +78,9 @@ def contextmanager(func): <cleanup> """ + @wraps(func) def helper(*args, **kwds): return GeneratorContextManager(func(*args, **kwds)) - try: - helper.__name__ = func.__name__ - helper.__doc__ = func.__doc__ - helper.__dict__ = func.__dict__ - except: - pass return helper diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py new file mode 100644 index 0000000..692d0f0 --- /dev/null +++ b/Lib/ctypes/test/test_frombuffer.py @@ -0,0 +1,81 @@ +from ctypes import * +import array +import gc +import unittest + +class X(Structure): + _fields_ = [("c_int", c_int)] + init_called = False + def __init__(self): + self._init_called = True + +class Test(unittest.TestCase): + def test_fom_buffer(self): + a = array.array("i", range(16)) + x = (c_int * 16).from_buffer(a) + + y = X.from_buffer(a) + self.assertEqual(y.c_int, a[0]) + self.failIf(y.init_called) + + self.assertEqual(x[:], a.tolist()) + + a[0], a[-1] = 200, -200 + self.assertEqual(x[:], a.tolist()) + + self.assert_(a in x._objects.values()) + + self.assertRaises(ValueError, + c_int.from_buffer, a, -1) + + expected = x[:] + del a; gc.collect(); gc.collect(); gc.collect() + self.assertEqual(x[:], expected) + + self.assertRaises(TypeError, + (c_char * 16).from_buffer, "a" * 16) + + def test_fom_buffer_with_offset(self): + a = array.array("i", range(16)) + x = (c_int * 15).from_buffer(a, sizeof(c_int)) + + self.assertEqual(x[:], a.tolist()[1:]) + self.assertRaises(ValueError, lambda: (c_int * 16).from_buffer(a, sizeof(c_int))) + self.assertRaises(ValueError, lambda: (c_int * 1).from_buffer(a, 16 * sizeof(c_int))) + + def BROKEN_test_from_buffer_copy(self): + a = array.array("i", range(16)) + x = (c_int * 16).from_buffer_copy(a) + + y = X.from_buffer_copy(a) + self.assertEqual(y.c_int, a[0]) + self.failIf(y.init_called) + + self.assertEqual(x[:], list(range(16))) + + a[0], a[-1] = 200, -200 + self.assertEqual(x[:], list(range(16))) + + self.assertEqual(x._objects, None) + + self.assertRaises(ValueError, + c_int.from_buffer, a, -1) + + del a; gc.collect(); gc.collect(); gc.collect() + self.assertEqual(x[:], list(range(16))) + + x = (c_char * 16).from_buffer_copy("a" * 16) + self.assertEqual(x[:], "a" * 16) + + def test_fom_buffer_copy_with_offset(self): + a = array.array("i", range(16)) + x = (c_int * 15).from_buffer_copy(a, sizeof(c_int)) + + self.assertEqual(x[:], a.tolist()[1:]) + self.assertRaises(ValueError, + (c_int * 16).from_buffer_copy, a, sizeof(c_int)) + self.assertRaises(ValueError, + (c_int * 1).from_buffer_copy, a, 16 * sizeof(c_int)) + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/decimal.py b/Lib/decimal.py index a2e7fa4..f008b5a 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -478,11 +478,7 @@ def localcontext(ctx=None): # General Decimal Arithmetic Specification return +s # Convert result to normal context - """ - # The string below can't be included in the docstring until Python 2.6 - # as the doctest module doesn't understand __future__ statements - """ - >>> from __future__ import with_statement + >>> setcontext(DefaultContext) >>> print(getcontext().prec) 28 >>> with localcontext(): @@ -3618,9 +3614,9 @@ class Context(object): if _ignored_flags is None: _ignored_flags = [] if not isinstance(flags, dict): - flags = dict([(s,s in flags) for s in _signals]) + flags = dict([(s, int(s in flags)) for s in _signals]) if traps is not None and not isinstance(traps, dict): - traps = dict([(s,s in traps) for s in _signals]) + traps = dict([(s, int(s in traps)) for s in _signals]) for name, val in locals().items(): if val is None: setattr(self, name, _copy.copy(getattr(DefaultContext, name))) diff --git a/Lib/distutils/command/bdist_wininst.py b/Lib/distutils/command/bdist_wininst.py index bf8d022..3f0e09b 100644 --- a/Lib/distutils/command/bdist_wininst.py +++ b/Lib/distutils/command/bdist_wininst.py @@ -48,6 +48,10 @@ class bdist_wininst(Command): "Fully qualified filename of a script to be run before " "any files are installed. This script need not be in the " "distribution"), + ('user-access-control=', None, + "specify Vista's UAC handling - 'none'/default=no " + "handling, 'auto'=use UAC if target Python installed for " + "all users, 'force'=always use UAC"), ] boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize', @@ -66,6 +70,7 @@ class bdist_wininst(Command): self.skip_build = 0 self.install_script = None self.pre_install_script = None + self.user_access_control = None def finalize_options(self): @@ -211,6 +216,8 @@ class bdist_wininst(Command): lines.append("target_optimize=%d" % (not self.no_target_optimize)) if self.target_version: lines.append("target_version=%s" % self.target_version) + if self.user_access_control: + lines.append("user_access_control=%s" % self.user_access_control) title = self.title or self.distribution.get_fullname() lines.append("title=%s" % escape(title)) diff --git a/Lib/distutils/command/wininst-6.0.exe b/Lib/distutils/command/wininst-6.0.exe Binary files differindex bd71525..10c9819 100644 --- a/Lib/distutils/command/wininst-6.0.exe +++ b/Lib/distutils/command/wininst-6.0.exe diff --git a/Lib/distutils/command/wininst-7.1.exe b/Lib/distutils/command/wininst-7.1.exe Binary files differindex ee35713..6779aa8 100644 --- a/Lib/distutils/command/wininst-7.1.exe +++ b/Lib/distutils/command/wininst-7.1.exe diff --git a/Lib/distutils/command/wininst-9.0-amd64.exe b/Lib/distutils/command/wininst-9.0-amd64.exe Binary files differindex c99ede4..b4cb062 100644 --- a/Lib/distutils/command/wininst-9.0-amd64.exe +++ b/Lib/distutils/command/wininst-9.0-amd64.exe diff --git a/Lib/distutils/command/wininst-9.0.exe b/Lib/distutils/command/wininst-9.0.exe Binary files differindex 5e0144c..0d04a66 100644 --- a/Lib/distutils/command/wininst-9.0.exe +++ b/Lib/distutils/command/wininst-9.0.exe diff --git a/Lib/distutils/msvc9compiler.py b/Lib/distutils/msvc9compiler.py index 8b1cf9a..c8d52c4 100644 --- a/Lib/distutils/msvc9compiler.py +++ b/Lib/distutils/msvc9compiler.py @@ -594,14 +594,25 @@ class MSVCCompiler(CCompiler) : # needed! Make sure they are generated in the temporary build # directory. Since they have different names for debug and release # builds, they can go into the same directory. + build_temp = os.path.dirname(objects[0]) if export_symbols is not None: (dll_name, dll_ext) = os.path.splitext( os.path.basename(output_filename)) implib_file = os.path.join( - os.path.dirname(objects[0]), + build_temp, self.library_filename(dll_name)) ld_args.append ('/IMPLIB:' + implib_file) + # Embedded manifests are recommended - see MSDN article titled + # "How to: Embed a Manifest Inside a C/C++ Application" + # (currently at http://msdn2.microsoft.com/en-us/library/ms235591(VS.80).aspx) + # Ask the linker to generate the manifest in the temp dir, so + # we can embed it later. + temp_manifest = os.path.join( + build_temp, + os.path.basename(output_filename) + ".manifest") + ld_args.append('/MANIFESTFILE:' + temp_manifest) + if extra_preargs: ld_args[:0] = extra_preargs if extra_postargs: @@ -613,6 +624,18 @@ class MSVCCompiler(CCompiler) : except DistutilsExecError as msg: raise LinkError(msg) + # embed the manifest + # XXX - this is somewhat fragile - if mt.exe fails, distutils + # will still consider the DLL up-to-date, but it will not have a + # manifest. Maybe we should link to a temp file? OTOH, that + # implies a build environment error that shouldn't go undetected. + mfid = 1 if target_desc == CCompiler.EXECUTABLE else 2 + out_arg = '-outputresource:%s;%s' % (output_filename, mfid) + try: + self.spawn(['mt.exe', '-nologo', '-manifest', + temp_manifest, out_arg]) + except DistutilsExecError as msg: + raise LinkError(msg) else: log.debug("skipping %s (up-to-date)", output_filename) diff --git a/Lib/getpass.py b/Lib/getpass.py index 3331c97..857188f 100644 --- a/Lib/getpass.py +++ b/Lib/getpass.py @@ -1,7 +1,10 @@ """Utilities to get a password and/or the current user name. -getpass(prompt) - prompt for a password, with echo turned off -getuser() - get the user name from the environment or password database +getpass(prompt[, stream]) - Prompt for a password, with echo turned off. +getuser() - Get the user name from the environment or password database. + +GetPassWarning - This UserWarning is issued when getpass() cannot prevent + echoing of the password contents while reading. On Windows, the msvcrt module will be used. On the Mac EasyDialogs.AskPassword is used, if available. @@ -10,34 +13,70 @@ On the Mac EasyDialogs.AskPassword is used, if available. # Authors: Piers Lauder (original) # Guido van Rossum (Windows support and cleanup) +# Gregory P. Smith (tty support & GetPassWarning) -import sys +import os, sys, warnings -__all__ = ["getpass","getuser"] +__all__ = ["getpass","getuser","GetPassWarning"] -def unix_getpass(prompt='Password: ', stream=None): - """Prompt for a password, with echo turned off. - The prompt is written on stream, by default stdout. - Restore terminal settings at end. - """ - if stream is None: - stream = sys.stdout +class GetPassWarning(UserWarning): pass - try: - fd = sys.stdin.fileno() - except: - return default_getpass(prompt) - old = termios.tcgetattr(fd) # a copy to save - new = old[:] +def unix_getpass(prompt='Password: ', stream=None): + """Prompt for a password, with echo turned off. - new[3] = new[3] & ~termios.ECHO # 3 == 'lflags' + Args: + prompt: Written on stream to ask for the input. Default: 'Password: ' + stream: A writable file object to display the prompt. Defaults to + the tty. If no tty is available defaults to sys.stderr. + Returns: + The seKr3t input. + Raises: + EOFError: If our input tty or stdin was closed. + GetPassWarning: When we were unable to turn echo off on the input. + + Always restores terminal settings before returning. + """ + fd = None + tty = None try: - termios.tcsetattr(fd, termios.TCSADRAIN, new) - passwd = _raw_input(prompt, stream) - finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old) + # Always try reading and writing directly on the tty first. + fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY) + tty = os.fdopen(fd, 'w+', 1) + input = tty + if not stream: + stream = tty + except EnvironmentError as e: + # If that fails, see if stdin can be controlled. + try: + fd = sys.stdin.fileno() + except: + passwd = fallback_getpass(prompt, stream) + input = sys.stdin + if not stream: + stream = sys.stderr + + if fd is not None: + passwd = None + try: + old = termios.tcgetattr(fd) # a copy to save + new = old[:] + new[3] &= ~termios.ECHO # 3 == 'lflags' + try: + termios.tcsetattr(fd, termios.TCSADRAIN, new) + passwd = _raw_input(prompt, stream, input=input) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old) + except termios.error as e: + if passwd is not None: + # _raw_input succeeded. The final tcsetattr failed. Reraise + # instead of leaving the terminal in an unknown state. + raise + # We can't control the tty or stdin. Give up and use normal IO. + # fallback_getpass() raises an appropriate warning. + del input, tty # clean up unused file objects before blocking + passwd = fallback_getpass(prompt, stream) stream.write('\n') return passwd @@ -46,7 +85,7 @@ def unix_getpass(prompt='Password: ', stream=None): def win_getpass(prompt='Password: ', stream=None): """Prompt for password with echo off, using Windows getch().""" if sys.stdin is not sys.__stdin__: - return default_getpass(prompt, stream) + return fallback_getpass(prompt, stream) import msvcrt for c in prompt: msvcrt.putwch(c) @@ -66,20 +105,26 @@ def win_getpass(prompt='Password: ', stream=None): return pw -def default_getpass(prompt='Password: ', stream=None): - print("Warning: Problem with getpass. Passwords may be echoed.", file=sys.stderr) +def fallback_getpass(prompt='Password: ', stream=None): + warnings.warn("Can not control echo on the terminal.", GetPassWarning, + stacklevel=2) + if not stream: + stream = sys.stderr + print("Warning: Password input may be echoed.", file=stream) return _raw_input(prompt, stream) -def _raw_input(prompt="", stream=None): +def _raw_input(prompt="", stream=None, input=None): # This doesn't save the string in the GNU readline history. - if stream is None: - stream = sys.stdout + if not stream: + stream = sys.stderr + if not input: + input = sys.stdin prompt = str(prompt) if prompt: stream.write(prompt) stream.flush() - line = sys.stdin.readline() + line = input.readline() if not line: raise EOFError if line[-1] == '\n': @@ -119,7 +164,7 @@ except (ImportError, AttributeError): try: from EasyDialogs import AskPassword except ImportError: - getpass = default_getpass + getpass = fallback_getpass else: getpass = AskPassword else: diff --git a/Lib/idlelib/AutoComplete.py b/Lib/idlelib/AutoComplete.py index ac0827c..9d9a81e 100644 --- a/Lib/idlelib/AutoComplete.py +++ b/Lib/idlelib/AutoComplete.py @@ -23,6 +23,10 @@ from idlelib.HyperParser import HyperParser import __main__ +SEPS = os.sep +if os.altsep: # e.g. '/' on Windows... + SEPS += os.altsep + class AutoComplete: menudefs = [ @@ -70,7 +74,7 @@ class AutoComplete: if lastchar == ".": self._open_completions_later(False, False, False, COMPLETE_ATTRIBUTES) - elif lastchar == os.sep: + elif lastchar in SEPS: self._open_completions_later(False, False, False, COMPLETE_FILES) @@ -126,7 +130,7 @@ class AutoComplete: i -= 1 comp_start = curline[i:j] j = i - while i and curline[i-1] in FILENAME_CHARS+os.sep: + while i and curline[i-1] in FILENAME_CHARS + SEPS: i -= 1 comp_what = curline[i:j] elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES): diff --git a/Lib/idlelib/AutoCompleteWindow.py b/Lib/idlelib/AutoCompleteWindow.py index ca9109b..298177f 100644 --- a/Lib/idlelib/AutoCompleteWindow.py +++ b/Lib/idlelib/AutoCompleteWindow.py @@ -54,9 +54,9 @@ class AutoCompleteWindow: self.lastkey_was_tab = False def _change_start(self, newstart): + min_len = min(len(self.start), len(newstart)) i = 0 - while i < len(self.start) and i < len(newstart) and \ - self.start[i] == newstart[i]: + while i < min_len and self.start[i] == newstart[i]: i += 1 if i < len(self.start): self.widget.delete("%s+%dc" % (self.startindex, i), @@ -98,13 +98,17 @@ class AutoCompleteWindow: i = m + 1 last = i-1 + if first == last: # only one possible completion + return self.completions[first] + # We should return the maximum prefix of first and last + first_comp = self.completions[first] + last_comp = self.completions[last] + min_len = min(len(first_comp), len(last_comp)) i = len(s) - while len(self.completions[first]) > i and \ - len(self.completions[last]) > i and \ - self.completions[first][i] == self.completions[last][i]: + while i < min_len and first_comp[i] == last_comp[i]: i += 1 - return self.completions[first][:i] + return first_comp[:i] def _selection_changed(self): """Should be called when the selection of the Listbox has changed. @@ -118,8 +122,9 @@ class AutoCompleteWindow: if self._binary_search(lts) == cursel: newstart = lts else: + min_len = min(len(lts), len(selstart)) i = 0 - while i < len(lts) and i < len(selstart) and lts[i] == selstart[i]: + while i < min_len and lts[i] == selstart[i]: i += 1 newstart = selstart[:i] self._change_start(newstart) diff --git a/Lib/idlelib/EditorWindow.py b/Lib/idlelib/EditorWindow.py index 2b5c527..e049b66 100644 --- a/Lib/idlelib/EditorWindow.py +++ b/Lib/idlelib/EditorWindow.py @@ -155,6 +155,7 @@ class EditorWindow(object): text.bind("<Right>", self.move_at_edge_if_selection(1)) text.bind("<<del-word-left>>", self.del_word_left) text.bind("<<del-word-right>>", self.del_word_right) + text.bind("<<beginning-of-line>>", self.home_callback) if flist: flist.inversedict[self] = key @@ -277,6 +278,50 @@ class EditorWindow(object): self.flist.new(dirname) return "break" + def home_callback(self, event): + if (event.state & 12) != 0 and event.keysym == "Home": + # state&1==shift, state&4==control, state&8==alt + return # <Modifier-Home>; fall back to class binding + + if self.text.index("iomark") and \ + self.text.compare("iomark", "<=", "insert lineend") and \ + self.text.compare("insert linestart", "<=", "iomark"): + insertpt = int(self.text.index("iomark").split(".")[1]) + else: + line = self.text.get("insert linestart", "insert lineend") + for insertpt in xrange(len(line)): + if line[insertpt] not in (' ','\t'): + break + else: + insertpt=len(line) + + lineat = int(self.text.index("insert").split('.')[1]) + + if insertpt == lineat: + insertpt = 0 + + dest = "insert linestart+"+str(insertpt)+"c" + + if (event.state&1) == 0: + # shift not pressed + self.text.tag_remove("sel", "1.0", "end") + else: + if not self.text.index("sel.first"): + self.text.mark_set("anchor","insert") + + first = self.text.index(dest) + last = self.text.index("anchor") + + if self.text.compare(first,">",last): + first,last = last,first + + self.text.tag_remove("sel", "1.0", "end") + self.text.tag_add("sel", first, last) + + self.text.mark_set("insert", dest) + self.text.see("insert") + return "break" + def set_status_bar(self): self.status_bar = self.MultiStatusBar(self.top) if macosxSupport.runningAsOSXApp(): diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py index 7c66088..7799232 100644 --- a/Lib/idlelib/PyShell.py +++ b/Lib/idlelib/PyShell.py @@ -806,7 +806,6 @@ class PyShell(OutputWindow): text.bind("<<newline-and-indent>>", self.enter_callback) text.bind("<<plain-newline-and-indent>>", self.linefeed_callback) text.bind("<<interrupt-execution>>", self.cancel_callback) - text.bind("<<beginning-of-line>>", self.home_callback) text.bind("<<end-of-file>>", self.eof_callback) text.bind("<<open-stack-viewer>>", self.open_stack_viewer) text.bind("<<toggle-debugger>>", self.toggle_debugger) @@ -1048,16 +1047,6 @@ class PyShell(OutputWindow): self.top.quit() return "break" - def home_callback(self, event): - if event.state != 0 and event.keysym == "Home": - return # <Modifier-Home>; fall back to class binding - if self.text.compare("iomark", "<=", "insert") and \ - self.text.compare("insert linestart", "<=", "iomark"): - self.text.mark_set("insert", "iomark") - self.text.tag_remove("sel", "1.0", "end") - self.text.see("insert") - return "break" - def linefeed_callback(self, event): # Insert a linefeed without entering anything (still autoindented) if self.reading: diff --git a/Lib/idlelib/configHandler.py b/Lib/idlelib/configHandler.py index 4906578..9b995cf 100644 --- a/Lib/idlelib/configHandler.py +++ b/Lib/idlelib/configHandler.py @@ -204,7 +204,10 @@ class IdleConf: if not os.path.exists(userDir): warn = ('\n Warning: os.path.expanduser("~") points to\n '+ userDir+',\n but the path does not exist.\n') - sys.stderr.write(warn) + try: + sys.stderr.write(warn) + except IOError: + pass userDir = '~' if userDir == "~": # still no path to home! # traditionally IDLE has defaulted to os.getcwd(), is this adequate? @@ -247,7 +250,10 @@ class IdleConf: ' from section %r.\n' ' returning default value: %r\n' % (option, section, default)) - sys.stderr.write(warning) + try: + sys.stderr.write(warning) + except IOError: + pass return default def SetOption(self, configType, section, option, value): @@ -356,7 +362,10 @@ class IdleConf: '\n from theme %r.\n' ' returning default value: %r\n' % (element, themeName, theme[element])) - sys.stderr.write(warning) + try: + sys.stderr.write(warning) + except IOError: + pass colour=cfgParser.Get(themeName,element,default=theme[element]) theme[element]=colour return theme @@ -610,7 +619,10 @@ class IdleConf: '\n from key set %r.\n' ' returning default value: %r\n' % (event, keySetName, keyBindings[event])) - sys.stderr.write(warning) + try: + sys.stderr.write(warning) + except IOError: + pass return keyBindings def GetExtraHelpSourceList(self,configSet): diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py index ae7d3cdf..ae9d7d0 100755 --- a/Lib/lib2to3/refactor.py +++ b/Lib/lib2to3/refactor.py @@ -18,6 +18,8 @@ import sys import difflib import optparse import logging +from collections import defaultdict +from itertools import chain # Local imports from .pgen2 import driver @@ -96,6 +98,43 @@ def get_all_fix_names(): fix_names.sort() return fix_names +def get_head_types(pat): + """ Accepts a pytree Pattern Node and returns a set + of the pattern types which will match first. """ + + if isinstance(pat, (pytree.NodePattern, pytree.LeafPattern)): + # NodePatters must either have no type and no content + # or a type and content -- so they don't get any farther + # Always return leafs + return set([pat.type]) + + if isinstance(pat, pytree.NegatedPattern): + if pat.content: + return get_head_types(pat.content) + return set([None]) # Negated Patterns don't have a type + + if isinstance(pat, pytree.WildcardPattern): + # Recurse on each node in content + r = set() + for p in pat.content: + for x in p: + r.update(get_head_types(x)) + return r + + raise Exception("Oh no! I don't understand pattern %s" %(pat)) + +def get_headnode_dict(fixer_list): + """ Accepts a list of fixers and returns a dictionary + of head node type --> fixer list. """ + head_nodes = defaultdict(list) + for fixer in fixer_list: + if not fixer.pattern: + head_nodes[None].append(fixer) + continue + for t in get_head_types(fixer.pattern): + head_nodes[t].append(fixer) + return head_nodes + class RefactoringTool(object): @@ -114,6 +153,10 @@ class RefactoringTool(object): convert=pytree.convert, logger=self.logger) self.pre_order, self.post_order = self.get_fixers() + + self.pre_order = get_headnode_dict(self.pre_order) + self.post_order = get_headnode_dict(self.post_order) + self.files = [] # List of files that were or should be modified def get_fixers(self): @@ -286,7 +329,11 @@ class RefactoringTool(object): Returns: True if the tree was modified, False otherwise. """ - all_fixers = self.pre_order + self.post_order + # Two calls to chain are required because pre_order.values() + # will be a list of lists of fixers: + # [[<fixer ...>, <fixer ...>], [<fixer ...>]] + all_fixers = chain(chain(*self.pre_order.values()),\ + chain(*self.post_order.values())) for fixer in all_fixers: fixer.start_tree(tree, name) @@ -312,7 +359,7 @@ class RefactoringTool(object): if not fixers: return for node in traversal: - for fixer in fixers: + for fixer in fixers[node.type] + fixers[None]: results = fixer.match(node) if results: new = fixer.transform(node, results) diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index eabfae5..f8cdfb9 100755 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -33,8 +33,10 @@ class FixerTestCase(support.TestCase): self.fixer_log = [] self.filename = "<string>" - for order in (self.refactor.pre_order, self.refactor.post_order): - for fixer in order: + from itertools import chain + for order in (self.refactor.pre_order.values(),\ + self.refactor.post_order.values()): + for fixer in chain(*order): fixer.log = self.fixer_log def _check(self, before, after): diff --git a/Lib/plat-mac/Carbon/AppleEvents.py b/Lib/plat-mac/Carbon/AppleEvents.py index ec19d19..4a2083e 100644 --- a/Lib/plat-mac/Carbon/AppleEvents.py +++ b/Lib/plat-mac/Carbon/AppleEvents.py @@ -1,6 +1,7 @@ # Generated from 'AEDataModel.h' def FOUR_CHAR_CODE(x): return x.encode("latin-1") +typeApplicationBundleID = FOUR_CHAR_CODE('bund') typeBoolean = FOUR_CHAR_CODE('bool') typeChar = FOUR_CHAR_CODE('TEXT') typeSInt16 = FOUR_CHAR_CODE('shor') diff --git a/Lib/plat-mac/macerrors.py b/Lib/plat-mac/macerrors.py index faa5244..4d7ca60 100644 --- a/Lib/plat-mac/macerrors.py +++ b/Lib/plat-mac/macerrors.py @@ -1,3 +1,4 @@ +# -coding=latin1- svTempDisable = -32768 #svTempDisable svDisabled = -32640 #Reserve range -32640 to -32768 for Apple temp disables. fontNotOutlineErr = -32615 #bitmap font passed to routine that does outlines only diff --git a/Lib/plat-mac/terminalcommand.py b/Lib/plat-mac/terminalcommand.py index da1c28a..0e6e26d 100644 --- a/Lib/plat-mac/terminalcommand.py +++ b/Lib/plat-mac/terminalcommand.py @@ -27,7 +27,7 @@ SEND_MODE = kAENoReply # kAEWaitReply hangs when run from Terminal.app itself def run(command): """Run a shell command in a new Terminal.app window.""" - termAddress = AE.AECreateDesc(typeApplSignature, TERMINAL_SIG) + termAddress = AE.AECreateDesc(typeApplicationBundleID, "com.apple.Terminal") theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress, kAutoGenerateReturnID, kAnyTransactionID) commandDesc = AE.AECreateDesc(typeChar, command) diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index cf4951c..c87ec66 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -41,8 +41,8 @@ Instances of this class have the following instance variables: import sys import imp -import tokenize # Python tokenizer -from token import NAME, DEDENT, NEWLINE, OP +import tokenize +from token import NAME, DEDENT, OP from operator import itemgetter __all__ = ["readmodule", "readmodule_ex", "Class", "Function"] @@ -73,37 +73,37 @@ class Function: self.file = file self.lineno = lineno -def readmodule(module, path=[]): +def readmodule(module, path=None): '''Backwards compatible interface. Call readmodule_ex() and then only keep Class objects from the resulting dictionary.''' - dict = _readmodule(module, path) res = {} - for key, value in dict.items(): + for key, value in _readmodule(module, path or []).items(): if isinstance(value, Class): res[key] = value return res -def readmodule_ex(module, path=[]): +def readmodule_ex(module, path=None): '''Read a module file and return a dictionary of classes. Search for MODULE in PATH and sys.path, read and parse the module and return a dictionary with one entry for each class found in the module. + ''' + return _readmodule(module, path or []) + +def _readmodule(module, path, inpackage=None): + '''Do the hard work for readmodule[_ex]. - If INPACKAGE is true, it must be the dotted name of the package in + If INPACKAGE is given, it must be the dotted name of the package in which we are searching for a submodule, and then PATH must be the package search path; otherwise, we are searching for a top-level module, and PATH is combined with sys.path. ''' - return _readmodule(module, path) - -def _readmodule(module, path, inpackage=None): - '''Do the hard work for readmodule[_ex].''' # Compute the full module name (prepending inpackage if set) - if inpackage: + if inpackage is not None: fullmodule = "%s.%s" % (inpackage, module) else: fullmodule = module @@ -116,7 +116,7 @@ def _readmodule(module, path, inpackage=None): dict = {} # Check if it is a built-in module; we don't do much for these - if module in sys.builtin_module_names and not inpackage: + if module in sys.builtin_module_names and inpackage is None: _modules[module] = dict return dict @@ -126,22 +126,22 @@ def _readmodule(module, path, inpackage=None): package = module[:i] submodule = module[i+1:] parent = _readmodule(package, path, inpackage) - if inpackage: + if inpackage is not None: package = "%s.%s" % (inpackage, package) return _readmodule(submodule, parent['__path__'], package) # Search the path for the module f = None - if inpackage: - f, file, (suff, mode, type) = imp.find_module(module, path) + if inpackage is not None: + f, fname, (_s, _m, ty) = imp.find_module(module, path) else: - f, file, (suff, mode, type) = imp.find_module(module, path + sys.path) - if type == imp.PKG_DIRECTORY: - dict['__path__'] = [file] - path = [file] + path - f, file, (suff, mode, type) = imp.find_module('__init__', [file]) + f, fname, (_s, _m, ty) = imp.find_module(module, path + sys.path) + if ty == imp.PKG_DIRECTORY: + dict['__path__'] = [fname] + path = [fname] + path + f, fname, (_s, _m, ty) = imp.find_module('__init__', [fname]) _modules[fullmodule] = dict - if type != imp.PY_SOURCE: + if ty != imp.PY_SOURCE: # not Python source, can't do anything with this module f.close() return dict @@ -150,7 +150,7 @@ def _readmodule(module, path, inpackage=None): g = tokenize.generate_tokens(f.readline) try: - for tokentype, token, start, end, line in g: + for tokentype, token, start, _end, _line in g: if tokentype == DEDENT: lineno, thisindent = start # close nested classes and defs @@ -161,7 +161,7 @@ def _readmodule(module, path, inpackage=None): # close previous nested classes and defs while stack and stack[-1][1] >= thisindent: del stack[-1] - tokentype, meth_name, start, end, line = next(g) + tokentype, meth_name, start = next(g)[0:3] if tokentype != NAME: continue # Syntax error if stack: @@ -172,18 +172,19 @@ def _readmodule(module, path, inpackage=None): # else it's a nested def else: # it's a function - dict[meth_name] = Function(fullmodule, meth_name, file, lineno) + dict[meth_name] = Function(fullmodule, meth_name, + fname, lineno) stack.append((None, thisindent)) # Marker for nested fns elif token == 'class': lineno, thisindent = start # close previous nested classes and defs while stack and stack[-1][1] >= thisindent: del stack[-1] - tokentype, class_name, start, end, line = next(g) + tokentype, class_name, start = next(g)[0:3] if tokentype != NAME: continue # Syntax error # parse what follows the class name - tokentype, token, start, end, line = next(g) + tokentype, token, start = next(g)[0:3] inherit = None if token == '(': names = [] # List of superclasses @@ -191,7 +192,7 @@ def _readmodule(module, path, inpackage=None): level = 1 super = [] # Tokens making up current superclass while True: - tokentype, token, start, end, line = next(g) + tokentype, token, start = next(g)[0:3] if token in (')', ',') and level == 1: n = "".join(super) if n in dict: @@ -224,16 +225,17 @@ def _readmodule(module, path, inpackage=None): super.append(token) # expressions in the base list are not supported inherit = names - cur_class = Class(fullmodule, class_name, inherit, file, lineno) + cur_class = Class(fullmodule, class_name, inherit, + fname, lineno) if not stack: dict[class_name] = cur_class stack.append((cur_class, thisindent)) elif token == 'import' and start[1] == 0: modules = _getnamelist(g) - for mod, mod2 in modules: + for mod, _mod2 in modules: try: # Recursively read the imported module - if not inpackage: + if inpackage is None: _readmodule(mod, path) else: try: @@ -287,7 +289,7 @@ def _getnamelist(g): name2 = None names.append((name, name2)) while token != "," and "\n" not in token: - tokentype, token, start, end, line = next(g) + token = next(g)[1] if token != ",": break return names @@ -297,15 +299,15 @@ def _getname(g): # name is the dotted name, or None if there was no dotted name, # and token is the next input token. parts = [] - tokentype, token, start, end, line = next(g) + tokentype, token = next(g)[0:2] if tokentype != NAME and token != '*': return (None, token) parts.append(token) while True: - tokentype, token, start, end, line = next(g) + tokentype, token = next(g)[0:2] if token != '.': break - tokentype, token, start, end, line = next(g) + tokentype, token = next(g)[0:2] if tokentype != NAME: break parts.append(token) diff --git a/Lib/robotparser.py b/Lib/robotparser.py index 638a261..d1ef460 100644 --- a/Lib/robotparser.py +++ b/Lib/robotparser.py @@ -9,16 +9,11 @@ The robots.txt Exclusion Protocol is implemented as specified in http://info.webcrawler.com/mak/projects/robots/norobots-rfc.html """ -import urlparse,urllib +import urlparse +import urllib __all__ = ["RobotFileParser"] -debug = 0 - -def _debug(msg): - if debug: print(msg) - - class RobotFileParser: """ This class provides a set of methods to read, parse and answer questions about a single robots.txt file. @@ -67,12 +62,9 @@ class RobotFileParser: self.errcode = opener.errcode if self.errcode in (401, 403): self.disallow_all = True - _debug("disallow all") elif self.errcode >= 400: self.allow_all = True - _debug("allow all") elif self.errcode == 200 and lines: - _debug("parse lines") self.parse(lines) def _add_entry(self, entry): @@ -93,19 +85,16 @@ class RobotFileParser: for line in lines: linenumber = linenumber + 1 if not line: - if state==1: - _debug("line %d: warning: you should insert" - " allow: or disallow: directives below any" - " user-agent: line" % linenumber) + if state == 1: entry = Entry() state = 0 - elif state==2: + elif state == 2: self._add_entry(entry) entry = Entry() state = 0 # remove optional comment and strip line i = line.find('#') - if i>=0: + if i >= 0: line = line[:i] line = line.strip() if not line: @@ -115,41 +104,24 @@ class RobotFileParser: line[0] = line[0].strip().lower() line[1] = urllib.unquote(line[1].strip()) if line[0] == "user-agent": - if state==2: - _debug("line %d: warning: you should insert a blank" - " line before any user-agent" - " directive" % linenumber) + if state == 2: self._add_entry(entry) entry = Entry() entry.useragents.append(line[1]) state = 1 elif line[0] == "disallow": - if state==0: - _debug("line %d: error: you must insert a user-agent:" - " directive before this line" % linenumber) - else: + if state != 0: entry.rulelines.append(RuleLine(line[1], False)) state = 2 elif line[0] == "allow": - if state==0: - _debug("line %d: error: you must insert a user-agent:" - " directive before this line" % linenumber) - else: + if state != 0: entry.rulelines.append(RuleLine(line[1], True)) - else: - _debug("line %d: warning: unknown key %s" % (linenumber, - line[0])) - else: - _debug("line %d: error: malformed line %s"%(linenumber, line)) - if state==2: + if state == 2: self.entries.append(entry) - _debug("Parsed rules:\n%s" % str(self)) def can_fetch(self, useragent, url): """using the parsed robots.txt decide if useragent can fetch url""" - _debug("Checking robots.txt allowance for:\n user agent: %s\n url: %s" % - (useragent, url)) if self.disallow_all: return False if self.allow_all: @@ -182,10 +154,10 @@ class RuleLine: self.allowance = allowance def applies_to(self, filename): - return self.path=="*" or filename.startswith(self.path) + return self.path == "*" or filename.startswith(self.path) def __str__(self): - return (self.allowance and "Allow" or "Disallow")+": "+self.path + return (self.allowance and "Allow" or "Disallow") + ": " + self.path class Entry: @@ -207,7 +179,7 @@ class Entry: # split the name token and make it lower case useragent = useragent.split("/")[0].lower() for agent in self.useragents: - if agent=='*': + if agent == '*': # we have the catch-all agent return True agent = agent.lower() @@ -220,7 +192,6 @@ class Entry: - our agent applies to this entry - filename is URL decoded""" for line in self.rulelines: - _debug((filename, str(line), line.allowance)) if line.applies_to(filename): return line.allowance return True @@ -239,56 +210,3 @@ class URLopener(urllib.FancyURLopener): self.errcode = errcode return urllib.FancyURLopener.http_error_default(self, url, fp, errcode, errmsg, headers) - -def _check(a,b): - if not b: - ac = "access denied" - else: - ac = "access allowed" - if a!=b: - print("failed") - else: - print("ok (%s)" % ac) - print() - -def _test(): - global debug - rp = RobotFileParser() - debug = 1 - - # robots.txt that exists, gotten to by redirection - rp.set_url('http://www.musi-cal.com/robots.txt') - rp.read() - - # test for re.escape - _check(rp.can_fetch('*', 'http://www.musi-cal.com/'), 1) - # this should match the first rule, which is a disallow - _check(rp.can_fetch('', 'http://www.musi-cal.com/'), 0) - # various cherry pickers - _check(rp.can_fetch('CherryPickerSE', - 'http://www.musi-cal.com/cgi-bin/event-search' - '?city=San+Francisco'), 0) - _check(rp.can_fetch('CherryPickerSE/1.0', - 'http://www.musi-cal.com/cgi-bin/event-search' - '?city=San+Francisco'), 0) - _check(rp.can_fetch('CherryPickerSE/1.5', - 'http://www.musi-cal.com/cgi-bin/event-search' - '?city=San+Francisco'), 0) - # case sensitivity - _check(rp.can_fetch('ExtractorPro', 'http://www.musi-cal.com/blubba'), 0) - _check(rp.can_fetch('extractorpro', 'http://www.musi-cal.com/blubba'), 0) - # substring test - _check(rp.can_fetch('toolpak/1.1', 'http://www.musi-cal.com/blubba'), 0) - # tests for catch-all * agent - _check(rp.can_fetch('spam', 'http://www.musi-cal.com/search'), 0) - _check(rp.can_fetch('spam', 'http://www.musi-cal.com/Musician/me'), 1) - _check(rp.can_fetch('spam', 'http://www.musi-cal.com/'), 1) - _check(rp.can_fetch('spam', 'http://www.musi-cal.com/'), 1) - - # robots.txt that does not exist - rp.set_url('http://www.lycos.com/robots.txt') - rp.read() - _check(rp.can_fetch('Mozilla', 'http://www.lycos.com/search'), 1) - -if __name__ == '__main__': - _test() diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py index bc56caa..345d5b3 100644 --- a/Lib/sqlite3/test/factory.py +++ b/Lib/sqlite3/test/factory.py @@ -131,6 +131,26 @@ class RowFactoryTests(unittest.TestCase): self.failUnlessEqual(d["a"], row["a"]) self.failUnlessEqual(d["b"], row["b"]) + def CheckSqliteRowHashCmp(self): + """Checks if the row object compares and hashes correctly""" + self.con.row_factory = sqlite.Row + row_1 = self.con.execute("select 1 as a, 2 as b").fetchone() + row_2 = self.con.execute("select 1 as a, 2 as b").fetchone() + row_3 = self.con.execute("select 1 as a, 3 as b").fetchone() + + self.failUnless(row_1 == row_1) + self.failUnless(row_1 == row_2) + self.failUnless(row_2 != row_3) + + self.failIf(row_1 != row_1) + self.failIf(row_1 != row_2) + self.failIf(row_2 == row_3) + + self.failUnlessEqual(row_1, row_2) + self.failUnlessEqual(hash(row_1), hash(row_2)) + self.failIfEqual(row_1, row_3) + self.failIfEqual(hash(row_1), hash(row_3)) + def tearDown(self): self.con.close() diff --git a/Lib/sqlite3/test/types.py b/Lib/sqlite3/test/types.py index ce740a5..bde1645 100644 --- a/Lib/sqlite3/test/types.py +++ b/Lib/sqlite3/test/types.py @@ -109,7 +109,7 @@ class DeclTypesTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) self.cur = self.con.cursor() - self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob)") + self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5))") # override float, make them always return the same number sqlite.converters["FLOAT"] = lambda x: 47.2 @@ -118,11 +118,13 @@ class DeclTypesTests(unittest.TestCase): sqlite.converters["BOOL"] = lambda x: bool(int(x)) sqlite.converters["FOO"] = DeclTypesTests.Foo sqlite.converters["WRONG"] = lambda x: "WRONG" + sqlite.converters["NUMBER"] = float def tearDown(self): del sqlite.converters["FLOAT"] del sqlite.converters["BOOL"] del sqlite.converters["FOO"] + del sqlite.converters["NUMBER"] self.cur.close() self.con.close() @@ -215,6 +217,19 @@ class DeclTypesTests(unittest.TestCase): row = self.cur.fetchone() self.failUnlessEqual(row[0], sample) + def CheckNumber1(self): + self.cur.execute("insert into test(n1) values (5)") + value = self.cur.execute("select n1 from test").fetchone()[0] + # if the converter is not used, it's an int instead of a float + self.failUnlessEqual(type(value), float) + + def CheckNumber2(self): + """Checks wether converter names are cut off at '(' characters""" + self.cur.execute("insert into test(n2) values (5)") + value = self.cur.execute("select n2 from test").fetchone()[0] + # if the converter is not used, it's an int instead of a float + self.failUnlessEqual(type(value), float) + class ColNamesTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index df1317d..0e2a460 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -78,8 +78,6 @@ else: template = "tmp" -tempdir = None - # Internal routines. _once_lock = _allocate_lock() @@ -252,7 +250,7 @@ def gettempprefix(): tempdir = None def gettempdir(): - """Accessor for tempdir.tempdir.""" + """Accessor for tempfile.tempdir.""" global tempdir if tempdir is None: _once_lock.acquire() @@ -264,8 +262,7 @@ def gettempdir(): return tempdir def mkstemp(suffix="", prefix=template, dir=None, text=False): - """mkstemp([suffix, [prefix, [dir, [text]]]]) - User-callable function to create and return a unique temporary + """User-callable function to create and return a unique temporary file. The return value is a pair (fd, name) where fd is the file descriptor returned by os.open, and name is the filename. @@ -302,8 +299,7 @@ def mkstemp(suffix="", prefix=template, dir=None, text=False): def mkdtemp(suffix="", prefix=template, dir=None): - """mkdtemp([suffix, [prefix, [dir]]]) - User-callable function to create and return a unique temporary + """User-callable function to create and return a unique temporary directory. The return value is the pathname of the directory. Arguments are as for mkstemp, except that the 'text' argument is @@ -334,8 +330,7 @@ def mkdtemp(suffix="", prefix=template, dir=None): raise IOError(_errno.EEXIST, "No usable temporary directory name found") def mktemp(suffix="", prefix=template, dir=None): - """mktemp([suffix, [prefix, [dir]]]) - User-callable function to return a unique temporary file name. The + """User-callable function to return a unique temporary file name. The file is not created. Arguments are as for mkstemp, except that the 'text' argument is diff --git a/Lib/test/crashers/mutation_inside_cyclegc.py b/Lib/test/crashers/mutation_inside_cyclegc.py new file mode 100644 index 0000000..2b67398 --- /dev/null +++ b/Lib/test/crashers/mutation_inside_cyclegc.py @@ -0,0 +1,31 @@ + +# The cycle GC collector can be executed when any GC-tracked object is +# allocated, e.g. during a call to PyList_New(), PyDict_New(), ... +# Moreover, it can invoke arbitrary Python code via a weakref callback. +# This means that there are many places in the source where an arbitrary +# mutation could unexpectedly occur. + +# The example below shows list_slice() not expecting the call to +# PyList_New to mutate the input list. (Of course there are many +# more examples like this one.) + + +import weakref + +class A(object): + pass + +def callback(x): + del lst[:] + + +keepalive = [] + +for i in range(100): + lst = [str(i)] + a = A() + a.cycle = a + keepalive.append(weakref.ref(a, callback)) + del a + while lst: + keepalive.append(lst[:]) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 7244aff..52b337c 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -484,201 +484,6 @@ class BuiltinTest(unittest.TestCase): self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4]) self.assertRaises(TypeError, list, filter(42, (1, 2))) - def test_float(self): - self.assertEqual(float(3.14), 3.14) - self.assertEqual(float(314), 314.0) - self.assertEqual(float(314), 314.0) - self.assertEqual(float(" 3.14 "), 3.14) - self.assertRaises(ValueError, float, " 0x3.1 ") - self.assertRaises(ValueError, float, " -0x3.p-1 ") - self.assertEqual(float(str(b" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14) - self.assertEqual(float("1"*10000), 1e10000) # Inf on both sides - - @run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE') - def test_float_with_comma(self): - # set locale to something that doesn't use '.' for the decimal point - # float must not accept the locale specific decimal point but - # it still has to accept the normal python syntac - import locale - if not locale.localeconv()['decimal_point'] == ',': - return - - self.assertEqual(float(" 3.14 "), 3.14) - self.assertEqual(float("+3.14 "), 3.14) - self.assertEqual(float("-3.14 "), -3.14) - self.assertEqual(float(".14 "), .14) - self.assertEqual(float("3. "), 3.0) - self.assertEqual(float("3.e3 "), 3000.0) - self.assertEqual(float("3.2e3 "), 3200.0) - self.assertEqual(float("2.5e-1 "), 0.25) - self.assertEqual(float("5e-1"), 0.5) - self.assertRaises(ValueError, float, " 3,14 ") - self.assertRaises(ValueError, float, " +3,14 ") - self.assertRaises(ValueError, float, " -3,14 ") - self.assertRaises(ValueError, float, " 0x3.1 ") - self.assertRaises(ValueError, float, " -0x3.p-1 ") - self.assertEqual(float(" 25.e-1 "), 2.5) - self.assertEqual(fcmp(float(" .25e-1 "), .025), 0) - - def test_floatconversion(self): - # Make sure that calls to __float__() work properly - class Foo0: - def __float__(self): - return 42. - - class Foo1(object): - def __float__(self): - return 42. - - class Foo2(float): - def __float__(self): - return 42. - - class Foo3(float): - def __new__(cls, value=0.): - return float.__new__(cls, 2*value) - - def __float__(self): - return self - - class Foo4(float): - def __float__(self): - return 42 - - self.assertAlmostEqual(float(Foo0()), 42.) - self.assertAlmostEqual(float(Foo1()), 42.) - self.assertAlmostEqual(float(Foo2()), 42.) - self.assertAlmostEqual(float(Foo3(21)), 42.) - self.assertRaises(TypeError, float, Foo4(42)) - - def test_format(self): - # Test the basic machinery of the format() builtin. Don't test - # the specifics of the various formatters - self.assertEqual(format(3, ''), '3') - - # Returns some classes to use for various tests. There's - # an old-style version, and a new-style version - def classes_new(): - class A(object): - def __init__(self, x): - self.x = x - def __format__(self, format_spec): - return str(self.x) + format_spec - class DerivedFromA(A): - pass - - class Simple(object): pass - class DerivedFromSimple(Simple): - def __init__(self, x): - self.x = x - def __format__(self, format_spec): - return str(self.x) + format_spec - class DerivedFromSimple2(DerivedFromSimple): pass - return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2 - - # In 3.0, classes_classic has the same meaning as classes_new - def classes_classic(): - class A: - def __init__(self, x): - self.x = x - def __format__(self, format_spec): - return str(self.x) + format_spec - class DerivedFromA(A): - pass - - class Simple: pass - class DerivedFromSimple(Simple): - def __init__(self, x): - self.x = x - def __format__(self, format_spec): - return str(self.x) + format_spec - class DerivedFromSimple2(DerivedFromSimple): pass - return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2 - - def class_test(A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2): - self.assertEqual(format(A(3), 'spec'), '3spec') - self.assertEqual(format(DerivedFromA(4), 'spec'), '4spec') - self.assertEqual(format(DerivedFromSimple(5), 'abc'), '5abc') - self.assertEqual(format(DerivedFromSimple2(10), 'abcdef'), - '10abcdef') - - class_test(*classes_new()) - class_test(*classes_classic()) - - def empty_format_spec(value): - # test that: - # format(x, '') == str(x) - # format(x) == str(x) - self.assertEqual(format(value, ""), str(value)) - self.assertEqual(format(value), str(value)) - - # for builtin types, format(x, "") == str(x) - empty_format_spec(17**13) - empty_format_spec(1.0) - empty_format_spec(3.1415e104) - empty_format_spec(-3.1415e104) - empty_format_spec(3.1415e-104) - empty_format_spec(-3.1415e-104) - empty_format_spec(object) - empty_format_spec(None) - - # TypeError because self.__format__ returns the wrong type - class BadFormatResult: - def __format__(self, format_spec): - return 1.0 - self.assertRaises(TypeError, format, BadFormatResult(), "") - - # TypeError because format_spec is not unicode or str - self.assertRaises(TypeError, format, object(), 4) - self.assertRaises(TypeError, format, object(), object()) - - # tests for object.__format__ really belong elsewhere, but - # there's no good place to put them - x = object().__format__('') - self.assert_(x.startswith('<object object at')) - - # first argument to object.__format__ must be string - self.assertRaises(TypeError, object().__format__, 3) - self.assertRaises(TypeError, object().__format__, object()) - self.assertRaises(TypeError, object().__format__, None) - - # make sure we can take a subclass of str as a format spec - class DerivedFromStr(str): pass - self.assertEqual(format(0, DerivedFromStr('10')), ' 0') - - def test_floatasratio(self): - for f, ratio in [ - (0.875, (7, 8)), - (-0.875, (-7, 8)), - (0.0, (0, 1)), - (11.5, (23, 2)), - ]: - self.assertEqual(f.as_integer_ratio(), ratio) - - for i in range(10000): - f = random.random() - f *= 10 ** random.randint(-100, 100) - n, d = f.as_integer_ratio() - self.assertEqual(float(n).__truediv__(d), f) - - R = fractions.Fraction - self.assertEqual(R(0, 1), - R(*float(0.0).as_integer_ratio())) - self.assertEqual(R(5, 2), - R(*float(2.5).as_integer_ratio())) - self.assertEqual(R(1, 2), - R(*float(0.5).as_integer_ratio())) - self.assertEqual(R(4728779608739021, 2251799813685248), - R(*float(2.1).as_integer_ratio())) - self.assertEqual(R(-4728779608739021, 2251799813685248), - R(*float(-2.1).as_integer_ratio())) - self.assertEqual(R(-2100, 1), - R(*float(-2100.0).as_integer_ratio())) - - self.assertRaises(OverflowError, float('inf').as_integer_ratio) - self.assertRaises(OverflowError, float('-inf').as_integer_ratio) - self.assertRaises(ValueError, float('nan').as_integer_ratio) - def test_getattr(self): import sys self.assert_(getattr(sys, 'stdout') is sys.stdout) @@ -737,266 +542,6 @@ class BuiltinTest(unittest.TestCase): # Test input() later, alphabetized as if it were raw_input - def test_int(self): - self.assertEqual(int(314), 314) - self.assertEqual(int(3.14), 3) - self.assertEqual(int(314), 314) - # Check that conversion from float truncates towards zero - self.assertEqual(int(-3.14), -3) - self.assertEqual(int(3.9), 3) - self.assertEqual(int(-3.9), -3) - self.assertEqual(int(3.5), 3) - self.assertEqual(int(-3.5), -3) - # Different base: - self.assertEqual(int("10",16), 16) - # Test conversion from strings and various anomalies - # Testing with no sign at front - for s, v in test_conv_no_sign: - for prefix in "", " ", "\t", " \t\t ": - ss = prefix + s - vv = v - try: - self.assertEqual(int(ss), vv) - except v: - pass - # No whitespaces allowed between + or - sign and the number - for s, v in test_conv_sign: - for sign in "+", "-": - for prefix in "", " ", "\t", " \t\t ": - ss = prefix + sign + s - vv = v - if sign == "-" and v is not ValueError: - vv = -v - try: - self.assertEqual(int(ss), vv) - except v: - pass - - s = repr(-1-sys.maxsize) - x = int(s) - self.assertEqual(x+1, -sys.maxsize) - self.assert_(isinstance(x, int)) - # should return long - self.assertEqual(int(s[1:]), sys.maxsize+1) - - # should return long - x = int(1e100) - self.assert_(isinstance(x, int)) - x = int(-1e100) - self.assert_(isinstance(x, int)) - - - # SF bug 434186: 0x80000000/2 != 0x80000000>>1. - # Worked by accident in Windows release build, but failed in debug build. - # Failed in all Linux builds. - x = -1-sys.maxsize - self.assertEqual(x >> 1, x//2) - - self.assertRaises(ValueError, int, '123\0') - self.assertRaises(ValueError, int, '53', 40) - - # SF bug 1545497: embedded NULs were not detected with - # explicit base - self.assertRaises(ValueError, int, '123\0', 10) - self.assertRaises(ValueError, int, '123\x00 245', 20) - - x = int('1' * 600) - self.assert_(isinstance(x, int)) - - x = int(chr(0x661) * 600) - self.assert_(isinstance(x, int)) - - self.assertRaises(TypeError, int, 1, 12) - - # tests with base 0 - self.assertRaises(ValueError, int, ' 0123 ', 0) # old octal syntax - self.assertEqual(int('000', 0), 0) - self.assertEqual(int('0o123', 0), 83) - self.assertEqual(int('0x123', 0), 291) - self.assertEqual(int('0b100', 0), 4) - self.assertEqual(int(' 0O123 ', 0), 83) - self.assertEqual(int(' 0X123 ', 0), 291) - self.assertEqual(int(' 0B100 ', 0), 4) - - # without base still base 10 - self.assertEqual(int('0123'), 123) - self.assertEqual(int('0123', 10), 123) - - # tests with prefix and base != 0 - self.assertEqual(int('0x123', 16), 291) - self.assertEqual(int('0o123', 8), 83) - self.assertEqual(int('0b100', 2), 4) - self.assertEqual(int('0X123', 16), 291) - self.assertEqual(int('0O123', 8), 83) - self.assertEqual(int('0B100', 2), 4) - - # Bug 1679: "0x" is not a valid hex literal - self.assertRaises(ValueError, int, "0x", 16) - self.assertRaises(ValueError, int, "0x", 0) - - - # SF bug 1334662: int(string, base) wrong answers - # Various representations of 2**32 evaluated to 0 - # rather than 2**32 in previous versions - - self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296) - self.assertEqual(int('102002022201221111211', 3), 4294967296) - self.assertEqual(int('10000000000000000', 4), 4294967296) - self.assertEqual(int('32244002423141', 5), 4294967296) - self.assertEqual(int('1550104015504', 6), 4294967296) - self.assertEqual(int('211301422354', 7), 4294967296) - self.assertEqual(int('40000000000', 8), 4294967296) - self.assertEqual(int('12068657454', 9), 4294967296) - self.assertEqual(int('4294967296', 10), 4294967296) - self.assertEqual(int('1904440554', 11), 4294967296) - self.assertEqual(int('9ba461594', 12), 4294967296) - self.assertEqual(int('535a79889', 13), 4294967296) - self.assertEqual(int('2ca5b7464', 14), 4294967296) - self.assertEqual(int('1a20dcd81', 15), 4294967296) - self.assertEqual(int('100000000', 16), 4294967296) - self.assertEqual(int('a7ffda91', 17), 4294967296) - self.assertEqual(int('704he7g4', 18), 4294967296) - self.assertEqual(int('4f5aff66', 19), 4294967296) - self.assertEqual(int('3723ai4g', 20), 4294967296) - self.assertEqual(int('281d55i4', 21), 4294967296) - self.assertEqual(int('1fj8b184', 22), 4294967296) - self.assertEqual(int('1606k7ic', 23), 4294967296) - self.assertEqual(int('mb994ag', 24), 4294967296) - self.assertEqual(int('hek2mgl', 25), 4294967296) - self.assertEqual(int('dnchbnm', 26), 4294967296) - self.assertEqual(int('b28jpdm', 27), 4294967296) - self.assertEqual(int('8pfgih4', 28), 4294967296) - self.assertEqual(int('76beigg', 29), 4294967296) - self.assertEqual(int('5qmcpqg', 30), 4294967296) - self.assertEqual(int('4q0jto4', 31), 4294967296) - self.assertEqual(int('4000000', 32), 4294967296) - self.assertEqual(int('3aokq94', 33), 4294967296) - self.assertEqual(int('2qhxjli', 34), 4294967296) - self.assertEqual(int('2br45qb', 35), 4294967296) - self.assertEqual(int('1z141z4', 36), 4294967296) - - # SF bug 1334662: int(string, base) wrong answers - # Checks for proper evaluation of 2**32 + 1 - self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297) - self.assertEqual(int('102002022201221111212', 3), 4294967297) - self.assertEqual(int('10000000000000001', 4), 4294967297) - self.assertEqual(int('32244002423142', 5), 4294967297) - self.assertEqual(int('1550104015505', 6), 4294967297) - self.assertEqual(int('211301422355', 7), 4294967297) - self.assertEqual(int('40000000001', 8), 4294967297) - self.assertEqual(int('12068657455', 9), 4294967297) - self.assertEqual(int('4294967297', 10), 4294967297) - self.assertEqual(int('1904440555', 11), 4294967297) - self.assertEqual(int('9ba461595', 12), 4294967297) - self.assertEqual(int('535a7988a', 13), 4294967297) - self.assertEqual(int('2ca5b7465', 14), 4294967297) - self.assertEqual(int('1a20dcd82', 15), 4294967297) - self.assertEqual(int('100000001', 16), 4294967297) - self.assertEqual(int('a7ffda92', 17), 4294967297) - self.assertEqual(int('704he7g5', 18), 4294967297) - self.assertEqual(int('4f5aff67', 19), 4294967297) - self.assertEqual(int('3723ai4h', 20), 4294967297) - self.assertEqual(int('281d55i5', 21), 4294967297) - self.assertEqual(int('1fj8b185', 22), 4294967297) - self.assertEqual(int('1606k7id', 23), 4294967297) - self.assertEqual(int('mb994ah', 24), 4294967297) - self.assertEqual(int('hek2mgm', 25), 4294967297) - self.assertEqual(int('dnchbnn', 26), 4294967297) - self.assertEqual(int('b28jpdn', 27), 4294967297) - self.assertEqual(int('8pfgih5', 28), 4294967297) - self.assertEqual(int('76beigh', 29), 4294967297) - self.assertEqual(int('5qmcpqh', 30), 4294967297) - self.assertEqual(int('4q0jto5', 31), 4294967297) - self.assertEqual(int('4000001', 32), 4294967297) - self.assertEqual(int('3aokq95', 33), 4294967297) - self.assertEqual(int('2qhxjlj', 34), 4294967297) - self.assertEqual(int('2br45qc', 35), 4294967297) - self.assertEqual(int('1z141z5', 36), 4294967297) - - def test_intconversion(self): - # Test __int__() - class ClassicMissingMethods: - pass - self.assertRaises(TypeError, int, ClassicMissingMethods()) - - class MissingMethods(object): - pass - self.assertRaises(TypeError, int, MissingMethods()) - - class Foo0: - def __int__(self): - return 42 - - class Foo1(object): - def __int__(self): - return 42 - - class Foo2(int): - def __int__(self): - return 42 - - class Foo3(int): - def __int__(self): - return self - - class Foo4(int): - def __int__(self): - return 42 - - class Foo5(int): - def __int__(self): - return 42. - - self.assertEqual(int(Foo0()), 42) - self.assertEqual(int(Foo1()), 42) - self.assertEqual(int(Foo2()), 42) - self.assertEqual(int(Foo3()), 0) - self.assertEqual(int(Foo4()), 42) - self.assertRaises(TypeError, int, Foo5()) - - class Classic: - pass - for base in (object, Classic): - class IntOverridesTrunc(base): - def __int__(self): - return 42 - def __trunc__(self): - return -12 - self.assertEqual(int(IntOverridesTrunc()), 42) - - class JustTrunc(base): - def __trunc__(self): - return 42 - self.assertEqual(int(JustTrunc()), 42) - - for trunc_result_base in (object, Classic): - class Integral(trunc_result_base): - def __int__(self): - return 42 - - class TruncReturnsNonInt(base): - def __trunc__(self): - return Integral() - self.assertEqual(int(TruncReturnsNonInt()), 42) - - class NonIntegral(trunc_result_base): - def __trunc__(self): - # Check that we avoid infinite recursion. - return NonIntegral() - - class TruncReturnsNonIntegral(base): - def __trunc__(self): - return NonIntegral() - try: - int(TruncReturnsNonIntegral()) - except TypeError as e: - self.assertEquals(str(e), - "__trunc__ returned non-Integral" - " (type NonIntegral)") - else: - self.fail("Failed to raise TypeError with %s" % - ((base, trunc_result_base),)) - def test_iter(self): self.assertRaises(TypeError, iter) self.assertRaises(TypeError, iter, 42, 42) @@ -1054,187 +599,6 @@ class BuiltinTest(unittest.TestCase): raise ValueError self.assertRaises(ValueError, len, BadSeq()) - def test_list(self): - self.assertEqual(list([]), []) - l0_3 = [0, 1, 2, 3] - l0_3_bis = list(l0_3) - self.assertEqual(l0_3, l0_3_bis) - self.assert_(l0_3 is not l0_3_bis) - self.assertEqual(list(()), []) - self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3]) - self.assertEqual(list(''), []) - self.assertEqual(list('spam'), ['s', 'p', 'a', 'm']) - - if sys.maxsize == 0x7fffffff: - # This test can currently only work on 32-bit machines. - # XXX If/when PySequence_Length() returns a ssize_t, it should be - # XXX re-enabled. - # Verify clearing of bug #556025. - # This assumes that the max data size (sys.maxsize) == max - # address size this also assumes that the address size is at - # least 4 bytes with 8 byte addresses, the bug is not well - # tested - # - # Note: This test is expected to SEGV under Cygwin 1.3.12 or - # earlier due to a newlib bug. See the following mailing list - # thread for the details: - - # http://sources.redhat.com/ml/newlib/2002/msg00369.html - self.assertRaises(MemoryError, list, range(sys.maxsize // 2)) - - # This code used to segfault in Py2.4a3 - x = [] - x.extend(-y for y in x) - self.assertEqual(x, []) - - def test_long(self): - self.assertEqual(int(314), 314) - self.assertEqual(int(3.14), 3) - self.assertEqual(int(314), 314) - # Check that conversion from float truncates towards zero - self.assertEqual(int(-3.14), -3) - self.assertEqual(int(3.9), 3) - self.assertEqual(int(-3.9), -3) - self.assertEqual(int(3.5), 3) - self.assertEqual(int(-3.5), -3) - self.assertEqual(int("-3"), -3) - # Different base: - self.assertEqual(int("10",16), 16) - # Check conversions from string (same test set as for int(), and then some) - LL = [ - ('1' + '0'*20, 10**20), - ('1' + '0'*100, 10**100) - ] - for s, v in LL: - for sign in "", "+", "-": - for prefix in "", " ", "\t", " \t\t ": - ss = prefix + sign + s - vv = v - if sign == "-" and v is not ValueError: - vv = -v - try: - self.assertEqual(int(ss), int(vv)) - except v: - pass - - self.assertRaises(ValueError, int, '123\0') - self.assertRaises(ValueError, int, '53', 40) - self.assertRaises(TypeError, int, 1, 12) - - # SF patch #1638879: embedded NULs were not detected with - # explicit base - self.assertRaises(ValueError, int, '123\0', 10) - self.assertRaises(ValueError, int, '123\x00 245', 20) - - self.assertEqual(int('100000000000000000000000000000000', 2), - 4294967296) - self.assertEqual(int('102002022201221111211', 3), 4294967296) - self.assertEqual(int('10000000000000000', 4), 4294967296) - self.assertEqual(int('32244002423141', 5), 4294967296) - self.assertEqual(int('1550104015504', 6), 4294967296) - self.assertEqual(int('211301422354', 7), 4294967296) - self.assertEqual(int('40000000000', 8), 4294967296) - self.assertEqual(int('12068657454', 9), 4294967296) - self.assertEqual(int('4294967296', 10), 4294967296) - self.assertEqual(int('1904440554', 11), 4294967296) - self.assertEqual(int('9ba461594', 12), 4294967296) - self.assertEqual(int('535a79889', 13), 4294967296) - self.assertEqual(int('2ca5b7464', 14), 4294967296) - self.assertEqual(int('1a20dcd81', 15), 4294967296) - self.assertEqual(int('100000000', 16), 4294967296) - self.assertEqual(int('a7ffda91', 17), 4294967296) - self.assertEqual(int('704he7g4', 18), 4294967296) - self.assertEqual(int('4f5aff66', 19), 4294967296) - self.assertEqual(int('3723ai4g', 20), 4294967296) - self.assertEqual(int('281d55i4', 21), 4294967296) - self.assertEqual(int('1fj8b184', 22), 4294967296) - self.assertEqual(int('1606k7ic', 23), 4294967296) - self.assertEqual(int('mb994ag', 24), 4294967296) - self.assertEqual(int('hek2mgl', 25), 4294967296) - self.assertEqual(int('dnchbnm', 26), 4294967296) - self.assertEqual(int('b28jpdm', 27), 4294967296) - self.assertEqual(int('8pfgih4', 28), 4294967296) - self.assertEqual(int('76beigg', 29), 4294967296) - self.assertEqual(int('5qmcpqg', 30), 4294967296) - self.assertEqual(int('4q0jto4', 31), 4294967296) - self.assertEqual(int('4000000', 32), 4294967296) - self.assertEqual(int('3aokq94', 33), 4294967296) - self.assertEqual(int('2qhxjli', 34), 4294967296) - self.assertEqual(int('2br45qb', 35), 4294967296) - self.assertEqual(int('1z141z4', 36), 4294967296) - - self.assertEqual(int('100000000000000000000000000000001', 2), - 4294967297) - self.assertEqual(int('102002022201221111212', 3), 4294967297) - self.assertEqual(int('10000000000000001', 4), 4294967297) - self.assertEqual(int('32244002423142', 5), 4294967297) - self.assertEqual(int('1550104015505', 6), 4294967297) - self.assertEqual(int('211301422355', 7), 4294967297) - self.assertEqual(int('40000000001', 8), 4294967297) - self.assertEqual(int('12068657455', 9), 4294967297) - self.assertEqual(int('4294967297', 10), 4294967297) - self.assertEqual(int('1904440555', 11), 4294967297) - self.assertEqual(int('9ba461595', 12), 4294967297) - self.assertEqual(int('535a7988a', 13), 4294967297) - self.assertEqual(int('2ca5b7465', 14), 4294967297) - self.assertEqual(int('1a20dcd82', 15), 4294967297) - self.assertEqual(int('100000001', 16), 4294967297) - self.assertEqual(int('a7ffda92', 17), 4294967297) - self.assertEqual(int('704he7g5', 18), 4294967297) - self.assertEqual(int('4f5aff67', 19), 4294967297) - self.assertEqual(int('3723ai4h', 20), 4294967297) - self.assertEqual(int('281d55i5', 21), 4294967297) - self.assertEqual(int('1fj8b185', 22), 4294967297) - self.assertEqual(int('1606k7id', 23), 4294967297) - self.assertEqual(int('mb994ah', 24), 4294967297) - self.assertEqual(int('hek2mgm', 25), 4294967297) - self.assertEqual(int('dnchbnn', 26), 4294967297) - self.assertEqual(int('b28jpdn', 27), 4294967297) - self.assertEqual(int('8pfgih5', 28), 4294967297) - self.assertEqual(int('76beigh', 29), 4294967297) - self.assertEqual(int('5qmcpqh', 30), 4294967297) - self.assertEqual(int('4q0jto5', 31), 4294967297) - self.assertEqual(int('4000001', 32), 4294967297) - self.assertEqual(int('3aokq95', 33), 4294967297) - self.assertEqual(int('2qhxjlj', 34), 4294967297) - self.assertEqual(int('2br45qc', 35), 4294967297) - self.assertEqual(int('1z141z5', 36), 4294967297) - - def test_longconversion(self): - # Test __long__() - class Foo0: - def __long__(self): - return 42 - - class Foo1(object): - def __long__(self): - return 42 - - class Foo2(int): - def __long__(self): - return 42 - - class Foo3(int): - def __long__(self): - return self - - class Foo4(int): - def __long__(self): - return 42 - - class Foo5(int): - def __long__(self): - return 42. - - self.assertEqual(int(Foo0()), 42) - self.assertEqual(int(Foo1()), 42) - # XXX invokes __int__ now - # self.assertEqual(long(Foo2()), 42L) - self.assertEqual(int(Foo3()), 0) - # XXX likewise - # self.assertEqual(long(Foo4()), 42) - # self.assertRaises(TypeError, long, Foo5()) - def test_map(self): self.assertEqual( list(map(lambda x: x*x, range(1,4))), @@ -1726,19 +1090,6 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(TypeError, setattr, sys, 1, 'spam') self.assertRaises(TypeError, setattr) - def test_str(self): - self.assertEqual(str(''), '') - self.assertEqual(str(0), '0') - self.assertEqual(str(0), '0') - self.assertEqual(str(()), '()') - self.assertEqual(str([]), '[]') - self.assertEqual(str({}), '{}') - a = [] - a.append(a) - self.assertEqual(str(a), '[[...]]') - a = {} - a[0] = a - self.assertEqual(str(a), '{0: {...}}') def test_sum(self): self.assertEqual(sum([]), 0) @@ -1761,16 +1112,6 @@ class BuiltinTest(unittest.TestCase): raise ValueError self.assertRaises(ValueError, sum, BadSeq()) - def test_tuple(self): - self.assertEqual(tuple(()), ()) - t0_3 = (0, 1, 2, 3) - t0_3_bis = tuple(t0_3) - self.assert_(t0_3 is t0_3_bis) - self.assertEqual(tuple([]), ()) - self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) - self.assertEqual(tuple(''), ()) - self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) - def test_type(self): self.assertEqual(type(''), type('123')) self.assertNotEqual(type(''), type(())) diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index e211916..74031c9 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -1,5 +1,4 @@ # Tests command line execution of scripts -from __future__ import with_statement import unittest import os diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index a3a9a5b..51d2436 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -1,6 +1,5 @@ """Unit tests for contextlib.py, and other context managers.""" -from __future__ import with_statement import sys import os diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index a76b505..bb27eec 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -23,7 +23,6 @@ or Behaviour) to test each part, or without parameter to test both parts. If you're working through IDLE, you can import this test module and call test_main() with the corresponding argument. """ -from __future__ import with_statement import glob import math @@ -47,10 +46,12 @@ Signals = tuple(getcontext().flags.keys()) def init(): global ORIGINAL_CONTEXT ORIGINAL_CONTEXT = getcontext().copy() - DefaultContext.prec = 9 - DefaultContext.rounding = ROUND_HALF_EVEN - DefaultContext.traps = dict.fromkeys(Signals, 0) - setcontext(DefaultContext) + DefaultTestContext = Context( + prec = 9, + rounding = ROUND_HALF_EVEN, + traps = dict.fromkeys(Signals, 0) + ) + setcontext(DefaultTestContext) TESTDATADIR = 'decimaltestdata' if __name__ == '__main__': @@ -180,8 +181,6 @@ class DecimalTest(unittest.TestCase): """ def setUp(self): self.context = Context() - for key in DefaultContext.traps.keys(): - DefaultContext.traps[key] = 1 self.ignore_list = ['#'] # Basically, a # means return NaN InvalidOperation. # Different from a sNaN in trim @@ -192,13 +191,6 @@ class DecimalTest(unittest.TestCase): 'minexponent' : self.change_min_exponent, 'clamp' : self.change_clamp} - def tearDown(self): - """Cleaning up enviroment.""" - # leaving context in original state - for key in DefaultContext.traps.keys(): - DefaultContext.traps[key] = 0 - return - def eval_file(self, file): global skip_expected if skip_expected: @@ -949,8 +941,8 @@ def thfunc1(cls): test2 = d1/d3 cls.finish1.set() - cls.assertEqual(test1, Decimal('0.333333333')) - cls.assertEqual(test2, Decimal('0.333333333')) + cls.assertEqual(test1, Decimal('0.3333333333333333333333333333')) + cls.assertEqual(test2, Decimal('0.3333333333333333333333333333')) return def thfunc2(cls): @@ -963,7 +955,7 @@ def thfunc2(cls): cls.synchro.set() cls.finish2.set() - cls.assertEqual(test1, Decimal('0.333333333')) + cls.assertEqual(test1, Decimal('0.3333333333333333333333333333')) cls.assertEqual(test2, Decimal('0.333333333333333333')) return diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index e89d723..5653c16 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -9,6 +9,116 @@ import operator INF = float("inf") NAN = float("nan") +class GeneralFloatCases(unittest.TestCase): + + def test_float(self): + self.assertEqual(float(3.14), 3.14) + self.assertEqual(float(314), 314.0) + self.assertEqual(float(" 3.14 "), 3.14) + self.assertRaises(ValueError, float, " 0x3.1 ") + self.assertRaises(ValueError, float, " -0x3.p-1 ") + self.assertRaises(ValueError, float, " +0x3.p-1 ") + self.assertRaises(ValueError, float, "++3.14") + self.assertRaises(ValueError, float, "+-3.14") + self.assertRaises(ValueError, float, "-+3.14") + self.assertRaises(ValueError, float, "--3.14") + if have_unicode: + self.assertEqual(float(unicode(" 3.14 ")), 3.14) + self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14) + # Implementation limitation in PyFloat_FromString() + self.assertRaises(ValueError, float, unicode("1"*10000)) + + @test_support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE') + def test_float_with_comma(self): + # set locale to something that doesn't use '.' for the decimal point + # float must not accept the locale specific decimal point but + # it still has to accept the normal python syntac + import locale + if not locale.localeconv()['decimal_point'] == ',': + return + + self.assertEqual(float(" 3.14 "), 3.14) + self.assertEqual(float("+3.14 "), 3.14) + self.assertEqual(float("-3.14 "), -3.14) + self.assertEqual(float(".14 "), .14) + self.assertEqual(float("3. "), 3.0) + self.assertEqual(float("3.e3 "), 3000.0) + self.assertEqual(float("3.2e3 "), 3200.0) + self.assertEqual(float("2.5e-1 "), 0.25) + self.assertEqual(float("5e-1"), 0.5) + self.assertRaises(ValueError, float, " 3,14 ") + self.assertRaises(ValueError, float, " +3,14 ") + self.assertRaises(ValueError, float, " -3,14 ") + self.assertRaises(ValueError, float, " 0x3.1 ") + self.assertRaises(ValueError, float, " -0x3.p-1 ") + self.assertRaises(ValueError, float, " +0x3.p-1 ") + self.assertEqual(float(" 25.e-1 "), 2.5) + self.assertEqual(fcmp(float(" .25e-1 "), .025), 0) + + def test_floatconversion(self): + # Make sure that calls to __float__() work properly + class Foo0: + def __float__(self): + return 42. + + class Foo1(object): + def __float__(self): + return 42. + + class Foo2(float): + def __float__(self): + return 42. + + class Foo3(float): + def __new__(cls, value=0.): + return float.__new__(cls, 2*value) + + def __float__(self): + return self + + class Foo4(float): + def __float__(self): + return 42 + + self.assertAlmostEqual(float(Foo0()), 42.) + self.assertAlmostEqual(float(Foo1()), 42.) + self.assertAlmostEqual(float(Foo2()), 42.) + self.assertAlmostEqual(float(Foo3(21)), 42.) + self.assertRaises(TypeError, float, Foo4(42)) + + def test_floatasratio(self): + for f, ratio in [ + (0.875, (7, 8)), + (-0.875, (-7, 8)), + (0.0, (0, 1)), + (11.5, (23, 2)), + ]: + self.assertEqual(f.as_integer_ratio(), ratio) + + for i in range(10000): + f = random.random() + f *= 10 ** random.randint(-100, 100) + n, d = f.as_integer_ratio() + self.assertEqual(float(n).__truediv__(d), f) + + R = fractions.Fraction + self.assertEqual(R(0, 1), + R(*float(0.0).as_integer_ratio())) + self.assertEqual(R(5, 2), + R(*float(2.5).as_integer_ratio())) + self.assertEqual(R(1, 2), + R(*float(0.5).as_integer_ratio())) + self.assertEqual(R(4728779608739021, 2251799813685248), + R(*float(2.1).as_integer_ratio())) + self.assertEqual(R(-4728779608739021, 2251799813685248), + R(*float(-2.1).as_integer_ratio())) + self.assertEqual(R(-2100, 1), + R(*float(-2100.0).as_integer_ratio())) + + self.assertRaises(OverflowError, float('inf').as_integer_ratio) + self.assertRaises(OverflowError, float('-inf').as_integer_ratio) + self.assertRaises(ValueError, float('nan').as_integer_ratio) + class FormatFunctionsTestCase(unittest.TestCase): def setUp(self): diff --git a/Lib/test/test_frozen.py b/Lib/test/test_frozen.py index c86a24d..de45b23 100644 --- a/Lib/test/test_frozen.py +++ b/Lib/test/test_frozen.py @@ -1,5 +1,4 @@ # Test the frozen module defined in frozen.c. -from __future__ import with_statement from test.test_support import captured_stdout, run_unittest import unittest diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index fe1da52..1c9dd3a 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -271,4 +271,6 @@ def test_main(verbose=None): run_unittest(ImportTest, PathsTests, RelativeImport) if __name__ == '__main__': + # test needs to be a package, so we can do relative import + from test.test_import import test_main test_main() diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py new file mode 100644 index 0000000..91d5273 --- /dev/null +++ b/Lib/test/test_int.py @@ -0,0 +1,304 @@ +import sys + +import unittest +from test.test_support import run_unittest + +L = [ + ('0', 0), + ('1', 1), + ('9', 9), + ('10', 10), + ('99', 99), + ('100', 100), + ('314', 314), + (' 314', 314), + ('314 ', 314), + (' \t\t 314 \t\t ', 314), + (repr(sys.maxsize), sys.maxsize), + (' 1x', ValueError), + (' 1 ', 1), + (' 1\02 ', ValueError), + ('', ValueError), + (' ', ValueError), + (' \t\t ', ValueError) +] + +class IntTestCases(unittest.TestCase): + + def test_basic(self): + self.assertEqual(int(314), 314) + self.assertEqual(int(3.14), 3) + # Check that conversion from float truncates towards zero + self.assertEqual(int(-3.14), -3) + self.assertEqual(int(3.9), 3) + self.assertEqual(int(-3.9), -3) + self.assertEqual(int(3.5), 3) + self.assertEqual(int(-3.5), -3) + # Different base: + self.assertEqual(int("10",16), 16) + # Test conversion from strings and various anomalies + for s, v in L: + for sign in "", "+", "-": + for prefix in "", " ", "\t", " \t\t ": + ss = prefix + sign + s + vv = v + if sign == "-" and v is not ValueError: + vv = -v + try: + self.assertEqual(int(ss), vv) + except ValueError: + pass + + s = repr(-1-sys.maxsize) + x = int(s) + self.assertEqual(x+1, -sys.maxsize) + self.assert_(isinstance(x, int)) + # should return long + self.assertEqual(int(s[1:]), sys.maxsize+1) + + # should return long + x = int(1e100) + self.assert_(isinstance(x, int)) + x = int(-1e100) + self.assert_(isinstance(x, int)) + + + # SF bug 434186: 0x80000000/2 != 0x80000000>>1. + # Worked by accident in Windows release build, but failed in debug build. + # Failed in all Linux builds. + x = -1-sys.maxsize + self.assertEqual(x >> 1, x//2) + + self.assertRaises(ValueError, int, '123\0') + self.assertRaises(ValueError, int, '53', 40) + + # SF bug 1545497: embedded NULs were not detected with + # explicit base + self.assertRaises(ValueError, int, '123\0', 10) + self.assertRaises(ValueError, int, '123\x00 245', 20) + + x = int('1' * 600) + self.assert_(isinstance(x, int)) + + self.assertRaises(TypeError, int, 1, 12) + + self.assertEqual(int('0o123', 0), 83) + self.assertEqual(int('0x123', 16), 291) + + # Bug 1679: "0x" is not a valid hex literal + self.assertRaises(ValueError, int, "0x", 16) + self.assertRaises(ValueError, int, "0x", 0) + + self.assertRaises(ValueError, int, "0o", 8) + self.assertRaises(ValueError, int, "0o", 0) + + self.assertRaises(ValueError, int, "0b", 2) + self.assertRaises(ValueError, int, "0b", 0) + + + # SF bug 1334662: int(string, base) wrong answers + # Various representations of 2**32 evaluated to 0 + # rather than 2**32 in previous versions + + self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296) + self.assertEqual(int('102002022201221111211', 3), 4294967296) + self.assertEqual(int('10000000000000000', 4), 4294967296) + self.assertEqual(int('32244002423141', 5), 4294967296) + self.assertEqual(int('1550104015504', 6), 4294967296) + self.assertEqual(int('211301422354', 7), 4294967296) + self.assertEqual(int('40000000000', 8), 4294967296) + self.assertEqual(int('12068657454', 9), 4294967296) + self.assertEqual(int('4294967296', 10), 4294967296) + self.assertEqual(int('1904440554', 11), 4294967296) + self.assertEqual(int('9ba461594', 12), 4294967296) + self.assertEqual(int('535a79889', 13), 4294967296) + self.assertEqual(int('2ca5b7464', 14), 4294967296) + self.assertEqual(int('1a20dcd81', 15), 4294967296) + self.assertEqual(int('100000000', 16), 4294967296) + self.assertEqual(int('a7ffda91', 17), 4294967296) + self.assertEqual(int('704he7g4', 18), 4294967296) + self.assertEqual(int('4f5aff66', 19), 4294967296) + self.assertEqual(int('3723ai4g', 20), 4294967296) + self.assertEqual(int('281d55i4', 21), 4294967296) + self.assertEqual(int('1fj8b184', 22), 4294967296) + self.assertEqual(int('1606k7ic', 23), 4294967296) + self.assertEqual(int('mb994ag', 24), 4294967296) + self.assertEqual(int('hek2mgl', 25), 4294967296) + self.assertEqual(int('dnchbnm', 26), 4294967296) + self.assertEqual(int('b28jpdm', 27), 4294967296) + self.assertEqual(int('8pfgih4', 28), 4294967296) + self.assertEqual(int('76beigg', 29), 4294967296) + self.assertEqual(int('5qmcpqg', 30), 4294967296) + self.assertEqual(int('4q0jto4', 31), 4294967296) + self.assertEqual(int('4000000', 32), 4294967296) + self.assertEqual(int('3aokq94', 33), 4294967296) + self.assertEqual(int('2qhxjli', 34), 4294967296) + self.assertEqual(int('2br45qb', 35), 4294967296) + self.assertEqual(int('1z141z4', 36), 4294967296) + + # tests with base 0 + # this fails on 3.0, but in 2.x the old octal syntax is allowed + self.assertEqual(int(' 0o123 ', 0), 83) + self.assertEqual(int(' 0o123 ', 0), 83) + self.assertEqual(int('000', 0), 0) + self.assertEqual(int('0o123', 0), 83) + self.assertEqual(int('0x123', 0), 291) + self.assertEqual(int('0b100', 0), 4) + self.assertEqual(int(' 0O123 ', 0), 83) + self.assertEqual(int(' 0X123 ', 0), 291) + self.assertEqual(int(' 0B100 ', 0), 4) + + # without base still base 10 + self.assertEqual(int('0123'), 123) + self.assertEqual(int('0123', 10), 123) + + # tests with prefix and base != 0 + self.assertEqual(int('0x123', 16), 291) + self.assertEqual(int('0o123', 8), 83) + self.assertEqual(int('0b100', 2), 4) + self.assertEqual(int('0X123', 16), 291) + self.assertEqual(int('0O123', 8), 83) + self.assertEqual(int('0B100', 2), 4) + + # the code has special checks for the first character after the + # type prefix + self.assertRaises(ValueError, int, '0b2', 2) + self.assertRaises(ValueError, int, '0b02', 2) + self.assertRaises(ValueError, int, '0B2', 2) + self.assertRaises(ValueError, int, '0B02', 2) + self.assertRaises(ValueError, int, '0o8', 8) + self.assertRaises(ValueError, int, '0o08', 8) + self.assertRaises(ValueError, int, '0O8', 8) + self.assertRaises(ValueError, int, '0O08', 8) + self.assertRaises(ValueError, int, '0xg', 16) + self.assertRaises(ValueError, int, '0x0g', 16) + self.assertRaises(ValueError, int, '0Xg', 16) + self.assertRaises(ValueError, int, '0X0g', 16) + + # SF bug 1334662: int(string, base) wrong answers + # Checks for proper evaluation of 2**32 + 1 + self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297) + self.assertEqual(int('102002022201221111212', 3), 4294967297) + self.assertEqual(int('10000000000000001', 4), 4294967297) + self.assertEqual(int('32244002423142', 5), 4294967297) + self.assertEqual(int('1550104015505', 6), 4294967297) + self.assertEqual(int('211301422355', 7), 4294967297) + self.assertEqual(int('40000000001', 8), 4294967297) + self.assertEqual(int('12068657455', 9), 4294967297) + self.assertEqual(int('4294967297', 10), 4294967297) + self.assertEqual(int('1904440555', 11), 4294967297) + self.assertEqual(int('9ba461595', 12), 4294967297) + self.assertEqual(int('535a7988a', 13), 4294967297) + self.assertEqual(int('2ca5b7465', 14), 4294967297) + self.assertEqual(int('1a20dcd82', 15), 4294967297) + self.assertEqual(int('100000001', 16), 4294967297) + self.assertEqual(int('a7ffda92', 17), 4294967297) + self.assertEqual(int('704he7g5', 18), 4294967297) + self.assertEqual(int('4f5aff67', 19), 4294967297) + self.assertEqual(int('3723ai4h', 20), 4294967297) + self.assertEqual(int('281d55i5', 21), 4294967297) + self.assertEqual(int('1fj8b185', 22), 4294967297) + self.assertEqual(int('1606k7id', 23), 4294967297) + self.assertEqual(int('mb994ah', 24), 4294967297) + self.assertEqual(int('hek2mgm', 25), 4294967297) + self.assertEqual(int('dnchbnn', 26), 4294967297) + self.assertEqual(int('b28jpdn', 27), 4294967297) + self.assertEqual(int('8pfgih5', 28), 4294967297) + self.assertEqual(int('76beigh', 29), 4294967297) + self.assertEqual(int('5qmcpqh', 30), 4294967297) + self.assertEqual(int('4q0jto5', 31), 4294967297) + self.assertEqual(int('4000001', 32), 4294967297) + self.assertEqual(int('3aokq95', 33), 4294967297) + self.assertEqual(int('2qhxjlj', 34), 4294967297) + self.assertEqual(int('2br45qc', 35), 4294967297) + self.assertEqual(int('1z141z5', 36), 4294967297) + + def test_intconversion(self): + # Test __int__() + class ClassicMissingMethods: + pass + self.assertRaises(TypeError, int, ClassicMissingMethods()) + + class MissingMethods(object): + pass + self.assertRaises(TypeError, int, MissingMethods()) + + class Foo0: + def __int__(self): + return 42 + + class Foo1(object): + def __int__(self): + return 42 + + class Foo2(int): + def __int__(self): + return 42 + + class Foo3(int): + def __int__(self): + return self + + class Foo4(int): + def __int__(self): + return 42 + + class Foo5(int): + def __int__(self): + return 42. + + self.assertEqual(int(Foo0()), 42) + self.assertEqual(int(Foo1()), 42) + self.assertEqual(int(Foo2()), 42) + self.assertEqual(int(Foo3()), 0) + self.assertEqual(int(Foo4()), 42) + self.assertRaises(TypeError, int, Foo5()) + + class Classic: + pass + for base in (object, Classic): + class IntOverridesTrunc(base): + def __int__(self): + return 42 + def __trunc__(self): + return -12 + self.assertEqual(int(IntOverridesTrunc()), 42) + + class JustTrunc(base): + def __trunc__(self): + return 42 + self.assertEqual(int(JustTrunc()), 42) + + for trunc_result_base in (object, Classic): + class Integral(trunc_result_base): + def __int__(self): + return 42 + + class TruncReturnsNonInt(base): + def __trunc__(self): + return Integral() + self.assertEqual(int(TruncReturnsNonInt()), 42) + + class NonIntegral(trunc_result_base): + def __trunc__(self): + # Check that we avoid infinite recursion. + return NonIntegral() + + class TruncReturnsNonIntegral(base): + def __trunc__(self): + return NonIntegral() + try: + int(TruncReturnsNonIntegral()) + except TypeError as e: + self.assertEquals(str(e), + "__trunc__ returned non-Integral" + " (type NonIntegral)") + else: + self.fail("Failed to raise TypeError with %s" % + ((base, trunc_result_base),)) + +def test_main(): + run_unittest(IntTestCases) + +if __name__ == "__main__": + test_main() diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 0c37cc7..ba3ceeb 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1161,10 +1161,10 @@ class MiscIOTest(unittest.TestCase): def test_main(): test_support.run_unittest(IOTest, BytesIOTest, StringIOTest, - BufferedReaderTest, - BufferedWriterTest, BufferedRWPairTest, - BufferedRandomTest, TextIOWrapperTest, - MiscIOTest) + BufferedReaderTest, BufferedWriterTest, + BufferedRWPairTest, BufferedRandomTest, + StatefulIncrementalDecoderTest, + TextIOWrapperTest, MiscIOTest) if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py index b3b2195..3b85117 100644 --- a/Lib/test/test_list.py +++ b/Lib/test/test_list.py @@ -4,6 +4,22 @@ from test import test_support, list_tests class ListTest(list_tests.CommonTest): type2test = list + def test_basic(self): + self.assertEqual(list([]), []) + l0_3 = [0, 1, 2, 3] + l0_3_bis = list(l0_3) + self.assertEqual(l0_3, l0_3_bis) + self.assert_(l0_3 is not l0_3_bis) + self.assertEqual(list(()), []) + self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3]) + self.assertEqual(list(''), []) + self.assertEqual(list('spam'), ['s', 'p', 'a', 'm']) + + # This code used to segfault in Py2.4a3 + x = [] + x.extend(-y for y in x) + self.assertEqual(x, []) + def test_truth(self): super().test_truth() self.assert_(not []) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index f98ba8d..7e159aa 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1,5 +1,6 @@ import unittest from test import test_support +import sys import random @@ -33,6 +34,26 @@ del p2 # add complements & negations special += [~x for x in special] + [-x for x in special] +L = [ + ('0', 0), + ('1', 1), + ('9', 9), + ('10', 10), + ('99', 99), + ('100', 100), + ('314', 314), + (' 314', 314), + ('314 ', 314), + (' \t\t 314 \t\t ', 314), + (repr(sys.maxsize), sys.maxsize), + (' 1x', ValueError), + (' 1 ', 1), + (' 1\02 ', ValueError), + ('', ValueError), + (' ', ValueError), + (' \t\t ', ValueError) +] + class LongTest(unittest.TestCase): @@ -229,8 +250,206 @@ class LongTest(unittest.TestCase): x = self.getran(lenx) self.check_format_1(x) + def test_long(self): + self.assertEqual(int(314), 314) + self.assertEqual(int(3.14), 3) + self.assertEqual(int(314), 314) + # Check that conversion from float truncates towards zero + self.assertEqual(int(-3.14), -3) + self.assertEqual(int(3.9), 3) + self.assertEqual(int(-3.9), -3) + self.assertEqual(int(3.5), 3) + self.assertEqual(int(-3.5), -3) + self.assertEqual(int("-3"), -3) + # Different base: + self.assertEqual(int("10",16), 16) + # Check conversions from string (same test set as for int(), and then some) + LL = [ + ('1' + '0'*20, 10**20), + ('1' + '0'*100, 10**100) + ] + L2 = L[:] + for s, v in L2 + LL: + for sign in "", "+", "-": + for prefix in "", " ", "\t", " \t\t ": + ss = prefix + sign + s + vv = v + if sign == "-" and v is not ValueError: + vv = -v + try: + self.assertEqual(int(ss), int(vv)) + except ValueError: + pass + + self.assertRaises(ValueError, int, '123\0') + self.assertRaises(ValueError, int, '53', 40) + self.assertRaises(TypeError, int, 1, 12) + + # SF patch #1638879: embedded NULs were not detected with + # explicit base + self.assertRaises(ValueError, int, '123\0', 10) + self.assertRaises(ValueError, int, '123\x00 245', 20) + + self.assertEqual(int('100000000000000000000000000000000', 2), + 4294967296) + self.assertEqual(int('102002022201221111211', 3), 4294967296) + self.assertEqual(int('10000000000000000', 4), 4294967296) + self.assertEqual(int('32244002423141', 5), 4294967296) + self.assertEqual(int('1550104015504', 6), 4294967296) + self.assertEqual(int('211301422354', 7), 4294967296) + self.assertEqual(int('40000000000', 8), 4294967296) + self.assertEqual(int('12068657454', 9), 4294967296) + self.assertEqual(int('4294967296', 10), 4294967296) + self.assertEqual(int('1904440554', 11), 4294967296) + self.assertEqual(int('9ba461594', 12), 4294967296) + self.assertEqual(int('535a79889', 13), 4294967296) + self.assertEqual(int('2ca5b7464', 14), 4294967296) + self.assertEqual(int('1a20dcd81', 15), 4294967296) + self.assertEqual(int('100000000', 16), 4294967296) + self.assertEqual(int('a7ffda91', 17), 4294967296) + self.assertEqual(int('704he7g4', 18), 4294967296) + self.assertEqual(int('4f5aff66', 19), 4294967296) + self.assertEqual(int('3723ai4g', 20), 4294967296) + self.assertEqual(int('281d55i4', 21), 4294967296) + self.assertEqual(int('1fj8b184', 22), 4294967296) + self.assertEqual(int('1606k7ic', 23), 4294967296) + self.assertEqual(int('mb994ag', 24), 4294967296) + self.assertEqual(int('hek2mgl', 25), 4294967296) + self.assertEqual(int('dnchbnm', 26), 4294967296) + self.assertEqual(int('b28jpdm', 27), 4294967296) + self.assertEqual(int('8pfgih4', 28), 4294967296) + self.assertEqual(int('76beigg', 29), 4294967296) + self.assertEqual(int('5qmcpqg', 30), 4294967296) + self.assertEqual(int('4q0jto4', 31), 4294967296) + self.assertEqual(int('4000000', 32), 4294967296) + self.assertEqual(int('3aokq94', 33), 4294967296) + self.assertEqual(int('2qhxjli', 34), 4294967296) + self.assertEqual(int('2br45qb', 35), 4294967296) + self.assertEqual(int('1z141z4', 36), 4294967296) + + self.assertEqual(int('100000000000000000000000000000001', 2), + 4294967297) + self.assertEqual(int('102002022201221111212', 3), 4294967297) + self.assertEqual(int('10000000000000001', 4), 4294967297) + self.assertEqual(int('32244002423142', 5), 4294967297) + self.assertEqual(int('1550104015505', 6), 4294967297) + self.assertEqual(int('211301422355', 7), 4294967297) + self.assertEqual(int('40000000001', 8), 4294967297) + self.assertEqual(int('12068657455', 9), 4294967297) + self.assertEqual(int('4294967297', 10), 4294967297) + self.assertEqual(int('1904440555', 11), 4294967297) + self.assertEqual(int('9ba461595', 12), 4294967297) + self.assertEqual(int('535a7988a', 13), 4294967297) + self.assertEqual(int('2ca5b7465', 14), 4294967297) + self.assertEqual(int('1a20dcd82', 15), 4294967297) + self.assertEqual(int('100000001', 16), 4294967297) + self.assertEqual(int('a7ffda92', 17), 4294967297) + self.assertEqual(int('704he7g5', 18), 4294967297) + self.assertEqual(int('4f5aff67', 19), 4294967297) + self.assertEqual(int('3723ai4h', 20), 4294967297) + self.assertEqual(int('281d55i5', 21), 4294967297) + self.assertEqual(int('1fj8b185', 22), 4294967297) + self.assertEqual(int('1606k7id', 23), 4294967297) + self.assertEqual(int('mb994ah', 24), 4294967297) + self.assertEqual(int('hek2mgm', 25), 4294967297) + self.assertEqual(int('dnchbnn', 26), 4294967297) + self.assertEqual(int('b28jpdn', 27), 4294967297) + self.assertEqual(int('8pfgih5', 28), 4294967297) + self.assertEqual(int('76beigh', 29), 4294967297) + self.assertEqual(int('5qmcpqh', 30), 4294967297) + self.assertEqual(int('4q0jto5', 31), 4294967297) + self.assertEqual(int('4000001', 32), 4294967297) + self.assertEqual(int('3aokq95', 33), 4294967297) + self.assertEqual(int('2qhxjlj', 34), 4294967297) + self.assertEqual(int('2br45qc', 35), 4294967297) + self.assertEqual(int('1z141z5', 36), 4294967297) + + + def test_conversion(self): + # Test __long__() + class ClassicMissingMethods: + pass + self.assertRaises(TypeError, int, ClassicMissingMethods()) + + class MissingMethods(object): + pass + self.assertRaises(TypeError, int, MissingMethods()) + + class Foo0: + def __int__(self): + return 42 + + class Foo1(object): + def __int__(self): + return 42 + + class Foo2(int): + def __int__(self): + return 42 + + class Foo3(int): + def __int__(self): + return self + + class Foo4(int): + def __int__(self): + return 42 + + class Foo5(int): + def __int__(self): + return 42. + + self.assertEqual(int(Foo0()), 42) + self.assertEqual(int(Foo1()), 42) + self.assertEqual(int(Foo2()), 42) + self.assertEqual(int(Foo3()), 0) + self.assertEqual(int(Foo4()), 42) + self.assertRaises(TypeError, int, Foo5()) + + class Classic: + pass + for base in (object, Classic): + class LongOverridesTrunc(base): + def __long__(self): + return 42 + def __trunc__(self): + return -12 + self.assertEqual(int(LongOverridesTrunc()), 42) + + class JustTrunc(base): + def __trunc__(self): + return 42 + self.assertEqual(int(JustTrunc()), 42) + + for trunc_result_base in (object, Classic): + class Integral(trunc_result_base): + def __int__(self): + return 42 + + class TruncReturnsNonLong(base): + def __trunc__(self): + return Integral() + self.assertEqual(int(TruncReturnsNonLong()), 42) + + class NonIntegral(trunc_result_base): + def __trunc__(self): + # Check that we avoid infinite recursion. + return NonIntegral() + + class TruncReturnsNonIntegral(base): + def __trunc__(self): + return NonIntegral() + try: + int(TruncReturnsNonIntegral()) + except TypeError as e: + self.assertEquals(str(e), + "__trunc__ returned non-Integral" + " (type NonIntegral)") + else: + self.fail("Failed to raise TypeError with %s" % + ((base, trunc_result_base),)) + def test_misc(self): - import sys # check the extremes in int<->long conversion hugepos = sys.maxsize @@ -403,7 +622,6 @@ class LongTest(unittest.TestCase): def test_mixed_compares(self): eq = self.assertEqual import math - import sys # We're mostly concerned with that mixing floats and longs does the # right stuff, even when longs are too large to fit in a float. diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py index 77d3f88..9f58706 100644 --- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -8,40 +8,23 @@ import warnings class TestUntestedModules(unittest.TestCase): def test_at_least_import_untested_modules(self): with test_support.catch_warning(): - import BaseHTTPServer - import DocXMLRPCServer import CGIHTTPServer - import SimpleHTTPServer - import SimpleXMLRPCServer import aifc import bdb import cgitb - import cmd import code import compileall - import distutils.archive_util import distutils.bcppcompiler import distutils.ccompiler - import distutils.cmd - import distutils.core import distutils.cygwinccompiler - import distutils.dep_util - import distutils.dir_util import distutils.emxccompiler - import distutils.errors - import distutils.extension - import distutils.file_util import distutils.filelist - import distutils.log if sys.platform.startswith('win'): import distutils.msvccompiler import distutils.mwerkscompiler - import distutils.sysconfig import distutils.text_file import distutils.unixccompiler - import distutils.util - import distutils.version import distutils.command.bdist_dumb if sys.platform.startswith('win'): @@ -52,23 +35,18 @@ class TestUntestedModules(unittest.TestCase): import distutils.command.build_clib import distutils.command.build_ext import distutils.command.build - import distutils.command.build_py - import distutils.command.build_scripts import distutils.command.clean import distutils.command.config import distutils.command.install_data import distutils.command.install_egg_info import distutils.command.install_headers import distutils.command.install_lib - import distutils.command.install - import distutils.command.install_scripts import distutils.command.register import distutils.command.sdist import distutils.command.upload import encodings import formatter - import ftplib import getpass import htmlentitydefs import ihooks @@ -78,7 +56,6 @@ class TestUntestedModules(unittest.TestCase): import linecache import macurl2path import mailcap - import mutex import nntplib import nturl2path import opcode @@ -88,14 +65,12 @@ class TestUntestedModules(unittest.TestCase): import py_compile import rlcompleter import sched - import smtplib import sndhdr import statvfs import sunau import sunaudio import symbol import tabnanny - import telnetlib import timeit import token try: diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index f61e70a..431b66b 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -473,8 +473,10 @@ def captured_output(stream_name): import io orig_stdout = getattr(sys, stream_name) setattr(sys, stream_name, io.StringIO()) - yield getattr(sys, stream_name) - setattr(sys, stream_name, orig_stdout) + try: + yield getattr(sys, stream_name) + finally: + setattr(sys, stream_name, orig_stdout) def captured_stdout(): return captured_output("stdout") diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py index 3f2239d..dc97d40 100644 --- a/Lib/test/test_textwrap.py +++ b/Lib/test/test_textwrap.py @@ -129,6 +129,10 @@ What a mess! expect = ['And she said, "Go to hell!" Can you believe that?'] self.check(wrapper.wrap(text), expect) + text = 'File stdio.h is nice.' + expect = ['File stdio.h is nice.'] + self.check(wrapper.wrap(text), expect) + def test_wrap_short(self): # Wrapping to make short lines longer diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 635e1bc..9ecd995 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -4,6 +4,7 @@ from test import test_support import unittest import sys import difflib +import gc # A very basic example. If this fails, we're in deep trouble. def basic(): @@ -244,6 +245,17 @@ class Tracer: return self.trace class TraceTestCase(unittest.TestCase): + + # Disable gc collection when tracing, otherwise the + # deallocators may be traced as well. + def setUp(self): + self.using_gc = gc.isenabled() + gc.disable() + + def tearDown(self): + if self.using_gc: + gc.enable() + def compare_events(self, line_offset, events, expected_events): events = [(l - line_offset, e) for (l, e) in events] if events != expected_events: diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 93e5126..600c7b5 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1,10 +1,24 @@ """Test cases for traceback module""" +from _testcapi import traceback_print +from io import StringIO +import sys import unittest -from test.test_support import run_unittest, is_jython +from test.test_support import run_unittest, is_jython, Error import traceback +try: + raise KeyError +except KeyError: + type_, value, tb = sys.exc_info() + file_ = StringIO() + traceback_print(tb, file_) + example_traceback = file_.getvalue() +else: + raise Error("unable to create test traceback string") + + class TracebackCases(unittest.TestCase): # For now, a very minimal set of tests. I want to be sure that # formatting of SyntaxErrors works based on changes for 2.1. @@ -76,8 +90,20 @@ class TracebackCases(unittest.TestCase): self.assertEqual(err, ['None\n']) +class TracebackFormatTests(unittest.TestCase): + + def test_traceback_indentation(self): + # Make sure that the traceback is properly indented. + tb_lines = example_traceback.splitlines() + self.assertEquals(len(tb_lines), 3) + banner, location, source_line = tb_lines + self.assert_(banner.startswith('Traceback')) + self.assert_(location.startswith(' File')) + self.assert_(source_line.startswith('raise')) + + def test_main(): - run_unittest(TracebackCases) + run_unittest(TracebackCases, TracebackFormatTests) if __name__ == "__main__": diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py index 62c5d71..77672e7 100644 --- a/Lib/test/test_tuple.py +++ b/Lib/test/test_tuple.py @@ -7,6 +7,13 @@ class TupleTest(seq_tests.CommonTest): super().test_len() # calling built-in types without argument must return empty self.assertEqual(tuple(), ()) + t0_3 = (0, 1, 2, 3) + t0_3_bis = tuple(t0_3) + self.assert_(t0_3 is t0_3_bis) + self.assertEqual(tuple([]), ()) + self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) + self.assertEqual(tuple(''), ()) + self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) def test_truth(self): super().test_truth() diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9f0b2c4..d34a177 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1059,6 +1059,12 @@ class MiscTests(unittest.TestCase): o = build_opener(urllib2.HTTPHandler()) self.opener_has_handler(o, urllib2.HTTPHandler) + # Issue2670: multiple handlers sharing the same base class + class MyOtherHTTPHandler(urllib2.HTTPHandler): pass + o = build_opener(MyHTTPHandler, MyOtherHTTPHandler) + self.opener_has_handler(o, MyHTTPHandler) + self.opener_has_handler(o, MyOtherHTTPHandler) + def opener_has_handler(self, opener, handler_class): for h in opener.handlers: if h.__class__ == handler_class: diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index a6cbddf..0f3ee8b 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -229,6 +229,77 @@ class WarnTests(unittest.TestCase): warning_tests.inner("spam7", stacklevel=9999) self.assertEqual(os.path.basename(w.filename), "sys") + def test_missing_filename_not_main(self): + # If __file__ is not specified and __main__ is not the module name, + # then __file__ should be set to the module name. + filename = warning_tests.__file__ + try: + del warning_tests.__file__ + with warnings_state(self.module): + with test_support.catch_warning(self.module) as w: + warning_tests.inner("spam8", stacklevel=1) + self.assertEqual(w.filename, warning_tests.__name__) + finally: + warning_tests.__file__ = filename + + def test_missing_filename_main_with_argv(self): + # If __file__ is not specified and the caller is __main__ and sys.argv + # exists, then use sys.argv[0] as the file. + if not hasattr(sys, 'argv'): + return + filename = warning_tests.__file__ + module_name = warning_tests.__name__ + try: + del warning_tests.__file__ + warning_tests.__name__ = '__main__' + with warnings_state(self.module): + with test_support.catch_warning(self.module) as w: + warning_tests.inner('spam9', stacklevel=1) + self.assertEqual(w.filename, sys.argv[0]) + finally: + warning_tests.__file__ = filename + warning_tests.__name__ = module_name + + def test_missing_filename_main_without_argv(self): + # If __file__ is not specified, the caller is __main__, and sys.argv + # is not set, then '__main__' is the file name. + filename = warning_tests.__file__ + module_name = warning_tests.__name__ + argv = sys.argv + try: + del warning_tests.__file__ + warning_tests.__name__ = '__main__' + del sys.argv + with warnings_state(self.module): + with test_support.catch_warning(self.module) as w: + warning_tests.inner('spam10', stacklevel=1) + self.assertEqual(w.filename, '__main__') + finally: + warning_tests.__file__ = filename + warning_tests.__name__ = module_name + sys.argv = argv + + def BROKEN_test_missing_filename_main_with_argv_empty_string(self): + # If __file__ is not specified, the caller is __main__, and sys.argv[0] + # is the empty string, then '__main__ is the file name. + # Tests issue 2743. + file_name = warning_tests.__file__ + module_name = warning_tests.__name__ + argv = sys.argv + try: + del warning_tests.__file__ + warning_tests.__name__ = '__main__' + sys.argv = [''] + with warnings_state(self.module): + with test_support.catch_warning(self.module) as w: + warning_tests.inner('spam11', stacklevel=1) + self.assertEqual(w.filename, '__main__') + finally: + warning_tests.__file__ = file_name + warning_tests.__name__ = module_name + sys.argv = argv + + class CWarnTests(BaseTest, WarnTests): module = c_warnings diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py index e778463..6e439ac 100644 --- a/Lib/test/test_with.py +++ b/Lib/test/test_with.py @@ -2,7 +2,6 @@ """Unit tests for the with statement specified in PEP 343.""" -from __future__ import with_statement __author__ = "Mike Bland" __email__ = "mbland at acm dot org" diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index a254eee..7ff078d 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -50,7 +50,7 @@ class XMLRPCTestCase(unittest.TestCase): self.assertEquals(newdt, xmlrpclib.DateTime('20050210T11:41:23')) def test_datetime_before_1900(self): - # same as before but with an date before 1900 + # same as before but with a date before 1900 dt = datetime.datetime(1, 2, 10, 11, 41, 23) s = xmlrpclib.dumps((dt,)) (newdt,), m = xmlrpclib.loads(s, use_datetime=1) diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 7e05c1a..b5f87efc 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -79,7 +79,7 @@ class TextWrapper: sentence_end_re = re.compile(r'[a-z]' # lowercase letter r'[\.\!\?]' # sentence-ending punct. r'[\"\']?' # optional end-of-quote - ) + r'\Z') # end of chunk def __init__(self, diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 76035a3..465aa5e 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -443,14 +443,14 @@ def build_opener(*handlers): FTPHandler, FileHandler, HTTPErrorProcessor] if hasattr(httplib, 'HTTPS'): default_classes.append(HTTPSHandler) - skip = [] + skip = set() for klass in default_classes: for check in handlers: if isclass(check): if issubclass(check, klass): - skip.append(klass) + skip.add(klass) elif isinstance(check, klass): - skip.append(klass) + skip.add(klass) for klass in skip: default_classes.remove(klass) diff --git a/Lib/weakref.py b/Lib/weakref.py index 6c479a4..64d962c 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -2,7 +2,7 @@ This module is an implementation of PEP 205: -http://python.sourceforge.net/peps/pep-0205.html +http://www.python.org/dev/peps/pep-0205/ """ # Naming convention: Variables named "wr" are weak reference objects; |