Fix AttributeError in constructor error handling (fixes #907)#927
Open
Fix AttributeError in constructor error handling (fixes #907)#927
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
construct_scalar,construct_sequence,construct_mapping, orconstruct_pairsreceive a non-Node object, the error-handling code accessesnode.idunconditionally, causing anAttributeErrorinstead of the intendedConstructorError.See issue #907 for reproduction steps.
Fix
Replace direct
node.idaccess withgetattr(node, 'id', type(node).__name__)in four methods ofBaseConstructor. When the object is a proper Node, behavior is unchanged. When it lacks anidattribute, the error message falls back to the Python type name.Changes
construct_scalar: safe access to node.idconstruct_sequence: safe access to node.idconstruct_mapping: safe access to node.idconstruct_pairs: safe access to node.idAll changes are in
lib/yaml/constructor.py, 4 lines changed.Testing
Verified that:
ConstructorError(notAttributeError)