Skip to content

Fix AttributeError in constructor error handling (fixes #907)#927

Open
r266-tech wants to merge 1 commit intoyaml:mainfrom
r266-tech:fix-construct-error-node-id
Open

Fix AttributeError in constructor error handling (fixes #907)#927
r266-tech wants to merge 1 commit intoyaml:mainfrom
r266-tech:fix-construct-error-node-id

Conversation

@r266-tech
Copy link
Copy Markdown

Problem

When construct_scalar, construct_sequence, construct_mapping, or construct_pairs receive a non-Node object, the error-handling code accesses node.id unconditionally, causing an AttributeError instead of the intended ConstructorError.

See issue #907 for reproduction steps.

Fix

Replace direct node.id access with getattr(node, 'id', type(node).__name__) in four methods of BaseConstructor. When the object is a proper Node, behavior is unchanged. When it lacks an id attribute, the error message falls back to the Python type name.

Changes

  • construct_scalar: safe access to node.id
  • construct_sequence: safe access to node.id
  • construct_mapping: safe access to node.id
  • construct_pairs: safe access to node.id

All changes are in lib/yaml/constructor.py, 4 lines changed.

Testing

Verified that:

  1. Passing wrong node types now raises ConstructorError (not AttributeError)
  2. Normal YAML loading/parsing is unaffected

When construct_scalar, construct_sequence, construct_mapping, or
construct_pairs receive a non-Node object, the error handling code
accessed node.id unconditionally, causing an AttributeError instead
of the intended ConstructorError.

Use getattr(node, 'id', type(node).__name__) to safely access the
attribute and fall back to the type name when the object lacks an
'id' field.

Fixes yaml#907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants