Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare the 2025 stable style #4558

Merged
merged 8 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/black/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"time",
"timeit",
))
TOKEN_HEX = secrets.token_hex


@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -160,7 +159,7 @@ def mask_cell(src: str) -> tuple[str, list[Replacement]]:

becomes

"25716f358c32750e"
b"25716f358c32750"
'foo'

The replacements are returned, along with the transformed code.
Expand Down Expand Up @@ -192,6 +191,18 @@ def mask_cell(src: str) -> tuple[str, list[Replacement]]:
return transformed, replacements


def create_token(n_chars: int) -> str:
"""Create a randomly generated token that is n_chars characters long."""
assert n_chars > 0
n_bytes = max(n_chars // 2 - 1, 1)
token = secrets.token_hex(n_bytes)
if len(token) + 3 > n_chars:
token = token[:-1]
# We use a bytestring so that the string does not get interpreted
# as a docstring.
return f'b"{token}"'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol



def get_token(src: str, magic: str) -> str:
"""Return randomly generated token to mask IPython magic with.

Expand All @@ -201,21 +212,19 @@ def get_token(src: str, magic: str) -> str:
not already present anywhere else in the cell.
"""
assert magic
nbytes = max(len(magic) // 2 - 1, 1)
token = TOKEN_HEX(nbytes)
n_chars = len(magic)
token = create_token(n_chars)
counter = 0
while token in src:
token = TOKEN_HEX(nbytes)
token = create_token(n_chars)
counter += 1
if counter > 100:
raise AssertionError(
"INTERNAL ERROR: Black was not able to replace IPython magic. "
"Please report a bug on https://github.com/psf/black/issues. "
f"The magic might be helpful: {magic}"
) from None
if len(token) + 2 < len(magic):
token = f"{token}."
return f'"{token}"'
return token


def replace_cell_magics(src: str) -> tuple[str, list[Replacement]]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ def test_ipynb_and_pyi_flags() -> None:


def test_unable_to_replace_magics(monkeypatch: MonkeyPatch) -> None:
src = "%%time\na = 'foo'"
monkeypatch.setattr("black.handle_ipynb_magics.TOKEN_HEX", lambda _: "foo")
src = '%%time\na = b"foo"'
monkeypatch.setattr("secrets.token_hex", lambda _: "foo")
with pytest.raises(
AssertionError, match="Black was not able to replace IPython magic"
):
Expand Down
Loading