scapy icon indicating copy to clipboard operation
scapy copied to clipboard

fields: preserve containers when copying fields

Open T3pp31 opened this issue 6 months ago • 1 comments

Summary

Fix copying of _FieldContainer instances so wrappers such as Emph remain wrappers instead of being replaced by their contained field.

The root cause was that _FieldContainer delegated missing attributes to fld, so container.copy() resolved to container.fld.copy() when the container class did not define its own copy() method. This also affected packet field copying when fields_desc references another packet class and overrides a copied field default.

Fixes #4657.

Changes

  • Add _FieldContainer.copy() to copy the container instance and its stored attributes.
  • Delegate unsupported attribute assignment to the contained field, matching the existing read delegation needed by field default replacement.
  • Add a regression test for direct Emph(...).copy() and the packet-metaclass copy path from the issue.

Validation

  • python3 -m scapy.tools.UTscapy -t test/fields.uts -N -qq -f xUnit -o /tmp/scapy-fields.xml
  • /tmp/scapy-flake8-venv/bin/flake8 scapy/

Note: tox is not installed in this local environment, so flake8 was run through a temporary venv using the repository-pinned flake8<6.0.0 version.

T3pp31 avatar Jun 01 '26 14:06 T3pp31

Codecov Report

:x: Patch coverage is 83.33333% with 6 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 80.04%. Comparing base (66ef96a) to head (159afb7). :warning: Report is 48 commits behind head on master.

Files with missing lines Patch % Lines
scapy/fields.py 83.33% 6 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5009      +/-   ##
==========================================
- Coverage   80.31%   80.04%   -0.28%     
==========================================
  Files         381      384       +3     
  Lines       93630    95658    +2028     
==========================================
+ Hits        75202    76565    +1363     
- Misses      18428    19093     +665     
Files with missing lines Coverage Δ
scapy/fields.py 92.60% <83.33%> (-0.50%) :arrow_down:

... and 62 files with indirect coverage changes

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Jun 01 '26 23:06 codecov[bot]

Thanks for the review! I've pushed an update addressing both comments:

  • copy(): Simplified the implementation. The issue wasn't that we shouldn't call the sub-field's copy() at all — it's that without an explicit _FieldContainer.copy(), container.copy() was resolved via __getattr__ and returned only container.fld.copy() (a plain ByteField instead of Emph). The new version creates a new container instance and copies each __slots__ entry, calling .copy() on values that support it (e.g. the wrapped field).

  • __setattr__: Added a guard so delegation only happens for attributes that aren't defined on the container class. Assignments like mtf.fld = ... on MultipleTypeField now correctly raise AttributeError instead of silently mutating the selected underlying field.

Rebased onto current master. All test/fields.uts tests pass, including the new regression test.

T3pp31 avatar Jun 26 '26 16:06 T3pp31