mirror of
https://github.com/python/cpython.git
synced 2025-10-25 18:54:53 +00:00
svn+ssh://pythondev@svn.python.org/python/trunk
................
r62425 | andrew.kuchling | 2008-04-21 03:45:57 +0200 (Mon, 21 Apr 2008) | 1 line
Comment typo
................
r62426 | mark.dickinson | 2008-04-21 03:55:50 +0200 (Mon, 21 Apr 2008) | 2 lines
Silence 'r may be used uninitialized' compiler warning.
................
r62427 | andrew.kuchling | 2008-04-21 04:08:00 +0200 (Mon, 21 Apr 2008) | 1 line
Markup fix
................
r62428 | andrew.kuchling | 2008-04-21 04:08:13 +0200 (Mon, 21 Apr 2008) | 1 line
Wording changes
................
r62429 | andrew.kuchling | 2008-04-21 04:14:24 +0200 (Mon, 21 Apr 2008) | 1 line
Add various items
................
r62434 | thomas.heller | 2008-04-21 15:46:55 +0200 (Mon, 21 Apr 2008) | 1 line
Fix typo.
................
r62435 | david.goodger | 2008-04-21 16:40:22 +0200 (Mon, 21 Apr 2008) | 1 line
corrections ("reStructuredText" is one word)
................
r62436 | david.goodger | 2008-04-21 16:43:33 +0200 (Mon, 21 Apr 2008) | 1 line
capitalization
................
r62441 | gregory.p.smith | 2008-04-21 19:46:40 +0200 (Mon, 21 Apr 2008) | 2 lines
explicitly flush after the ... since there wasn't a newline
................
r62444 | jeroen.ruigrok | 2008-04-21 22:15:39 +0200 (Mon, 21 Apr 2008) | 2 lines
Windows x64 also falls under VER_PLATFORM_WIN32_NT.
................
r62446 | gregory.p.smith | 2008-04-21 23:31:08 +0200 (Mon, 21 Apr 2008) | 3 lines
If sys.stdin is not a tty, fall back to default_getpass after printing
a warning instead of failing with a termios.error.
................
r62447 | mark.dickinson | 2008-04-22 00:32:24 +0200 (Tue, 22 Apr 2008) | 8 lines
test_math and test_cmath are failing on the FreeBSD 6.2 trunk buildbot,
apparently because tanh(-0.) loses the sign of zero on that platform.
If true, this is a bug in FreeBSD.
Added a configure test to verify this. I still need to figure out
how best to deal with this failure.
................
r62448 | amaury.forgeotdarc | 2008-04-22 00:35:30 +0200 (Tue, 22 Apr 2008) | 7 lines
Issue 2665: On Windows, sys.stderr does not contain a valid file when running without a console.
It seems to work, but will fail at the first flush.
This causes IDLE to crash when too many warnings are printed.
Will backport.
................
r62450 | benjamin.peterson | 2008-04-22 00:57:00 +0200 (Tue, 22 Apr 2008) | 2 lines
Fix Sphinx warnings
................
r62451 | mark.dickinson | 2008-04-22 02:54:27 +0200 (Tue, 22 Apr 2008) | 3 lines
Make configure test for tanh(-0.) == -0. committed in r62447 actually
work. (The test wasn't properly linked with libm. Sigh.)
................
r62452 | benjamin.peterson | 2008-04-22 04:16:03 +0200 (Tue, 22 Apr 2008) | 2 lines
Various io doc updates
................
r62453 | neal.norwitz | 2008-04-22 07:07:47 +0200 (Tue, 22 Apr 2008) | 1 line
Add Thomas Lee
................
r62454 | gregory.p.smith | 2008-04-22 10:08:41 +0200 (Tue, 22 Apr 2008) | 8 lines
Major improvements:
* Default to using /dev/tty for the password prompt and input before
falling back to sys.stdin and sys.stderr.
* Use sys.stderr instead of sys.stdout.
* print the 'password may be echoed' warning to stream used to display
the prompt rather than always sys.stderr.
* warn() with GetPassWarning when input may be echoed.
................
r62455 | gregory.p.smith | 2008-04-22 10:11:33 +0200 (Tue, 22 Apr 2008) | 2 lines
update the getpass entry
................
r62463 | amaury.forgeotdarc | 2008-04-22 23:14:41 +0200 (Tue, 22 Apr 2008) | 5 lines
Issue #2670: urllib2.build_opener() failed when two handlers
derive the same default base class.
Will backport.
................
r62465 | skip.montanaro | 2008-04-23 00:45:09 +0200 (Wed, 23 Apr 2008) | 3 lines
Factor in documentation changes from issue 1753732.
................
r62466 | gregory.p.smith | 2008-04-23 03:06:42 +0200 (Wed, 23 Apr 2008) | 2 lines
syntax fixup
................
r62469 | benjamin.peterson | 2008-04-23 22:38:06 +0200 (Wed, 23 Apr 2008) | 2 lines
#2673 Fix example typo in optparse docs
................
r62474 | martin.v.loewis | 2008-04-24 11:50:50 +0200 (Thu, 24 Apr 2008) | 2 lines
Add Guilherme Polo.
................
r62476 | martin.v.loewis | 2008-04-24 15:16:36 +0200 (Thu, 24 Apr 2008) | 3 lines
Remove Py_Refcnt, Py_Type, Py_Size, as they were added only
for backwards compatibility, yet 2.5 did not have them at all.
................
r62477 | martin.v.loewis | 2008-04-24 15:17:24 +0200 (Thu, 24 Apr 2008) | 2 lines
Fix typo.
................
r62478 | martin.v.loewis | 2008-04-24 15:18:03 +0200 (Thu, 24 Apr 2008) | 2 lines
Add Jesus Cea.
................
r62480 | amaury.forgeotdarc | 2008-04-24 20:07:05 +0200 (Thu, 24 Apr 2008) | 4 lines
Issue2681: the literal 0o8 was wrongly accepted, and evaluated as float(0.0).
This happened only when 8 is the first digit.
Credits go to Lukas Meuser.
................
r62485 | amaury.forgeotdarc | 2008-04-24 22:10:26 +0200 (Thu, 24 Apr 2008) | 5 lines
Disable gc when running test_trace, or we may record the __del__ of collected objects.
See http://mail.python.org/pipermail/python-checkins/2008-April/068633.html
the extra events perfectly match several calls to socket._fileobject.__del__()
................
r62492 | neal.norwitz | 2008-04-25 05:40:17 +0200 (Fri, 25 Apr 2008) | 1 line
Fix typo (now -> no)
................
r62497 | armin.rigo | 2008-04-25 11:35:18 +0200 (Fri, 25 Apr 2008) | 2 lines
A new crasher.
................
r62498 | thomas.heller | 2008-04-25 17:44:16 +0200 (Fri, 25 Apr 2008) | 1 line
Add from_buffer and from_buffer_copy class methods to ctypes types.
................
r62500 | mark.dickinson | 2008-04-25 18:59:09 +0200 (Fri, 25 Apr 2008) | 3 lines
Issue 2635: fix bug in the fix_sentence_endings option to textwrap.fill.
................
r62507 | benjamin.peterson | 2008-04-25 23:43:56 +0200 (Fri, 25 Apr 2008) | 2 lines
Allow test_import to work when it is invoked directly
................
r62513 | georg.brandl | 2008-04-26 20:31:07 +0200 (Sat, 26 Apr 2008) | 2 lines
#2691: document PyLong (s)size_t APIs, patch by Alexander Belopolsky.
................
r62514 | georg.brandl | 2008-04-26 20:32:17 +0200 (Sat, 26 Apr 2008) | 2 lines
Add missing return type to dealloc.
................
r62516 | alexandre.vassalotti | 2008-04-27 02:52:24 +0200 (Sun, 27 Apr 2008) | 2 lines
Fixed URL of PEP 205 in weakref's module docstring.
................
r62521 | georg.brandl | 2008-04-27 11:39:59 +0200 (Sun, 27 Apr 2008) | 2 lines
#2677: add note that not all functions may accept keyword args.
................
r62531 | georg.brandl | 2008-04-27 19:38:55 +0200 (Sun, 27 Apr 2008) | 2 lines
Use correct XHTML tags.
................
r62535 | benjamin.peterson | 2008-04-27 20:14:39 +0200 (Sun, 27 Apr 2008) | 2 lines
#2700 Document PyNumber_ToBase
................
r62545 | skip.montanaro | 2008-04-27 22:53:57 +0200 (Sun, 27 Apr 2008) | 1 line
minor wording changes, rewrap a few lines
................
r62546 | kurt.kaiser | 2008-04-27 23:07:41 +0200 (Sun, 27 Apr 2008) | 7 lines
Home / Control-A toggles between left margin and end of leading white
space. Patch 1196903 Jeff Shute.
M idlelib/PyShell.py
M idlelib/EditorWindow.py
M idlelib/NEWS.txt
................
r62548 | kurt.kaiser | 2008-04-27 23:38:05 +0200 (Sun, 27 Apr 2008) | 2 lines
Improved AutoCompleteWindow logic. Patch 2062 Tal Einat.
................
r62549 | kurt.kaiser | 2008-04-27 23:52:19 +0200 (Sun, 27 Apr 2008) | 4 lines
Autocompletion of filenames now support alternate separators, e.g. the
'/' char on Windows. Patch 2061 Tal Einat.
................
r62550 | skip.montanaro | 2008-04-28 00:49:56 +0200 (Mon, 28 Apr 2008) | 6 lines
A few small changes:
* The only exception we should catch when trying to import cStringIO is an
ImportError.
* Delete the function signatures embedded in the mk*temp docstrings.
* The tempdir global variable was initialized twice.
................
r62551 | skip.montanaro | 2008-04-28 00:52:02 +0200 (Mon, 28 Apr 2008) | 4 lines
Wrap some long paragraphs and include the default values for optional
function parameters.
................
r62553 | skip.montanaro | 2008-04-28 04:57:23 +0200 (Mon, 28 Apr 2008) | 7 lines
Minor cleanups:
* Avoid creating unused local variables where we can. Where we can't prefix
the unused variables with '_'.
* Avoid shadowing builtins where it won't change the external interface of a
function.
* Use None as default path arg to readmodule and readmodule_ex.
................
r62554 | skip.montanaro | 2008-04-28 04:59:45 +0200 (Mon, 28 Apr 2008) | 6 lines
Correct documentation to match implementation: "Class" instead of
"class_descriptor", "Function" instead of "function_descriptor". Note
default path value for readmodule*. Wrap some long paragraphs. Don't
mention 'inpackage' which isn't part of the public API.
................
r62555 | brett.cannon | 2008-04-28 05:23:50 +0200 (Mon, 28 Apr 2008) | 5 lines
Fix a bug introduced by the warnings rewrite where tracebacks were being
improperly indented.
Closes issue #2699.
................
r62556 | skip.montanaro | 2008-04-28 05:25:37 +0200 (Mon, 28 Apr 2008) | 2 lines
Wrap some long lines.
................
r62557 | skip.montanaro | 2008-04-28 05:27:53 +0200 (Mon, 28 Apr 2008) | 6 lines
Get rid of _test(), _main(), _debug() and _check(). Tests are no longer
needed (better set available in Lib/test/test_robotparser.py). Clean up a
few PEP 8 nits (compound statements on a single line, whitespace around
operators).
................
r62558 | brett.cannon | 2008-04-28 06:50:06 +0200 (Mon, 28 Apr 2008) | 3 lines
Rename the test_traceback_print() function to traceback_print() to prevent
test_capi from automatically calling the function.
................
r62559 | georg.brandl | 2008-04-28 07:16:30 +0200 (Mon, 28 Apr 2008) | 2 lines
Fix markup.
................
r62569 | amaury.forgeotdarc | 2008-04-28 23:07:06 +0200 (Mon, 28 Apr 2008) | 5 lines
test_sundry performs minimal tests (a simple import...) on modules that are not tested otherwise.
Some of them now have tests and can be removed.
Only 70 to go...
................
r62574 | andrew.kuchling | 2008-04-29 04:03:54 +0200 (Tue, 29 Apr 2008) | 1 line
Strip down SSL docs; I'm not managing to get test programs working, so I'll just give a minimal description
................
r62577 | martin.v.loewis | 2008-04-29 08:10:53 +0200 (Tue, 29 Apr 2008) | 2 lines
Add Rodrigo and Heiko.
................
r62593 | nick.coghlan | 2008-04-30 16:23:36 +0200 (Wed, 30 Apr 2008) | 1 line
Update command line usage documentation to reflect 2.6 changes (also includes some minor cleanups). Addresses TODO list issue 2258
................
r62595 | andrew.kuchling | 2008-04-30 18:19:55 +0200 (Wed, 30 Apr 2008) | 1 line
Typo fix
................
r62604 | benjamin.peterson | 2008-04-30 23:03:58 +0200 (Wed, 30 Apr 2008) | 2 lines
make test_support's captured_output a bit more robust when exceptions happen
................
r62605 | georg.brandl | 2008-04-30 23:08:42 +0200 (Wed, 30 Apr 2008) | 2 lines
#1748: use functools.wraps instead of rolling own metadata update.
................
r62606 | benjamin.peterson | 2008-04-30 23:25:55 +0200 (Wed, 30 Apr 2008) | 2 lines
Remove some from __future__ import with_statements
................
r62608 | benjamin.peterson | 2008-05-01 00:03:36 +0200 (Thu, 01 May 2008) | 2 lines
Fix typo in whatsnew
................
r62616 | georg.brandl | 2008-05-01 20:24:32 +0200 (Thu, 01 May 2008) | 2 lines
Fix synopsis.
................
r62626 | brett.cannon | 2008-05-02 04:25:09 +0200 (Fri, 02 May 2008) | 6 lines
Fix a backwards-compatibility mistake where a new optional argument for
warnings.showwarning() was being used. This broke pre-existing replacements for
the function since they didn't support the extra argument.
Closes issue 2705.
................
r62627 | gregory.p.smith | 2008-05-02 09:26:52 +0200 (Fri, 02 May 2008) | 20 lines
This should fix issue2632. A long description of the two competing
problems is in the bug report (one old, one recently introduced trying
to fix the old one). In short:
buffer data during socket._fileobject.read() and readlines() within a
cStringIO object instead of a [] of str()s returned from the recv()
call.
This prevents excessive memory use due to the size parameter being
passed to recv() being grossly larger than the actual size of the data
returned *and* prevents excessive cpu usage due to looping in python
calling recv() with a very tiny size value if min() is used as the
previous memory-use bug "fix" did.
It also documents what the socket._fileobject._rbufsize member is
actually used for.
This is a candidate for back porting to 2.5.
................
r62636 | mark.hammond | 2008-05-02 14:48:15 +0200 (Fri, 02 May 2008) | 2 lines
#2581: Vista UAC/elevation support for bdist_wininst
................
r62638 | facundo.batista | 2008-05-02 19:39:00 +0200 (Fri, 02 May 2008) | 3 lines
Fixed some test structures. Thanks Mark Dickinson.
................
r62644 | ronald.oussoren | 2008-05-02 21:45:11 +0200 (Fri, 02 May 2008) | 7 lines
Fix for issue #2573: Can't change the framework name on OS X builds
This introduces a new configure option: --with-framework-name=NAME
(defaulting to 'Python'). This allows you to install several copies
of the Python framework with different names (such as a normal build
and a debug build).
................
r62645 | ronald.oussoren | 2008-05-02 21:58:56 +0200 (Fri, 02 May 2008) | 2 lines
Finish fix for issue2573, previous patch was incomplete.
................
r62647 | martin.v.loewis | 2008-05-02 23:30:20 +0200 (Fri, 02 May 2008) | 13 lines
Merged revisions 62263-62646 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r62470 | david.wolever | 2008-04-24 02:11:07 +0200 (Do, 24 Apr 2008) | 3 lines
Fixed up and applied the patch for #2431 -- speeding up 2to3 with a lookup table.
........
r62646 | martin.v.loewis | 2008-05-02 23:29:27 +0200 (Fr, 02 Mai 2008) | 2 lines
Fix whitespace.
........
................
r62648 | ronald.oussoren | 2008-05-02 23:42:35 +0200 (Fri, 02 May 2008) | 4 lines
Fix for #1905: PythonLauncher not working correctly on OSX 10.5/Leopard
This fixes both Python Launchar and the terminalcommand module.
................
r62651 | ronald.oussoren | 2008-05-02 23:54:56 +0200 (Fri, 02 May 2008) | 2 lines
Fix for issue #2520 (cannot import macerrors)
................
r62652 | benjamin.peterson | 2008-05-03 00:12:58 +0200 (Sat, 03 May 2008) | 2 lines
capitalization nit for reStructuredText
................
r62653 | brett.cannon | 2008-05-03 03:02:41 +0200 (Sat, 03 May 2008) | 2 lines
Fix some indentation errors.
................
r62656 | brett.cannon | 2008-05-03 05:19:39 +0200 (Sat, 03 May 2008) | 6 lines
Fix the C implementation of 'warnings' to infer the filename of the module that
raised an exception properly when __file__ is not set, __name__ == '__main__',
and sys.argv[0] is a false value.
Closes issue2743.
................
r62661 | amaury.forgeotdarc | 2008-05-03 14:21:13 +0200 (Sat, 03 May 2008) | 8 lines
In test_io, StatefulIncrementalDecoderTest was not part of the test suite.
And of course, the test failed:
a bytearray was used without reason in io.TextIOWrapper.tell().
The difference is that iterating over bytes (i.e. str in python2.6) returns 1-char bytes,
whereas bytearrays yield integers.
This code should still work with python3.0
................
r62663 | benjamin.peterson | 2008-05-03 17:56:42 +0200 (Sat, 03 May 2008) | 2 lines
The compiling struct is now passed around to all AST helpers (see issue 2720)
................
r62680 | benjamin.peterson | 2008-05-03 23:35:18 +0200 (Sat, 03 May 2008) | 2 lines
Moved testing of builtin types out of test_builtin and into type specific modules
................
r62686 | mark.dickinson | 2008-05-04 04:25:46 +0200 (Sun, 04 May 2008) | 4 lines
Make sure that Context traps and flags dictionaries have values 0 and 1
(as documented) rather than True and False.
................
r62687 | benjamin.peterson | 2008-05-04 05:05:49 +0200 (Sun, 04 May 2008) | 2 lines
Fix typo in whatsnew
................
r62696 | georg.brandl | 2008-05-04 11:15:04 +0200 (Sun, 04 May 2008) | 2 lines
#2752: wrong meaning of '' for socket host.
................
r62699 | christian.heimes | 2008-05-04 13:50:53 +0200 (Sun, 04 May 2008) | 1 line
Added note that Python requires at least Win2k SP4
................
r62700 | gerhard.haering | 2008-05-04 14:59:57 +0200 (Sun, 04 May 2008) | 3 lines
SQLite requires 64-bit integers in order to build. So the whole HAVE_LONG_LONG
#ifdefing was useless.
................
r62701 | gerhard.haering | 2008-05-04 15:15:12 +0200 (Sun, 04 May 2008) | 3 lines
Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
sqlite3.Row type is now correctly hashable.
................
r62702 | gerhard.haering | 2008-05-04 15:42:44 +0200 (Sun, 04 May 2008) | 5 lines
Implemented feature request 2157: Converter names are cut off at '('
characters. This avoids the common case of something like 'NUMBER(10)' not
being parsed as 'NUMBER', like expected. Also corrected the docs about
converter names being case-sensitive. They aren't any longer.
................
r62703 | georg.brandl | 2008-05-04 17:45:05 +0200 (Sun, 04 May 2008) | 2 lines
#2757: Remove spare newline.
................
r62711 | benjamin.peterson | 2008-05-04 21:10:02 +0200 (Sun, 04 May 2008) | 2 lines
Fix typo in bugs.rst
................
1242 lines
42 KiB
Python
1242 lines
42 KiB
Python
# Python test set -- built-in functions
|
|
|
|
import test.test_support, unittest
|
|
from test.test_support import fcmp, TESTFN, unlink, run_unittest, \
|
|
run_with_locale
|
|
from operator import neg
|
|
|
|
import sys, warnings, random, collections, io, fractions
|
|
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
|
|
FutureWarning, __name__)
|
|
warnings.filterwarnings("ignore", "integer argument expected",
|
|
DeprecationWarning, "unittest")
|
|
|
|
class Squares:
|
|
|
|
def __init__(self, max):
|
|
self.max = max
|
|
self.sofar = []
|
|
|
|
def __len__(self): return len(self.sofar)
|
|
|
|
def __getitem__(self, i):
|
|
if not 0 <= i < self.max: raise IndexError
|
|
n = len(self.sofar)
|
|
while n <= i:
|
|
self.sofar.append(n*n)
|
|
n += 1
|
|
return self.sofar[i]
|
|
|
|
class StrSquares:
|
|
|
|
def __init__(self, max):
|
|
self.max = max
|
|
self.sofar = []
|
|
|
|
def __len__(self):
|
|
return len(self.sofar)
|
|
|
|
def __getitem__(self, i):
|
|
if not 0 <= i < self.max:
|
|
raise IndexError
|
|
n = len(self.sofar)
|
|
while n <= i:
|
|
self.sofar.append(str(n*n))
|
|
n += 1
|
|
return self.sofar[i]
|
|
|
|
class BitBucket:
|
|
def write(self, line):
|
|
pass
|
|
|
|
test_conv_no_sign = [
|
|
('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),
|
|
(str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
|
|
(chr(0x200), ValueError),
|
|
]
|
|
|
|
test_conv_sign = [
|
|
('0', 0),
|
|
('1', 1),
|
|
('9', 9),
|
|
('10', 10),
|
|
('99', 99),
|
|
('100', 100),
|
|
('314', 314),
|
|
(' 314', ValueError),
|
|
('314 ', 314),
|
|
(' \t\t 314 \t\t ', ValueError),
|
|
(repr(sys.maxsize), sys.maxsize),
|
|
(' 1x', ValueError),
|
|
(' 1 ', ValueError),
|
|
(' 1\02 ', ValueError),
|
|
('', ValueError),
|
|
(' ', ValueError),
|
|
(' \t\t ', ValueError),
|
|
(str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
|
|
(chr(0x200), ValueError),
|
|
]
|
|
|
|
class TestFailingBool:
|
|
def __bool__(self):
|
|
raise RuntimeError
|
|
|
|
class TestFailingIter:
|
|
def __iter__(self):
|
|
raise RuntimeError
|
|
|
|
class BuiltinTest(unittest.TestCase):
|
|
|
|
def test_import(self):
|
|
__import__('sys')
|
|
__import__('time')
|
|
__import__('string')
|
|
__import__(name='sys')
|
|
__import__(name='time', level=0)
|
|
self.assertRaises(ImportError, __import__, 'spamspam')
|
|
self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
|
|
self.assertRaises(ValueError, __import__, '')
|
|
self.assertRaises(TypeError, __import__, 'sys', name='sys')
|
|
|
|
def test_abs(self):
|
|
# int
|
|
self.assertEqual(abs(0), 0)
|
|
self.assertEqual(abs(1234), 1234)
|
|
self.assertEqual(abs(-1234), 1234)
|
|
self.assertTrue(abs(-sys.maxsize-1) > 0)
|
|
# float
|
|
self.assertEqual(abs(0.0), 0.0)
|
|
self.assertEqual(abs(3.14), 3.14)
|
|
self.assertEqual(abs(-3.14), 3.14)
|
|
# long
|
|
self.assertEqual(abs(0), 0)
|
|
self.assertEqual(abs(1234), 1234)
|
|
self.assertEqual(abs(-1234), 1234)
|
|
# str
|
|
self.assertRaises(TypeError, abs, 'a')
|
|
|
|
def test_all(self):
|
|
self.assertEqual(all([2, 4, 6]), True)
|
|
self.assertEqual(all([2, None, 6]), False)
|
|
self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6])
|
|
self.assertRaises(RuntimeError, all, TestFailingIter())
|
|
self.assertRaises(TypeError, all, 10) # Non-iterable
|
|
self.assertRaises(TypeError, all) # No args
|
|
self.assertRaises(TypeError, all, [2, 4, 6], []) # Too many args
|
|
self.assertEqual(all([]), True) # Empty iterator
|
|
S = [50, 60]
|
|
self.assertEqual(all(x > 42 for x in S), True)
|
|
S = [50, 40, 60]
|
|
self.assertEqual(all(x > 42 for x in S), False)
|
|
|
|
def test_any(self):
|
|
self.assertEqual(any([None, None, None]), False)
|
|
self.assertEqual(any([None, 4, None]), True)
|
|
self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6])
|
|
self.assertRaises(RuntimeError, all, TestFailingIter())
|
|
self.assertRaises(TypeError, any, 10) # Non-iterable
|
|
self.assertRaises(TypeError, any) # No args
|
|
self.assertRaises(TypeError, any, [2, 4, 6], []) # Too many args
|
|
self.assertEqual(any([]), False) # Empty iterator
|
|
S = [40, 60, 30]
|
|
self.assertEqual(any(x > 42 for x in S), True)
|
|
S = [10, 20, 30]
|
|
self.assertEqual(any(x > 42 for x in S), False)
|
|
|
|
def test_neg(self):
|
|
x = -sys.maxsize-1
|
|
self.assert_(isinstance(x, int))
|
|
self.assertEqual(-x, sys.maxsize+1)
|
|
|
|
# XXX(nnorwitz): This test case for callable should probably be removed.
|
|
def test_callable(self):
|
|
self.assert_(hasattr(len, '__call__'))
|
|
def f(): pass
|
|
self.assert_(hasattr(f, '__call__'))
|
|
class C:
|
|
def meth(self): pass
|
|
self.assert_(hasattr(C, '__call__'))
|
|
x = C()
|
|
self.assert_(hasattr(x.meth, '__call__'))
|
|
self.assert_(not hasattr(x, '__call__'))
|
|
class D(C):
|
|
def __call__(self): pass
|
|
y = D()
|
|
self.assert_(hasattr(y, '__call__'))
|
|
y()
|
|
|
|
def test_chr(self):
|
|
self.assertEqual(chr(32), ' ')
|
|
self.assertEqual(chr(65), 'A')
|
|
self.assertEqual(chr(97), 'a')
|
|
self.assertEqual(chr(0xff), '\xff')
|
|
self.assertRaises(ValueError, chr, 1<<24)
|
|
self.assertEqual(chr(sys.maxunicode),
|
|
str(('\\U%08x' % (sys.maxunicode)).encode("ascii"),
|
|
'unicode-escape'))
|
|
self.assertRaises(TypeError, chr)
|
|
self.assertEqual(chr(0x0000FFFF), "\U0000FFFF")
|
|
self.assertEqual(chr(0x00010000), "\U00010000")
|
|
self.assertEqual(chr(0x00010001), "\U00010001")
|
|
self.assertEqual(chr(0x000FFFFE), "\U000FFFFE")
|
|
self.assertEqual(chr(0x000FFFFF), "\U000FFFFF")
|
|
self.assertEqual(chr(0x00100000), "\U00100000")
|
|
self.assertEqual(chr(0x00100001), "\U00100001")
|
|
self.assertEqual(chr(0x0010FFFE), "\U0010FFFE")
|
|
self.assertEqual(chr(0x0010FFFF), "\U0010FFFF")
|
|
self.assertRaises(ValueError, chr, -1)
|
|
self.assertRaises(ValueError, chr, 0x00110000)
|
|
|
|
def test_cmp(self):
|
|
self.assertEqual(cmp(-1, 1), -1)
|
|
self.assertEqual(cmp(1, -1), 1)
|
|
self.assertEqual(cmp(1, 1), 0)
|
|
# verify that circular objects are not handled
|
|
a = []; a.append(a)
|
|
b = []; b.append(b)
|
|
from collections import UserList
|
|
c = UserList(); c.append(c)
|
|
self.assertRaises(RuntimeError, cmp, a, b)
|
|
self.assertRaises(RuntimeError, cmp, b, c)
|
|
self.assertRaises(RuntimeError, cmp, c, a)
|
|
self.assertRaises(RuntimeError, cmp, a, c)
|
|
# okay, now break the cycles
|
|
a.pop(); b.pop(); c.pop()
|
|
self.assertRaises(TypeError, cmp)
|
|
|
|
def test_compile(self):
|
|
compile('print(1)\n', '', 'exec')
|
|
bom = b'\xef\xbb\xbf'
|
|
compile(bom + b'print(1)\n', '', 'exec')
|
|
compile(source='pass', filename='?', mode='exec')
|
|
compile(dont_inherit=0, filename='tmp', source='0', mode='eval')
|
|
compile('pass', '?', dont_inherit=1, mode='exec')
|
|
self.assertRaises(TypeError, compile)
|
|
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
|
|
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff)
|
|
self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
|
|
self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
|
|
mode='eval', source='0', filename='tmp')
|
|
compile('print("\xe5")\n', '', 'exec')
|
|
self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
|
|
self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
|
|
|
|
def test_delattr(self):
|
|
import sys
|
|
sys.spam = 1
|
|
delattr(sys, 'spam')
|
|
self.assertRaises(TypeError, delattr)
|
|
|
|
def test_dir(self):
|
|
# dir(wrong number of arguments)
|
|
self.assertRaises(TypeError, dir, 42, 42)
|
|
|
|
# dir() - local scope
|
|
local_var = 1
|
|
self.assert_('local_var' in dir())
|
|
|
|
# dir(module)
|
|
import sys
|
|
self.assert_('exit' in dir(sys))
|
|
|
|
# dir(module_with_invalid__dict__)
|
|
import types
|
|
class Foo(types.ModuleType):
|
|
__dict__ = 8
|
|
f = Foo("foo")
|
|
self.assertRaises(TypeError, dir, f)
|
|
|
|
# dir(type)
|
|
self.assert_("strip" in dir(str))
|
|
self.assert_("__mro__" not in dir(str))
|
|
|
|
# dir(obj)
|
|
class Foo(object):
|
|
def __init__(self):
|
|
self.x = 7
|
|
self.y = 8
|
|
self.z = 9
|
|
f = Foo()
|
|
self.assert_("y" in dir(f))
|
|
|
|
# dir(obj_no__dict__)
|
|
class Foo(object):
|
|
__slots__ = []
|
|
f = Foo()
|
|
self.assert_("__repr__" in dir(f))
|
|
|
|
# dir(obj_no__class__with__dict__)
|
|
# (an ugly trick to cause getattr(f, "__class__") to fail)
|
|
class Foo(object):
|
|
__slots__ = ["__class__", "__dict__"]
|
|
def __init__(self):
|
|
self.bar = "wow"
|
|
f = Foo()
|
|
self.assert_("__repr__" not in dir(f))
|
|
self.assert_("bar" in dir(f))
|
|
|
|
# dir(obj_using __dir__)
|
|
class Foo(object):
|
|
def __dir__(self):
|
|
return ["kan", "ga", "roo"]
|
|
f = Foo()
|
|
self.assert_(dir(f) == ["ga", "kan", "roo"])
|
|
|
|
# dir(obj__dir__not_list)
|
|
class Foo(object):
|
|
def __dir__(self):
|
|
return 7
|
|
f = Foo()
|
|
self.assertRaises(TypeError, dir, f)
|
|
|
|
# dir(traceback)
|
|
try:
|
|
raise IndexError
|
|
except:
|
|
self.assertEqual(len(dir(sys.exc_info()[2])), 4)
|
|
|
|
|
|
def test_divmod(self):
|
|
self.assertEqual(divmod(12, 7), (1, 5))
|
|
self.assertEqual(divmod(-12, 7), (-2, 2))
|
|
self.assertEqual(divmod(12, -7), (-2, -2))
|
|
self.assertEqual(divmod(-12, -7), (1, -5))
|
|
|
|
self.assertEqual(divmod(12, 7), (1, 5))
|
|
self.assertEqual(divmod(-12, 7), (-2, 2))
|
|
self.assertEqual(divmod(12, -7), (-2, -2))
|
|
self.assertEqual(divmod(-12, -7), (1, -5))
|
|
|
|
self.assertEqual(divmod(12, 7), (1, 5))
|
|
self.assertEqual(divmod(-12, 7), (-2, 2))
|
|
self.assertEqual(divmod(12, -7), (-2, -2))
|
|
self.assertEqual(divmod(-12, -7), (1, -5))
|
|
|
|
self.assertEqual(divmod(-sys.maxsize-1, -1),
|
|
(sys.maxsize+1, 0))
|
|
|
|
self.assert_(not fcmp(divmod(3.25, 1.0), (3.0, 0.25)))
|
|
self.assert_(not fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)))
|
|
self.assert_(not fcmp(divmod(3.25, -1.0), (-4.0, -0.75)))
|
|
self.assert_(not fcmp(divmod(-3.25, -1.0), (3.0, -0.25)))
|
|
|
|
self.assertRaises(TypeError, divmod)
|
|
|
|
def test_eval(self):
|
|
self.assertEqual(eval('1+1'), 2)
|
|
self.assertEqual(eval(' 1+1\n'), 2)
|
|
globals = {'a': 1, 'b': 2}
|
|
locals = {'b': 200, 'c': 300}
|
|
self.assertEqual(eval('a', globals) , 1)
|
|
self.assertEqual(eval('a', globals, locals), 1)
|
|
self.assertEqual(eval('b', globals, locals), 200)
|
|
self.assertEqual(eval('c', globals, locals), 300)
|
|
globals = {'a': 1, 'b': 2}
|
|
locals = {'b': 200, 'c': 300}
|
|
bom = b'\xef\xbb\xbf'
|
|
self.assertEqual(eval(bom + b'a', globals, locals), 1)
|
|
self.assertEqual(eval('"\xe5"', globals), "\xe5")
|
|
self.assertRaises(TypeError, eval)
|
|
self.assertRaises(TypeError, eval, ())
|
|
self.assertRaises(SyntaxError, eval, bom[:2] + b'a')
|
|
|
|
def test_general_eval(self):
|
|
# Tests that general mappings can be used for the locals argument
|
|
|
|
class M:
|
|
"Test mapping interface versus possible calls from eval()."
|
|
def __getitem__(self, key):
|
|
if key == 'a':
|
|
return 12
|
|
raise KeyError
|
|
def keys(self):
|
|
return list('xyz')
|
|
|
|
m = M()
|
|
g = globals()
|
|
self.assertEqual(eval('a', g, m), 12)
|
|
self.assertRaises(NameError, eval, 'b', g, m)
|
|
self.assertEqual(eval('dir()', g, m), list('xyz'))
|
|
self.assertEqual(eval('globals()', g, m), g)
|
|
self.assertEqual(eval('locals()', g, m), m)
|
|
self.assertRaises(TypeError, eval, 'a', m)
|
|
class A:
|
|
"Non-mapping"
|
|
pass
|
|
m = A()
|
|
self.assertRaises(TypeError, eval, 'a', g, m)
|
|
|
|
# Verify that dict subclasses work as well
|
|
class D(dict):
|
|
def __getitem__(self, key):
|
|
if key == 'a':
|
|
return 12
|
|
return dict.__getitem__(self, key)
|
|
def keys(self):
|
|
return list('xyz')
|
|
|
|
d = D()
|
|
self.assertEqual(eval('a', g, d), 12)
|
|
self.assertRaises(NameError, eval, 'b', g, d)
|
|
self.assertEqual(eval('dir()', g, d), list('xyz'))
|
|
self.assertEqual(eval('globals()', g, d), g)
|
|
self.assertEqual(eval('locals()', g, d), d)
|
|
|
|
# Verify locals stores (used by list comps)
|
|
eval('[locals() for i in (2,3)]', g, d)
|
|
eval('[locals() for i in (2,3)]', g, collections.UserDict())
|
|
|
|
class SpreadSheet:
|
|
"Sample application showing nested, calculated lookups."
|
|
_cells = {}
|
|
def __setitem__(self, key, formula):
|
|
self._cells[key] = formula
|
|
def __getitem__(self, key):
|
|
return eval(self._cells[key], globals(), self)
|
|
|
|
ss = SpreadSheet()
|
|
ss['a1'] = '5'
|
|
ss['a2'] = 'a1*6'
|
|
ss['a3'] = 'a2*7'
|
|
self.assertEqual(ss['a3'], 210)
|
|
|
|
# Verify that dir() catches a non-list returned by eval
|
|
# SF bug #1004669
|
|
class C:
|
|
def __getitem__(self, item):
|
|
raise KeyError(item)
|
|
def keys(self):
|
|
return 1 # used to be 'a' but that's no longer an error
|
|
self.assertRaises(TypeError, eval, 'dir()', globals(), C())
|
|
|
|
def test_exec(self):
|
|
g = {}
|
|
exec('z = 1', g)
|
|
if '__builtins__' in g:
|
|
del g['__builtins__']
|
|
self.assertEqual(g, {'z': 1})
|
|
|
|
exec('z = 1+1', g)
|
|
if '__builtins__' in g:
|
|
del g['__builtins__']
|
|
self.assertEqual(g, {'z': 2})
|
|
g = {}
|
|
l = {}
|
|
|
|
import warnings
|
|
warnings.filterwarnings("ignore", "global statement", module="<string>")
|
|
exec('global a; a = 1; b = 2', g, l)
|
|
if '__builtins__' in g:
|
|
del g['__builtins__']
|
|
if '__builtins__' in l:
|
|
del l['__builtins__']
|
|
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
|
|
|
|
def test_exec_redirected(self):
|
|
savestdout = sys.stdout
|
|
sys.stdout = None # Whatever that cannot flush()
|
|
try:
|
|
# Used to raise SystemError('error return without exception set')
|
|
exec('a')
|
|
except NameError:
|
|
pass
|
|
finally:
|
|
sys.stdout = savestdout
|
|
|
|
def test_filter(self):
|
|
self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld'))
|
|
self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9])
|
|
self.assertEqual(list(filter(lambda x: x > 0, [1, -3, 9, 0, 2])), [1, 9, 2])
|
|
self.assertEqual(list(filter(None, Squares(10))), [1, 4, 9, 16, 25, 36, 49, 64, 81])
|
|
self.assertEqual(list(filter(lambda x: x%2, Squares(10))), [1, 9, 25, 49, 81])
|
|
def identity(item):
|
|
return 1
|
|
filter(identity, Squares(5))
|
|
self.assertRaises(TypeError, filter)
|
|
class BadSeq(object):
|
|
def __getitem__(self, index):
|
|
if index<4:
|
|
return 42
|
|
raise ValueError
|
|
self.assertRaises(ValueError, list, filter(lambda x: x, BadSeq()))
|
|
def badfunc():
|
|
pass
|
|
self.assertRaises(TypeError, list, filter(badfunc, range(5)))
|
|
|
|
# test bltinmodule.c::filtertuple()
|
|
self.assertEqual(list(filter(None, (1, 2))), [1, 2])
|
|
self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4])
|
|
self.assertRaises(TypeError, list, filter(42, (1, 2)))
|
|
|
|
def test_getattr(self):
|
|
import sys
|
|
self.assert_(getattr(sys, 'stdout') is sys.stdout)
|
|
self.assertRaises(TypeError, getattr, sys, 1)
|
|
self.assertRaises(TypeError, getattr, sys, 1, "foo")
|
|
self.assertRaises(TypeError, getattr)
|
|
self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode))
|
|
|
|
def test_hasattr(self):
|
|
import sys
|
|
self.assert_(hasattr(sys, 'stdout'))
|
|
self.assertRaises(TypeError, hasattr, sys, 1)
|
|
self.assertRaises(TypeError, hasattr)
|
|
self.assertEqual(False, hasattr(sys, chr(sys.maxunicode)))
|
|
|
|
def test_hash(self):
|
|
hash(None)
|
|
self.assertEqual(hash(1), hash(1))
|
|
self.assertEqual(hash(1), hash(1.0))
|
|
hash('spam')
|
|
self.assertEqual(hash('spam'), hash(b'spam'))
|
|
hash((0,1,2,3))
|
|
def f(): pass
|
|
self.assertRaises(TypeError, hash, [])
|
|
self.assertRaises(TypeError, hash, {})
|
|
# Bug 1536021: Allow hash to return long objects
|
|
class X:
|
|
def __hash__(self):
|
|
return 2**100
|
|
self.assertEquals(type(hash(X())), int)
|
|
class Y(object):
|
|
def __hash__(self):
|
|
return 2**100
|
|
self.assertEquals(type(hash(Y())), int)
|
|
class Z(int):
|
|
def __hash__(self):
|
|
return self
|
|
self.assertEquals(hash(Z(42)), hash(42))
|
|
|
|
def test_hex(self):
|
|
self.assertEqual(hex(16), '0x10')
|
|
self.assertEqual(hex(16), '0x10')
|
|
self.assertEqual(hex(-16), '-0x10')
|
|
self.assertEqual(hex(-16), '-0x10')
|
|
self.assertRaises(TypeError, hex, {})
|
|
|
|
def test_id(self):
|
|
id(None)
|
|
id(1)
|
|
id(1)
|
|
id(1.0)
|
|
id('spam')
|
|
id((0,1,2,3))
|
|
id([0,1,2,3])
|
|
id({'spam': 1, 'eggs': 2, 'ham': 3})
|
|
|
|
# Test input() later, alphabetized as if it were raw_input
|
|
|
|
def test_iter(self):
|
|
self.assertRaises(TypeError, iter)
|
|
self.assertRaises(TypeError, iter, 42, 42)
|
|
lists = [("1", "2"), ["1", "2"], "12"]
|
|
for l in lists:
|
|
i = iter(l)
|
|
self.assertEqual(next(i), '1')
|
|
self.assertEqual(next(i), '2')
|
|
self.assertRaises(StopIteration, next, i)
|
|
|
|
def test_isinstance(self):
|
|
class C:
|
|
pass
|
|
class D(C):
|
|
pass
|
|
class E:
|
|
pass
|
|
c = C()
|
|
d = D()
|
|
e = E()
|
|
self.assert_(isinstance(c, C))
|
|
self.assert_(isinstance(d, C))
|
|
self.assert_(not isinstance(e, C))
|
|
self.assert_(not isinstance(c, D))
|
|
self.assert_(not isinstance('foo', E))
|
|
self.assertRaises(TypeError, isinstance, E, 'foo')
|
|
self.assertRaises(TypeError, isinstance)
|
|
|
|
def test_issubclass(self):
|
|
class C:
|
|
pass
|
|
class D(C):
|
|
pass
|
|
class E:
|
|
pass
|
|
c = C()
|
|
d = D()
|
|
e = E()
|
|
self.assert_(issubclass(D, C))
|
|
self.assert_(issubclass(C, C))
|
|
self.assert_(not issubclass(C, D))
|
|
self.assertRaises(TypeError, issubclass, 'foo', E)
|
|
self.assertRaises(TypeError, issubclass, E, 'foo')
|
|
self.assertRaises(TypeError, issubclass)
|
|
|
|
def test_len(self):
|
|
self.assertEqual(len('123'), 3)
|
|
self.assertEqual(len(()), 0)
|
|
self.assertEqual(len((1, 2, 3, 4)), 4)
|
|
self.assertEqual(len([1, 2, 3, 4]), 4)
|
|
self.assertEqual(len({}), 0)
|
|
self.assertEqual(len({'a':1, 'b': 2}), 2)
|
|
class BadSeq:
|
|
def __len__(self):
|
|
raise ValueError
|
|
self.assertRaises(ValueError, len, BadSeq())
|
|
|
|
def test_map(self):
|
|
self.assertEqual(
|
|
list(map(lambda x: x*x, range(1,4))),
|
|
[1, 4, 9]
|
|
)
|
|
try:
|
|
from math import sqrt
|
|
except ImportError:
|
|
def sqrt(x):
|
|
return pow(x, 0.5)
|
|
self.assertEqual(
|
|
list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])),
|
|
[[4.0, 2.0], [9.0, 3.0]]
|
|
)
|
|
self.assertEqual(
|
|
list(map(lambda x, y: x+y, [1,3,2], [9,1,4])),
|
|
[10, 4, 6]
|
|
)
|
|
|
|
def plus(*v):
|
|
accu = 0
|
|
for i in v: accu = accu + i
|
|
return accu
|
|
self.assertEqual(
|
|
list(map(plus, [1, 3, 7])),
|
|
[1, 3, 7]
|
|
)
|
|
self.assertEqual(
|
|
list(map(plus, [1, 3, 7], [4, 9, 2])),
|
|
[1+4, 3+9, 7+2]
|
|
)
|
|
self.assertEqual(
|
|
list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])),
|
|
[1+4+1, 3+9+1, 7+2+0]
|
|
)
|
|
self.assertEqual(
|
|
list(map(int, Squares(10))),
|
|
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
|
)
|
|
def Max(a, b):
|
|
if a is None:
|
|
return b
|
|
if b is None:
|
|
return a
|
|
return max(a, b)
|
|
self.assertEqual(
|
|
list(map(Max, Squares(3), Squares(2))),
|
|
[0, 1]
|
|
)
|
|
self.assertRaises(TypeError, map)
|
|
self.assertRaises(TypeError, map, lambda x: x, 42)
|
|
class BadSeq:
|
|
def __iter__(self):
|
|
raise ValueError
|
|
yield None
|
|
self.assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
|
|
def badfunc(x):
|
|
raise RuntimeError
|
|
self.assertRaises(RuntimeError, list, map(badfunc, range(5)))
|
|
|
|
def test_max(self):
|
|
self.assertEqual(max('123123'), '3')
|
|
self.assertEqual(max(1, 2, 3), 3)
|
|
self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3)
|
|
self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3)
|
|
|
|
self.assertEqual(max(1, 2, 3.0), 3.0)
|
|
self.assertEqual(max(1, 2.0, 3), 3)
|
|
self.assertEqual(max(1.0, 2, 3), 3)
|
|
|
|
for stmt in (
|
|
"max(key=int)", # no args
|
|
"max(1, key=int)", # single arg not iterable
|
|
"max(1, 2, keystone=int)", # wrong keyword
|
|
"max(1, 2, key=int, abc=int)", # two many keywords
|
|
"max(1, 2, key=1)", # keyfunc is not callable
|
|
):
|
|
try:
|
|
exec(stmt, globals())
|
|
except TypeError:
|
|
pass
|
|
else:
|
|
self.fail(stmt)
|
|
|
|
self.assertEqual(max((1,), key=neg), 1) # one elem iterable
|
|
self.assertEqual(max((1,2), key=neg), 1) # two elem iterable
|
|
self.assertEqual(max(1, 2, key=neg), 1) # two elems
|
|
|
|
data = [random.randrange(200) for i in range(100)]
|
|
keys = dict((elem, random.randrange(50)) for elem in data)
|
|
f = keys.__getitem__
|
|
self.assertEqual(max(data, key=f),
|
|
sorted(reversed(data), key=f)[-1])
|
|
|
|
def test_min(self):
|
|
self.assertEqual(min('123123'), '1')
|
|
self.assertEqual(min(1, 2, 3), 1)
|
|
self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1)
|
|
self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1)
|
|
|
|
self.assertEqual(min(1, 2, 3.0), 1)
|
|
self.assertEqual(min(1, 2.0, 3), 1)
|
|
self.assertEqual(min(1.0, 2, 3), 1.0)
|
|
|
|
self.assertRaises(TypeError, min)
|
|
self.assertRaises(TypeError, min, 42)
|
|
self.assertRaises(ValueError, min, ())
|
|
class BadSeq:
|
|
def __getitem__(self, index):
|
|
raise ValueError
|
|
self.assertRaises(ValueError, min, BadSeq())
|
|
class BadNumber:
|
|
def __cmp__(self, other):
|
|
raise ValueError
|
|
self.assertRaises(TypeError, min, (42, BadNumber()))
|
|
|
|
for stmt in (
|
|
"min(key=int)", # no args
|
|
"min(1, key=int)", # single arg not iterable
|
|
"min(1, 2, keystone=int)", # wrong keyword
|
|
"min(1, 2, key=int, abc=int)", # two many keywords
|
|
"min(1, 2, key=1)", # keyfunc is not callable
|
|
):
|
|
try:
|
|
exec(stmt, globals())
|
|
except TypeError:
|
|
pass
|
|
else:
|
|
self.fail(stmt)
|
|
|
|
self.assertEqual(min((1,), key=neg), 1) # one elem iterable
|
|
self.assertEqual(min((1,2), key=neg), 2) # two elem iterable
|
|
self.assertEqual(min(1, 2, key=neg), 2) # two elems
|
|
|
|
data = [random.randrange(200) for i in range(100)]
|
|
keys = dict((elem, random.randrange(50)) for elem in data)
|
|
f = keys.__getitem__
|
|
self.assertEqual(min(data, key=f),
|
|
sorted(data, key=f)[0])
|
|
|
|
def test_next(self):
|
|
it = iter(range(2))
|
|
self.assertEqual(next(it), 0)
|
|
self.assertEqual(next(it), 1)
|
|
self.assertRaises(StopIteration, next, it)
|
|
self.assertRaises(StopIteration, next, it)
|
|
self.assertEquals(next(it, 42), 42)
|
|
|
|
class Iter(object):
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
raise StopIteration
|
|
|
|
it = iter(Iter())
|
|
self.assertEquals(next(it, 42), 42)
|
|
self.assertRaises(StopIteration, next, it)
|
|
|
|
def gen():
|
|
yield 1
|
|
return
|
|
|
|
it = gen()
|
|
self.assertEquals(next(it), 1)
|
|
self.assertRaises(StopIteration, next, it)
|
|
self.assertEquals(next(it, 42), 42)
|
|
|
|
def test_oct(self):
|
|
self.assertEqual(oct(100), '0o144')
|
|
self.assertEqual(oct(100), '0o144')
|
|
self.assertEqual(oct(-100), '-0o144')
|
|
self.assertEqual(oct(-100), '-0o144')
|
|
self.assertRaises(TypeError, oct, ())
|
|
|
|
def write_testfile(self):
|
|
# NB the first 4 lines are also used to test input, below
|
|
fp = open(TESTFN, 'w')
|
|
try:
|
|
fp.write('1+1\n')
|
|
fp.write('1+1\n')
|
|
fp.write('The quick brown fox jumps over the lazy dog')
|
|
fp.write('.\n')
|
|
fp.write('Dear John\n')
|
|
fp.write('XXX'*100)
|
|
fp.write('YYY'*100)
|
|
finally:
|
|
fp.close()
|
|
|
|
def test_open(self):
|
|
self.write_testfile()
|
|
fp = open(TESTFN, 'r')
|
|
try:
|
|
self.assertEqual(fp.readline(4), '1+1\n')
|
|
self.assertEqual(fp.readline(4), '1+1\n')
|
|
self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n')
|
|
self.assertEqual(fp.readline(4), 'Dear')
|
|
self.assertEqual(fp.readline(100), ' John\n')
|
|
self.assertEqual(fp.read(300), 'XXX'*100)
|
|
self.assertEqual(fp.read(1000), 'YYY'*100)
|
|
finally:
|
|
fp.close()
|
|
unlink(TESTFN)
|
|
|
|
def test_ord(self):
|
|
self.assertEqual(ord(' '), 32)
|
|
self.assertEqual(ord('A'), 65)
|
|
self.assertEqual(ord('a'), 97)
|
|
self.assertEqual(ord('\x80'), 128)
|
|
self.assertEqual(ord('\xff'), 255)
|
|
|
|
self.assertEqual(ord(b' '), 32)
|
|
self.assertEqual(ord(b'A'), 65)
|
|
self.assertEqual(ord(b'a'), 97)
|
|
self.assertEqual(ord(b'\x80'), 128)
|
|
self.assertEqual(ord(b'\xff'), 255)
|
|
|
|
self.assertEqual(ord(chr(sys.maxunicode)), sys.maxunicode)
|
|
self.assertRaises(TypeError, ord, 42)
|
|
|
|
self.assertEqual(ord(chr(0x10FFFF)), 0x10FFFF)
|
|
self.assertEqual(ord("\U0000FFFF"), 0x0000FFFF)
|
|
self.assertEqual(ord("\U00010000"), 0x00010000)
|
|
self.assertEqual(ord("\U00010001"), 0x00010001)
|
|
self.assertEqual(ord("\U000FFFFE"), 0x000FFFFE)
|
|
self.assertEqual(ord("\U000FFFFF"), 0x000FFFFF)
|
|
self.assertEqual(ord("\U00100000"), 0x00100000)
|
|
self.assertEqual(ord("\U00100001"), 0x00100001)
|
|
self.assertEqual(ord("\U0010FFFE"), 0x0010FFFE)
|
|
self.assertEqual(ord("\U0010FFFF"), 0x0010FFFF)
|
|
|
|
def test_pow(self):
|
|
self.assertEqual(pow(0,0), 1)
|
|
self.assertEqual(pow(0,1), 0)
|
|
self.assertEqual(pow(1,0), 1)
|
|
self.assertEqual(pow(1,1), 1)
|
|
|
|
self.assertEqual(pow(2,0), 1)
|
|
self.assertEqual(pow(2,10), 1024)
|
|
self.assertEqual(pow(2,20), 1024*1024)
|
|
self.assertEqual(pow(2,30), 1024*1024*1024)
|
|
|
|
self.assertEqual(pow(-2,0), 1)
|
|
self.assertEqual(pow(-2,1), -2)
|
|
self.assertEqual(pow(-2,2), 4)
|
|
self.assertEqual(pow(-2,3), -8)
|
|
|
|
self.assertEqual(pow(0,0), 1)
|
|
self.assertEqual(pow(0,1), 0)
|
|
self.assertEqual(pow(1,0), 1)
|
|
self.assertEqual(pow(1,1), 1)
|
|
|
|
self.assertEqual(pow(2,0), 1)
|
|
self.assertEqual(pow(2,10), 1024)
|
|
self.assertEqual(pow(2,20), 1024*1024)
|
|
self.assertEqual(pow(2,30), 1024*1024*1024)
|
|
|
|
self.assertEqual(pow(-2,0), 1)
|
|
self.assertEqual(pow(-2,1), -2)
|
|
self.assertEqual(pow(-2,2), 4)
|
|
self.assertEqual(pow(-2,3), -8)
|
|
|
|
self.assertAlmostEqual(pow(0.,0), 1.)
|
|
self.assertAlmostEqual(pow(0.,1), 0.)
|
|
self.assertAlmostEqual(pow(1.,0), 1.)
|
|
self.assertAlmostEqual(pow(1.,1), 1.)
|
|
|
|
self.assertAlmostEqual(pow(2.,0), 1.)
|
|
self.assertAlmostEqual(pow(2.,10), 1024.)
|
|
self.assertAlmostEqual(pow(2.,20), 1024.*1024.)
|
|
self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.)
|
|
|
|
self.assertAlmostEqual(pow(-2.,0), 1.)
|
|
self.assertAlmostEqual(pow(-2.,1), -2.)
|
|
self.assertAlmostEqual(pow(-2.,2), 4.)
|
|
self.assertAlmostEqual(pow(-2.,3), -8.)
|
|
|
|
for x in 2, 2, 2.0:
|
|
for y in 10, 10, 10.0:
|
|
for z in 1000, 1000, 1000.0:
|
|
if isinstance(x, float) or \
|
|
isinstance(y, float) or \
|
|
isinstance(z, float):
|
|
self.assertRaises(TypeError, pow, x, y, z)
|
|
else:
|
|
self.assertAlmostEqual(pow(x, y, z), 24.0)
|
|
|
|
self.assertAlmostEqual(pow(-1, 0.5), 1j)
|
|
self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j)
|
|
|
|
self.assertRaises(TypeError, pow, -1, -2, 3)
|
|
self.assertRaises(ValueError, pow, 1, 2, 0)
|
|
self.assertRaises(TypeError, pow, -1, -2, 3)
|
|
self.assertRaises(ValueError, pow, 1, 2, 0)
|
|
|
|
self.assertRaises(TypeError, pow)
|
|
|
|
def test_range(self):
|
|
self.assertEqual(list(range(3)), [0, 1, 2])
|
|
self.assertEqual(list(range(1, 5)), [1, 2, 3, 4])
|
|
self.assertEqual(list(range(0)), [])
|
|
self.assertEqual(list(range(-3)), [])
|
|
self.assertEqual(list(range(1, 10, 3)), [1, 4, 7])
|
|
#self.assertEqual(list(range(5, -5, -3)), [5, 2, -1, -4])
|
|
|
|
""" XXX(nnorwitz):
|
|
# Now test range() with longs
|
|
self.assertEqual(list(range(-2**100)), [])
|
|
self.assertEqual(list(range(0, -2**100)), [])
|
|
self.assertEqual(list(range(0, 2**100, -1)), [])
|
|
self.assertEqual(list(range(0, 2**100, -1)), [])
|
|
|
|
a = int(10 * sys.maxsize)
|
|
b = int(100 * sys.maxsize)
|
|
c = int(50 * sys.maxsize)
|
|
|
|
self.assertEqual(list(range(a, a+2)), [a, a+1])
|
|
self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1])
|
|
self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2])
|
|
|
|
seq = list(range(a, b, c))
|
|
self.assert_(a in seq)
|
|
self.assert_(b not in seq)
|
|
self.assertEqual(len(seq), 2)
|
|
|
|
seq = list(range(b, a, -c))
|
|
self.assert_(b in seq)
|
|
self.assert_(a not in seq)
|
|
self.assertEqual(len(seq), 2)
|
|
|
|
seq = list(range(-a, -b, -c))
|
|
self.assert_(-a in seq)
|
|
self.assert_(-b not in seq)
|
|
self.assertEqual(len(seq), 2)
|
|
|
|
self.assertRaises(TypeError, range)
|
|
self.assertRaises(TypeError, range, 1, 2, 3, 4)
|
|
self.assertRaises(ValueError, range, 1, 2, 0)
|
|
self.assertRaises(ValueError, range, a, a + 1, int(0))
|
|
|
|
class badzero(int):
|
|
def __eq__(self, other):
|
|
raise RuntimeError
|
|
__ne__ = __lt__ = __gt__ = __le__ = __ge__ = __eq__
|
|
|
|
# XXX This won't (but should!) raise RuntimeError if a is an int...
|
|
self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
|
|
"""
|
|
|
|
# Reject floats when it would require PyLongs to represent.
|
|
# (smaller floats still accepted, but deprecated)
|
|
self.assertRaises(TypeError, range, 1e100, 1e101, 1e101)
|
|
|
|
self.assertRaises(TypeError, range, 0, "spam")
|
|
self.assertRaises(TypeError, range, 0, 42, "spam")
|
|
|
|
#NEAL self.assertRaises(OverflowError, range, -sys.maxsize, sys.maxsize)
|
|
#NEAL self.assertRaises(OverflowError, range, 0, 2*sys.maxsize)
|
|
|
|
self.assertRaises(OverflowError, len, range(0, sys.maxsize**10))
|
|
|
|
def test_input(self):
|
|
self.write_testfile()
|
|
fp = open(TESTFN, 'r')
|
|
savestdin = sys.stdin
|
|
savestdout = sys.stdout # Eats the echo
|
|
try:
|
|
sys.stdin = fp
|
|
sys.stdout = BitBucket()
|
|
self.assertEqual(input(), "1+1")
|
|
self.assertEqual(input('testing\n'), "1+1")
|
|
self.assertEqual(input(), 'The quick brown fox jumps over the lazy dog.')
|
|
self.assertEqual(input('testing\n'), 'Dear John')
|
|
|
|
# SF 1535165: don't segfault on closed stdin
|
|
# sys.stdout must be a regular file for triggering
|
|
sys.stdout = savestdout
|
|
sys.stdin.close()
|
|
self.assertRaises(ValueError, input)
|
|
|
|
sys.stdout = BitBucket()
|
|
sys.stdin = io.StringIO("NULL\0")
|
|
self.assertRaises(TypeError, input, 42, 42)
|
|
sys.stdin = io.StringIO(" 'whitespace'")
|
|
self.assertEqual(input(), " 'whitespace'")
|
|
sys.stdin = io.StringIO()
|
|
self.assertRaises(EOFError, input)
|
|
|
|
del sys.stdout
|
|
self.assertRaises(RuntimeError, input, 'prompt')
|
|
del sys.stdin
|
|
self.assertRaises(RuntimeError, input, 'prompt')
|
|
finally:
|
|
sys.stdin = savestdin
|
|
sys.stdout = savestdout
|
|
fp.close()
|
|
unlink(TESTFN)
|
|
|
|
def test_repr(self):
|
|
self.assertEqual(repr(''), '\'\'')
|
|
self.assertEqual(repr(0), '0')
|
|
self.assertEqual(repr(0), '0')
|
|
self.assertEqual(repr(()), '()')
|
|
self.assertEqual(repr([]), '[]')
|
|
self.assertEqual(repr({}), '{}')
|
|
a = []
|
|
a.append(a)
|
|
self.assertEqual(repr(a), '[[...]]')
|
|
a = {}
|
|
a[0] = a
|
|
self.assertEqual(repr(a), '{0: {...}}')
|
|
|
|
def test_round(self):
|
|
self.assertEqual(round(0.0), 0.0)
|
|
self.assertEqual(type(round(0.0)), int)
|
|
self.assertEqual(round(1.0), 1.0)
|
|
self.assertEqual(round(10.0), 10.0)
|
|
self.assertEqual(round(1000000000.0), 1000000000.0)
|
|
self.assertEqual(round(1e20), 1e20)
|
|
|
|
self.assertEqual(round(-1.0), -1.0)
|
|
self.assertEqual(round(-10.0), -10.0)
|
|
self.assertEqual(round(-1000000000.0), -1000000000.0)
|
|
self.assertEqual(round(-1e20), -1e20)
|
|
|
|
self.assertEqual(round(0.1), 0.0)
|
|
self.assertEqual(round(1.1), 1.0)
|
|
self.assertEqual(round(10.1), 10.0)
|
|
self.assertEqual(round(1000000000.1), 1000000000.0)
|
|
|
|
self.assertEqual(round(-1.1), -1.0)
|
|
self.assertEqual(round(-10.1), -10.0)
|
|
self.assertEqual(round(-1000000000.1), -1000000000.0)
|
|
|
|
self.assertEqual(round(0.9), 1.0)
|
|
self.assertEqual(round(9.9), 10.0)
|
|
self.assertEqual(round(999999999.9), 1000000000.0)
|
|
|
|
self.assertEqual(round(-0.9), -1.0)
|
|
self.assertEqual(round(-9.9), -10.0)
|
|
self.assertEqual(round(-999999999.9), -1000000000.0)
|
|
|
|
self.assertEqual(round(-8.0, -1), -10.0)
|
|
self.assertEqual(type(round(-8.0, -1)), float)
|
|
|
|
self.assertEqual(type(round(-8.0, 0)), float)
|
|
self.assertEqual(type(round(-8.0, 1)), float)
|
|
|
|
# Check even / odd rounding behaviour
|
|
self.assertEqual(round(5.5), 6)
|
|
self.assertEqual(round(6.5), 6)
|
|
self.assertEqual(round(-5.5), -6)
|
|
self.assertEqual(round(-6.5), -6)
|
|
|
|
# Check behavior on ints
|
|
self.assertEqual(round(0), 0)
|
|
self.assertEqual(round(8), 8)
|
|
self.assertEqual(round(-8), -8)
|
|
self.assertEqual(type(round(0)), int)
|
|
self.assertEqual(type(round(-8, -1)), float)
|
|
self.assertEqual(type(round(-8, 0)), float)
|
|
self.assertEqual(type(round(-8, 1)), float)
|
|
|
|
# test new kwargs
|
|
self.assertEqual(round(number=-8.0, ndigits=-1), -10.0)
|
|
|
|
self.assertRaises(TypeError, round)
|
|
|
|
# test generic rounding delegation for reals
|
|
class TestRound:
|
|
def __round__(self):
|
|
return 23
|
|
|
|
class TestNoRound:
|
|
pass
|
|
|
|
self.assertEqual(round(TestRound()), 23)
|
|
|
|
self.assertRaises(TypeError, round, 1, 2, 3)
|
|
self.assertRaises(TypeError, round, TestNoRound())
|
|
|
|
t = TestNoRound()
|
|
t.__round__ = lambda *args: args
|
|
self.assertRaises(TypeError, round, t)
|
|
self.assertRaises(TypeError, round, t, 0)
|
|
|
|
def test_setattr(self):
|
|
setattr(sys, 'spam', 1)
|
|
self.assertEqual(sys.spam, 1)
|
|
self.assertRaises(TypeError, setattr, sys, 1, 'spam')
|
|
self.assertRaises(TypeError, setattr)
|
|
|
|
|
|
def test_sum(self):
|
|
self.assertEqual(sum([]), 0)
|
|
self.assertEqual(sum(list(range(2,8))), 27)
|
|
self.assertEqual(sum(iter(list(range(2,8)))), 27)
|
|
self.assertEqual(sum(Squares(10)), 285)
|
|
self.assertEqual(sum(iter(Squares(10))), 285)
|
|
self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3])
|
|
|
|
self.assertRaises(TypeError, sum)
|
|
self.assertRaises(TypeError, sum, 42)
|
|
self.assertRaises(TypeError, sum, ['a', 'b', 'c'])
|
|
self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '')
|
|
self.assertRaises(TypeError, sum, [[1], [2], [3]])
|
|
self.assertRaises(TypeError, sum, [{2:3}])
|
|
self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3})
|
|
|
|
class BadSeq:
|
|
def __getitem__(self, index):
|
|
raise ValueError
|
|
self.assertRaises(ValueError, sum, BadSeq())
|
|
|
|
def test_type(self):
|
|
self.assertEqual(type(''), type('123'))
|
|
self.assertNotEqual(type(''), type(()))
|
|
|
|
# We don't want self in vars(), so these are static methods
|
|
|
|
@staticmethod
|
|
def get_vars_f0():
|
|
return vars()
|
|
|
|
@staticmethod
|
|
def get_vars_f2():
|
|
BuiltinTest.get_vars_f0()
|
|
a = 1
|
|
b = 2
|
|
return vars()
|
|
|
|
def test_vars(self):
|
|
self.assertEqual(set(vars()), set(dir()))
|
|
import sys
|
|
self.assertEqual(set(vars(sys)), set(dir(sys)))
|
|
self.assertEqual(self.get_vars_f0(), {})
|
|
self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
|
|
self.assertRaises(TypeError, vars, 42, 42)
|
|
self.assertRaises(TypeError, vars, 42)
|
|
|
|
def test_zip(self):
|
|
a = (1, 2, 3)
|
|
b = (4, 5, 6)
|
|
t = [(1, 4), (2, 5), (3, 6)]
|
|
self.assertEqual(list(zip(a, b)), t)
|
|
b = [4, 5, 6]
|
|
self.assertEqual(list(zip(a, b)), t)
|
|
b = (4, 5, 6, 7)
|
|
self.assertEqual(list(zip(a, b)), t)
|
|
class I:
|
|
def __getitem__(self, i):
|
|
if i < 0 or i > 2: raise IndexError
|
|
return i + 4
|
|
self.assertEqual(list(zip(a, I())), t)
|
|
self.assertEqual(list(zip()), [])
|
|
self.assertEqual(list(zip(*[])), [])
|
|
self.assertRaises(TypeError, zip, None)
|
|
class G:
|
|
pass
|
|
self.assertRaises(TypeError, zip, a, G())
|
|
|
|
# Make sure zip doesn't try to allocate a billion elements for the
|
|
# result list when one of its arguments doesn't say how long it is.
|
|
# A MemoryError is the most likely failure mode.
|
|
class SequenceWithoutALength:
|
|
def __getitem__(self, i):
|
|
if i == 5:
|
|
raise IndexError
|
|
else:
|
|
return i
|
|
self.assertEqual(
|
|
list(zip(SequenceWithoutALength(), range(2**30))),
|
|
list(enumerate(range(5)))
|
|
)
|
|
|
|
class BadSeq:
|
|
def __getitem__(self, i):
|
|
if i == 5:
|
|
raise ValueError
|
|
else:
|
|
return i
|
|
self.assertRaises(ValueError, list, zip(BadSeq(), BadSeq()))
|
|
|
|
def test_bin(self):
|
|
self.assertEqual(bin(0), '0b0')
|
|
self.assertEqual(bin(1), '0b1')
|
|
self.assertEqual(bin(-1), '-0b1')
|
|
self.assertEqual(bin(2**65), '0b1' + '0' * 65)
|
|
self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
|
|
self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
|
|
self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
|
|
|
|
class TestSorted(unittest.TestCase):
|
|
|
|
def test_basic(self):
|
|
data = list(range(100))
|
|
copy = data[:]
|
|
random.shuffle(copy)
|
|
self.assertEqual(data, sorted(copy))
|
|
self.assertNotEqual(data, copy)
|
|
|
|
data.reverse()
|
|
random.shuffle(copy)
|
|
self.assertEqual(data, sorted(copy, key=lambda x: -x))
|
|
self.assertNotEqual(data, copy)
|
|
random.shuffle(copy)
|
|
self.assertEqual(data, sorted(copy, reverse=1))
|
|
self.assertNotEqual(data, copy)
|
|
|
|
def test_inputtypes(self):
|
|
s = 'abracadabra'
|
|
types = [list, tuple, str]
|
|
for T in types:
|
|
self.assertEqual(sorted(s), sorted(T(s)))
|
|
|
|
s = ''.join(set(s)) # unique letters only
|
|
types = [str, set, frozenset, list, tuple, dict.fromkeys]
|
|
for T in types:
|
|
self.assertEqual(sorted(s), sorted(T(s)))
|
|
|
|
def test_baddecorator(self):
|
|
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
|
|
self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
|
|
|
|
def test_main(verbose=None):
|
|
test_classes = (BuiltinTest, TestSorted)
|
|
|
|
run_unittest(*test_classes)
|
|
|
|
# verify reference counting
|
|
if verbose and hasattr(sys, "gettotalrefcount"):
|
|
import gc
|
|
counts = [None] * 5
|
|
for i in range(len(counts)):
|
|
run_unittest(*test_classes)
|
|
gc.collect()
|
|
counts[i] = sys.gettotalrefcount()
|
|
print(counts)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_main(verbose=True)
|