;
Íâ"Ic               @   s@  d  Z  d d l m Z d d l m Z m Z m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z d d l m Z d d l m Z d d l Z d d l Z d d l Z d „  Z d „  Z d	 „  Z Gd
 „  d e ƒ Z Gd „  d e ƒ Z Gd „  d e ƒ Z Gd „  d e ƒ Z i e  d 6e d 6Z d „  Z e d k o e ƒ  n d S(   u6O  
Tests for the tokenize module.

The tests can be really simple. Given a small fragment of source
code, print out a table with tokens. The ENDMARK is omitted for
brevity.

    >>> dump_tokens("1 + 1")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '1'           (1, 0) (1, 1)
    OP         '+'           (1, 2) (1, 3)
    NUMBER     '1'           (1, 4) (1, 5)

    >>> dump_tokens("if False:\n"
    ...             "    # NL\n"
    ...             "    True = False # NEWLINE\n")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'if'          (1, 0) (1, 2)
    NAME       'False'       (1, 3) (1, 8)
    OP         ':'           (1, 8) (1, 9)
    NEWLINE    '\n'          (1, 9) (1, 10)
    COMMENT    '# NL'        (2, 4) (2, 8)
    NL         '\n'          (2, 8) (2, 9)
    INDENT     '    '        (3, 0) (3, 4)
    NAME       'True'        (3, 4) (3, 8)
    OP         '='           (3, 9) (3, 10)
    NAME       'False'       (3, 11) (3, 16)
    COMMENT    '# NEWLINE'   (3, 17) (3, 26)
    NEWLINE    '\n'          (3, 26) (3, 27)
    DEDENT     ''            (4, 0) (4, 0)

    >>> indent_error_file = """
    ... def k(x):
    ...     x += 2
    ...   x += 5
    ... """
    >>> readline = BytesIO(indent_error_file.encode('utf-8')).readline
    >>> for tok in tokenize(readline): pass
    Traceback (most recent call last):
        ...
    IndentationError: unindent does not match any outer indentation level

There are some standard formattig practises that are easy to get right.

    >>> roundtrip("if x == 1:\n"
    ...           "    print(x)\n")
    True

    >>> roundtrip("# This is a comment\n# This also")
    True

Some people use different formatting conventions, which makes
untokenize a little trickier. Note that this test involves trailing
whitespace after the colon. Note that we use hex escapes to make the
two trailing blanks apparent in the expected output.

    >>> roundtrip("if x == 1 : \n"
    ...           "  print(x)\n")
    True

    >>> f = support.findfile("tokenize_tests.txt")
    >>> roundtrip(open(f, 'rb'))
    True

    >>> roundtrip("if x == 1:\n"
    ...           "    # A comment by itself.\n"
    ...           "    print(x) # Comment here, too.\n"
    ...           "    # Another comment.\n"
    ...           "after_if = True\n")
    True

    >>> roundtrip("if (x # The comments need to go in the right place\n"
    ...           "    == 1):\n"
    ...           "    print('x==1')\n")
    True

    >>> roundtrip("class Test: # A comment here\n"
    ...           "  # A comment with weird indent\n"
    ...           "  after_com = 5\n"
    ...           "  def x(m): return m*5 # a one liner\n"
    ...           "  def y(m): # A whitespace after the colon\n"
    ...           "     return y*4 # 3-space indent\n")
    True

Some error-handling code

    >>> roundtrip("try: import somemodule\n"
    ...           "except ImportError: # comment\n"
    ...           "    print('Can not import' # comment2\n)"
    ...           "else:   print('Loaded')\n")
    True

Balancing continuation

    >>> roundtrip("a = (3,4, \n"
    ...           "5,6)\n"
    ...           "y = [3, 4,\n"
    ...           "5]\n"
    ...           "z = {'a': 5,\n"
    ...           "'b':15, 'c':True}\n"
    ...           "x = len(y) + 5 - a[\n"
    ...           "3] - a[2]\n"
    ...           "+ len(z) - z[\n"
    ...           "'b']\n")
    True

Ordinary integers and binary operators

    >>> dump_tokens("0xff <= 255")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '0xff'        (1, 0) (1, 4)
    OP         '<='          (1, 5) (1, 7)
    NUMBER     '255'         (1, 8) (1, 11)
    >>> dump_tokens("0b10 <= 255")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '0b10'        (1, 0) (1, 4)
    OP         '<='          (1, 5) (1, 7)
    NUMBER     '255'         (1, 8) (1, 11)
    >>> dump_tokens("0o123 <= 0O123")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '0o123'       (1, 0) (1, 5)
    OP         '<='          (1, 6) (1, 8)
    NUMBER     '0O123'       (1, 9) (1, 14)
    >>> dump_tokens("1234567 > ~0x15")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '1234567'     (1, 0) (1, 7)
    OP         '>'           (1, 8) (1, 9)
    OP         '~'           (1, 10) (1, 11)
    NUMBER     '0x15'        (1, 11) (1, 15)
    >>> dump_tokens("2134568 != 1231515")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '2134568'     (1, 0) (1, 7)
    OP         '!='          (1, 8) (1, 10)
    NUMBER     '1231515'     (1, 11) (1, 18)
    >>> dump_tokens("(-124561-1) & 200000000")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    OP         '('           (1, 0) (1, 1)
    OP         '-'           (1, 1) (1, 2)
    NUMBER     '124561'      (1, 2) (1, 8)
    OP         '-'           (1, 8) (1, 9)
    NUMBER     '1'           (1, 9) (1, 10)
    OP         ')'           (1, 10) (1, 11)
    OP         '&'           (1, 12) (1, 13)
    NUMBER     '200000000'   (1, 14) (1, 23)
    >>> dump_tokens("0xdeadbeef != -1")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '0xdeadbeef'  (1, 0) (1, 10)
    OP         '!='          (1, 11) (1, 13)
    OP         '-'           (1, 14) (1, 15)
    NUMBER     '1'           (1, 15) (1, 16)
    >>> dump_tokens("0xdeadc0de & 12345")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '0xdeadc0de'  (1, 0) (1, 10)
    OP         '&'           (1, 11) (1, 12)
    NUMBER     '12345'       (1, 13) (1, 18)
    >>> dump_tokens("0xFF & 0x15 | 1234")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NUMBER     '0xFF'        (1, 0) (1, 4)
    OP         '&'           (1, 5) (1, 6)
    NUMBER     '0x15'        (1, 7) (1, 11)
    OP         '|'           (1, 12) (1, 13)
    NUMBER     '1234'        (1, 14) (1, 18)

Long integers

    >>> dump_tokens("x = 0")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '0'           (1, 4) (1, 5)
    >>> dump_tokens("x = 0xfffffffffff")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '0xffffffffff (1, 4) (1, 17)
    >>> dump_tokens("x = 123141242151251616110")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '123141242151 (1, 4) (1, 25)
    >>> dump_tokens("x = -15921590215012591")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    OP         '-'           (1, 4) (1, 5)
    NUMBER     '159215902150 (1, 5) (1, 22)

Floating point numbers

    >>> dump_tokens("x = 3.14159")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '3.14159'     (1, 4) (1, 11)
    >>> dump_tokens("x = 314159.")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '314159.'     (1, 4) (1, 11)
    >>> dump_tokens("x = .314159")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '.314159'     (1, 4) (1, 11)
    >>> dump_tokens("x = 3e14159")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '3e14159'     (1, 4) (1, 11)
    >>> dump_tokens("x = 3E123")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '3E123'       (1, 4) (1, 9)
    >>> dump_tokens("x+y = 3e-1230")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '+'           (1, 1) (1, 2)
    NAME       'y'           (1, 2) (1, 3)
    OP         '='           (1, 4) (1, 5)
    NUMBER     '3e-1230'     (1, 6) (1, 13)
    >>> dump_tokens("x = 3.14e159")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '3.14e159'    (1, 4) (1, 12)

String literals

    >>> dump_tokens("x = ''; y = \"\"")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     "''"          (1, 4) (1, 6)
    OP         ';'           (1, 6) (1, 7)
    NAME       'y'           (1, 8) (1, 9)
    OP         '='           (1, 10) (1, 11)
    STRING     '""'          (1, 12) (1, 14)
    >>> dump_tokens("x = '\"'; y = \"'\"")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     '\'"\''       (1, 4) (1, 7)
    OP         ';'           (1, 7) (1, 8)
    NAME       'y'           (1, 9) (1, 10)
    OP         '='           (1, 11) (1, 12)
    STRING     '"\'"'        (1, 13) (1, 16)
    >>> dump_tokens("x = \"doesn't \"shrink\", does it\"")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     '"doesn\'t "' (1, 4) (1, 14)
    NAME       'shrink'      (1, 14) (1, 20)
    STRING     '", does it"' (1, 20) (1, 31)
    >>> dump_tokens("x = 'abc' + 'ABC'")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     "'abc'"       (1, 4) (1, 9)
    OP         '+'           (1, 10) (1, 11)
    STRING     "'ABC'"       (1, 12) (1, 17)
    >>> dump_tokens('y = "ABC" + "ABC"')
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'y'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     '"ABC"'       (1, 4) (1, 9)
    OP         '+'           (1, 10) (1, 11)
    STRING     '"ABC"'       (1, 12) (1, 17)
    >>> dump_tokens("x = r'abc' + r'ABC' + R'ABC' + R'ABC'")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     "r'abc'"      (1, 4) (1, 10)
    OP         '+'           (1, 11) (1, 12)
    STRING     "r'ABC'"      (1, 13) (1, 19)
    OP         '+'           (1, 20) (1, 21)
    STRING     "R'ABC'"      (1, 22) (1, 28)
    OP         '+'           (1, 29) (1, 30)
    STRING     "R'ABC'"      (1, 31) (1, 37)
    >>> dump_tokens('y = r"abc" + r"ABC" + R"ABC" + R"ABC"')
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'y'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    STRING     'r"abc"'      (1, 4) (1, 10)
    OP         '+'           (1, 11) (1, 12)
    STRING     'r"ABC"'      (1, 13) (1, 19)
    OP         '+'           (1, 20) (1, 21)
    STRING     'R"ABC"'      (1, 22) (1, 28)
    OP         '+'           (1, 29) (1, 30)
    STRING     'R"ABC"'      (1, 31) (1, 37)

Operators

    >>> dump_tokens("def d22(a, b, c=2, d=2, *k): pass")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'def'         (1, 0) (1, 3)
    NAME       'd22'         (1, 4) (1, 7)
    OP         '('           (1, 7) (1, 8)
    NAME       'a'           (1, 8) (1, 9)
    OP         ','           (1, 9) (1, 10)
    NAME       'b'           (1, 11) (1, 12)
    OP         ','           (1, 12) (1, 13)
    NAME       'c'           (1, 14) (1, 15)
    OP         '='           (1, 15) (1, 16)
    NUMBER     '2'           (1, 16) (1, 17)
    OP         ','           (1, 17) (1, 18)
    NAME       'd'           (1, 19) (1, 20)
    OP         '='           (1, 20) (1, 21)
    NUMBER     '2'           (1, 21) (1, 22)
    OP         ','           (1, 22) (1, 23)
    OP         '*'           (1, 24) (1, 25)
    NAME       'k'           (1, 25) (1, 26)
    OP         ')'           (1, 26) (1, 27)
    OP         ':'           (1, 27) (1, 28)
    NAME       'pass'        (1, 29) (1, 33)
    >>> dump_tokens("def d01v_(a=1, *k, **w): pass")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'def'         (1, 0) (1, 3)
    NAME       'd01v_'       (1, 4) (1, 9)
    OP         '('           (1, 9) (1, 10)
    NAME       'a'           (1, 10) (1, 11)
    OP         '='           (1, 11) (1, 12)
    NUMBER     '1'           (1, 12) (1, 13)
    OP         ','           (1, 13) (1, 14)
    OP         '*'           (1, 15) (1, 16)
    NAME       'k'           (1, 16) (1, 17)
    OP         ','           (1, 17) (1, 18)
    OP         '**'          (1, 19) (1, 21)
    NAME       'w'           (1, 21) (1, 22)
    OP         ')'           (1, 22) (1, 23)
    OP         ':'           (1, 23) (1, 24)
    NAME       'pass'        (1, 25) (1, 29)

Comparison

    >>> dump_tokens("if 1 < 1 > 1 == 1 >= 5 <= 0x15 <= 0x12 != " +
    ...             "1 and 5 in 1 not in 1 is 1 or 5 is not 1: pass")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'if'          (1, 0) (1, 2)
    NUMBER     '1'           (1, 3) (1, 4)
    OP         '<'           (1, 5) (1, 6)
    NUMBER     '1'           (1, 7) (1, 8)
    OP         '>'           (1, 9) (1, 10)
    NUMBER     '1'           (1, 11) (1, 12)
    OP         '=='          (1, 13) (1, 15)
    NUMBER     '1'           (1, 16) (1, 17)
    OP         '>='          (1, 18) (1, 20)
    NUMBER     '5'           (1, 21) (1, 22)
    OP         '<='          (1, 23) (1, 25)
    NUMBER     '0x15'        (1, 26) (1, 30)
    OP         '<='          (1, 31) (1, 33)
    NUMBER     '0x12'        (1, 34) (1, 38)
    OP         '!='          (1, 39) (1, 41)
    NUMBER     '1'           (1, 42) (1, 43)
    NAME       'and'         (1, 44) (1, 47)
    NUMBER     '5'           (1, 48) (1, 49)
    NAME       'in'          (1, 50) (1, 52)
    NUMBER     '1'           (1, 53) (1, 54)
    NAME       'not'         (1, 55) (1, 58)
    NAME       'in'          (1, 59) (1, 61)
    NUMBER     '1'           (1, 62) (1, 63)
    NAME       'is'          (1, 64) (1, 66)
    NUMBER     '1'           (1, 67) (1, 68)
    NAME       'or'          (1, 69) (1, 71)
    NUMBER     '5'           (1, 72) (1, 73)
    NAME       'is'          (1, 74) (1, 76)
    NAME       'not'         (1, 77) (1, 80)
    NUMBER     '1'           (1, 81) (1, 82)
    OP         ':'           (1, 82) (1, 83)
    NAME       'pass'        (1, 84) (1, 88)

Shift

    >>> dump_tokens("x = 1 << 1 >> 5")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '1'           (1, 4) (1, 5)
    OP         '<<'          (1, 6) (1, 8)
    NUMBER     '1'           (1, 9) (1, 10)
    OP         '>>'          (1, 11) (1, 13)
    NUMBER     '5'           (1, 14) (1, 15)

Additive

    >>> dump_tokens("x = 1 - y + 15 - 1 + 0x124 + z + a[5]")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '1'           (1, 4) (1, 5)
    OP         '-'           (1, 6) (1, 7)
    NAME       'y'           (1, 8) (1, 9)
    OP         '+'           (1, 10) (1, 11)
    NUMBER     '15'          (1, 12) (1, 14)
    OP         '-'           (1, 15) (1, 16)
    NUMBER     '1'           (1, 17) (1, 18)
    OP         '+'           (1, 19) (1, 20)
    NUMBER     '0x124'       (1, 21) (1, 26)
    OP         '+'           (1, 27) (1, 28)
    NAME       'z'           (1, 29) (1, 30)
    OP         '+'           (1, 31) (1, 32)
    NAME       'a'           (1, 33) (1, 34)
    OP         '['           (1, 34) (1, 35)
    NUMBER     '5'           (1, 35) (1, 36)
    OP         ']'           (1, 36) (1, 37)

Multiplicative

    >>> dump_tokens("x = 1//1*1/5*12%0x12")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'x'           (1, 0) (1, 1)
    OP         '='           (1, 2) (1, 3)
    NUMBER     '1'           (1, 4) (1, 5)
    OP         '//'          (1, 5) (1, 7)
    NUMBER     '1'           (1, 7) (1, 8)
    OP         '*'           (1, 8) (1, 9)
    NUMBER     '1'           (1, 9) (1, 10)
    OP         '/'           (1, 10) (1, 11)
    NUMBER     '5'           (1, 11) (1, 12)
    OP         '*'           (1, 12) (1, 13)
    NUMBER     '12'          (1, 13) (1, 15)
    OP         '%'           (1, 15) (1, 16)
    NUMBER     '0x12'        (1, 16) (1, 20)

Unary

    >>> dump_tokens("~1 ^ 1 & 1 |1 ^ -1")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    OP         '~'           (1, 0) (1, 1)
    NUMBER     '1'           (1, 1) (1, 2)
    OP         '^'           (1, 3) (1, 4)
    NUMBER     '1'           (1, 5) (1, 6)
    OP         '&'           (1, 7) (1, 8)
    NUMBER     '1'           (1, 9) (1, 10)
    OP         '|'           (1, 11) (1, 12)
    NUMBER     '1'           (1, 12) (1, 13)
    OP         '^'           (1, 14) (1, 15)
    OP         '-'           (1, 16) (1, 17)
    NUMBER     '1'           (1, 17) (1, 18)
    >>> dump_tokens("-1*1/1+1*1//1 - ---1**1")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    OP         '-'           (1, 0) (1, 1)
    NUMBER     '1'           (1, 1) (1, 2)
    OP         '*'           (1, 2) (1, 3)
    NUMBER     '1'           (1, 3) (1, 4)
    OP         '/'           (1, 4) (1, 5)
    NUMBER     '1'           (1, 5) (1, 6)
    OP         '+'           (1, 6) (1, 7)
    NUMBER     '1'           (1, 7) (1, 8)
    OP         '*'           (1, 8) (1, 9)
    NUMBER     '1'           (1, 9) (1, 10)
    OP         '//'          (1, 10) (1, 12)
    NUMBER     '1'           (1, 12) (1, 13)
    OP         '-'           (1, 14) (1, 15)
    OP         '-'           (1, 16) (1, 17)
    OP         '-'           (1, 17) (1, 18)
    OP         '-'           (1, 18) (1, 19)
    NUMBER     '1'           (1, 19) (1, 20)
    OP         '**'          (1, 20) (1, 22)
    NUMBER     '1'           (1, 22) (1, 23)

Selector

    >>> dump_tokens("import sys, time\nx = sys.modules['time'].time()")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    NAME       'import'      (1, 0) (1, 6)
    NAME       'sys'         (1, 7) (1, 10)
    OP         ','           (1, 10) (1, 11)
    NAME       'time'        (1, 12) (1, 16)
    NEWLINE    '\n'          (1, 16) (1, 17)
    NAME       'x'           (2, 0) (2, 1)
    OP         '='           (2, 2) (2, 3)
    NAME       'sys'         (2, 4) (2, 7)
    OP         '.'           (2, 7) (2, 8)
    NAME       'modules'     (2, 8) (2, 15)
    OP         '['           (2, 15) (2, 16)
    STRING     "'time'"      (2, 16) (2, 22)
    OP         ']'           (2, 22) (2, 23)
    OP         '.'           (2, 23) (2, 24)
    NAME       'time'        (2, 24) (2, 28)
    OP         '('           (2, 28) (2, 29)
    OP         ')'           (2, 29) (2, 30)

Methods

    >>> dump_tokens("@staticmethod\ndef foo(x,y): pass")
    ENCODING   'utf-8'       (0, 0) (0, 0)
    OP         '@'           (1, 0) (1, 1)
    NAME       'staticmethod (1, 1) (1, 13)
    NEWLINE    '\n'          (1, 13) (1, 14)
    NAME       'def'         (2, 0) (2, 3)
    NAME       'foo'         (2, 4) (2, 7)
    OP         '('           (2, 7) (2, 8)
    NAME       'x'           (2, 8) (2, 9)
    OP         ','           (2, 9) (2, 10)
    NAME       'y'           (2, 10) (2, 11)
    OP         ')'           (2, 11) (2, 12)
    OP         ':'           (2, 12) (2, 13)
    NAME       'pass'        (2, 14) (2, 18)

Backslash means line continuation, except for comments

    >>> roundtrip("x=1+\\n"
    ...           "1\n"
    ...           "# This is a comment\\n"
    ...           "# This also\n")
    True
    >>> roundtrip("# Comment \\nx = 0")
    True

Two string literals on the same line

    >>> roundtrip("'' ''")
    True

Test roundtrip on random python modules.
pass the '-ucompiler' option to process the full directory.

    >>> import random
    >>> tempdir = os.path.dirname(f) or os.curdir
    >>> testfiles = glob.glob(os.path.join(tempdir, "test*.py"))

    >>> if not support.is_resource_enabled("compiler"):
    ...     testfiles = random.sample(testfiles, 10)
    ...
    >>> for testfile in testfiles:
    ...     if not roundtrip(open(testfile, 'rb')):
    ...         print("Roundtrip failed for file %s" % testfile)
    ...         break
    ... else: True
    True
i    (   u   support(
   u   tokenizeu	   _tokenizeu
   untokenizeu   NUMBERu   NAMEu   OPu   STRINGu	   ENDMARKERu   tok_nameu   detect_encoding(   u   BytesIO(   u   TestCaseNc             C   so   t  |  j d ƒ ƒ } xS t | j ƒ D]B \ } } } } } | t k o Pn t | } t d t ƒ  ƒ q% Wd S(   uP   Print out the tokens in s in a table format.

    The ENDMARKER is omitted.
    u   utf-8u0   %(type)-10.10s %(token)-13.13r %(start)s %(end)sN(   u   BytesIOu   encodeu   tokenizeu   readlineu	   ENDMARKERu   tok_nameu   printu   locals(   u   su   fu   typeu   tokenu   startu   endu   line(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   dump_tokens  s     
c             C   s    t  |  t ƒ o t |  j d ƒ ƒ }  n t t |  j ƒ ƒ } |  j ƒ  d „  | Dƒ } t | ƒ } d „  | j	 d ƒ Dƒ j
 } d „  t | ƒ Dƒ } | | k S(   u  
    Test roundtrip for `untokenize`. `f` is an open file or a string.
    The source code in f is tokenized, converted back to source code via
    tokenize.untokenize(), and tokenized again from the latter. The test
    fails if the second tokenization doesn't match the first.
    u   utf-8c             S   s%   g  } |  ] } | | d  d … q
 S(   Ni   (    (   u   .0u   _[1]u   tok(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu
   <listcomp>6  s    c             s   s   |  ] } | Vq d  S(   N(    (   u   .0u   line(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu	   <genexpr>8  s    i   c             S   s%   g  } |  ] } | | d  d … q
 S(   Ni   (    (   u   .0u   _[1]u   tok(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu
   <listcomp>9  s    (   u
   isinstanceu   stru   BytesIOu   encodeu   listu   tokenizeu   readlineu   closeu
   untokenizeu
   splitlinesu   __next__(   u   fu
   token_listu   tokens1u	   new_bytesu   readlineu   tokens2(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu	   roundtrip+  s    
c             C   s¿   g  } t  t |  j d ƒ ƒ j ƒ } x… | D]} \ } } } } } | t k oH d | k o; | j t d f t d f t t	 | ƒ f t d f g ƒ q+ | j
 | | f ƒ q+ Wt | ƒ j d ƒ S(   uÎ  Substitute Decimals for floats in a string of statements.

    >>> from decimal import Decimal
    >>> s = 'print(+21.3e-5*-.1234/81.7)'
    >>> decistmt(s)
    "print (+Decimal ('21.3e-5')*-Decimal ('.1234')/Decimal ('81.7'))"

    The format of the exponent is inherited from the platform C library.
    Known cases are "e-007" (Windows) and "e-07" (not Windows).  Since
    we're only showing 12 digits, and the 13th isn't close to 5, the
    rest of the output should be platform-independent.

    >>> exec(s) #doctest: +ELLIPSIS
    -3.21716034272e-0...7

    Output from calculations with Decimal should be identical across all
    platforms.

    >>> exec(decistmt(s))
    -3.217160342717258261933904529E-7
    u   utf-8u   .u   Decimalu   (u   )(   u   tokenizeu   BytesIOu   encodeu   readlineu   NUMBERu   extendu   NAMEu   OPu   STRINGu   repru   appendu
   untokenizeu   decode(   u   su   resultu   gu   toknumu   tokvalu   _(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   decistmt=  s     		c             B   sA   |  Ee  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   uU   
    Test that tokenizer adheres to the coding behaviour stipulated in PEP 0263.
    c             C   s4   t  j j t  j j t ƒ | ƒ } t t | d ƒ ƒ S(   Nu   rb(   u   osu   pathu   joinu   dirnameu   __file__u	   roundtripu   open(   u   selfu   filenameu   path(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu	   _testFileg  s    !c             C   s    d } |  j  |  j | ƒ ƒ d  S(   Nu6   tokenize_tests-utf8-coding-cookie-and-utf8-bom-sig.txt(   u
   assertTrueu	   _testFile(   u   selfu   f(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu'   test_utf8_coding_cookie_and_no_utf8_bomk  s    c             C   s    d } |  j  t |  j | ƒ d S(   u†  
        As per PEP 0263, if a file starts with a utf-8 BOM signature, the only
        allowed encoding for the comment is 'utf-8'.  The text file used in
        this test starts with a BOM signature, but specifies latin1 as the
        coding, so verify that a SyntaxError is raised, which matches the
        behaviour of the interpreter when it encounters a similar condition.
        u8   tokenize_tests-latin1-coding-cookie-and-utf8-bom-sig.txtN(   u   failUnlessRaisesu   SyntaxErroru	   _testFile(   u   selfu   f(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu&   test_latin1_coding_cookie_and_utf8_bomo  s    c             C   s    d } |  j  |  j | ƒ ƒ d  S(   Nu9   tokenize_tests-no-coding-cookie-and-utf8-bom-sig-only.txt(   u
   assertTrueu	   _testFile(   u   selfu   f(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu"   test_no_coding_cookie_and_utf8_bomz  s    c             C   s    d } |  j  |  j | ƒ ƒ d  S(   Nu6   tokenize_tests-utf8-coding-cookie-and-utf8-bom-sig.txt(   u
   assertTrueu	   _testFile(   u   selfu   f(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu$   test_utf8_coding_cookie_and_utf8_bom~  s    N(   u   __name__u
   __module__u   __doc__u	   _testFileu'   test_utf8_coding_cookie_and_no_utf8_bomu&   test_latin1_coding_cookie_and_utf8_bomu"   test_no_coding_cookie_and_utf8_bomu$   test_utf8_coding_cookie_and_utf8_bom(   u
   __locals__(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   TestTokenizerAdheresToPep0263b  s   
				u   TestTokenizerAdheresToPep0263c             B   s    |  Ee  Z d  „  Z d „  Z d S(   c                s~   d } | j  d ƒ ‰  d ‰ ‡  ‡ f d †  } t t | d d ƒƒ d d … } d d d d d f g } |  j | | d
 ƒ d  S(   Nu   "Ð‰ÐŠÐˆÐÐ‚"u   utf-8c                  s   ˆ p d ‰ ˆ  Sd Sd  S(   Ns    T(   u   True(    (   u   lineu   first(    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   readline‰  s    u   encodingi   iÿÿÿÿi   i    i   u   bytes not decoded with encodingF(   i   i    (   i   i   (   u   encodeu   Falseu   listu	   _tokenizeu   assertEquals(   u   selfu   literalu   readlineu   tokensu   expected_tokens(    (   u   lineu   firstu/   /mit/python/lib/python3.0/test/test_tokenize.pyu.   test__tokenize_decodes_with_specified_encoding…  s    	"c                so   d ‰  d
 ‰ ‡  ‡ f d †  } t t | d d  ƒƒ d  d … } d d d d d f g } |  j | | d	 ƒ d  S(   Nu   "Ð‰ÐŠÐˆÐÐ‚"c                  s   ˆ p d ‰ ˆ  Sd Sd  S(   Ns    T(   u   True(    (   u   literalu   first(    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   readlineš  s    u   encodingiÿÿÿÿi   i   i    i   u*   string not tokenized when encoding is NoneF(   i   i    (   i   i   (   u   Falseu   listu	   _tokenizeu   Noneu   assertEquals(   u   selfu   readlineu   tokensu   expected_tokens(    (   u   literalu   firstu/   /mit/python/lib/python3.0/test/test_tokenize.pyu1   test__tokenize_does_not_decode_with_encoding_none—  s    	"N(   u   __name__u
   __module__u.   test__tokenize_decodes_with_specified_encodingu1   test__tokenize_does_not_decode_with_encoding_none(   u
   __locals__(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   Test_Tokenizeƒ  s   
	u   Test_Tokenizec             B   sh   |  Ee  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 S(   c                s   d ‰  ‡  ‡ f d †  } | S(   Ni    c                 s5   ˆ  t  ˆ ƒ k o
 t ‚ n ˆ ˆ  }  ˆ  d 7‰  |  S(   Ni   (   u   lenu   StopIteration(   u   line(   u   indexu   lines(    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   readline­  s
    


(    (   u   selfu   linesu   readline(    (   u   indexu   linesu/   /mit/python/lib/python3.0/test/test_tokenize.pyu   get_readline«  s    c             C   sU   d } t  |  j | ƒ ƒ \ } } |  j | d ƒ |  j | t | d  d … ƒ ƒ d  S(   Ns   # something
s   print(something)
s   do_something(else)
u   utf-8i   (   s   # something
s   print(something)
s   do_something(else)
(   u   detect_encodingu   get_readlineu   assertEqualsu   list(   u   selfu   linesu   encodingu   consumed_lines(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   test_no_bom_no_encoding_cookie¶  s      c             C   sK   d } t  |  j | ƒ ƒ \ } } |  j | d ƒ |  j | d d g ƒ d  S(   Ns   ï»¿# something
s   print(something)
s   do_something(else)
u   utf-8s   # something
(   s   ï»¿# something
s   print(something)
s   do_something(else)
(   u   detect_encodingu   get_readlineu   assertEquals(   u   selfu   linesu   encodingu   consumed_lines(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   test_bom_no_cookieÀ  s      	c             C   sH   d } t  |  j | ƒ ƒ \ } } |  j | d ƒ |  j | d g ƒ d  S(   Ns   # -*- coding: latin-1 -*-
s   print(something)
s   do_something(else)
u   latin-1(   s   # -*- coding: latin-1 -*-
s   print(something)
s   do_something(else)
(   u   detect_encodingu   get_readlineu   assertEquals(   u   selfu   linesu   encodingu   consumed_lines(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   test_cookie_first_line_no_bomË  s      c             C   sH   d } t  |  j | ƒ ƒ \ } } |  j | d ƒ |  j | d g ƒ d  S(   Ns   ï»¿# coding=utf-8
s   print(something)
s   do_something(else)
u   utf-8s   # coding=utf-8
(   s   ï»¿# coding=utf-8
s   print(something)
s   do_something(else)
(   u   detect_encodingu   get_readlineu   assertEquals(   u   selfu   linesu   encodingu   consumed_lines(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu&   test_matched_bom_and_cookie_first_lineÕ  s      c             C   s,   d } |  j  | ƒ } |  j t t | ƒ d  S(   Ns#   ï»¿# vim: set fileencoding=ascii :
s   print(something)
s   do_something(else)
(   s#   ï»¿# vim: set fileencoding=ascii :
s   print(something)
s   do_something(else)
(   u   get_readlineu   assertRaisesu   SyntaxErroru   detect_encoding(   u   selfu   linesu   readline(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu<   test_mismatched_bom_and_cookie_first_line_raises_syntaxerrorß  s
      c             C   sQ   d } t  |  j | ƒ ƒ \ } } |  j | d ƒ d d g } |  j | | ƒ d  S(   Ns   #! something
s    # vim: set fileencoding=ascii :
s   print(something)
s   do_something(else)
u   ascii(   s   #! something
s    # vim: set fileencoding=ascii :
s   print(something)
s   do_something(else)
(   u   detect_encodingu   get_readlineu   assertEquals(   u   selfu   linesu   encodingu   consumed_linesu   expected(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   test_cookie_second_line_no_bomè  s       c             C   sK   d } t  |  j | ƒ ƒ \ } } |  j | d ƒ |  j | d d g ƒ d  S(   Ns   ï»¿#! something
s   f# coding=utf-8
s   print(something)
s   do_something(else)
u   utf-8s   #! something
(   s   ï»¿#! something
s   f# coding=utf-8
s   print(something)
s   do_something(else)
(   u   detect_encodingu   get_readlineu   assertEquals(   u   selfu   linesu   encodingu   consumed_lines(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu'   test_matched_bom_and_cookie_second_lineô  s       	c             C   s,   d } |  j  | ƒ } |  j t t | ƒ d  S(   Ns   ï»¿#! something
s    # vim: set fileencoding=ascii :
s   print(something)
s   do_something(else)
(   s   ï»¿#! something
s    # vim: set fileencoding=ascii :
s   print(something)
s   do_something(else)
(   u   get_readlineu   assertRaisesu   SyntaxErroru   detect_encoding(   u   selfu   linesu   readline(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu=   test_mismatched_bom_and_cookie_second_line_raises_syntaxerror   s       c             C   s  |  j  d ƒ } t | ƒ \ } } |  j | d ƒ |  j | d g ƒ t |  j  d ƒ ƒ \ } } |  j | d ƒ |  j | g  ƒ |  j  d ƒ } t | ƒ \ } } |  j | d ƒ |  j | d g ƒ |  j  d ƒ } t | ƒ \ } } |  j | d ƒ |  j | g  ƒ d  S(	   Ns   print(something)
u   utf-8s   ï»¿print(something)
s   ï»¿(   s   print(something)
(    (   s   ï»¿print(something)
(   s   ï»¿(   u   get_readlineu   detect_encodingu   assertEquals(   u   selfu   readlineu   encodingu   consumed_lines(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   test_short_files
  s    N(   u   __name__u
   __module__u   get_readlineu   test_no_bom_no_encoding_cookieu   test_bom_no_cookieu   test_cookie_first_line_no_bomu&   test_matched_bom_and_cookie_first_lineu<   test_mismatched_bom_and_cookie_first_line_raises_syntaxerroru   test_cookie_second_line_no_bomu'   test_matched_bom_and_cookie_second_lineu=   test_mismatched_bom_and_cookie_second_line_raises_syntaxerroru   test_short_files(   u
   __locals__(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   TestDetectEncoding©  s   
		
		
	
					
u   TestDetectEncodingc             B   s   |  Ee  Z d  „  Z d S(   c                sÔ   d d  l  } t ƒ  ‰ d  ‰ ‡ f d †  } ‡ f d †  } d ‰  ‡  f d †  } | j } | j } | | _ | | _ z8 t  | ƒ } |  j t | ƒ d d d d d	 d
 g ƒ Wd  | | _ | | _ X|  j ˆ ˆ ƒ d  S(   Ni    c                s   ˆ  d d g f S(   Nu   firstu   second(    (   u   readline(   u   encoding(    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   mock_detect_encoding%  s    c                s;   | ‰  g  } x( |  ƒ  } | o | j  | ƒ q n | Sd  S(   N(   u   append(   u   readlineu   encodingu   outu	   next_line(   u   encoding_used(    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   mock__tokenize(  s    	c                  s    ˆ  d 7‰  ˆ  d k o d Sˆ  S(   Ni   i   s    (    (    (   u   counter(    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   mock_readline4  s    
u   firstu   secondi   i   i   i   (   u   tokenizeu   objectu   Noneu   detect_encodingu	   _tokenizeu   assertEqualsu   listu
   assertTrue(   u   selfu   tokenize_moduleu   mock_detect_encodingu   mock__tokenizeu   mock_readlineu   orig_detect_encodingu   orig__tokenizeu   results(    (   u   counteru   encodingu   encoding_usedu/   /mit/python/lib/python3.0/test/test_tokenize.pyu   test_tokenize!  s"    					,	
N(   u   __name__u
   __module__u   test_tokenize(   u
   __locals__(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   TestTokenize  s   
u   TestTokenizeu   doctestsu   decistmtc              C   sX   d d l  m }  t j |  d ƒ t j t ƒ t j t ƒ t j t ƒ t j t	 ƒ d  S(   Ni    (   u   test_tokenizeT(
   u   testu   test_tokenizeu   supportu   run_doctestu   Trueu   run_unittestu   TestTokenizerAdheresToPep0263u   Test_Tokenizeu   TestDetectEncodingu   TestTokenize(   u   test_tokenize(    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu	   test_mainK  s    u   __main__(   u   doctestsu   testu   supportu   tokenizeu	   _tokenizeu
   untokenizeu   NUMBERu   NAMEu   OPu   STRINGu	   ENDMARKERu   tok_nameu   detect_encodingu   iou   BytesIOu   unittestu   TestCaseu   osu   sysu   globu   dump_tokensu	   roundtripu   decistmtu   TestTokenizerAdheresToPep0263u   Test_Tokenizeu   TestDetectEncodingu   TestTokenizeu   __test__u	   test_mainu   __name__(    (    (    u/   /mit/python/lib/python3.0/test/test_tokenize.pyu   <module>  s    F$			%!&v*	