mirror of
https://github.com/python/cpython.git
synced 2025-10-24 18:33:49 +00:00
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r55837 | guido.van.rossum | 2007-06-08 16:04:42 -0700 (Fri, 08 Jun 2007) | 2 lines
PEP 3119 -- the abc module.
................
r55838 | guido.van.rossum | 2007-06-08 17:38:55 -0700 (Fri, 08 Jun 2007) | 2 lines
Implement part of PEP 3119 -- One Trick Ponies.
................
r55847 | guido.van.rossum | 2007-06-09 08:28:06 -0700 (Sat, 09 Jun 2007) | 2 lines
Different way to do one trick ponies, allowing registration (per PEP strawman).
................
r55849 | guido.van.rossum | 2007-06-09 18:06:38 -0700 (Sat, 09 Jun 2007) | 3 lines
Make sure that the magic looking for __hash__ (etc.) doesn't apply to
real subclasses of Hashable.
................
r55852 | guido.van.rossum | 2007-06-10 08:29:51 -0700 (Sun, 10 Jun 2007) | 2 lines
Add some more examples, e.g. generators and dict views.
................
r55853 | guido.van.rossum | 2007-06-10 08:31:59 -0700 (Sun, 10 Jun 2007) | 2 lines
keys() and items() *are* containers -- just values() isn't.
................
r55864 | georg.brandl | 2007-06-10 15:29:40 -0700 (Sun, 10 Jun 2007) | 2 lines
PEP 3127: new octal literals, binary literals.
................
r55865 | georg.brandl | 2007-06-10 15:31:37 -0700 (Sun, 10 Jun 2007) | 2 lines
Some octal literal fixes in Tools.
................
r55866 | georg.brandl | 2007-06-10 15:37:43 -0700 (Sun, 10 Jun 2007) | 2 lines
Tokenizer changes for PEP 3127.
................
r55867 | georg.brandl | 2007-06-10 15:37:55 -0700 (Sun, 10 Jun 2007) | 2 lines
Some docs for PEP 3127.
................
r55868 | georg.brandl | 2007-06-10 15:44:39 -0700 (Sun, 10 Jun 2007) | 2 lines
Missed a place in intobject.c. Is that used anymore anyway?
................
r55871 | neal.norwitz | 2007-06-10 18:31:49 -0700 (Sun, 10 Jun 2007) | 182 lines
Merged revisions 55729-55868 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55731 | neal.norwitz | 2007-06-01 00:29:12 -0700 (Fri, 01 Jun 2007) | 7 lines
SF 1668596/1720897: distutils now copies data files
even if package_dir is empty.
This needs to be backported. I'm too tired tonight. It would be great
if someone backports this if the buildbots are ok with it. Otherwise,
I will try to get to it tomorrow.
........
r55732 | georg.brandl | 2007-06-01 04:33:33 -0700 (Fri, 01 Jun 2007) | 2 lines
Bug #1722484: remove docstrings again when running with -OO.
........
r55735 | georg.brandl | 2007-06-01 12:20:27 -0700 (Fri, 01 Jun 2007) | 2 lines
Fix wrong issue number.
........
r55739 | brett.cannon | 2007-06-01 20:02:29 -0700 (Fri, 01 Jun 2007) | 3 lines
Have configure raise an error when building on AtheOS. Code specific to AtheOS
will be removed in Python 2.7.
........
r55746 | neal.norwitz | 2007-06-02 11:33:53 -0700 (Sat, 02 Jun 2007) | 1 line
Update expected birthday of 2.6
........
r55751 | neal.norwitz | 2007-06-03 13:32:50 -0700 (Sun, 03 Jun 2007) | 10 lines
Backout the original 'fix' to 1721309 which had no effect.
Different versions of Berkeley DB handle this differently.
The comments and bug report should have the details. Memory is allocated
in 4.4 (and presumably earlier), but not in 4.5. Thus
4.5 has the free error, but not earlier versions.
Mostly update comments, plus make the free conditional.
This fix was already applied to the 2.5 branch.
........
r55752 | brett.cannon | 2007-06-03 16:13:41 -0700 (Sun, 03 Jun 2007) | 6 lines
Make _strptime.TimeRE().pattern() use ``\s+`` for matching whitespace instead
of ``\s*``. This prevents patterns from "stealing" bits from other patterns in
order to make a match work.
Closes bug #1730389. Will be backported.
........
r55766 | hyeshik.chang | 2007-06-05 11:16:52 -0700 (Tue, 05 Jun 2007) | 4 lines
Fix build on FreeBSD. Bluetooth HCI API in FreeBSD is quite different
from Linux's. Just fix the build for now but the code doesn't
support the complete capability of HCI on FreeBSD yet.
........
r55770 | hyeshik.chang | 2007-06-05 11:58:51 -0700 (Tue, 05 Jun 2007) | 4 lines
Bug #1728403: Fix a bug that CJKCodecs StreamReader hangs when it
reads a file that ends with incomplete sequence and sizehint argument
for .read() is specified.
........
r55775 | hyeshik.chang | 2007-06-05 12:28:15 -0700 (Tue, 05 Jun 2007) | 2 lines
Fix for Windows: close a temporary file before trying to delete it.
........
r55783 | guido.van.rossum | 2007-06-05 14:24:47 -0700 (Tue, 05 Jun 2007) | 2 lines
Patch by Tim Delany (missing DECREF). SF #1731330.
........
r55785 | collin.winter | 2007-06-05 17:17:35 -0700 (Tue, 05 Jun 2007) | 3 lines
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
........
r55786 | facundo.batista | 2007-06-06 08:13:37 -0700 (Wed, 06 Jun 2007) | 4 lines
FTP.ntransfercmd method now uses create_connection when passive,
using the timeout received in connection time.
........
r55792 | facundo.batista | 2007-06-06 10:15:23 -0700 (Wed, 06 Jun 2007) | 7 lines
Added an optional timeout parameter to function urllib2.urlopen,
with tests in test_urllib2net.py (must have network resource
enabled to execute them). Also modified test_urllib2.py because
testing mock classes must take it into acount. Docs are also
updated.
........
r55793 | thomas.heller | 2007-06-06 13:19:19 -0700 (Wed, 06 Jun 2007) | 1 line
Build _ctypes and _ctypes_test in the ReleaseAMD64 configuration.
........
r55802 | georg.brandl | 2007-06-07 06:23:24 -0700 (Thu, 07 Jun 2007) | 3 lines
Disallow function calls like foo(None=1).
Backport from py3k rev. 55708 by Guido.
........
r55804 | georg.brandl | 2007-06-07 06:30:24 -0700 (Thu, 07 Jun 2007) | 2 lines
Make reindent.py executable.
........
r55805 | georg.brandl | 2007-06-07 06:34:10 -0700 (Thu, 07 Jun 2007) | 2 lines
Patch #1667860: Fix UnboundLocalError in urllib2.
........
r55821 | kristjan.jonsson | 2007-06-07 16:53:49 -0700 (Thu, 07 Jun 2007) | 1 line
Fixing changes to getbuildinfo.c that broke linux builds
........
r55828 | thomas.heller | 2007-06-08 09:10:27 -0700 (Fri, 08 Jun 2007) | 1 line
Make this test work with older Python releases where struct has no 't' format character.
........
r55829 | martin.v.loewis | 2007-06-08 10:29:20 -0700 (Fri, 08 Jun 2007) | 3 lines
Bug #1733488: Fix compilation of bufferobject.c on AIX.
Will backport to 2.5.
........
r55831 | thomas.heller | 2007-06-08 11:20:09 -0700 (Fri, 08 Jun 2007) | 2 lines
[ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
with small modifications.
........
r55832 | thomas.heller | 2007-06-08 12:01:06 -0700 (Fri, 08 Jun 2007) | 1 line
Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls.
........
r55833 | thomas.heller | 2007-06-08 12:08:31 -0700 (Fri, 08 Jun 2007) | 2 lines
Fix wrong documentation, and correct the punktuation.
Closes [1700455].
........
r55834 | thomas.heller | 2007-06-08 12:14:23 -0700 (Fri, 08 Jun 2007) | 1 line
Fix warnings by using proper function prototype.
........
r55839 | neal.norwitz | 2007-06-08 20:36:34 -0700 (Fri, 08 Jun 2007) | 7 lines
Prevent expandtabs() on string and unicode objects from causing a segfault when
a large width is passed on 32-bit platforms. Found by Google.
It would be good for people to review this especially carefully and verify
I don't have an off by one error and there is no other way to cause overflow.
........
r55841 | neal.norwitz | 2007-06-08 21:48:22 -0700 (Fri, 08 Jun 2007) | 1 line
Use macro version of GET_SIZE to avoid Coverity warning (#150) about a possible error.
........
r55842 | martin.v.loewis | 2007-06-09 00:42:52 -0700 (Sat, 09 Jun 2007) | 3 lines
Patch #1733960: Allow T_LONGLONG to accept ints.
Will backport to 2.5.
........
r55843 | martin.v.loewis | 2007-06-09 00:58:05 -0700 (Sat, 09 Jun 2007) | 2 lines
Fix Windows build.
........
r55845 | martin.v.loewis | 2007-06-09 03:10:26 -0700 (Sat, 09 Jun 2007) | 2 lines
Provide LLONG_MAX for S390.
........
r55854 | thomas.heller | 2007-06-10 08:59:17 -0700 (Sun, 10 Jun 2007) | 4 lines
First version of build scripts for Windows/AMD64 (no external
components are built yet, and 'kill_python' is disabled).
........
r55855 | thomas.heller | 2007-06-10 10:55:51 -0700 (Sun, 10 Jun 2007) | 3 lines
For now, disable the _bsddb, _sqlite3, _ssl, _testcapi, _tkinter
modules in the ReleaseAMD64 configuration because they do not compile.
........
r55856 | thomas.heller | 2007-06-10 11:27:54 -0700 (Sun, 10 Jun 2007) | 1 line
Need to set the environment variables, otherwise devenv.com is not found.
........
r55860 | thomas.heller | 2007-06-10 14:01:17 -0700 (Sun, 10 Jun 2007) | 1 line
Revert commit 55855.
........
................
r55880 | neal.norwitz | 2007-06-10 22:07:36 -0700 (Sun, 10 Jun 2007) | 5 lines
Fix the refleak counter on test_collections. The ABC metaclass creates
a registry which must be cleared on each run. Otherwise, there *seem*
to be refleaks when there really aren't any. (The class is held within
the registry even though it's no longer needed.)
................
r55884 | neal.norwitz | 2007-06-10 22:46:33 -0700 (Sun, 10 Jun 2007) | 1 line
These tests have been removed, so they are no longer needed here
................
r55886 | georg.brandl | 2007-06-11 00:26:37 -0700 (Mon, 11 Jun 2007) | 3 lines
Optimize access to True and False in the compiler (if True)
and the peepholer (LOAD_NAME True).
................
r55905 | georg.brandl | 2007-06-11 10:02:26 -0700 (Mon, 11 Jun 2007) | 5 lines
Remove __oct__ and __hex__ and use __index__ for converting
non-ints before formatting in a base.
Add a bin() builtin.
................
r55906 | georg.brandl | 2007-06-11 10:04:44 -0700 (Mon, 11 Jun 2007) | 2 lines
int(x, 0) does not "guess".
................
r55907 | georg.brandl | 2007-06-11 10:05:47 -0700 (Mon, 11 Jun 2007) | 2 lines
Add a comment to explain that nb_oct and nb_hex are nonfunctional.
................
r55908 | guido.van.rossum | 2007-06-11 10:49:18 -0700 (Mon, 11 Jun 2007) | 2 lines
Get rid of unused imports and comment.
................
r55910 | guido.van.rossum | 2007-06-11 13:05:17 -0700 (Mon, 11 Jun 2007) | 2 lines
_Abstract.__new__ now requires either no arguments or __init__ overridden.
................
r55911 | guido.van.rossum | 2007-06-11 13:07:49 -0700 (Mon, 11 Jun 2007) | 7 lines
Move the collections ABCs to a separate file, _abcoll.py, in order to avoid
needing to import _collections.so during the bootstrap (this will become
apparent in the next submit of os.py).
Add (plain and mutable) ABCs for Set, Mapping, Sequence.
................
r55912 | guido.van.rossum | 2007-06-11 13:09:31 -0700 (Mon, 11 Jun 2007) | 2 lines
Rewrite the _Environ class to use the new collections ABCs.
................
r55913 | guido.van.rossum | 2007-06-11 13:59:45 -0700 (Mon, 11 Jun 2007) | 72 lines
Merged revisions 55869-55912 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55869 | neal.norwitz | 2007-06-10 17:42:11 -0700 (Sun, 10 Jun 2007) | 1 line
Add Atul Varma for patch # 1667860
........
r55870 | neal.norwitz | 2007-06-10 18:22:03 -0700 (Sun, 10 Jun 2007) | 1 line
Ignore valgrind problems on Ubuntu from ld
........
r55872 | neal.norwitz | 2007-06-10 18:48:46 -0700 (Sun, 10 Jun 2007) | 2 lines
Ignore config.status.lineno which seems new (new autoconf?)
........
r55873 | neal.norwitz | 2007-06-10 19:14:39 -0700 (Sun, 10 Jun 2007) | 1 line
Prevent these tests from running on Win64 since they don\'t apply there either
........
r55874 | neal.norwitz | 2007-06-10 19:16:10 -0700 (Sun, 10 Jun 2007) | 5 lines
Fix a bug when there was a newline in the string expandtabs was called on.
This also catches another condition that can overflow.
Will backport.
........
r55879 | neal.norwitz | 2007-06-10 21:52:37 -0700 (Sun, 10 Jun 2007) | 1 line
Prevent hang if the port cannot be opened.
........
r55881 | neal.norwitz | 2007-06-10 22:28:45 -0700 (Sun, 10 Jun 2007) | 4 lines
Add all of the distuils modules that don't seem to have explicit tests. :-(
Move an import in mworkscompiler so that this module can be imported on
any platform. Hopefully this works on all platforms.
........
r55882 | neal.norwitz | 2007-06-10 22:35:10 -0700 (Sun, 10 Jun 2007) | 4 lines
SF #1734732, lower case the module names per PEP 8.
Will backport.
........
r55885 | neal.norwitz | 2007-06-10 23:16:48 -0700 (Sun, 10 Jun 2007) | 4 lines
Not sure why this only fails sometimes on Unix machines. Better
to disable it and only import msvccompiler on Windows since that's
the only place it can work anyways.
........
r55887 | neal.norwitz | 2007-06-11 00:29:43 -0700 (Mon, 11 Jun 2007) | 4 lines
Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Will backport
........
r55889 | neal.norwitz | 2007-06-11 00:36:24 -0700 (Mon, 11 Jun 2007) | 1 line
Reflow long line
........
r55896 | thomas.heller | 2007-06-11 08:58:33 -0700 (Mon, 11 Jun 2007) | 3 lines
Use "O&" in calls to PyArg_Parse when we need a 'void*' instead of "k"
or "K" codes.
........
r55901 | facundo.batista | 2007-06-11 09:27:08 -0700 (Mon, 11 Jun 2007) | 5 lines
Added versionchanged flag to all the methods which received
a new optional timeout parameter, and a versionadded flag to
the socket.create_connection function.
........
................
r55914 | guido.van.rossum | 2007-06-11 14:19:50 -0700 (Mon, 11 Jun 2007) | 3 lines
New super() implementation, for PEP 3135 (though the PEP is not yet updated
to this design, and small tweaks may still be made later).
................
r55923 | guido.van.rossum | 2007-06-11 21:15:24 -0700 (Mon, 11 Jun 2007) | 4 lines
I'm guessing this module broke when Neal ripped out the types module --
it used 'list' both as a local variable and as the built-in list type.
Renamed the local variable since that was easier.
................
r55924 | guido.van.rossum | 2007-06-11 21:20:05 -0700 (Mon, 11 Jun 2007) | 5 lines
Change all occurrences of super(<thisclass>, <firstarg>) to super().
Seems to have worked, all the tests still pass.
Exception: test_descr and test_descrtut, which have tons of these
and are there to test the various usages.
................
r55939 | collin.winter | 2007-06-12 13:57:33 -0700 (Tue, 12 Jun 2007) | 1 line
Patch #1735485: remove StandardError from the exception hierarchy.
................
r55954 | neal.norwitz | 2007-06-12 21:56:32 -0700 (Tue, 12 Jun 2007) | 51 lines
Merged revisions 55913-55950 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55926 | marc-andre.lemburg | 2007-06-12 02:09:58 -0700 (Tue, 12 Jun 2007) | 3 lines
Apply patch #1734945 to support TurboLinux as distribution.
........
r55927 | marc-andre.lemburg | 2007-06-12 02:26:49 -0700 (Tue, 12 Jun 2007) | 3 lines
Add patch #1726668: Windows Vista support.
........
r55929 | thomas.heller | 2007-06-12 08:36:22 -0700 (Tue, 12 Jun 2007) | 1 line
Checkout, but do not yet try to build, exernal sources.
........
r55930 | thomas.heller | 2007-06-12 09:08:27 -0700 (Tue, 12 Jun 2007) | 6 lines
Add bufferoverflowU.lib to the libraries needed by _ssl (is this the
right thing to do?).
Set the /XP64 /RETAIL build enviroment in the makefile when building
ReleaseAMD64.
........
r55931 | thomas.heller | 2007-06-12 09:23:19 -0700 (Tue, 12 Jun 2007) | 5 lines
Revert this change, since it breaks the win32 build:
Add bufferoverflowU.lib to the libraries needed by _ssl (is this the
right thing to do?).
........
r55934 | thomas.heller | 2007-06-12 10:28:31 -0700 (Tue, 12 Jun 2007) | 3 lines
Specify the bufferoverflowU.lib to the makefile on the command line
(for ReleaseAMD64 builds).
........
r55937 | thomas.heller | 2007-06-12 12:02:59 -0700 (Tue, 12 Jun 2007) | 3 lines
Add bufferoverflowU.lib to PCBuild\_bsddb.vcproj.
Build sqlite3.dll and bsddb.
........
r55938 | thomas.heller | 2007-06-12 12:56:12 -0700 (Tue, 12 Jun 2007) | 2 lines
Don't rebuild Berkeley DB if not needed (this was committed by accident).
........
r55948 | martin.v.loewis | 2007-06-12 20:42:19 -0700 (Tue, 12 Jun 2007) | 3 lines
Provide PY_LLONG_MAX on all systems having long long.
Will backport to 2.5.
........
................
r55959 | guido.van.rossum | 2007-06-13 09:22:41 -0700 (Wed, 13 Jun 2007) | 2 lines
Fix a compilation warning.
................
801 lines
33 KiB
Python
801 lines
33 KiB
Python
# -*- coding: iso-8859-1 -*-
|
|
""" Test script for the Unicode implementation.
|
|
|
|
Written by Marc-Andre Lemburg (mal@lemburg.com).
|
|
|
|
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
|
|
|
|
"""#"
|
|
import unittest, sys, struct, codecs, new
|
|
from test import test_support, string_tests
|
|
|
|
# Error handling (bad decoder return)
|
|
def search_function(encoding):
|
|
def decode1(input, errors="strict"):
|
|
return 42 # not a tuple
|
|
def encode1(input, errors="strict"):
|
|
return 42 # not a tuple
|
|
def encode2(input, errors="strict"):
|
|
return (42, 42) # no unicode
|
|
def decode2(input, errors="strict"):
|
|
return (42, 42) # no unicode
|
|
if encoding=="test.unicode1":
|
|
return (encode1, decode1, None, None)
|
|
elif encoding=="test.unicode2":
|
|
return (encode2, decode2, None, None)
|
|
else:
|
|
return None
|
|
codecs.register(search_function)
|
|
|
|
class UnicodeTest(
|
|
string_tests.CommonTest,
|
|
string_tests.MixinStrUnicodeUserStringTest,
|
|
string_tests.MixinStrUnicodeTest,
|
|
):
|
|
type2test = str
|
|
|
|
def checkequalnofix(self, result, object, methodname, *args):
|
|
method = getattr(object, methodname)
|
|
realresult = method(*args)
|
|
self.assertEqual(realresult, result)
|
|
self.assert_(type(realresult) is type(result))
|
|
|
|
# if the original is returned make sure that
|
|
# this doesn't happen with subclasses
|
|
if realresult is object:
|
|
class usub(str):
|
|
def __repr__(self):
|
|
return 'usub(%r)' % str.__repr__(self)
|
|
object = usub(object)
|
|
method = getattr(object, methodname)
|
|
realresult = method(*args)
|
|
self.assertEqual(realresult, result)
|
|
self.assert_(object is not realresult)
|
|
|
|
def test_literals(self):
|
|
self.assertEqual('\xff', '\u00ff')
|
|
self.assertEqual('\uffff', '\U0000ffff')
|
|
self.assertRaises(UnicodeError, eval, '\'\\Ufffffffe\'')
|
|
self.assertRaises(UnicodeError, eval, '\'\\Uffffffff\'')
|
|
self.assertRaises(UnicodeError, eval, '\'\\U%08x\'' % 0x110000)
|
|
|
|
def test_repr(self):
|
|
if not sys.platform.startswith('java'):
|
|
# Test basic sanity of repr()
|
|
self.assertEqual(repr('abc'), "'abc'")
|
|
self.assertEqual(repr('ab\\c'), "'ab\\\\c'")
|
|
self.assertEqual(repr('ab\\'), "'ab\\\\'")
|
|
self.assertEqual(repr('\\c'), "'\\\\c'")
|
|
self.assertEqual(repr('\\'), "'\\\\'")
|
|
self.assertEqual(repr('\n'), "'\\n'")
|
|
self.assertEqual(repr('\r'), "'\\r'")
|
|
self.assertEqual(repr('\t'), "'\\t'")
|
|
self.assertEqual(repr('\b'), "'\\x08'")
|
|
self.assertEqual(repr("'\""), """'\\'"'""")
|
|
self.assertEqual(repr("'\""), """'\\'"'""")
|
|
self.assertEqual(repr("'"), '''"'"''')
|
|
self.assertEqual(repr('"'), """'"'""")
|
|
latin1repr = (
|
|
"'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
|
|
"\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
|
|
"\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
|
|
"JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
|
|
"\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
|
|
"\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
|
|
"\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9"
|
|
"\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7"
|
|
"\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5"
|
|
"\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3"
|
|
"\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1"
|
|
"\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef"
|
|
"\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd"
|
|
"\\xfe\\xff'")
|
|
testrepr = repr(''.join(map(chr, range(256))))
|
|
self.assertEqual(testrepr, latin1repr)
|
|
# Test repr works on wide unicode escapes without overflow.
|
|
self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096),
|
|
repr("\U00010000" * 39 + "\uffff" * 4096))
|
|
|
|
def test_iterators(self):
|
|
# Make sure unicode objects have an __iter__ method
|
|
it = "\u1111\u2222\u3333".__iter__()
|
|
self.assertEqual(next(it), "\u1111")
|
|
self.assertEqual(next(it), "\u2222")
|
|
self.assertEqual(next(it), "\u3333")
|
|
self.assertRaises(StopIteration, next, it)
|
|
|
|
def test_count(self):
|
|
string_tests.CommonTest.test_count(self)
|
|
# check mixed argument types
|
|
self.checkequalnofix(3, 'aaa', 'count', 'a')
|
|
self.checkequalnofix(0, 'aaa', 'count', 'b')
|
|
self.checkequalnofix(3, 'aaa', 'count', 'a')
|
|
self.checkequalnofix(0, 'aaa', 'count', 'b')
|
|
self.checkequalnofix(0, 'aaa', 'count', 'b')
|
|
self.checkequalnofix(1, 'aaa', 'count', 'a', -1)
|
|
self.checkequalnofix(3, 'aaa', 'count', 'a', -10)
|
|
self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1)
|
|
self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10)
|
|
|
|
def test_find(self):
|
|
self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc')
|
|
self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1)
|
|
self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4)
|
|
|
|
self.assertRaises(TypeError, 'hello'.find)
|
|
self.assertRaises(TypeError, 'hello'.find, 42)
|
|
|
|
def test_rfind(self):
|
|
string_tests.CommonTest.test_rfind(self)
|
|
# check mixed argument types
|
|
self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc')
|
|
self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
|
|
self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
|
|
|
|
def test_index(self):
|
|
string_tests.CommonTest.test_index(self)
|
|
self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
|
|
self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
|
|
self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
|
|
self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1)
|
|
self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib')
|
|
self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1)
|
|
self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8)
|
|
self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1)
|
|
|
|
def test_rindex(self):
|
|
string_tests.CommonTest.test_rindex(self)
|
|
self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
|
|
self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
|
|
self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
|
|
self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
|
|
|
|
self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib')
|
|
self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1)
|
|
self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1)
|
|
self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8)
|
|
self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1)
|
|
|
|
def test_translate(self):
|
|
self.checkequalnofix('bbbc', 'abababc', 'translate', {ord('a'):None})
|
|
self.checkequalnofix('iiic', 'abababc', 'translate', {ord('a'):None, ord('b'):ord('i')})
|
|
self.checkequalnofix('iiix', 'abababc', 'translate', {ord('a'):None, ord('b'):ord('i'), ord('c'):'x'})
|
|
self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', {ord('a'):None, ord('b'):'<i>'})
|
|
self.checkequalnofix('c', 'abababc', 'translate', {ord('a'):None, ord('b'):''})
|
|
self.checkequalnofix('xyyx', 'xzx', 'translate', {ord('z'):'yy'})
|
|
|
|
self.assertRaises(TypeError, 'hello'.translate)
|
|
self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
|
|
|
|
def test_split(self):
|
|
string_tests.CommonTest.test_split(self)
|
|
|
|
# Mixed arguments
|
|
self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
|
|
self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
|
|
self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test')
|
|
|
|
def test_join(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_join(self)
|
|
|
|
# mixed arguments
|
|
self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
|
|
self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
|
|
self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
|
|
self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
|
|
self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
|
|
self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
|
|
self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
|
|
|
|
def test_replace(self):
|
|
string_tests.CommonTest.test_replace(self)
|
|
|
|
# method call forwarded from str implementation because of unicode argument
|
|
self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
|
|
self.assertRaises(TypeError, 'replace'.replace, "r", 42)
|
|
|
|
def test_comparison(self):
|
|
# Comparisons:
|
|
self.assertEqual('abc', 'abc')
|
|
self.assertEqual('abc', 'abc')
|
|
self.assertEqual('abc', 'abc')
|
|
self.assert_('abcd' > 'abc')
|
|
self.assert_('abcd' > 'abc')
|
|
self.assert_('abcd' > 'abc')
|
|
self.assert_('abc' < 'abcd')
|
|
self.assert_('abc' < 'abcd')
|
|
self.assert_('abc' < 'abcd')
|
|
|
|
if 0:
|
|
# Move these tests to a Unicode collation module test...
|
|
# Testing UTF-16 code point order comparisons...
|
|
|
|
# No surrogates, no fixup required.
|
|
self.assert_('\u0061' < '\u20ac')
|
|
# Non surrogate below surrogate value, no fixup required
|
|
self.assert_('\u0061' < '\ud800\udc02')
|
|
|
|
# Non surrogate above surrogate value, fixup required
|
|
def test_lecmp(s, s2):
|
|
self.assert_(s < s2)
|
|
|
|
def test_fixup(s):
|
|
s2 = '\ud800\udc01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud900\udc01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\uda00\udc01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\udb00\udc01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud800\udd01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud900\udd01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\uda00\udd01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\udb00\udd01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud800\ude01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud900\ude01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\uda00\ude01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\udb00\ude01'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud800\udfff'
|
|
test_lecmp(s, s2)
|
|
s2 = '\ud900\udfff'
|
|
test_lecmp(s, s2)
|
|
s2 = '\uda00\udfff'
|
|
test_lecmp(s, s2)
|
|
s2 = '\udb00\udfff'
|
|
test_lecmp(s, s2)
|
|
|
|
test_fixup('\ue000')
|
|
test_fixup('\uff61')
|
|
|
|
# Surrogates on both sides, no fixup required
|
|
self.assert_('\ud800\udc02' < '\ud84d\udc56')
|
|
|
|
def test_islower(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_islower(self)
|
|
self.checkequalnofix(False, '\u1FFc', 'islower')
|
|
|
|
def test_isupper(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_isupper(self)
|
|
if not sys.platform.startswith('java'):
|
|
self.checkequalnofix(False, '\u1FFc', 'isupper')
|
|
|
|
def test_istitle(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_title(self)
|
|
self.checkequalnofix(True, '\u1FFc', 'istitle')
|
|
self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle')
|
|
|
|
def test_isspace(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_isspace(self)
|
|
self.checkequalnofix(True, '\u2000', 'isspace')
|
|
self.checkequalnofix(True, '\u200a', 'isspace')
|
|
self.checkequalnofix(False, '\u2014', 'isspace')
|
|
|
|
def test_isalpha(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self)
|
|
self.checkequalnofix(True, '\u1FFc', 'isalpha')
|
|
|
|
def test_isdecimal(self):
|
|
self.checkequalnofix(False, '', 'isdecimal')
|
|
self.checkequalnofix(False, 'a', 'isdecimal')
|
|
self.checkequalnofix(True, '0', 'isdecimal')
|
|
self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE
|
|
self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER
|
|
self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO
|
|
self.checkequalnofix(True, '0123456789', 'isdecimal')
|
|
self.checkequalnofix(False, '0123456789a', 'isdecimal')
|
|
|
|
self.checkraises(TypeError, 'abc', 'isdecimal', 42)
|
|
|
|
def test_isdigit(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self)
|
|
self.checkequalnofix(True, '\u2460', 'isdigit')
|
|
self.checkequalnofix(False, '\xbc', 'isdigit')
|
|
self.checkequalnofix(True, '\u0660', 'isdigit')
|
|
|
|
def test_isnumeric(self):
|
|
self.checkequalnofix(False, '', 'isnumeric')
|
|
self.checkequalnofix(False, 'a', 'isnumeric')
|
|
self.checkequalnofix(True, '0', 'isnumeric')
|
|
self.checkequalnofix(True, '\u2460', 'isnumeric')
|
|
self.checkequalnofix(True, '\xbc', 'isnumeric')
|
|
self.checkequalnofix(True, '\u0660', 'isnumeric')
|
|
self.checkequalnofix(True, '0123456789', 'isnumeric')
|
|
self.checkequalnofix(False, '0123456789a', 'isnumeric')
|
|
|
|
self.assertRaises(TypeError, "abc".isnumeric, 42)
|
|
|
|
def test_contains(self):
|
|
# Testing Unicode contains method
|
|
self.assert_('a' in 'abdb')
|
|
self.assert_('a' in 'bdab')
|
|
self.assert_('a' in 'bdaba')
|
|
self.assert_('a' in 'bdba')
|
|
self.assert_('a' not in 'bdb')
|
|
self.assert_('a' in 'bdba')
|
|
self.assert_('a' in ('a',1,None))
|
|
self.assert_('a' in (1,None,'a'))
|
|
self.assert_('a' in ('a',1,None))
|
|
self.assert_('a' in (1,None,'a'))
|
|
self.assert_('a' not in ('x',1,'y'))
|
|
self.assert_('a' not in ('x',1,None))
|
|
self.assert_('abcd' not in 'abcxxxx')
|
|
self.assert_('ab' in 'abcd')
|
|
self.assert_('ab' in 'abc')
|
|
self.assert_('ab' in (1,None,'ab'))
|
|
self.assert_('' in 'abc')
|
|
self.assert_('' in '')
|
|
self.assert_('' in 'abc')
|
|
self.assert_('\0' not in 'abc')
|
|
self.assert_('\0' in '\0abc')
|
|
self.assert_('\0' in 'abc\0')
|
|
self.assert_('a' in '\0abc')
|
|
self.assert_('asdf' in 'asdf')
|
|
self.assert_('asdf' not in 'asd')
|
|
self.assert_('asdf' not in '')
|
|
|
|
self.assertRaises(TypeError, "abc".__contains__)
|
|
|
|
def test_formatting(self):
|
|
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
|
|
# Testing Unicode formatting strings...
|
|
self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc')
|
|
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00')
|
|
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00')
|
|
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50')
|
|
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57')
|
|
self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 1003.57), 'abc, abc, -1, -2.000000, 1003.57')
|
|
if not sys.platform.startswith('java'):
|
|
self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'")
|
|
self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def')
|
|
self.assertEqual("%(x)s, %(\xfc)s" % {'x':"abc", '\xfc':"def"}, 'abc, def')
|
|
|
|
self.assertEqual('%c' % 0x1234, '\u1234')
|
|
self.assertRaises(OverflowError, "%c".__mod__, (sys.maxunicode+1,))
|
|
|
|
# formatting jobs delegated from the string implementation:
|
|
self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
|
|
self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
|
|
self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
|
|
self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
|
|
self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
|
|
self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
|
|
self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...')
|
|
self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...')
|
|
self.assertEqual('...%s...' % "abc", '...abc...')
|
|
self.assertEqual('%*s' % (5,'abc',), ' abc')
|
|
self.assertEqual('%*s' % (-5,'abc',), 'abc ')
|
|
self.assertEqual('%*.*s' % (5,2,'abc',), ' ab')
|
|
self.assertEqual('%*.*s' % (5,3,'abc',), ' abc')
|
|
self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc')
|
|
self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc')
|
|
self.assertEqual('%c' % 'a', 'a')
|
|
class Wrapper:
|
|
def __str__(self):
|
|
return '\u1234'
|
|
self.assertEqual('%s' % Wrapper(), '\u1234')
|
|
|
|
@test_support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
|
|
def test_format_float(self):
|
|
# should not format with a comma, but always with C locale
|
|
self.assertEqual('1.0', '%.1f' % 1.0)
|
|
|
|
def test_constructor(self):
|
|
# unicode(obj) tests (this maps to PyObject_Unicode() at C level)
|
|
|
|
self.assertEqual(
|
|
str('unicode remains unicode'),
|
|
'unicode remains unicode'
|
|
)
|
|
|
|
class UnicodeSubclass(str):
|
|
pass
|
|
|
|
self.assertEqual(
|
|
str(UnicodeSubclass('unicode subclass becomes unicode')),
|
|
'unicode subclass becomes unicode'
|
|
)
|
|
|
|
self.assertEqual(
|
|
str('strings are converted to unicode'),
|
|
'strings are converted to unicode'
|
|
)
|
|
|
|
class UnicodeCompat:
|
|
def __init__(self, x):
|
|
self.x = x
|
|
def __unicode__(self):
|
|
return self.x
|
|
|
|
self.assertEqual(
|
|
str(UnicodeCompat('__unicode__ compatible objects are recognized')),
|
|
'__unicode__ compatible objects are recognized')
|
|
|
|
class StringCompat:
|
|
def __init__(self, x):
|
|
self.x = x
|
|
def __str__(self):
|
|
return self.x
|
|
|
|
self.assertEqual(
|
|
str(StringCompat('__str__ compatible objects are recognized')),
|
|
'__str__ compatible objects are recognized'
|
|
)
|
|
|
|
# unicode(obj) is compatible to str():
|
|
|
|
o = StringCompat('unicode(obj) is compatible to str()')
|
|
self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
|
|
self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
|
|
|
|
# %-formatting and .__unicode__()
|
|
self.assertEqual('%s' %
|
|
UnicodeCompat("u'%s' % obj uses obj.__unicode__()"),
|
|
"u'%s' % obj uses obj.__unicode__()")
|
|
self.assertEqual('%s' %
|
|
UnicodeCompat("u'%s' % obj falls back to obj.__str__()"),
|
|
"u'%s' % obj falls back to obj.__str__()")
|
|
|
|
for obj in (123, 123.45, 123):
|
|
self.assertEqual(str(obj), str(str(obj)))
|
|
|
|
# unicode(obj, encoding, error) tests (this maps to
|
|
# PyUnicode_FromEncodedObject() at C level)
|
|
|
|
if not sys.platform.startswith('java'):
|
|
self.assertRaises(
|
|
TypeError,
|
|
str,
|
|
'decoding unicode is not supported',
|
|
'utf-8',
|
|
'strict'
|
|
)
|
|
|
|
self.assertEqual(
|
|
str(b'strings are decoded to unicode', 'utf-8', 'strict'),
|
|
'strings are decoded to unicode'
|
|
)
|
|
|
|
if not sys.platform.startswith('java'):
|
|
self.assertEqual(
|
|
str(
|
|
buffer('character buffers are decoded to unicode'),
|
|
'utf-8',
|
|
'strict'
|
|
),
|
|
'character buffers are decoded to unicode'
|
|
)
|
|
|
|
self.assertRaises(TypeError, str, 42, 42, 42)
|
|
|
|
def test_codecs_utf7(self):
|
|
utfTests = [
|
|
('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example
|
|
('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example
|
|
('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example
|
|
('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example
|
|
('+', b'+-'),
|
|
('+-', b'+--'),
|
|
('+?', b'+-?'),
|
|
('\?', b'+AFw?'),
|
|
('+?', b'+-?'),
|
|
(r'\\?', b'+AFwAXA?'),
|
|
(r'\\\?', b'+AFwAXABc?'),
|
|
(r'++--', b'+-+---')
|
|
]
|
|
|
|
for (x, y) in utfTests:
|
|
self.assertEqual(x.encode('utf-7'), y)
|
|
|
|
# surrogates not supported
|
|
self.assertRaises(UnicodeError, str, b'+3ADYAA-', 'utf-7')
|
|
|
|
self.assertEqual(str(b'+3ADYAA-', 'utf-7', 'replace'), '\ufffd')
|
|
|
|
def test_codecs_utf8(self):
|
|
self.assertEqual(''.encode('utf-8'), b'')
|
|
self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac')
|
|
self.assertEqual('\ud800\udc02'.encode('utf-8'), b'\xf0\x90\x80\x82')
|
|
self.assertEqual('\ud84d\udc56'.encode('utf-8'), b'\xf0\xa3\x91\x96')
|
|
self.assertEqual('\ud800'.encode('utf-8'), b'\xed\xa0\x80')
|
|
self.assertEqual('\udc00'.encode('utf-8'), b'\xed\xb0\x80')
|
|
self.assertEqual(
|
|
('\ud800\udc02'*1000).encode('utf-8'),
|
|
b'\xf0\x90\x80\x82'*1000
|
|
)
|
|
self.assertEqual(
|
|
'\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
|
|
'\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
|
|
'\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
|
|
'\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
|
|
'\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das'
|
|
' Nunstuck git und'.encode('utf-8'),
|
|
b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81'
|
|
b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3'
|
|
b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe'
|
|
b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
|
|
b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8'
|
|
b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81'
|
|
b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81'
|
|
b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3'
|
|
b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf'
|
|
b'\xe3\x80\x8cWenn ist das Nunstuck git und'
|
|
)
|
|
|
|
# UTF-8 specific decoding tests
|
|
self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' )
|
|
self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' )
|
|
self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' )
|
|
|
|
# Other possible utf-8 test cases:
|
|
# * strict decoding testing for all of the
|
|
# UTF8_ERROR cases in PyUnicode_DecodeUTF8
|
|
|
|
def test_codecs_idna(self):
|
|
# Test whether trailing dot is preserved
|
|
self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
|
|
|
|
def test_codecs_errors(self):
|
|
# Error handling (encoding)
|
|
self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
|
|
self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict')
|
|
self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x")
|
|
self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x")
|
|
|
|
# Error handling (decoding)
|
|
self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii')
|
|
self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict')
|
|
self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x")
|
|
self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x')
|
|
|
|
# Error handling (unknown character names)
|
|
self.assertEqual("\\N{foo}xx".decode("unicode-escape", "ignore"), "xx")
|
|
|
|
# Error handling (truncated escape sequence)
|
|
self.assertRaises(UnicodeError, "\\".decode, "unicode-escape")
|
|
|
|
self.assertRaises(TypeError, "hello".decode, "test.unicode1")
|
|
self.assertRaises(TypeError, str, "hello", "test.unicode2")
|
|
self.assertRaises(TypeError, "hello".encode, "test.unicode1")
|
|
self.assertRaises(TypeError, "hello".encode, "test.unicode2")
|
|
# executes PyUnicode_Encode()
|
|
import imp
|
|
self.assertRaises(
|
|
ImportError,
|
|
imp.find_module,
|
|
"non-existing module",
|
|
["non-existing dir"]
|
|
)
|
|
|
|
# Error handling (wrong arguments)
|
|
self.assertRaises(TypeError, "hello".encode, 42, 42, 42)
|
|
|
|
# Error handling (PyUnicode_EncodeDecimal())
|
|
self.assertRaises(UnicodeError, int, "\u0200")
|
|
|
|
def test_codecs(self):
|
|
# Encoding
|
|
self.assertEqual('hello'.encode('ascii'), b'hello')
|
|
self.assertEqual('hello'.encode('utf-7'), b'hello')
|
|
self.assertEqual('hello'.encode('utf-8'), b'hello')
|
|
self.assertEqual('hello'.encode('utf8'), b'hello')
|
|
self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000')
|
|
self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o')
|
|
self.assertEqual('hello'.encode('latin-1'), b'hello')
|
|
|
|
# Roundtrip safety for BMP (just the first 1024 chars)
|
|
for c in range(1024):
|
|
u = chr(c)
|
|
for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le',
|
|
'utf-16-be', 'raw_unicode_escape',
|
|
'unicode_escape', 'unicode_internal'):
|
|
self.assertEqual(str(u.encode(encoding),encoding), u)
|
|
|
|
# Roundtrip safety for BMP (just the first 256 chars)
|
|
for c in range(256):
|
|
u = chr(c)
|
|
for encoding in ('latin-1',):
|
|
self.assertEqual(str(u.encode(encoding),encoding), u)
|
|
|
|
# Roundtrip safety for BMP (just the first 128 chars)
|
|
for c in range(128):
|
|
u = chr(c)
|
|
for encoding in ('ascii',):
|
|
self.assertEqual(str(u.encode(encoding),encoding), u)
|
|
|
|
# Roundtrip safety for non-BMP (just a few chars)
|
|
u = '\U00010001\U00020002\U00030003\U00040004\U00050005'
|
|
for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
|
|
#'raw_unicode_escape',
|
|
'unicode_escape', 'unicode_internal'):
|
|
self.assertEqual(str(u.encode(encoding),encoding), u)
|
|
|
|
# UTF-8 must be roundtrip safe for all UCS-2 code points
|
|
# This excludes surrogates: in the full range, there would be
|
|
# a surrogate pair (\udbff\udc00), which gets converted back
|
|
# to a non-BMP character (\U0010fc00)
|
|
u = ''.join(map(chr, list(range(0,0xd800)) +
|
|
list(range(0xe000,0x10000))))
|
|
for encoding in ('utf-8',):
|
|
self.assertEqual(str(u.encode(encoding),encoding), u)
|
|
|
|
def test_codecs_charmap(self):
|
|
# 0-127
|
|
s = bytes(range(128))
|
|
for encoding in (
|
|
'cp037', 'cp1026',
|
|
'cp437', 'cp500', 'cp737', 'cp775', 'cp850',
|
|
'cp852', 'cp855', 'cp860', 'cp861', 'cp862',
|
|
'cp863', 'cp865', 'cp866',
|
|
'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
|
|
'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6',
|
|
'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1',
|
|
'mac_cyrillic', 'mac_latin2',
|
|
|
|
'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
|
|
'cp1256', 'cp1257', 'cp1258',
|
|
'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
|
|
|
|
'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
|
|
'cp1006', 'iso8859_8',
|
|
|
|
### These have undefined mappings:
|
|
#'cp424',
|
|
|
|
### These fail the round-trip:
|
|
#'cp875'
|
|
|
|
):
|
|
self.assertEqual(str(s, encoding).encode(encoding), s)
|
|
|
|
# 128-255
|
|
s = bytes(range(128, 256))
|
|
for encoding in (
|
|
'cp037', 'cp1026',
|
|
'cp437', 'cp500', 'cp737', 'cp775', 'cp850',
|
|
'cp852', 'cp855', 'cp860', 'cp861', 'cp862',
|
|
'cp863', 'cp865', 'cp866',
|
|
'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
|
|
'iso8859_2', 'iso8859_4', 'iso8859_5',
|
|
'iso8859_9', 'koi8_r', 'latin_1',
|
|
'mac_cyrillic', 'mac_latin2',
|
|
|
|
### These have undefined mappings:
|
|
#'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
|
|
#'cp1256', 'cp1257', 'cp1258',
|
|
#'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
|
|
#'iso8859_3', 'iso8859_6', 'iso8859_7',
|
|
#'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
|
|
|
|
### These fail the round-trip:
|
|
#'cp1006', 'cp875', 'iso8859_8',
|
|
|
|
):
|
|
self.assertEqual(str(s, encoding).encode(encoding), s)
|
|
|
|
def test_concatenation(self):
|
|
self.assertEqual(("abc" "def"), "abcdef")
|
|
self.assertEqual(("abc" "def"), "abcdef")
|
|
self.assertEqual(("abc" "def"), "abcdef")
|
|
self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
|
|
self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
|
|
|
|
def test_printing(self):
|
|
class BitBucket:
|
|
def write(self, text):
|
|
pass
|
|
|
|
out = BitBucket()
|
|
print('abc', file=out)
|
|
print('abc', 'def', file=out)
|
|
print('abc', 'def', file=out)
|
|
print('abc', 'def', file=out)
|
|
print('abc\n', file=out)
|
|
print('abc\n', end=' ', file=out)
|
|
print('abc\n', end=' ', file=out)
|
|
print('def\n', file=out)
|
|
print('def\n', file=out)
|
|
|
|
def test_ucs4(self):
|
|
if sys.maxunicode == 0xFFFF:
|
|
return
|
|
x = '\U00100000'
|
|
y = x.encode("raw-unicode-escape").decode("raw-unicode-escape")
|
|
self.assertEqual(x, y)
|
|
|
|
def test_conversion(self):
|
|
# Make sure __unicode__() works properly
|
|
class Foo0:
|
|
def __str__(self):
|
|
return "foo"
|
|
|
|
class Foo1:
|
|
def __unicode__(self):
|
|
return "foo"
|
|
|
|
class Foo2(object):
|
|
def __unicode__(self):
|
|
return "foo"
|
|
|
|
class Foo3(object):
|
|
def __unicode__(self):
|
|
return "foo"
|
|
|
|
class Foo4(str):
|
|
def __unicode__(self):
|
|
return "foo"
|
|
|
|
class Foo5(str):
|
|
def __unicode__(self):
|
|
return "foo"
|
|
|
|
class Foo6(str):
|
|
def __str__(self):
|
|
return "foos"
|
|
|
|
def __unicode__(self):
|
|
return "foou"
|
|
|
|
class Foo7(str):
|
|
def __str__(self):
|
|
return "foos"
|
|
def __unicode__(self):
|
|
return "foou"
|
|
|
|
class Foo8(str):
|
|
def __new__(cls, content=""):
|
|
return str.__new__(cls, 2*content)
|
|
def __unicode__(self):
|
|
return self
|
|
|
|
class Foo9(str):
|
|
def __str__(self):
|
|
return "string"
|
|
def __unicode__(self):
|
|
return "not unicode"
|
|
|
|
self.assertEqual(str(Foo0()), "foo")
|
|
self.assertEqual(str(Foo1()), "foo")
|
|
self.assertEqual(str(Foo2()), "foo")
|
|
self.assertEqual(str(Foo3()), "foo")
|
|
self.assertEqual(str(Foo4("bar")), "foo")
|
|
self.assertEqual(str(Foo5("bar")), "foo")
|
|
self.assertEqual(str(Foo6("bar")), "foou")
|
|
self.assertEqual(str(Foo7("bar")), "foou")
|
|
self.assertEqual(str(Foo8("foo")), "foofoo")
|
|
self.assertEqual(str(Foo9("foo")), "not unicode")
|
|
|
|
def test_unicode_repr(self):
|
|
class s1:
|
|
def __repr__(self):
|
|
return '\\n'
|
|
|
|
class s2:
|
|
def __repr__(self):
|
|
return '\\n'
|
|
|
|
self.assertEqual(repr(s1()), '\\n')
|
|
self.assertEqual(repr(s2()), '\\n')
|
|
|
|
def test_expandtabs_overflows_gracefully(self):
|
|
# This test only affects 32-bit platforms because expandtabs can only take
|
|
# an int as the max value, not a 64-bit C long. If expandtabs is changed
|
|
# to take a 64-bit long, this test should apply to all platforms.
|
|
if sys.maxint > (1 << 32) or struct.calcsize('P') != 4:
|
|
return
|
|
self.assertRaises(OverflowError, u't\tt\t'.expandtabs, sys.maxint)
|
|
|
|
|
|
def test_main():
|
|
test_support.run_unittest(__name__)
|
|
|
|
if __name__ == "__main__":
|
|
test_main()
|