Skip to content
Draft
Changes from all commits
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
29 changes: 13 additions & 16 deletions cblack.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__version__ = "22.6.0"

import sys
from os.path import isdir

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This import was unused

from pathlib import Path

import importlib
import importlib.util
Expand All @@ -31,25 +31,24 @@ class CBlackModuleLoader(type(_real_pathfinder)):
https://stupidpythonideas.blogspot.com/2015/06/hacking-python-without-hacking-python.html
"""

# all "black" folders contain the substring "/black-<version>"
# (e.g "/usr/local/lib/python3.8/site-packages/black-22.3.0-py3.8-linux-x86_64.egg")
_black_folder = "/black-%s" % __version__

# local installation of black folder don't necessarily need to contain the
# version, so the path includes '/black/'
_black_local_folder = "/black/"
_black_folder_patterns = (

@sztomi sztomi Sep 2, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Paths on windows are usually represented with backslashes, so hardcoding the forward slash (even though it is supported on Windows) will filter out the actual module names for black.

# all "black" folders contain the substring "/black-<version>"
# (e.g "/usr/local/lib/python3.8/site-packages/black-22.3.0-py3.8-linux-x86_64.egg")
"black-%s" % __version__,
# local installation of black folder don't necessarily need to contain the
# version, so the path includes '/black/'
"black",
)

@classmethod
def find_module(cls, fullname, path=None):
""" """
spec = _real_pathfinder.find_spec(fullname, path)
if not spec:
return spec

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it's OK to short-circuit here because if spec were None, the remaining code would not run.


if (
spec
and (
(CBlackModuleLoader._black_folder in spec.origin)
or (CBlackModuleLoader._black_local_folder in spec.origin)
)
and spec.origin.endswith(".so")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hardcoding this extension will filter out Windows dynamic modules (which are usually .pyd, but sometimes .dll). But it also doesn't matter if we filter here, because later it filters on importlib.machinery.EXTENSION_SUFFIXES

Path(spec.origin).parent.name in CBlackModuleLoader._black_folder_patterns

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rather than looking for an exact string match in the full path string, lean on pathlib to parse the path and check the parent folder name.

):
# replace known dynamic loader module extensions by their "py" counterpart
location = spec.origin
Expand All @@ -61,8 +60,6 @@ def find_module(cls, fullname, path=None):
# load & replace the spec from the python file
spec = importlib.util.spec_from_file_location(spec.name, location)

if not spec:
return spec
return spec.loader


Expand Down