Skip to content

refactor(protocols)!: replace the pcapkit.protocols.__proto__ dict with ProtocolBase.__registry__ (#2763) - #2770

Merged
JarryShaw merged 1 commit into
mainfrom
refactor/2763-protocol-registry
Nov 4, 2026
Merged

JarryShaw merged 1 commit into
mainfrom
refactor/2763-protocol-registry

Conversation

@JarryShaw

Copy link
Copy Markdown
Owner

What is the purpose of your pull request?

  • refactor — restructures code without changing behaviour

Description of your pull request and other information

This replaces the module-level dict pcapkit.protocols.__proto__ with a registry on the protocol classes, as ruled on #2763. Breaking: the old name is gone, with no alias.

  • ProtocolBase.__registry__ is a class-level map from name to protocol. It holds all 62 built-ins as ModuleDescriptor entries, so it is complete from the start and loads each protocol only when looked up. pcapkit/protocols/registry.py is removed.
  • ProtocolBase.register_protocol and get_protocol(name, default=None) are classmethods. The getter resolves a descriptor to its class. The foundation.registry wrapper now calls register_protocol and is tagged # NOTE: __registry__.
  • AnyProtocol.registry is a read-only view of the whole map, as stored. It is a property on ProtocolMeta, so reading it imports nothing.
  • Auto-registration happens in Protocol.__init_subclass__, the public base, for concrete subclasses only. A name clash keeps the existing entry and warns. Built-ins subclass ProtocolBase and are covered by their descriptors.
  • docs/source/pcapkit/protocols/index.rst has a versionremoved:: 1.6.0 note for the old name. Import time is unchanged.
  • The new test module fails on main (4 failures, 18 errors) and passes here. All plain-unittest legs pass, and the docs build has 0 warnings.

Closes #2763

…th ProtocolBase.__registry__ (#2763)

- ProtocolBase.__registry__ lists every built-in as a ModuleDescriptor, so the
  map is whole before any protocol is imported; get_protocol() resolves a name
  in any case, one thread at a time, as racing first imports can deadlock.
- ProtocolMeta.registry is a read-only view of the entries as stored, so
  AnyProtocol.registry imports nothing.
- A concrete Protocol subclass names itself from Protocol.__init_subclass__, after
  its code= is accepted; an abstract one is skipped, a clash keeps the entry and
  warns, and a class meeting its own descriptor is silent.
- register_protocol() replaces an entry and puts a built-in's shipped descriptor
  back; the foundation wrapper calls it and the name leases compare through it.
- Readers move to the getter; pcapkit.protocols.__proto__ goes, with no alias.
- Docs and conventions record the ruling. Tests: guard, registration, getter,
  view, cold laziness and a thread race, mutation-proven; old readers adapted.
@JarryShaw JarryShaw added breaking Breaks public-facing behaviour or API (apply alongside the type label) refactor Restructuring for its own sake — neither a fix nor a new capability (refactor: prefix) review: running A cross-review is in flight against the current head - no verdict yet labels Nov 4, 2026
@github-actions github-actions Bot added this to the PyPCAPKit v1.6 milestone Nov 4, 2026
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Nov 4, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

GOOD TO GO at 1e1140099 (Sonnet cross-review, and my own probe)

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: running A cross-review is in flight against the current head - no verdict yet labels Nov 4, 2026
@JarryShaw
JarryShaw merged commit 3c4b30e into main Nov 4, 2026
68 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Nov 4, 2026
@JarryShaw
JarryShaw deleted the refactor/2763-protocol-registry branch November 4, 2026 17:20
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Nov 4, 2026
JarryShaw added a commit that referenced this pull request Nov 4, 2026
- Breaking (#2770), a new entry after the package layout one:
  pcapkit.protocols.__proto__, a dict keyed by upper-cased name in 1.5.1, is
  removed with no alias; ProtocolBase.__registry__, get_protocol(),
  register_protocol() and the read-only registry view replace it, and a
  concrete Protocol subclass registers itself, measured against v1.5.1. The
  ruling goes into the conventions entry.
- The lazy-import entry no longer describes the removed registry module,
  which never shipped and stays unnamed.
- 1.5.0's __proto__ role points at ProtocolBase.__registry__, its text
  unchanged, so the nitpicky build does not warn on it.
- CHANGELOG.md regenerated; util/changelog_md.py --check passes.
JarryShaw added a commit that referenced this pull request Nov 4, 2026
- #2770: 1.5.1's Protocol.__init_subclass__ already registered a subclass
  as it was defined. The entry names what changed, measured on a v1.5.1
  export: an abstract subclass is skipped, where 1.5.x registered it, and a
  name clash keeps the existing entry and warns, where 1.5.x replaced it and
  warned that it was overwriting.
- #2767: drops "length", since IPv6_Ext's length at cut 1 is still open on
  #2761.
- CHANGELOG.md regenerated; util/changelog_md.py --check passes.
JarryShaw added a commit that referenced this pull request Nov 4, 2026
- #2777: the cut-header sentence reads the length back too, the rebuild read
  at the width its parse was given (__short_width__); the ruling into the
  conventions entry.
- #2778: a PCAP-NG block with octets after opt_endofopt rebuilds an info
  holding its options and padding_opts, where 1.5.1 rebuilt none, into the
  whole-header rebuild entry; 1.5.0's frozen entry is left as written.
- #2779: SCTP's reorder, the source-ordered member lists and the sorted
  Option.register clash warning ('opt', 'if', 'epb', ... in 1.5.1), into the
  member-order entry; the layer-grouped registry and the warning name restore
  ('put back silently' in 1.5.1), into the #2770 entry; the page wording into
  the conventions entry.
- #2781: import_lock serialising first resolutions, into the lazy-import
  entry; the removed registry lock never shipped and stays unnamed.
- CHANGELOG.md regenerated; util/changelog_md.py --check passes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Breaks public-facing behaviour or API (apply alongside the type label) refactor Restructuring for its own sake — neither a fix nor a new capability (refactor: prefix)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

refactor(protocols)!: replace the pcapkit.protocols.__proto__ dict with an auto-registering Registry class

1 participant