Skip to content

Commit

Permalink
Merge branch 'main' into pathlib_abc
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Jan 14, 2024
2 parents 4682f48 + 1709020 commit 86dac34
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
18 changes: 8 additions & 10 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,11 @@ def split(self, path):
"""
self._unsupported('split()')

def splitroot(self, path):
"""Split the pathname path into a 3-item tuple (drive, root, tail),
where *drive* is a device name or mount point, *root* is a string of
separators after the drive, and *tail* is everything after the root.
Any part may be empty."""
self._unsupported('splitroot()')
def splitdrive(self, path):
"""Split the path into a 2-item tuple (drive, tail), where *drive* is
a device name or mount point, and *tail* is everything after the
drive. Either part may be empty."""
self._unsupported('splitdrive()')

def normcase(self, path):
"""Normalize the case of the path."""
Expand Down Expand Up @@ -227,18 +226,17 @@ def as_posix(self):
@property
def drive(self):
"""The drive prefix (letter or UNC path), if any."""
return self.pathmod.splitroot(self._raw_path)[0]
return self.pathmod.splitdrive(self.anchor)[0]

@property
def root(self):
"""The root of the path, if any."""
return self.pathmod.splitroot(self._raw_path)[1]
return self.pathmod.splitdrive(self.anchor)[1]

@property
def anchor(self):
"""The concatenation of the drive and root, or ''."""
drive, root, _ = self.pathmod.splitroot(self._raw_path)
return drive + root
return self._stack[0]

@property
def name(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_unsupported_operation(self):
m.sep
self.assertRaises(e, m.join, 'foo')
self.assertRaises(e, m.split, 'foo')
self.assertRaises(e, m.splitroot, 'foo')
self.assertRaises(e, m.splitdrive, 'foo')
self.assertRaises(e, m.normcase, 'foo')
self.assertRaises(e, m.isabs, 'foo')

Expand Down
21 changes: 1 addition & 20 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import pprint
import re
import shlex
import string
import sys
import textwrap

Expand Down Expand Up @@ -270,24 +269,6 @@ def __init__(self) -> None:
self.unlock: list[str] = []


class FormatCounterFormatter(string.Formatter):
"""
This counts how many instances of each formatter
"replacement string" appear in the format string.
e.g. after evaluating "string {a}, {b}, {c}, {a}"
the counts dict would now look like
{'a': 2, 'b': 1, 'c': 1}
"""
def __init__(self) -> None:
self.counts = collections.Counter[str]()

def get_value(
self, key: str, args: object, kwargs: object # type: ignore[override]
) -> Literal['']:
self.counts[key] += 1
return ''

class Language(metaclass=abc.ABCMeta):

start_line = ""
Expand Down Expand Up @@ -341,7 +322,7 @@ def assert_only_one(
fields = ['dsl_name']
fields.extend(additional_fields)
line: str = getattr(self, attr)
fcf = FormatCounterFormatter()
fcf = libclinic.FormatCounterFormatter()
fcf.format(line)
def local_fail(should_be_there_but_isnt: bool) -> None:
if should_be_there_but_isnt:
Expand Down
6 changes: 4 additions & 2 deletions Tools/clinic/libclinic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
wrapped_c_string_literal,
)
from .utils import (
create_regex,
FormatCounterFormatter,
compute_checksum,
create_regex,
write_file,
)

Expand All @@ -39,8 +40,9 @@
"wrapped_c_string_literal",

# Utility functions
"create_regex",
"FormatCounterFormatter",
"compute_checksum",
"create_regex",
"write_file",
]

Expand Down
29 changes: 26 additions & 3 deletions Tools/clinic/libclinic/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import collections
import hashlib
import re
import os
import re
import string
from typing import Literal


def write_file(filename: str, new_contents: str) -> None:
Expand Down Expand Up @@ -39,7 +42,27 @@ def create_regex(
group_re = r"\w+" if word else ".+"
before = re.escape(before)
after = re.escape(after)
pattern = fr"{before}({group_re}){after}"
pattern = rf"{before}({group_re}){after}"
if whole_line:
pattern = fr"^{pattern}$"
pattern = rf"^{pattern}$"
return re.compile(pattern)


class FormatCounterFormatter(string.Formatter):
"""
This counts how many instances of each formatter
"replacement string" appear in the format string.
e.g. after evaluating "string {a}, {b}, {c}, {a}"
the counts dict would now look like
{'a': 2, 'b': 1, 'c': 1}
"""

def __init__(self) -> None:
self.counts = collections.Counter[str]()

def get_value(
self, key: str, args: object, kwargs: object # type: ignore[override]
) -> Literal[""]:
self.counts[key] += 1
return ""

0 comments on commit 86dac34

Please sign in to comment.