/usr/lib/python2.7/dist-packages/testtools/tests/test_compat.py is in python-testtools 0.9.39-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | # Copyright (c) 2010 testtools developers. See LICENSE for details.
"""Tests for miscellaneous compatibility functions"""
import io
import linecache
import os
import sys
import tempfile
import traceback
import testtools
from testtools.compat import (
_b,
_detect_encoding,
_format_exc_info,
_format_exception_only,
_format_stack_list,
_get_source_encoding,
_u,
reraise,
str_is_unicode,
text_repr,
unicode_output_stream,
)
from testtools.matchers import (
Equals,
Is,
IsInstance,
MatchesException,
Not,
Raises,
)
class TestDetectEncoding(testtools.TestCase):
"""Test detection of Python source encodings"""
def _check_encoding(self, expected, lines, possibly_invalid=False):
"""Check lines are valid Python and encoding is as expected"""
if not possibly_invalid:
compile(_b("".join(lines)), "<str>", "exec")
encoding = _detect_encoding(lines)
self.assertEqual(expected, encoding,
"Encoding %r expected but got %r from lines %r" %
(expected, encoding, lines))
def test_examples_from_pep(self):
"""Check the examples given in PEP 263 all work as specified
See 'Examples' section of <http://www.python.org/dev/peps/pep-0263/>
"""
# With interpreter binary and using Emacs style file encoding comment:
self._check_encoding("latin-1", (
"#!/usr/bin/python\n",
"# -*- coding: latin-1 -*-\n",
"import os, sys\n"))
self._check_encoding("iso-8859-15", (
"#!/usr/bin/python\n",
"# -*- coding: iso-8859-15 -*-\n",
"import os, sys\n"))
self._check_encoding("ascii", (
"#!/usr/bin/python\n",
"# -*- coding: ascii -*-\n",
"import os, sys\n"))
# Without interpreter line, using plain text:
self._check_encoding("utf-8", (
"# This Python file uses the following encoding: utf-8\n",
"import os, sys\n"))
# Text editors might have different ways of defining the file's
# encoding, e.g.
self._check_encoding("latin-1", (
"#!/usr/local/bin/python\n",
"# coding: latin-1\n",
"import os, sys\n"))
# Without encoding comment, Python's parser will assume ASCII text:
self._check_encoding("ascii", (
"#!/usr/local/bin/python\n",
"import os, sys\n"))
# Encoding comments which don't work:
# Missing "coding:" prefix:
self._check_encoding("ascii", (
"#!/usr/local/bin/python\n",
"# latin-1\n",
"import os, sys\n"))
# Encoding comment not on line 1 or 2:
self._check_encoding("ascii", (
"#!/usr/local/bin/python\n",
"#\n",
"# -*- coding: latin-1 -*-\n",
"import os, sys\n"))
# Unsupported encoding:
self._check_encoding("ascii", (
"#!/usr/local/bin/python\n",
"# -*- coding: utf-42 -*-\n",
"import os, sys\n"),
possibly_invalid=True)
def test_bom(self):
"""Test the UTF-8 BOM counts as an encoding declaration"""
self._check_encoding("utf-8", (
"\xef\xbb\xbfimport sys\n",
))
self._check_encoding("utf-8", (
"\xef\xbb\xbf# File encoding: utf-8\n",
))
self._check_encoding("utf-8", (
'\xef\xbb\xbf"""Module docstring\n',
'\xef\xbb\xbfThat should just be a ZWNB"""\n'))
self._check_encoding("latin-1", (
'"""Is this coding: latin-1 or coding: utf-8 instead?\n',
'\xef\xbb\xbfThose should be latin-1 bytes"""\n'))
self._check_encoding("utf-8", (
"\xef\xbb\xbf# Is the coding: utf-8 or coding: euc-jp instead?\n",
'"""Module docstring say \xe2\x98\x86"""\n'),
possibly_invalid=True)
def test_multiple_coding_comments(self):
"""Test only the first of multiple coding declarations counts"""
self._check_encoding("iso-8859-1", (
"# Is the coding: iso-8859-1\n",
"# Or is it coding: iso-8859-2\n"),
possibly_invalid=True)
self._check_encoding("iso-8859-1", (
"#!/usr/bin/python\n",
"# Is the coding: iso-8859-1\n",
"# Or is it coding: iso-8859-2\n"))
self._check_encoding("iso-8859-1", (
"# Is the coding: iso-8859-1 or coding: iso-8859-2\n",
"# Or coding: iso-8859-3 or coding: iso-8859-4\n"),
possibly_invalid=True)
self._check_encoding("iso-8859-2", (
"# Is the coding iso-8859-1 or coding: iso-8859-2\n",
"# Spot the missing colon above\n"))
class TestGetSourceEncoding(testtools.TestCase):
"""Test reading and caching the encodings of source files"""
def setUp(self):
testtools.TestCase.setUp(self)
dir = tempfile.mkdtemp()
self.addCleanup(os.rmdir, dir)
self.filename = os.path.join(dir, self.id().rsplit(".", 1)[1] + ".py")
self._written = False
def put_source(self, text):
f = open(self.filename, "w")
try:
f.write(text)
finally:
f.close()
if not self._written:
self._written = True
self.addCleanup(os.remove, self.filename)
self.addCleanup(linecache.cache.pop, self.filename, None)
def test_nonexistant_file_as_ascii(self):
"""When file can't be found, the encoding should default to ascii"""
self.assertEquals("ascii", _get_source_encoding(self.filename))
def test_encoding_is_cached(self):
"""The encoding should stay the same if the cache isn't invalidated"""
self.put_source(
"# coding: iso-8859-13\n"
"import os\n")
self.assertEquals("iso-8859-13", _get_source_encoding(self.filename))
self.put_source(
"# coding: rot-13\n"
"vzcbeg bf\n")
self.assertEquals("iso-8859-13", _get_source_encoding(self.filename))
def test_traceback_rechecks_encoding(self):
"""A traceback function checks the cache and resets the encoding"""
self.put_source(
"# coding: iso-8859-8\n"
"import os\n")
self.assertEquals("iso-8859-8", _get_source_encoding(self.filename))
self.put_source(
"# coding: utf-8\n"
"import os\n")
try:
exec (compile("raise RuntimeError\n", self.filename, "exec"))
except RuntimeError:
traceback.extract_tb(sys.exc_info()[2])
else:
self.fail("RuntimeError not raised")
self.assertEquals("utf-8", _get_source_encoding(self.filename))
class _FakeOutputStream(object):
"""A simple file-like object for testing"""
def __init__(self):
self.writelog = []
def write(self, obj):
self.writelog.append(obj)
class TestUnicodeOutputStream(testtools.TestCase):
"""Test wrapping output streams so they work with arbitrary unicode"""
uni = _u("pa\u026a\u03b8\u0259n")
def setUp(self):
super(TestUnicodeOutputStream, self).setUp()
if sys.platform == "cli":
self.skip("IronPython shouldn't wrap streams to do encoding")
def test_no_encoding_becomes_ascii(self):
"""A stream with no encoding attribute gets ascii/replace strings"""
sout = _FakeOutputStream()
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa???n")], sout.writelog)
def test_encoding_as_none_becomes_ascii(self):
"""A stream with encoding value of None gets ascii/replace strings"""
sout = _FakeOutputStream()
sout.encoding = None
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa???n")], sout.writelog)
def test_bogus_encoding_becomes_ascii(self):
"""A stream with a bogus encoding gets ascii/replace strings"""
sout = _FakeOutputStream()
sout.encoding = "bogus"
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa???n")], sout.writelog)
def test_partial_encoding_replace(self):
"""A string which can be partly encoded correctly should be"""
sout = _FakeOutputStream()
sout.encoding = "iso-8859-7"
unicode_output_stream(sout).write(self.uni)
self.assertEqual([_b("pa?\xe8?n")], sout.writelog)
@testtools.skipIf(str_is_unicode, "Tests behaviour when str is not unicode")
def test_unicode_encodings_wrapped_when_str_is_not_unicode(self):
"""A unicode encoding is wrapped but needs no error handler"""
sout = _FakeOutputStream()
sout.encoding = "utf-8"
uout = unicode_output_stream(sout)
self.assertEqual(uout.errors, "strict")
uout.write(self.uni)
self.assertEqual([_b("pa\xc9\xaa\xce\xb8\xc9\x99n")], sout.writelog)
@testtools.skipIf(not str_is_unicode, "Tests behaviour when str is unicode")
def test_unicode_encodings_not_wrapped_when_str_is_unicode(self):
# No wrapping needed if native str type is unicode
sout = _FakeOutputStream()
sout.encoding = "utf-8"
uout = unicode_output_stream(sout)
self.assertIs(uout, sout)
def test_stringio(self):
"""A StringIO object should maybe get an ascii native str type"""
try:
from cStringIO import StringIO
newio = False
except ImportError:
from io import StringIO
newio = True
sout = StringIO()
soutwrapper = unicode_output_stream(sout)
soutwrapper.write(self.uni)
if newio:
self.assertEqual(self.uni, sout.getvalue())
else:
self.assertEqual("pa???n", sout.getvalue())
def test_io_stringio(self):
# io.StringIO only accepts unicode so should be returned as itself.
s = io.StringIO()
self.assertEqual(s, unicode_output_stream(s))
def test_io_bytesio(self):
# io.BytesIO only accepts bytes so should be wrapped.
bytes_io = io.BytesIO()
self.assertThat(bytes_io, Not(Is(unicode_output_stream(bytes_io))))
# Will error if s was not wrapped properly.
unicode_output_stream(bytes_io).write(_u('foo'))
def test_io_textwrapper(self):
# textwrapper is unicode, should be returned as itself.
text_io = io.TextIOWrapper(io.BytesIO())
self.assertThat(unicode_output_stream(text_io), Is(text_io))
# To be sure...
unicode_output_stream(text_io).write(_u('foo'))
class TestTextRepr(testtools.TestCase):
"""Ensure in extending repr, basic behaviours are not being broken"""
ascii_examples = (
# Single character examples
# C0 control codes should be escaped except multiline \n
("\x00", "'\\x00'", "'''\\\n\\x00'''"),
("\b", "'\\x08'", "'''\\\n\\x08'''"),
("\t", "'\\t'", "'''\\\n\\t'''"),
("\n", "'\\n'", "'''\\\n\n'''"),
("\r", "'\\r'", "'''\\\n\\r'''"),
# Quotes and backslash should match normal repr behaviour
('"', "'\"'", "'''\\\n\"'''"),
("'", "\"'\"", "'''\\\n\\''''"),
("\\", "'\\\\'", "'''\\\n\\\\'''"),
# DEL is also unprintable and should be escaped
("\x7F", "'\\x7f'", "'''\\\n\\x7f'''"),
# Character combinations that need double checking
("\r\n", "'\\r\\n'", "'''\\\n\\r\n'''"),
("\"'", "'\"\\''", "'''\\\n\"\\''''"),
("'\"", "'\\'\"'", "'''\\\n'\"'''"),
("\\n", "'\\\\n'", "'''\\\n\\\\n'''"),
("\\\n", "'\\\\\\n'", "'''\\\n\\\\\n'''"),
("\\' ", "\"\\\\' \"", "'''\\\n\\\\' '''"),
("\\'\n", "\"\\\\'\\n\"", "'''\\\n\\\\'\n'''"),
("\\'\"", "'\\\\\\'\"'", "'''\\\n\\\\'\"'''"),
("\\'''", "\"\\\\'''\"", "'''\\\n\\\\\\'\\'\\''''"),
)
# Bytes with the high bit set should always be escaped
bytes_examples = (
(_b("\x80"), "'\\x80'", "'''\\\n\\x80'''"),
(_b("\xA0"), "'\\xa0'", "'''\\\n\\xa0'''"),
(_b("\xC0"), "'\\xc0'", "'''\\\n\\xc0'''"),
(_b("\xFF"), "'\\xff'", "'''\\\n\\xff'''"),
(_b("\xC2\xA7"), "'\\xc2\\xa7'", "'''\\\n\\xc2\\xa7'''"),
)
# Unicode doesn't escape printable characters as per the Python 3 model
unicode_examples = (
# C1 codes are unprintable
(_u("\x80"), "'\\x80'", "'''\\\n\\x80'''"),
(_u("\x9F"), "'\\x9f'", "'''\\\n\\x9f'''"),
# No-break space is unprintable
(_u("\xA0"), "'\\xa0'", "'''\\\n\\xa0'''"),
# Letters latin alphabets are printable
(_u("\xA1"), _u("'\xa1'"), _u("'''\\\n\xa1'''")),
(_u("\xFF"), _u("'\xff'"), _u("'''\\\n\xff'''")),
(_u("\u0100"), _u("'\u0100'"), _u("'''\\\n\u0100'''")),
# Line and paragraph seperators are unprintable
(_u("\u2028"), "'\\u2028'", "'''\\\n\\u2028'''"),
(_u("\u2029"), "'\\u2029'", "'''\\\n\\u2029'''"),
# Unpaired surrogates are unprintable
(_u("\uD800"), "'\\ud800'", "'''\\\n\\ud800'''"),
(_u("\uDFFF"), "'\\udfff'", "'''\\\n\\udfff'''"),
# Unprintable general categories not fully tested: Cc, Cf, Co, Cn, Zs
)
b_prefix = repr(_b(""))[:-2]
u_prefix = repr(_u(""))[:-2]
def test_ascii_examples_oneline_bytes(self):
for s, expected, _ in self.ascii_examples:
b = _b(s)
actual = text_repr(b, multiline=False)
# Add self.assertIsInstance check?
self.assertEqual(actual, self.b_prefix + expected)
self.assertEqual(eval(actual), b)
def test_ascii_examples_oneline_unicode(self):
for s, expected, _ in self.ascii_examples:
u = _u(s)
actual = text_repr(u, multiline=False)
self.assertEqual(actual, self.u_prefix + expected)
self.assertEqual(eval(actual), u)
def test_ascii_examples_multiline_bytes(self):
for s, _, expected in self.ascii_examples:
b = _b(s)
actual = text_repr(b, multiline=True)
self.assertEqual(actual, self.b_prefix + expected)
self.assertEqual(eval(actual), b)
def test_ascii_examples_multiline_unicode(self):
for s, _, expected in self.ascii_examples:
u = _u(s)
actual = text_repr(u, multiline=True)
self.assertEqual(actual, self.u_prefix + expected)
self.assertEqual(eval(actual), u)
def test_ascii_examples_defaultline_bytes(self):
for s, one, multi in self.ascii_examples:
expected = "\n" in s and multi or one
self.assertEqual(text_repr(_b(s)), self.b_prefix + expected)
def test_ascii_examples_defaultline_unicode(self):
for s, one, multi in self.ascii_examples:
expected = "\n" in s and multi or one
self.assertEqual(text_repr(_u(s)), self.u_prefix + expected)
def test_bytes_examples_oneline(self):
for b, expected, _ in self.bytes_examples:
actual = text_repr(b, multiline=False)
self.assertEqual(actual, self.b_prefix + expected)
self.assertEqual(eval(actual), b)
def test_bytes_examples_multiline(self):
for b, _, expected in self.bytes_examples:
actual = text_repr(b, multiline=True)
self.assertEqual(actual, self.b_prefix + expected)
self.assertEqual(eval(actual), b)
def test_unicode_examples_oneline(self):
for u, expected, _ in self.unicode_examples:
actual = text_repr(u, multiline=False)
self.assertEqual(actual, self.u_prefix + expected)
self.assertEqual(eval(actual), u)
def test_unicode_examples_multiline(self):
for u, _, expected in self.unicode_examples:
actual = text_repr(u, multiline=True)
self.assertEqual(actual, self.u_prefix + expected)
self.assertEqual(eval(actual), u)
class TestReraise(testtools.TestCase):
"""Tests for trivial reraise wrapper needed for Python 2/3 changes"""
def test_exc_info(self):
"""After reraise exc_info matches plus some extra traceback"""
try:
raise ValueError("Bad value")
except ValueError:
_exc_info = sys.exc_info()
try:
reraise(*_exc_info)
except ValueError:
_new_exc_info = sys.exc_info()
self.assertIs(_exc_info[0], _new_exc_info[0])
self.assertIs(_exc_info[1], _new_exc_info[1])
expected_tb = traceback.extract_tb(_exc_info[2])
self.assertEqual(expected_tb,
traceback.extract_tb(_new_exc_info[2])[-len(expected_tb):])
def test_custom_exception_no_args(self):
"""Reraising does not require args attribute to contain params"""
class CustomException(Exception):
"""Exception that expects and sets attrs but not args"""
def __init__(self, value):
Exception.__init__(self)
self.value = value
try:
raise CustomException("Some value")
except CustomException:
_exc_info = sys.exc_info()
self.assertRaises(CustomException, reraise, *_exc_info)
class Python2CompatibilityTests(testtools.TestCase):
def setUp(self):
super(Python2CompatibilityTests, self).setUp()
if sys.version[0] >= '3':
self.skip("These tests are only applicable to python 2.")
class TestExceptionFormatting(Python2CompatibilityTests):
"""Test the _format_exception_only function."""
def _assert_exception_format(self, eclass, evalue, expected):
actual = _format_exception_only(eclass, evalue)
self.assertThat(actual, Equals(expected))
self.assertThat(''.join(actual), IsInstance(unicode))
def test_supports_string_exception(self):
self._assert_exception_format(
"String_Exception",
None,
[_u("String_Exception\n")]
)
def test_supports_regular_exception(self):
self._assert_exception_format(
RuntimeError,
RuntimeError("Something went wrong"),
[_u("RuntimeError: Something went wrong\n")]
)
def test_supports_unprintable_exceptions(self):
"""Verify support for exception classes that raise an exception when
__unicode__ or __str__ is called.
"""
class UnprintableException(Exception):
def __str__(self):
raise Exception()
def __unicode__(self):
raise Exception()
self._assert_exception_format(
UnprintableException,
UnprintableException("Foo"),
[_u("UnprintableException: <unprintable UnprintableException object>\n")]
)
def test_supports_exceptions_with_no_string_value(self):
class NoStringException(Exception):
def __str__(self):
return ""
def __unicode__(self):
return _u("")
self._assert_exception_format(
NoStringException,
NoStringException("Foo"),
[_u("NoStringException\n")]
)
def test_supports_strange_syntax_error(self):
"""Test support for syntax errors with unusual number of arguments"""
self._assert_exception_format(
SyntaxError,
SyntaxError("Message"),
[_u("SyntaxError: Message\n")]
)
def test_supports_syntax_error(self):
self._assert_exception_format(
SyntaxError,
SyntaxError(
"Some Syntax Message",
(
"/path/to/file",
12,
2,
"This is the line of code",
)
),
[
_u(' File "/path/to/file", line 12\n'),
_u(' This is the line of code\n'),
_u(' ^\n'),
_u('SyntaxError: Some Syntax Message\n'),
]
)
class StackListFormattingTests(Python2CompatibilityTests):
"""Test the _format_stack_list function."""
def _assert_stack_format(self, stack_lines, expected_output):
actual = _format_stack_list(stack_lines)
self.assertThat(actual, Equals([expected_output]))
def test_single_complete_stack_line(self):
stack_lines = [(
'/path/to/filename',
12,
'func_name',
'some_code()',
)]
expected = \
_u(' File "/path/to/filename", line 12, in func_name\n' \
' some_code()\n')
self._assert_stack_format(stack_lines, expected)
def test_single_stack_line_no_code(self):
stack_lines = [(
'/path/to/filename',
12,
'func_name',
None
)]
expected = _u(' File "/path/to/filename", line 12, in func_name\n')
self._assert_stack_format(stack_lines, expected)
class FormatExceptionInfoTests(Python2CompatibilityTests):
def test_individual_functions_called(self):
self.patch(
testtools.compat,
'_format_stack_list',
lambda stack_list: [_u("format stack list called\n")]
)
self.patch(
testtools.compat,
'_format_exception_only',
lambda etype, evalue: [_u("format exception only called\n")]
)
result = _format_exc_info(None, None, None)
expected = [
_u("Traceback (most recent call last):\n"),
_u("format stack list called\n"),
_u("format exception only called\n"),
]
self.assertThat(expected, Equals(result))
def test_suite():
from unittest import TestLoader
return TestLoader().loadTestsFromName(__name__)
|