Unverified Commit 7d032fa8 authored by Casper Weiss Bang's avatar Casper Weiss Bang Committed by GitHub
Browse files

Use lowercase hex numbers fixes #1692 (#1775)

* Made hex lower case

* Refactored numeric formatting section

* Redid some refactoring and removed bloat

* Removed additions from test_requirements.txt

* Primer now expects expected changes

* Undid some refactoring

* added to changelog

* Update src/black/__init__.py

Co-authored-by: default avatarZsolt Dollenstein <zsol.zsol@gmail.com>

Co-authored-by: default avatarZsolt Dollenstein <zsol.zsol@gmail.com>
Co-authored-by: default avatarCooper Lees <me@cooperlees.com>
parent 1d8b4d76
Loading
Loading
Loading
Loading
+1 −0
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
.venv
.coverage
_build
.DS_Store
+2 −0
Changes for CHANGES.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@

- Added support for PEP 614 relaxed decorator syntax on python 3.9 (#1711)

- use lowercase hex strings (#1692)

#### _Packaging_

- Self-contained native _Black_ binaries are now provided for releases via GitHub
+30 −9
Changes for src/black/__init__.py: 30 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -5192,10 +5192,31 @@ def normalize_numeric_literal(leaf: Leaf) -> None:
        # Leave octal and binary literals alone.
        pass
    elif text.startswith("0x"):
        # Change hex literals to upper case.
        before, after = text[:2], text[2:]
        text = f"{before}{after.upper()}"
        text = format_hex(text)
    elif "e" in text:
        text = format_scientific_notation(text)
    elif text.endswith(("j", "l")):
        text = format_long_or_complex_number(text)
    else:
        text = format_float_or_int_string(text)
    leaf.value = text


def format_hex(text: str) -> str:
    """
    Formats a hexadecimal string like "0x12b3"

    Uses lowercase because of similarity between "B" and "8", which
    can cause security issues.
    see: https://github.com/psf/black/issues/1692
    """

    before, after = text[:2], text[2:]
    return f"{before}{after.lower()}"


def format_scientific_notation(text: str) -> str:
    """Formats a numeric string utilizing scentific notation"""
    before, after = text.split("e")
    sign = ""
    if after.startswith("-"):
@@ -5204,17 +5225,17 @@ def normalize_numeric_literal(leaf: Leaf) -> None:
    elif after.startswith("+"):
        after = after[1:]
    before = format_float_or_int_string(before)
        text = f"{before}e{sign}{after}"
    elif text.endswith(("j", "l")):
    return f"{before}e{sign}{after}"


def format_long_or_complex_number(text: str) -> str:
    """Formats a long or complex string like `10L` or `10j`"""
    number = text[:-1]
    suffix = text[-1]
    # Capitalize in "2L" because "l" looks too similar to "1".
    if suffix == "l":
        suffix = "L"
        text = f"{format_float_or_int_string(number)}{suffix}"
    else:
        text = format_float_or_int_string(text)
    leaf.value = text
    return f"{format_float_or_int_string(number)}{suffix}"


def format_float_or_int_string(text: str) -> str:
+5 −5
Changes for src/black_primer/primer.json: 5 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
    },
    "attrs": {
      "cli_arguments": [],
      "expect_formatting_changes": false,
      "expect_formatting_changes": true,
      "git_clone_url": "https://github.com/python-attrs/attrs.git",
      "long_checkout": false,
      "py_versions": ["all"]
@@ -47,7 +47,7 @@
    },
    "hypothesis": {
      "cli_arguments": [],
      "expect_formatting_changes": false,
      "expect_formatting_changes": true,
      "git_clone_url": "https://github.com/HypothesisWorks/hypothesis.git",
      "long_checkout": false,
      "py_versions": ["all"]
@@ -63,7 +63,7 @@
    },
    "pillow": {
      "cli_arguments": [],
      "expect_formatting_changes": false,
      "expect_formatting_changes": true,
      "git_clone_url": "https://github.com/python-pillow/Pillow.git",
      "long_checkout": false,
      "py_versions": ["all"]
@@ -77,7 +77,7 @@
    },
    "pyramid": {
      "cli_arguments": [],
      "expect_formatting_changes": false,
      "expect_formatting_changes": true,
      "git_clone_url": "https://github.com/Pylons/pyramid.git",
      "long_checkout": false,
      "py_versions": ["all"]
@@ -112,7 +112,7 @@
    },
    "virtualenv": {
      "cli_arguments": [],
      "expect_formatting_changes": false,
      "expect_formatting_changes": true,
      "git_clone_url": "https://github.com/pypa/virtualenv.git",
      "long_checkout": false,
      "py_versions": ["all"]
+1 −1
Changes for src/blib2to3/pytree.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@
import sys
from io import StringIO

HUGE: int = 0x7FFFFFFF  # maximum repeat count, default max
HUGE: int = 0x7fffffff  # maximum repeat count, default max

_type_reprs: Dict[int, Union[Text, int]] = {}

Loading