Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,14 @@ jobs:
- name: Check for unsupported C global variables
if: github.event_name == 'pull_request' # $GITHUB_EVENT_NAME
run: make check-c-globals
- name: Check for undocumented C APIs
run: make check-c-api-docs

check-c-api-docs:
name: C API Docs
needs: build-context
if: >-
needs.build-context.outputs.run-tests == 'true'
|| needs.build-context.outputs.run-docs == 'true'
uses: ./.github/workflows/reusable-check-c-api-docs.yml

build-windows:
name: >-
Expand Down Expand Up @@ -685,6 +690,7 @@ jobs:
- check-docs
- check-autoconf-regen
- check-generated-files
- check-c-api-docs
- build-windows
- build-windows-msi
- build-macos
Expand Down Expand Up @@ -721,6 +727,12 @@ jobs:
'
|| ''
}}
${{
!fromJSON(needs.build-context.outputs.run-tests)
&& !fromJSON(needs.build-context.outputs.run-docs)
&& 'check-c-api-docs,'
|| ''
}}
${{ !fromJSON(needs.build-context.outputs.run-windows-tests) && 'build-windows,' || '' }}
${{ !fromJSON(needs.build-context.outputs.run-ci-fuzz) && 'cifuzz,' || '' }}
${{ !fromJSON(needs.build-context.outputs.run-macos) && 'build-macos,' || '' }}
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/reusable-check-c-api-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Reusable C API Docs Check

on:
workflow_call:

permissions:
contents: read

env:
FORCE_COLOR: 1

jobs:
check-c-api-docs:
name: 'Check if all C APIs are documented'
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Check for undocumented C APIs
run: python Tools/check-c-api-docs/main.py
16 changes: 15 additions & 1 deletion Lib/importlib/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,14 @@ def search(self, prepared: Prepared):
return itertools.chain(infos, eggs)


# Translation table for Prepared.normalize: lowercase and
# replace "-" (hyphen) and "." (dot) with "_" (underscore).
_normalize_table = str.maketrans(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-.",
"abcdefghijklmnopqrstuvwxyz__",
)


class Prepared:
"""
A prepared search query for metadata on a possibly-named package.
Expand Down Expand Up @@ -925,7 +933,13 @@ def normalize(name):
"""
PEP 503 normalization plus dashes as underscores.
"""
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
# Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503
# About 3x faster, safe since packages only support alphanumeric characters
value = name.translate(_normalize_table)
# Condense repeats (faster than regex)
while "__" in value:
value = value.replace("__", "_")
return value

@staticmethod
def legacy_normalize(name):
Expand Down
34 changes: 34 additions & 0 deletions Lib/test/test_importlib/metadata/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from importlib.metadata import (
Distribution,
PackageNotFoundError,
Prepared,
distribution,
entry_points,
files,
Expand Down Expand Up @@ -313,3 +314,36 @@ class InvalidateCache(unittest.TestCase):
def test_invalidate_cache(self):
# No externally observable behavior, but ensures test coverage...
importlib.invalidate_caches()


class PreparedTests(unittest.TestCase):
def test_normalize(self):
tests = [
# Simple
("sample", "sample"),
# Mixed case
("Sample", "sample"),
("SAMPLE", "sample"),
("SaMpLe", "sample"),
# Separator conversions
("sample-pkg", "sample_pkg"),
("sample.pkg", "sample_pkg"),
("sample_pkg", "sample_pkg"),
# Multiple separators
("sample---pkg", "sample_pkg"),
("sample___pkg", "sample_pkg"),
("sample...pkg", "sample_pkg"),
# Mixed separators
("sample-._pkg", "sample_pkg"),
("sample_.-pkg", "sample_pkg"),
# Complex
("Sample__Pkg-name.foo", "sample_pkg_name_foo"),
("Sample__Pkg.name__foo", "sample_pkg_name_foo"),
# Uppercase with separators
("SAMPLE-PKG", "sample_pkg"),
("Sample.Pkg", "sample_pkg"),
("SAMPLE_PKG", "sample_pkg"),
]
for name, expected in tests:
with self.subTest(name=name):
self.assertEqual(Prepared.normalize(name), expected)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up :meth:`int.from_bytes` when passed object supports :ref:`buffer
protocol <bufferobjects>`, like :class:`bytearray` by ~1.2x.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`importlib.metadata`: Use :meth:`str.translate` to improve performance of
:meth:`!importlib.metadata.Prepared.normalize`. Patch by Hugo van Kemenade and
Henry Schreiner.
35 changes: 27 additions & 8 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6476,14 +6476,33 @@ int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
return NULL;
}

bytes = PyObject_Bytes(bytes_obj);
if (bytes == NULL)
return NULL;

long_obj = _PyLong_FromByteArray(
(unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
little_endian, is_signed);
Py_DECREF(bytes);
/* Fast-path exact bytes. */
if (PyBytes_CheckExact(bytes_obj)) {
long_obj = _PyLong_FromByteArray(
(unsigned char *)PyBytes_AS_STRING(bytes_obj), Py_SIZE(bytes_obj),
little_endian, is_signed);
}
/* Use buffer protocol to avoid copies. */
else if (PyObject_CheckBuffer(bytes_obj)) {
Py_buffer view;
if (PyObject_GetBuffer(bytes_obj, &view, PyBUF_SIMPLE) != 0) {
return NULL;
}
long_obj = _PyLong_FromByteArray(view.buf, view.len, little_endian,
is_signed);
PyBuffer_Release(&view);
}
else {
/* fallback: Construct a bytes then convert. */
bytes = PyObject_Bytes(bytes_obj);
if (bytes == NULL) {
return NULL;
}
long_obj = _PyLong_FromByteArray(
(unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
little_endian, is_signed);
Py_DECREF(bytes);
}

if (long_obj != NULL && type != &PyLong_Type) {
Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj));
Expand Down
Loading