token-utils 0.3.4__tar.gz → 0.3.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {token_utils-0.3.4 → token_utils-0.3.5}/PKG-INFO +1 -1
- {token_utils-0.3.4 → token_utils-0.3.5}/pyproject.toml +3 -2
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/__about__.py +1 -1
- token_utils-0.3.5/src/token_utils/py.typed +0 -0
- token_utils-0.3.4/docs/source/conf.py +0 -176
- token_utils-0.3.4/docs_examples/add_decorator.py +0 -18
- token_utils-0.3.4/docs_examples/decimal_py.py +0 -19
- token_utils-0.3.4/docs_examples/decimal_tok.py +0 -11
- token_utils-0.3.4/docs_examples/decimal_tok2.py +0 -14
- token_utils-0.3.4/docs_examples/factorial_1.py +0 -22
- token_utils-0.3.4/docs_examples/factorial_2.py +0 -40
- token_utils-0.3.4/docs_examples/inside_blocks.py +0 -21
- token_utils-0.3.4/docs_examples/nobreak.py +0 -22
- token_utils-0.3.4/docs_examples/view_stack.py +0 -16
- token_utils-0.3.4/tests/__init__.py +0 -1
- token_utils-0.3.4/tests/test_roundtrip.py +0 -167
- token_utils-0.3.4/tests/test_token_methods.py +0 -270
- token_utils-0.3.4/tests/test_utils.py +0 -111
- {token_utils-0.3.4 → token_utils-0.3.5}/.gitignore +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/LICENSE +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/README.md +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/__about__.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/__init__.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/__init__.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/_py_tokenize.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/_py_tokenize.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/_token_plus.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/_token_plus.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/custom_warnings.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/custom_warnings.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/token_class.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/token_class.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/tokenizing.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/tokenizing.pyi +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/utils.py +0 -0
- {token_utils-0.3.4 → token_utils-0.3.5}/src/token_utils/utils.pyi +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: token-utils
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.5
|
|
4
4
|
Summary: Convenient tools to deal with tokens.
|
|
5
5
|
Project-URL: Documentation, https://aroberge.github.io/token-utils/docs/html/
|
|
6
6
|
Project-URL: Issues, https://github.com/aroberge/token-utils/issues
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
version = "0.3.
|
|
1
|
+
version = "0.3.5"
|
|
2
2
|
name = "token_utils"
|
|
File without changes
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
#
|
|
4
|
-
# token-utils documentation build configuration file, created by
|
|
5
|
-
# sphinx-quickstart on Thu Jul 12 10:12:48 2018.
|
|
6
|
-
#
|
|
7
|
-
# This file is execfile()d with the current directory set to its
|
|
8
|
-
# containing dir.
|
|
9
|
-
#
|
|
10
|
-
# Note that not all possible configuration values are present in this
|
|
11
|
-
# autogenerated file.
|
|
12
|
-
#
|
|
13
|
-
# All configuration values have a default; values that are commented out
|
|
14
|
-
# serve to show the default.
|
|
15
|
-
|
|
16
|
-
# If extensions (or modules to document with autodoc) are in another directory,
|
|
17
|
-
# add these directories to sys.path here. If the directory is relative to the
|
|
18
|
-
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
|
19
|
-
#
|
|
20
|
-
import os
|
|
21
|
-
import sys
|
|
22
|
-
|
|
23
|
-
sys.path.insert(0, os.path.abspath("../../src"))
|
|
24
|
-
from token_utils import version
|
|
25
|
-
|
|
26
|
-
# -- General configuration ------------------------------------------------
|
|
27
|
-
|
|
28
|
-
# If your documentation needs a minimal Sphinx version, state it here.
|
|
29
|
-
#
|
|
30
|
-
# needs_sphinx = '1.0'
|
|
31
|
-
|
|
32
|
-
# Add any Sphinx extension module names here, as strings. They can be
|
|
33
|
-
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
|
34
|
-
# ones.
|
|
35
|
-
extensions = [
|
|
36
|
-
"sphinx.ext.autodoc",
|
|
37
|
-
"sphinx.ext.todo",
|
|
38
|
-
"sphinx.ext.viewcode",
|
|
39
|
-
"sphinx.ext.githubpages",
|
|
40
|
-
"sphinx.ext.mathjax",
|
|
41
|
-
"sphinx_new_tab_link",
|
|
42
|
-
]
|
|
43
|
-
|
|
44
|
-
# Add any paths that contain templates here, relative to this directory.
|
|
45
|
-
templates_path = ["_templates"]
|
|
46
|
-
|
|
47
|
-
# The suffix(es) of source filenames.
|
|
48
|
-
# You can specify multiple suffix as a list of string:
|
|
49
|
-
#
|
|
50
|
-
# source_suffix = ['.rst', '.md']
|
|
51
|
-
source_suffix = {".rst": "restructuredtext"}
|
|
52
|
-
|
|
53
|
-
# The master toctree document.
|
|
54
|
-
master_doc = "index"
|
|
55
|
-
|
|
56
|
-
# General information about the project.
|
|
57
|
-
project = "token-utils"
|
|
58
|
-
copyright = "2026, André Roberge"
|
|
59
|
-
author = "André Roberge"
|
|
60
|
-
|
|
61
|
-
# The version info for the project you're documenting, acts as replacement for
|
|
62
|
-
# |version| and |release|, also used in various other places throughout the
|
|
63
|
-
# built documents.
|
|
64
|
-
#
|
|
65
|
-
# The short X.Y version.
|
|
66
|
-
version = version
|
|
67
|
-
# The full version, including alpha/beta/rc tags.
|
|
68
|
-
release = version
|
|
69
|
-
|
|
70
|
-
# The language for content autogenerated by Sphinx. Refer to documentation
|
|
71
|
-
# for a list of supported languages.
|
|
72
|
-
#
|
|
73
|
-
# This is also used if you do content translation via gettext catalogs.
|
|
74
|
-
# Usually you set "language" from the command line for these cases.
|
|
75
|
-
language = "en"
|
|
76
|
-
|
|
77
|
-
# List of patterns, relative to source directory, that match files and
|
|
78
|
-
# directories to ignore when looking for source files.
|
|
79
|
-
# This patterns also effect to html_static_path and html_extra_path
|
|
80
|
-
exclude_patterns = ["_build"]
|
|
81
|
-
nbsphinx_allow_errors = True
|
|
82
|
-
|
|
83
|
-
# The name of the Pygments (syntax highlighting) style to use.
|
|
84
|
-
pygments_style = "sphinx"
|
|
85
|
-
|
|
86
|
-
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
|
87
|
-
todo_include_todos = True
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
# -- Options for HTML output ----------------------------------------------
|
|
91
|
-
|
|
92
|
-
# The theme to use for HTML and HTML Help pages. See the documentation for
|
|
93
|
-
# a list of builtin themes.
|
|
94
|
-
#
|
|
95
|
-
html_theme = "sphinx_rtd_theme" # installed separately
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
# Theme options are theme-specific and customize the look and feel of a theme
|
|
99
|
-
# further. For a list of options available for each theme, see the
|
|
100
|
-
# documentation.
|
|
101
|
-
#
|
|
102
|
-
html_theme_options = {
|
|
103
|
-
"prev_next_buttons_location": "both",
|
|
104
|
-
"style_external_links": True,
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
# Add any paths that contain custom static files (such as style sheets) here,
|
|
108
|
-
# relative to this directory. They are copied after the builtin static files,
|
|
109
|
-
# so a file named "default.css" will overwrite the builtin "default.css".
|
|
110
|
-
html_static_path = ["_static"]
|
|
111
|
-
|
|
112
|
-
html_css_files = [
|
|
113
|
-
"custom.css",
|
|
114
|
-
]
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
# -- Options for HTMLHelp output ------------------------------------------
|
|
118
|
-
|
|
119
|
-
# Output file base name for HTML help builder.
|
|
120
|
-
htmlhelp_basename = "token-utils-doc"
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
# -- Options for LaTeX output ---------------------------------------------
|
|
124
|
-
|
|
125
|
-
latex_elements = {
|
|
126
|
-
# The paper size ('letterpaper' or 'a4paper').
|
|
127
|
-
#
|
|
128
|
-
# 'papersize': 'letterpaper',
|
|
129
|
-
# The font size ('10pt', '11pt' or '12pt').
|
|
130
|
-
#
|
|
131
|
-
# 'pointsize': '10pt',
|
|
132
|
-
# Additional stuff for the LaTeX preamble.
|
|
133
|
-
#
|
|
134
|
-
# 'preamble': '',
|
|
135
|
-
# Latex figure (float) alignment
|
|
136
|
-
#
|
|
137
|
-
# 'figure_align': 'htbp',
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
# Grouping the document tree into LaTeX files. List of tuples
|
|
141
|
-
# (source start file, target name, title,
|
|
142
|
-
# author, documentclass [howto, manual, or own class]).
|
|
143
|
-
latex_documents = [
|
|
144
|
-
(
|
|
145
|
-
master_doc,
|
|
146
|
-
"token-utils.tex",
|
|
147
|
-
"token-utils Documentation",
|
|
148
|
-
"André Roberge",
|
|
149
|
-
"manual",
|
|
150
|
-
)
|
|
151
|
-
]
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
# -- Options for manual page output ---------------------------------------
|
|
155
|
-
|
|
156
|
-
# One entry per manual page. List of tuples
|
|
157
|
-
# (source start file, name, description, authors, manual section).
|
|
158
|
-
man_pages = [(master_doc, "token-utils", "token-utils Documentation", [author], 1)]
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
# -- Options for Texinfo output -------------------------------------------
|
|
162
|
-
|
|
163
|
-
# Grouping the document tree into Texinfo files. List of tuples
|
|
164
|
-
# (source start file, target name, title, author,
|
|
165
|
-
# dir menu entry, description, category)
|
|
166
|
-
texinfo_documents = [
|
|
167
|
-
(
|
|
168
|
-
master_doc,
|
|
169
|
-
"token-utils",
|
|
170
|
-
"token-utils Documentation",
|
|
171
|
-
author,
|
|
172
|
-
"token-utils",
|
|
173
|
-
"One line description of project.",
|
|
174
|
-
"Miscellaneous",
|
|
175
|
-
)
|
|
176
|
-
]
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# add_decorator.py
|
|
2
|
-
|
|
3
|
-
from token_utils import get_logical_lines, IndentStack, untokenize, stringify
|
|
4
|
-
|
|
5
|
-
with open("decorator_example.txt", "r") as f:
|
|
6
|
-
source = f.read()
|
|
7
|
-
|
|
8
|
-
stack = IndentStack()
|
|
9
|
-
new_lines = []
|
|
10
|
-
for line in get_logical_lines(source):
|
|
11
|
-
stack.update(line)
|
|
12
|
-
first_token = line[0]
|
|
13
|
-
if first_token == "def" and not stack.is_token_in_named_block(first_token, "class"):
|
|
14
|
-
new_lines.append([" " * first_token.indentation() + "@decorator\n"])
|
|
15
|
-
|
|
16
|
-
new_lines.append(line)
|
|
17
|
-
|
|
18
|
-
print(stringify(new_lines))
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# decimal_py.py
|
|
2
|
-
#
|
|
3
|
-
# Example taken from Python's tokenize module documentation
|
|
4
|
-
|
|
5
|
-
from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
|
|
6
|
-
from io import BytesIO
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def float_to_decimal(s): # changed name from Python's example
|
|
10
|
-
result = []
|
|
11
|
-
g = tokenize(BytesIO(s.encode("utf-8")).readline) # tokenize the string
|
|
12
|
-
for toknum, tokval, _, _, _ in g:
|
|
13
|
-
if toknum == NUMBER and "." in tokval: # replace NUMBER tokens
|
|
14
|
-
result.extend(
|
|
15
|
-
[(NAME, "Decimal"), (OP, "("), (STRING, repr(tokval)), (OP, ")")]
|
|
16
|
-
)
|
|
17
|
-
else:
|
|
18
|
-
result.append((toknum, tokval))
|
|
19
|
-
return untokenize(result).decode("utf-8")
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# decimal_tok2.py
|
|
2
|
-
|
|
3
|
-
from token_utils import generate_tokens, untokenize
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def float_to_decimal(source):
|
|
7
|
-
new_tokens = ["from decimal import Decimal\n\n"]
|
|
8
|
-
for token in generate_tokens(source):
|
|
9
|
-
if token.is_float():
|
|
10
|
-
token.string = f"Decimal('{token.string}')"
|
|
11
|
-
elif token.is_complex():
|
|
12
|
-
token.string = "0.0"
|
|
13
|
-
new_tokens.append(token)
|
|
14
|
-
return untokenize(new_tokens)
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# factorial_1.py
|
|
2
|
-
|
|
3
|
-
from token_utils import tokenize, untokenize, add_operator, pairwise
|
|
4
|
-
|
|
5
|
-
add_operator("!", "exclamation")
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def make_factorial(source):
|
|
9
|
-
tokens = tokenize(source)
|
|
10
|
-
new_tokens = []
|
|
11
|
-
|
|
12
|
-
for token, next_ in pairwise(tokens):
|
|
13
|
-
if (
|
|
14
|
-
next_ == "!"
|
|
15
|
-
and token.is_immediately_before(next_)
|
|
16
|
-
and (token.is_integer() or token.is_identifier())
|
|
17
|
-
):
|
|
18
|
-
token.string = f"factorial({token.string})"
|
|
19
|
-
next_.string = ""
|
|
20
|
-
new_tokens.append(token)
|
|
21
|
-
|
|
22
|
-
return untokenize(new_tokens)
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# factorial_2.py
|
|
2
|
-
|
|
3
|
-
from token_utils import (
|
|
4
|
-
tokenize,
|
|
5
|
-
untokenize,
|
|
6
|
-
add_operator,
|
|
7
|
-
pairwise,
|
|
8
|
-
BracketStack,
|
|
9
|
-
split_at_token,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
add_operator("!", "exclamation")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def make_factorial(source):
|
|
16
|
-
tokens = tokenize(source)
|
|
17
|
-
stack = BracketStack()
|
|
18
|
-
new_tokens = []
|
|
19
|
-
|
|
20
|
-
for token, next_ in pairwise(tokens):
|
|
21
|
-
if (
|
|
22
|
-
next_ == "!"
|
|
23
|
-
and token.is_immediately_before(next_)
|
|
24
|
-
and (token.is_integer() or token.is_identifier())
|
|
25
|
-
):
|
|
26
|
-
token.string = f"factorial({token.string})"
|
|
27
|
-
next_.string = ""
|
|
28
|
-
elif next_ == "!" and token.is_immediately_before(next_) and token == ")":
|
|
29
|
-
matching_bracket = stack.add(token)
|
|
30
|
-
next_.string = ""
|
|
31
|
-
new_tokens, remainder = split_at_token(new_tokens, matching_bracket)
|
|
32
|
-
matching_bracket.string = "factorial("
|
|
33
|
-
new_tokens.append(matching_bracket)
|
|
34
|
-
new_tokens.extend(remainder)
|
|
35
|
-
else:
|
|
36
|
-
if token.is_bracket():
|
|
37
|
-
stack.add(token)
|
|
38
|
-
new_tokens.append(token)
|
|
39
|
-
|
|
40
|
-
return untokenize(new_tokens)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# inside_blocks.py
|
|
2
|
-
from token_utils import get_logical_lines, IndentStack, stringify
|
|
3
|
-
|
|
4
|
-
with open("decorator_example.txt", "r") as f:
|
|
5
|
-
source = f.read()
|
|
6
|
-
|
|
7
|
-
stack = IndentStack()
|
|
8
|
-
|
|
9
|
-
for line in get_logical_lines(source):
|
|
10
|
-
stack.update(line)
|
|
11
|
-
first_token = line[0]
|
|
12
|
-
inside_class = stack.is_token_in_named_block(first_token, "class")
|
|
13
|
-
inside_def = stack.is_token_in_named_block(first_token, "def")
|
|
14
|
-
print(
|
|
15
|
-
f"{stringify(line).rstrip():20}",
|
|
16
|
-
f"{inside_def=};",
|
|
17
|
-
f"{inside_class=};",
|
|
18
|
-
" stack:",
|
|
19
|
-
end=" ",
|
|
20
|
-
)
|
|
21
|
-
stack.print_stack()
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# nobreak.py
|
|
2
|
-
from token_utils import get_logical_lines, untokenize, IndentStack
|
|
3
|
-
|
|
4
|
-
with open("nobreak.txt", "r") as f:
|
|
5
|
-
source = f.read()
|
|
6
|
-
|
|
7
|
-
new_lines = []
|
|
8
|
-
stack = IndentStack()
|
|
9
|
-
stack.add_same_indent_keyword("nobreak")
|
|
10
|
-
|
|
11
|
-
for line in get_logical_lines(source):
|
|
12
|
-
top = stack.update(line)
|
|
13
|
-
if not top: # this line does not introduce an indented block
|
|
14
|
-
new_lines.append(line)
|
|
15
|
-
continue
|
|
16
|
-
|
|
17
|
-
if line[0] == "nobreak" and top.is_in(["for", "while"]):
|
|
18
|
-
line[0].string = "else" # modify in place
|
|
19
|
-
|
|
20
|
-
new_lines.append(line)
|
|
21
|
-
|
|
22
|
-
print(untokenize(new_lines))
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# view_stack.py
|
|
2
|
-
from token_utils import get_physical_lines, IndentStack, untokenize
|
|
3
|
-
|
|
4
|
-
with open("decorator_example.txt", "r") as f:
|
|
5
|
-
source = f.read()
|
|
6
|
-
stack = IndentStack()
|
|
7
|
-
|
|
8
|
-
# It is highly preferable to use get_logical_lines as this example shows.
|
|
9
|
-
for line in get_physical_lines(source):
|
|
10
|
-
print(
|
|
11
|
-
f"{untokenize(line).rstrip():<25}",
|
|
12
|
-
f".update() --> {str(stack.update(line)):<7}",
|
|
13
|
-
"==> ",
|
|
14
|
-
end=" ",
|
|
15
|
-
)
|
|
16
|
-
stack.print_stack()
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
pass
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"""This is meant to check that roundtrips (tokenize/untokenize) are
|
|
2
|
-
working correctly.
|
|
3
|
-
|
|
4
|
-
Most of these tests have been adapted from https://github.com/myint/untokenize.
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
from token_utils import (
|
|
8
|
-
tokenize,
|
|
9
|
-
untokenize,
|
|
10
|
-
get_physical_lines,
|
|
11
|
-
stringify,
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def check_all(source):
|
|
16
|
-
assert untokenize(tokenize(source)) == source
|
|
17
|
-
assert untokenize(get_physical_lines(source)) == source
|
|
18
|
-
assert untokenize(get_physical_lines(source, remove_comments=False)) == source
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def check(source):
|
|
22
|
-
assert source == untokenize(tokenize(source))
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
complex_source = '''
|
|
26
|
-
|
|
27
|
-
def zap():
|
|
28
|
-
|
|
29
|
-
"""Hello zap.
|
|
30
|
-
|
|
31
|
-
"""; 1
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
x \t= \t\t \t 1
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
'''
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def test_complex_source():
|
|
41
|
-
check_all(complex_source)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
tab_indentation = """
|
|
45
|
-
if True:
|
|
46
|
-
\tdef zap():
|
|
47
|
-
\t\tx \t= \t\t \t 1
|
|
48
|
-
"""
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def test_untokenize_with_tab_indentation():
|
|
52
|
-
check_all(tab_indentation)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
backslash_in_comment = r'''
|
|
56
|
-
def foo():
|
|
57
|
-
"""Hello foo."""
|
|
58
|
-
def zap(): bar(1) # \
|
|
59
|
-
'''
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def test_untokenize_with_backslash_in_comment():
|
|
63
|
-
source = backslash_in_comment
|
|
64
|
-
assert untokenize(tokenize(source)) == source
|
|
65
|
-
assert untokenize(get_physical_lines(source, remove_comments=False)) == source
|
|
66
|
-
assert stringify(tokenize(source)) == source
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
escaped_newline = r'''def foo():
|
|
70
|
-
"""Hello foo."""
|
|
71
|
-
x = \
|
|
72
|
-
1
|
|
73
|
-
'''
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def test_untokenize_with_escaped_newline():
|
|
77
|
-
check_all(escaped_newline)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def test_cpython_bug_35107():
|
|
81
|
-
# Checking https://bugs.python.org/issue35107#msg328884
|
|
82
|
-
check_all("#")
|
|
83
|
-
check_all("#\n")
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def test_last_line_empty():
|
|
87
|
-
"""If the last line contains only space characters with no newline
|
|
88
|
-
Python's tokenizer drops this content. To ensure that the
|
|
89
|
-
tokenize-untokenize returns the original value, we have introduced
|
|
90
|
-
a fix in our utility functions"""
|
|
91
|
-
|
|
92
|
-
source = "a\n "
|
|
93
|
-
source2 = "a\n\t"
|
|
94
|
-
check_all(source)
|
|
95
|
-
check_all(source2)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def test_bad_dedent():
|
|
99
|
-
"""Instead of raising and IndentationError, we should have a special
|
|
100
|
-
token inserted, allowing us to reconstruct the source."""
|
|
101
|
-
source = """
|
|
102
|
-
def test():
|
|
103
|
-
a = b
|
|
104
|
-
c = d
|
|
105
|
-
e = f
|
|
106
|
-
"""
|
|
107
|
-
check_all(source)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
def test_problematic_newline():
|
|
111
|
-
"""When modifying Python's tokenize, I accidently added a
|
|
112
|
-
string attribute to "NEWLINE" token, but all the existing
|
|
113
|
-
tests passed even though this caused a bug. This test is to
|
|
114
|
-
ensure that this bug does not happen again!
|
|
115
|
-
"""
|
|
116
|
-
source = "2n"
|
|
117
|
-
assert untokenize(tokenize(source)) == source
|
|
118
|
-
#
|
|
119
|
-
# test inserting non-token
|
|
120
|
-
tokens = tokenize(source)
|
|
121
|
-
new_tokens = []
|
|
122
|
-
for tok in tokens:
|
|
123
|
-
new_tokens.append(tok)
|
|
124
|
-
if tok.is_number():
|
|
125
|
-
new_tokens.append("*")
|
|
126
|
-
assert untokenize(new_tokens) == "2*n"
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
def test_py_tokenize():
|
|
130
|
-
"""Complex example!"""
|
|
131
|
-
from token_utils import _py_tokenize
|
|
132
|
-
|
|
133
|
-
with open(_py_tokenize.__file__, "r") as f:
|
|
134
|
-
source = f.read()
|
|
135
|
-
assert untokenize(tokenize(source)) == source
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
def test_unterminated_string():
|
|
139
|
-
source = "name = 'Bob "
|
|
140
|
-
assert untokenize(tokenize(source)) == source
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
unclosed_triple_quoted_string = """
|
|
144
|
-
a = b # see next line
|
|
145
|
-
''' this is meant to be a long
|
|
146
|
-
comment string but it never terminated.
|
|
147
|
-
"""
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
def test_unterminated_triple_quoted_string():
|
|
151
|
-
assert (
|
|
152
|
-
untokenize(tokenize(unclosed_triple_quoted_string))
|
|
153
|
-
== unclosed_triple_quoted_string
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def test_invalid_octal():
|
|
158
|
-
# See https://github.com/friendly-traceback/friendly-traceback/issues/242
|
|
159
|
-
source = "b = 0o1876 + 0o2"
|
|
160
|
-
assert untokenize(tokenize(source)) == source
|
|
161
|
-
source = "a = 0o23 + 0O2987"
|
|
162
|
-
assert untokenize(tokenize(source)) == source
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
def test_non_printable_character():
|
|
166
|
-
source = 'print\x17("Hello")'
|
|
167
|
-
assert untokenize(tokenize(source)) == source
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
# Most of the following tests are trivial but they are there in
|
|
2
|
-
# case we make a typo when changing code. It happens...
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
from token_utils import tokenize, untokenize, make_fake_token, add_operator
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def test_contains():
|
|
9
|
-
tokens = tokenize("name = 'Bob'")
|
|
10
|
-
assert "name" in tokens[0]
|
|
11
|
-
assert "Bob" in tokens[2]
|
|
12
|
-
assert "'Bob'" in tokens[2]
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def test_clone():
|
|
16
|
-
tokens = tokenize("a")
|
|
17
|
-
copy = tokens[0].clone()
|
|
18
|
-
assert copy.is_identical(tokens[0])
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def test_len():
|
|
22
|
-
tokens = tokenize("name = 'Albert'")
|
|
23
|
-
assert tokens[0] == "name"
|
|
24
|
-
assert len(tokens[0]) == 4
|
|
25
|
-
assert tokens[1] == "="
|
|
26
|
-
assert len(tokens[1]) == 1
|
|
27
|
-
assert tokens[2] == "'Albert'"
|
|
28
|
-
assert len(tokens[2]) == 8
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def test_is_assignment():
|
|
32
|
-
for op_string in [
|
|
33
|
-
"=",
|
|
34
|
-
"+=",
|
|
35
|
-
"-=",
|
|
36
|
-
"*=",
|
|
37
|
-
"@=",
|
|
38
|
-
"/=",
|
|
39
|
-
"//=",
|
|
40
|
-
"%=",
|
|
41
|
-
"**=",
|
|
42
|
-
">>=",
|
|
43
|
-
"<<=",
|
|
44
|
-
"&=",
|
|
45
|
-
"^=",
|
|
46
|
-
"|=",
|
|
47
|
-
":=",
|
|
48
|
-
]:
|
|
49
|
-
token = make_fake_token(string=op_string)
|
|
50
|
-
assert token.is_assignment()
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def test_is_bitwise():
|
|
54
|
-
for op_string in ["^", "&", "|", "<<", ">>", "~"]:
|
|
55
|
-
token = make_fake_token(string=op_string)
|
|
56
|
-
assert token.is_bitwise()
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def test_is_comparison():
|
|
60
|
-
for op_string in ["<", ">", "<=", ">=", "==", "!="]:
|
|
61
|
-
token = make_fake_token(string=op_string)
|
|
62
|
-
assert token.is_comparison()
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def test_is_math_operator():
|
|
66
|
-
for op_string in ["+", "-", "*", "**", "@", "/", "//", "%"]:
|
|
67
|
-
token = make_fake_token(string=op_string)
|
|
68
|
-
assert token.is_math_operator()
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def test_is_other_operatar():
|
|
72
|
-
token = make_fake_token(string=":")
|
|
73
|
-
assert token.is_other_operator()
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def test_is_open_bracket():
|
|
77
|
-
tokens = tokenize("{([")
|
|
78
|
-
for token in tokens:
|
|
79
|
-
if token.is_space():
|
|
80
|
-
continue
|
|
81
|
-
assert token.is_open_bracket()
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def test_is_close_bracket():
|
|
85
|
-
tokens = tokenize("]})")
|
|
86
|
-
for token in tokens:
|
|
87
|
-
if token.is_space():
|
|
88
|
-
continue
|
|
89
|
-
assert token.is_close_bracket()
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def test_is_bracket():
|
|
93
|
-
tokens = tokenize("{}()[]")
|
|
94
|
-
for token in tokens:
|
|
95
|
-
if token.is_space():
|
|
96
|
-
continue
|
|
97
|
-
assert token.is_bracket()
|
|
98
|
-
fake_token = make_fake_token(string="()")
|
|
99
|
-
assert not fake_token.is_bracket()
|
|
100
|
-
tokens = tokenize("a b c 1 2 3")
|
|
101
|
-
for token in tokens:
|
|
102
|
-
if token.is_space():
|
|
103
|
-
continue
|
|
104
|
-
assert len(token.string) == 1
|
|
105
|
-
assert not token.is_bracket()
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
def test_is_comment():
|
|
109
|
-
tokens = tokenize("a # comment")
|
|
110
|
-
assert not tokens[0].is_comment()
|
|
111
|
-
assert tokens[0] == "a"
|
|
112
|
-
assert tokens[1].is_comment()
|
|
113
|
-
assert tokens[1] == "# comment"
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
def test_is_complex():
|
|
117
|
-
tokens = tokenize("1.0 + 2.0j - 1")
|
|
118
|
-
assert tokens[0] == "1.0"
|
|
119
|
-
assert not tokens[0].is_complex()
|
|
120
|
-
assert tokens[2] == "2.0j"
|
|
121
|
-
assert tokens[2].is_complex()
|
|
122
|
-
assert tokens[4] == "1"
|
|
123
|
-
assert not tokens[4].is_complex()
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
def test_is_f_string():
|
|
127
|
-
tokens = tokenize("f'{something}' 'other'")
|
|
128
|
-
assert tokens[0].is_string()
|
|
129
|
-
assert tokens[0].is_f_string()
|
|
130
|
-
assert tokens[1].is_string()
|
|
131
|
-
assert not tokens[1].is_f_string()
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
def test_is_float():
|
|
135
|
-
tokens = tokenize("1.0 + 2.0j - 1")
|
|
136
|
-
assert tokens[0] == "1.0"
|
|
137
|
-
assert tokens[0].is_float()
|
|
138
|
-
assert tokens[2] == "2.0j"
|
|
139
|
-
assert not tokens[2].is_float()
|
|
140
|
-
assert tokens[4] == "1"
|
|
141
|
-
assert not tokens[4].is_float()
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
def test_is_identical():
|
|
145
|
-
tok1 = make_fake_token()
|
|
146
|
-
tok2 = make_fake_token()
|
|
147
|
-
tok3 = make_fake_token(string="|")
|
|
148
|
-
|
|
149
|
-
assert tok1.is_identical(tok2)
|
|
150
|
-
assert not tok1.is_identical(tok3)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def test_is_identifier():
|
|
154
|
-
tokens = tokenize("def test")
|
|
155
|
-
assert tokens[0] == "def"
|
|
156
|
-
assert not tokens[0].is_identifier()
|
|
157
|
-
assert tokens[1] == "test"
|
|
158
|
-
assert tokens[1].is_identifier()
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
def test_immediately_before_and_after():
|
|
162
|
-
tokens = tokenize("**/ =")
|
|
163
|
-
assert tokens[0] == "**"
|
|
164
|
-
assert tokens[1] == "/"
|
|
165
|
-
assert tokens[2] == "="
|
|
166
|
-
assert tokens[0].is_immediately_before(tokens[1])
|
|
167
|
-
assert tokens[1].is_immediately_after(tokens[0])
|
|
168
|
-
assert not tokens[1].is_immediately_before(tokens[2])
|
|
169
|
-
assert not tokens[2].is_immediately_after(tokens[1])
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
def test_is_in():
|
|
173
|
-
tokens = tokenize("2+4-5")
|
|
174
|
-
for token in tokens:
|
|
175
|
-
if not token.string.strip():
|
|
176
|
-
continue
|
|
177
|
-
assert token.is_in(["2", "+", "4", "-", "5"])
|
|
178
|
-
assert not token.is_in(["1", "3", "*", "/"])
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
def test_is_integer():
|
|
182
|
-
tokens = tokenize("1.0 + 2.0j - 1")
|
|
183
|
-
assert tokens[0] == "1.0"
|
|
184
|
-
assert not tokens[0].is_integer()
|
|
185
|
-
assert tokens[2] == "2.0j"
|
|
186
|
-
assert not tokens[2].is_integer()
|
|
187
|
-
assert tokens[4] == "1"
|
|
188
|
-
assert tokens[4].is_integer()
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
def test_is_keyword():
|
|
192
|
-
tokens = tokenize("def test")
|
|
193
|
-
assert tokens[0] == "def"
|
|
194
|
-
assert tokens[0].is_keyword()
|
|
195
|
-
assert tokens[1] == "test"
|
|
196
|
-
assert not tokens[1].is_keyword()
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def test_is_matching_bracket():
|
|
200
|
-
tokens = [make_fake_token(string=br) for br in ["(", ")", "[", "]", "{", "}"]]
|
|
201
|
-
for i in range(0, 5, 2):
|
|
202
|
-
assert tokens[i].is_matching_bracket(tokens[i + 1])
|
|
203
|
-
assert tokens[i + 1].is_matching_bracket(tokens[i])
|
|
204
|
-
assert not tokens[i].is_matching_bracket(tokens[i])
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
def test_is_name():
|
|
208
|
-
tokens = tokenize("def test")
|
|
209
|
-
assert tokens[0] == "def"
|
|
210
|
-
assert tokens[0].is_name()
|
|
211
|
-
assert tokens[1] == "test"
|
|
212
|
-
assert tokens[1].is_name()
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
def test_is_number():
|
|
216
|
-
tokens = tokenize("1.0 + 2.0j - 1 + 0o123 + 0x1A")
|
|
217
|
-
assert tokens[0] == "1.0"
|
|
218
|
-
assert tokens[0].is_number()
|
|
219
|
-
assert tokens[2] == "2.0j"
|
|
220
|
-
assert tokens[2].is_number()
|
|
221
|
-
assert tokens[4] == "1"
|
|
222
|
-
assert tokens[4].is_number()
|
|
223
|
-
assert tokens[6] == "0o123"
|
|
224
|
-
assert tokens[6].is_number()
|
|
225
|
-
assert tokens[8] == "0x1A"
|
|
226
|
-
assert tokens[8].is_number()
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
def test_is_operator():
|
|
230
|
-
tokens = tokenize("a = - 3")
|
|
231
|
-
assert tokens[1] == "="
|
|
232
|
-
assert tokens[1].is_operator()
|
|
233
|
-
assert tokens[2] == "-"
|
|
234
|
-
assert tokens[2].is_operator()
|
|
235
|
-
assert not tokens[0].is_operator()
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
def test_is_string():
|
|
239
|
-
tokens = tokenize("name = 'Bob'")
|
|
240
|
-
assert not tokens[0].is_string()
|
|
241
|
-
assert tokens[2].is_string()
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
def test_is_unclosed_string():
|
|
245
|
-
tokens = tokenize("' a")
|
|
246
|
-
assert tokens[0].is_unclosed_string()
|
|
247
|
-
|
|
248
|
-
tokens = tokenize("''' ")
|
|
249
|
-
assert tokens[0].is_unclosed_string()
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
def test_make_fake():
|
|
253
|
-
from token_utils import make_fake_token
|
|
254
|
-
|
|
255
|
-
fake = make_fake_token()
|
|
256
|
-
source = "a = b"
|
|
257
|
-
tokens = tokenize(source)
|
|
258
|
-
tokens.insert(0, fake)
|
|
259
|
-
assert untokenize(tokens) == "$a = b"
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
def test_add_operator():
|
|
263
|
-
assert add_operator("=", "equal") is False
|
|
264
|
-
assert add_operator("!!!!!", "surprise", category="other") is True
|
|
265
|
-
tokens = tokenize("!!!!!")
|
|
266
|
-
new_tokens = [tok for tok in tokens if tok.is_operator()]
|
|
267
|
-
assert len(new_tokens) == 1
|
|
268
|
-
surprise = new_tokens[0]
|
|
269
|
-
assert surprise == "!!!!!"
|
|
270
|
-
assert surprise.is_other_operator()
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
from token_utils import (
|
|
2
|
-
generate_tokens,
|
|
3
|
-
tokenize,
|
|
4
|
-
make_fake_token,
|
|
5
|
-
get_physical_lines,
|
|
6
|
-
dedent,
|
|
7
|
-
pairwise,
|
|
8
|
-
sliding_window,
|
|
9
|
-
BracketStack,
|
|
10
|
-
stringify, # for testing commment removal
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
source1 = "a = b"
|
|
14
|
-
source2 = "a = b # comment\n"
|
|
15
|
-
source3 = """
|
|
16
|
-
if True:
|
|
17
|
-
a = b # comment
|
|
18
|
-
"""
|
|
19
|
-
tokens1 = tokenize(source1)
|
|
20
|
-
tokens2 = tokenize(source2)
|
|
21
|
-
lines2 = get_physical_lines(source2)
|
|
22
|
-
lines3 = get_physical_lines(source3)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def test_pairwise():
|
|
26
|
-
# test with lists
|
|
27
|
-
tokens = [make_fake_token(string=str(i)) for i in range(5)]
|
|
28
|
-
new_tokens = []
|
|
29
|
-
for _, token in pairwise(tokens, prev=1):
|
|
30
|
-
new_tokens.append(str(token))
|
|
31
|
-
assert new_tokens == ["0", "1", "2", "3", "4"]
|
|
32
|
-
|
|
33
|
-
new_tokens = []
|
|
34
|
-
for token, _ in pairwise(tokens): # noqa
|
|
35
|
-
new_tokens.append(str(token))
|
|
36
|
-
assert new_tokens == ["0", "1", "2", "3", "4"]
|
|
37
|
-
|
|
38
|
-
# test with generator
|
|
39
|
-
new_tokens = []
|
|
40
|
-
for _, token in pairwise(generate_tokens("0 1 2 3 4"), prev=1):
|
|
41
|
-
if token.is_integer():
|
|
42
|
-
new_tokens.append(str(token))
|
|
43
|
-
assert new_tokens == ["0", "1", "2", "3", "4"]
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def test_sliding_window():
|
|
47
|
-
# tests with list
|
|
48
|
-
tokens = [make_fake_token(string=str(i)) for i in range(5)]
|
|
49
|
-
|
|
50
|
-
new_tokens = []
|
|
51
|
-
for token, _, _ in sliding_window(tokens, 3):
|
|
52
|
-
new_tokens.append(str(token))
|
|
53
|
-
assert new_tokens == ["0", "1", "2", "3", "4"]
|
|
54
|
-
|
|
55
|
-
new_tokens = []
|
|
56
|
-
for _, token, _ in sliding_window(tokens, 3, prev=1):
|
|
57
|
-
new_tokens.append(str(token))
|
|
58
|
-
assert new_tokens == ["0", "1", "2", "3", "4"]
|
|
59
|
-
|
|
60
|
-
new_tokens = []
|
|
61
|
-
for _, _, token in sliding_window(tokens, 3, prev=2):
|
|
62
|
-
new_tokens.append(str(token))
|
|
63
|
-
assert new_tokens == ["0", "1", "2", "3", "4"]
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
def test_dedent():
|
|
67
|
-
new_tokens = dedent(lines3[2], 4)
|
|
68
|
-
assert new_tokens == tokens2
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
# spaces between the last token and the comment
|
|
72
|
-
# would be left behind, so we put the comments
|
|
73
|
-
# right next to a non-space token for testing
|
|
74
|
-
with_comments = """
|
|
75
|
-
def test(): # a good function
|
|
76
|
-
a = b # fantastic mathematical operation
|
|
77
|
-
return 3
|
|
78
|
-
|
|
79
|
-
# another comment to end things
|
|
80
|
-
"""
|
|
81
|
-
|
|
82
|
-
without_comments = """
|
|
83
|
-
def test():
|
|
84
|
-
a = b
|
|
85
|
-
return 3
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"""
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def test_stringify_remove_comments():
|
|
92
|
-
assert (
|
|
93
|
-
stringify(list(generate_tokens(with_comments)), remove_comments=True)
|
|
94
|
-
== without_comments
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def test_bracket_stack():
|
|
99
|
-
bracket_stack = BracketStack()
|
|
100
|
-
assert bracket_stack.is_empty()
|
|
101
|
-
|
|
102
|
-
tokens = tokenize(" a b c ({ d []})")
|
|
103
|
-
for token in tokens:
|
|
104
|
-
if token.is_bracket():
|
|
105
|
-
result = bracket_stack.add(token)
|
|
106
|
-
if token.is_open_bracket():
|
|
107
|
-
assert not bracket_stack.is_empty()
|
|
108
|
-
assert not result # bracket appended
|
|
109
|
-
else:
|
|
110
|
-
assert result # bracket popped
|
|
111
|
-
assert bracket_stack.is_empty()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|