This commit is contained in:
Raymond Hettinger 2011-03-23 12:53:32 -07:00
commit 41da10e62c
8 changed files with 222 additions and 24 deletions

View File

@ -135,9 +135,9 @@ def header_encode(header_bytes, charset='iso-8859-1'):
charset names the character set to use in the RFC 2046 header. It
defaults to iso-8859-1.
"""
# Return empty headers unchanged
# Return empty headers as an empty string.
if not header_bytes:
return str(header_bytes)
return ''
# Iterate over every byte, encoding if necessary.
encoded = []
for octet in header_bytes:
@ -268,7 +268,7 @@ def decode(encoded, eol=NL):
if i == n:
decoded += eol
# Special case if original string did not end with eol
if not encoded.endswith(eol) and decoded.endswith(eol):
if encoded[-1] not in '\r\n' and decoded.endswith(eol):
decoded = decoded[:-1]
return decoded

View File

@ -374,6 +374,13 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
forever = True
elif o in ('-j', '--multiprocess'):
use_mp = int(a)
if use_mp <= 0:
try:
import multiprocessing
# Use all cores + extras for tests that like to sleep
use_mp = 2 + multiprocessing.cpu_count()
except (ImportError, NotImplementedError):
use_mp = 3
elif o == '--header':
header = True
elif o == '--slaveargs':

View File

@ -1389,9 +1389,8 @@ def args_from_interpreter_flags():
v = getattr(sys.flags, flag)
if v > 0:
args.append('-' + opt * v)
if sys.warnoptions:
args.append('-W')
args.extend(sys.warnoptions)
for opt in sys.warnoptions:
args.append('-W' + opt)
return args
#============================================================

View File

@ -3344,21 +3344,200 @@ class TestQuopri(unittest.TestCase):
c = chr(x)
self.assertEqual(quoprimime.unquote(quoprimime.quote(c)), c)
def test_header_encode(self):
eq = self.assertEqual
he = quoprimime.header_encode
eq(he(b'hello'), '=?iso-8859-1?q?hello?=')
eq(he(b'hello', charset='iso-8859-2'), '=?iso-8859-2?q?hello?=')
eq(he(b'hello\nworld'), '=?iso-8859-1?q?hello=0Aworld?=')
# Test a non-ASCII character
eq(he(b'hello\xc7there'), '=?iso-8859-1?q?hello=C7there?=')
def _test_header_encode(self, header, expected_encoded_header, charset=None):
if charset is None:
encoded_header = quoprimime.header_encode(header)
else:
encoded_header = quoprimime.header_encode(header, charset)
self.assertEqual(encoded_header, expected_encoded_header)
def test_decode(self):
eq = self.assertEqual
eq(quoprimime.decode(''), '')
eq(quoprimime.decode('hello'), 'hello')
eq(quoprimime.decode('hello', 'X'), 'hello')
eq(quoprimime.decode('hello\nworld', 'X'), 'helloXworld')
def test_header_encode_null(self):
self._test_header_encode(b'', '')
def test_header_encode_one_word(self):
self._test_header_encode(b'hello', '=?iso-8859-1?q?hello?=')
def test_header_encode_two_lines(self):
self._test_header_encode(b'hello\nworld',
'=?iso-8859-1?q?hello=0Aworld?=')
def test_header_encode_non_ascii(self):
self._test_header_encode(b'hello\xc7there',
'=?iso-8859-1?q?hello=C7there?=')
def test_header_encode_alt_charset(self):
self._test_header_encode(b'hello', '=?iso-8859-2?q?hello?=',
charset='iso-8859-2')
def _test_header_decode(self, encoded_header, expected_decoded_header):
decoded_header = quoprimime.header_decode(encoded_header)
self.assertEqual(decoded_header, expected_decoded_header)
def test_header_decode_null(self):
self._test_header_decode('', '')
def test_header_decode_one_word(self):
self._test_header_decode('hello', 'hello')
def test_header_decode_two_lines(self):
self._test_header_decode('hello=0Aworld', 'hello\nworld')
def test_header_decode_non_ascii(self):
self._test_header_decode('hello=C7there', 'hello\xc7there')
def _test_decode(self, encoded, expected_decoded, eol=None):
if eol is None:
decoded = quoprimime.decode(encoded)
else:
decoded = quoprimime.decode(encoded, eol=eol)
self.assertEqual(decoded, expected_decoded)
def test_decode_null_word(self):
self._test_decode('', '')
def test_decode_null_line_null_word(self):
self._test_decode('\r\n', '\n')
def test_decode_one_word(self):
self._test_decode('hello', 'hello')
def test_decode_one_word_eol(self):
self._test_decode('hello', 'hello', eol='X')
def test_decode_one_line(self):
self._test_decode('hello\r\n', 'hello\n')
def test_decode_one_line_lf(self):
self._test_decode('hello\n', 'hello\n')
def test_decode_one_line_cr(self):
self._test_decode('hello\r', 'hello\n')
def test_decode_one_line_nl(self):
self._test_decode('hello\n', 'helloX', eol='X')
def test_decode_one_line_crnl(self):
self._test_decode('hello\r\n', 'helloX', eol='X')
def test_decode_one_line_one_word(self):
self._test_decode('hello\r\nworld', 'hello\nworld')
def test_decode_one_line_one_word_eol(self):
self._test_decode('hello\r\nworld', 'helloXworld', eol='X')
def test_decode_two_lines(self):
self._test_decode('hello\r\nworld\r\n', 'hello\nworld\n')
def test_decode_two_lines_eol(self):
self._test_decode('hello\r\nworld\r\n', 'helloXworldX', eol='X')
def test_decode_one_long_line(self):
self._test_decode('Spam' * 250, 'Spam' * 250)
def test_decode_one_space(self):
self._test_decode(' ', '')
def test_decode_multiple_spaces(self):
self._test_decode(' ' * 5, '')
def test_decode_one_line_trailing_spaces(self):
self._test_decode('hello \r\n', 'hello\n')
def test_decode_two_lines_trailing_spaces(self):
self._test_decode('hello \r\nworld \r\n', 'hello\nworld\n')
def test_decode_quoted_word(self):
self._test_decode('=22quoted=20words=22', '"quoted words"')
def test_decode_uppercase_quoting(self):
self._test_decode('ab=CD=EF', 'ab\xcd\xef')
def test_decode_lowercase_quoting(self):
self._test_decode('ab=cd=ef', 'ab\xcd\xef')
def test_decode_soft_line_break(self):
self._test_decode('soft line=\r\nbreak', 'soft linebreak')
def test_decode_false_quoting(self):
self._test_decode('A=1,B=A ==> A+B==2', 'A=1,B=A ==> A+B==2')
def _test_encode(self, body, expected_encoded_body, maxlinelen=None, eol=None):
kwargs = {}
if maxlinelen is None:
# Use body_encode's default.
maxlinelen = 76
else:
kwargs['maxlinelen'] = maxlinelen
if eol is None:
# Use body_encode's default.
eol = '\n'
else:
kwargs['eol'] = eol
encoded_body = quoprimime.body_encode(body, **kwargs)
self.assertEqual(encoded_body, expected_encoded_body)
if eol == '\n' or eol == '\r\n':
# We know how to split the result back into lines, so maxlinelen
# can be checked.
for line in encoded_body.splitlines():
self.assertLessEqual(len(line), maxlinelen)
def test_encode_null(self):
self._test_encode('', '')
def test_encode_null_lines(self):
self._test_encode('\n\n', '\n\n')
def test_encode_one_line(self):
self._test_encode('hello\n', 'hello\n')
def test_encode_one_line_crlf(self):
self._test_encode('hello\r\n', 'hello\n')
def test_encode_one_line_eol(self):
self._test_encode('hello\n', 'hello\r\n', eol='\r\n')
def test_encode_one_space(self):
self._test_encode(' ', '=20')
def test_encode_one_line_one_space(self):
self._test_encode(' \n', '=20\n')
def test_encode_one_word_trailing_spaces(self):
self._test_encode('hello ', 'hello =20')
def test_encode_one_line_trailing_spaces(self):
self._test_encode('hello \n', 'hello =20\n')
def test_encode_one_word_trailing_tab(self):
self._test_encode('hello \t', 'hello =09')
def test_encode_one_line_trailing_tab(self):
self._test_encode('hello \t\n', 'hello =09\n')
def test_encode_trailing_space_before_maxlinelen(self):
self._test_encode('abcd \n1234', 'abcd =\n\n1234', maxlinelen=6)
def test_encode_trailing_space_beyond_maxlinelen(self):
self._test_encode('abcd \n1234', 'abc=\nd =\n\n1234', maxlinelen=4)
def test_encode_quoted_equals(self):
self._test_encode('a = b', 'a =3D b')
def test_encode_one_long_string(self):
self._test_encode('x' * 100, 'x' * 75 + '=\n' + 'x' * 25)
def test_encode_one_long_line(self):
self._test_encode('x' * 100 + '\n', 'x' * 75 + '=\n' + 'x' * 25 + '\n')
def test_encode_one_very_long_line(self):
self._test_encode('x' * 200 + '\n',
2 * ('x' * 75 + '=\n') + 'x' * 50 + '\n')
def test_encode_one_long_line(self):
self._test_encode('x' * 100 + '\n', 'x' * 75 + '=\n' + 'x' * 25 + '\n')
def test_encode_shortest_maxlinelen(self):
self._test_encode('=' * 5, '=3D=\n' * 4 + '=3D', maxlinelen=4)
def test_encode(self):
eq = self.assertEqual

View File

@ -3,6 +3,7 @@ import re
import sys
from io import StringIO
import unittest
from math import copysign
def disassemble(func):
f = StringIO()
@ -207,6 +208,9 @@ class TestTranforms(unittest.TestCase):
def test_folding_of_unaryops_on_constants(self):
for line, elem in (
('-0.5', '(-0.5)'), # unary negative
('-0.0', '(-0.0)'), # -0.0
('-(1.0-1.0)','(-0.0)'), # -0.0 after folding
('-0', '(0)'), # -0
('~-2', '(1)'), # unary invert
('+1', '(1)'), # unary positive
):
@ -214,6 +218,13 @@ class TestTranforms(unittest.TestCase):
self.assertIn(elem, asm, asm)
self.assertNotIn('UNARY_', asm)
# Check that -0.0 works after marshaling
def negzero():
return -(1.0-1.0)
self.assertNotIn('UNARY_', disassemble(negzero))
self.assertTrue(copysign(1.0, negzero()) < 0)
# Verify that unfoldables are skipped
for line, elem in (
('-"abc"', "('abc')"), # unary negative

View File

@ -867,6 +867,7 @@ Christian Tismer
Frank J. Tobin
R Lindsay Todd
Bennett Todd
Eugene Toder
Matias Torchinsky
Sandro Tosi
Richard Townsend

View File

@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins
-----------------
- Issue #11244: Remove an unnecessary peepholer check that was preventing
negative zeros from being constant-folded properly.
- Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
Windows if the file is a TTY to workaround a Windows bug. The Windows console
returns an error (12: not enough space error) on writing into stdout if

View File

@ -238,7 +238,7 @@ fold_binops_on_constants(unsigned char *codestr, PyObject *consts, PyObject **ob
static int
fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts, PyObject *v)
{
PyObject *newconst=NULL/*, *v*/;
PyObject *newconst;
Py_ssize_t len_consts;
int opcode;
@ -250,9 +250,7 @@ fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts, PyObject *v
opcode = codestr[3];
switch (opcode) {
case UNARY_NEGATIVE:
/* Preserve the sign of -0.0 */
if (PyObject_IsTrue(v) == 1)
newconst = PyNumber_Negative(v);
newconst = PyNumber_Negative(v);
break;
case UNARY_INVERT:
newconst = PyNumber_Invert(v);