(cls: Type[_T],
name, # type: str
bases, # type: Tuple[type, ...]
dct # type: Dict[str, Any]
)
| 363 | |
| 364 | class Packet_metaclass(type): |
| 365 | def __new__(cls: Type[_T], |
| 366 | name, # type: str |
| 367 | bases, # type: Tuple[type, ...] |
| 368 | dct # type: Dict[str, Any] |
| 369 | ): |
| 370 | # type: (...) -> Type['Packet'] |
| 371 | if "fields_desc" in dct: # perform resolution of references to other packets # noqa: E501 |
| 372 | current_fld = dct["fields_desc"] # type: List[Union[scapy.fields.Field[Any, Any], Packet_metaclass]] # noqa: E501 |
| 373 | resolved_fld = [] # type: List[scapy.fields.Field[Any, Any]] |
| 374 | for fld_or_pkt in current_fld: |
| 375 | if isinstance(fld_or_pkt, Packet_metaclass): |
| 376 | # reference to another fields_desc |
| 377 | for pkt_fld in fld_or_pkt.fields_desc: |
| 378 | resolved_fld.append(pkt_fld) |
| 379 | else: |
| 380 | resolved_fld.append(fld_or_pkt) |
| 381 | else: # look for a fields_desc in parent classes |
| 382 | resolved_fld = [] |
| 383 | for b in bases: |
| 384 | if hasattr(b, "fields_desc"): |
| 385 | resolved_fld = b.fields_desc |
| 386 | break |
| 387 | |
| 388 | if resolved_fld: # perform default value replacements |
| 389 | final_fld = [] # type: List[scapy.fields.Field[Any, Any]] |
| 390 | names = [] |
| 391 | for f in resolved_fld: |
| 392 | if f.name in names: |
| 393 | war_msg = ( |
| 394 | "Packet '%s' has a duplicated '%s' field ! " |
| 395 | "If you are using several ConditionalFields, have " |
| 396 | "a look at MultipleTypeField instead ! This will " |
| 397 | "become a SyntaxError in a future version of " |
| 398 | "Scapy !" % ( |
| 399 | name, f.name |
| 400 | ) |
| 401 | ) |
| 402 | warnings.warn(war_msg, SyntaxWarning) |
| 403 | names.append(f.name) |
| 404 | if f.name in dct: |
| 405 | f = f.copy() |
| 406 | f.default = dct[f.name] |
| 407 | del dct[f.name] |
| 408 | final_fld.append(f) |
| 409 | |
| 410 | dct["fields_desc"] = final_fld |
| 411 | |
| 412 | dct.setdefault("__slots__", []) |
| 413 | for attr in ["name", "overload_fields"]: |
| 414 | try: |
| 415 | dct["_%s" % attr] = dct.pop(attr) |
| 416 | except KeyError: |
| 417 | pass |
| 418 | # Build and inject signature |
| 419 | try: |
| 420 | # Py3 only |
| 421 | import inspect |
| 422 | dct["__signature__"] = inspect.Signature([ |
nothing calls this directly
no test coverage detected