Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Simplify parse task to improve compatibility
Browse files Browse the repository at this point in the history
Turns out the idea I was saving import time was a lie because
unfortunately I was accessing Black's internal APIs anyway. So
instead of giving myself a maintenance burden that has no good reason
to exist other than "don't touch what's not broken" let's just use a
still internal but much more high-level API from black.

This allows me to both clean up my mypyc-support patch (since it
involved changes to the blib2to3 driver blackbench's parse task
would crash) AND support black>=19.3b0 which is just awesome!
  • Loading branch information
ichard26 committed Aug 13, 2021
1 parent b0bbdca commit fefbdcd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 83 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ Helpful links:
Blackbench: MIT.

Targets based off real code maintain their original license. Please check the directory
containing the target in question for a license file. There's also one task based off
Black's code. Please also check the task templates directory for more information.
containing the target in question for a license file.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Date of release: *n/a*
- Fixed the pyperf requirement which was originally pinning pyperf to 2.0.0 by accident.
- Added pre-checks for both pyperf arguments and --format-config configuration so issues
are caught earlier.
- The parse task now supports installations of Black as old as 19.3b0!

## 21.7.dev2

Expand Down
3 changes: 1 addition & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,4 @@ A breakdown of what's happening here:
Blackbench: MIT.

Targets based off real code maintain their original license. Please check the directory
containing the target in question for a license file. There's also one task based off
Black's code. Please also check the task templates directory for more information.
containing the target in question for a license file.
3 changes: 1 addition & 2 deletions docs/tasks_and_targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ implementation. This provides a benchmarking speedup and is also unavoidable for
level tasks like `parse`. Due to this, each task imposes restrictions to what version of
Black their benchmarks can be run under:

- `fmt` and `fmt-fast`: >= 19.3b0
- `parse`: >= 21.5b1
- `fmt`, `fmt-fast`, and `parse`: >= 19.3b0

## Useful commands

Expand Down
26 changes: 0 additions & 26 deletions src/blackbench/task-templates/parse-template-LICENSE.txt

This file was deleted.

62 changes: 12 additions & 50 deletions src/blackbench/task-templates/parse-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,19 @@

import pyperf

from blib2to3.pytree import Node, Leaf
from blib2to3 import pygram, pytree
from blib2to3.pgen2 import driver
from blib2to3.pgen2.parse import ParseError
from black.nodes import syms


class InvalidInput(ValueError):
"""Raised when input source code fails all parse attempts."""
from blib2to3 import pygram

# First try the relevant function post-refactor in 21.5b1, and fallback
# to old function location.
try:
from black.parsing import lib2to3_parse
except ImportError:
from black import lib2to3_parse

runner = pyperf.Runner()
code = Path(r"{target}").read_text(encoding="utf8")
pygram.initialize(tempfile.gettempdir())

grammars = [
# Python 3.7+
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords,
# Python 3.0-3.6
pygram.python_grammar_no_print_statement_no_exec_statement,
# Python 2.7 with future print_function import
pygram.python_grammar_no_print_statement,
# Python 2.7
pygram.python_grammar,
]


def blib2to3_parse(src_txt: str) -> Node:
if not src_txt.endswith("\n"):
src_txt += "\n"

for grammar in grammars:
drv = driver.Driver(grammar, pytree.convert)
try:
result = drv.parse_string(src_txt, True)
break

except ParseError as pe:
lineno, column = pe.context[1]
lines = src_txt.splitlines()
try:
faulty_line = lines[lineno - 1]
except IndexError:
faulty_line = "<line number missing in source>"
exc = InvalidInput("Cannot parse: " + str(lineno) + ":" + str(column) + ":" + str(faulty_line))
else:
raise exc from None

if isinstance(result, Leaf):
result = Node(syms.file_input, [result])
return result

code = Path(r"{target}").read_text(encoding="utf8")

runner.bench_func("{name}", blib2to3_parse, code)
with tempfile.TemporaryDirectory(prefix="blackbench-parsing-") as path:
# Block the parser code from using any pre-existing cache.
pygram.initialize(path)
runner.bench_func("{name}", lib2to3_parse, code)
2 changes: 1 addition & 1 deletion tests/smoke_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_provided_targets(tmp_result: Path, run_cmd, target: Target):
# because their external usages makes maintenance harder :/
code = target.path.read_text("utf8")
try:
black.format_file_contents(code, fast=False, mode=black.Mode())
black.format_file_contents(code, fast=False, mode=black.FileMode())
except black.NothingChanged:
pass

Expand Down

0 comments on commit fefbdcd

Please sign in to comment.