From 9a57cfcf8d28875132abbd8b93f63a433f8a53dd Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Thu, 18 Oct 2018 09:04:32 +0200 Subject: Fix defaulted constructed SEQUENCE component initialization When SEQUENCE has defaulted component of constructed type, recursively instantiate defaulted component and assign instantiated asn1 object to SEQUENCE field. --- CHANGES.rst | 3 ++- pyasn1/type/univ.py | 4 +++- tests/type/test_univ.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e9a8de5..f17e27c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,5 +1,5 @@ -Revision 0.4.5, released XX-08-2018 +Revision 0.4.5, released XX-10-2018 ----------------------------------- - Debug logging refactored for more efficiency when disabled and @@ -8,6 +8,7 @@ Revision 0.4.5, released XX-08-2018 from codec main loop as it used to be. - More debug logging added to BER family of codecs to ease encoding problems troubleshooting. +- Fixed defaulted constructed SEQUENCE component initialization. Revision 0.4.4, released 26-07-2018 ----------------------------------- diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py index 898cf25..492a118 100644 --- a/pyasn1/type/univ.py +++ b/pyasn1/type/univ.py @@ -2347,7 +2347,9 @@ class SequenceAndSetBase(base.AbstractConstructedAsn1Item): if value is noValue: if componentTypeLen: - value = componentType.getTypeByPosition(idx).clone() + value = componentType.getTypeByPosition(idx) + if isinstance(value, base.AbstractConstructedAsn1Item): + value = value.clone(cloneValueFlag=componentType[idx].isDefaulted) elif currentValue is noValue: raise error.PyAsn1Error('Component type not defined') diff --git a/tests/type/test_univ.py b/tests/type/test_univ.py index 44a3add..357c9e5 100644 --- a/tests/type/test_univ.py +++ b/tests/type/test_univ.py @@ -1407,6 +1407,22 @@ class Sequence(BaseTestCase): s.clear() assert s.getComponentByPosition(1, default=None) is None + def testGetComponentWithConstructedDefault(self): + + class Sequence(univ.Sequence): + componentType = namedtype.NamedTypes( + namedtype.NamedType('name', univ.OctetString()), + namedtype.DefaultedNamedType('nick', univ.SequenceOf( + componentType=univ.Integer() + ).setComponentByPosition(0, 1)), + ) + + s = Sequence() + + assert s.getComponentByPosition(1, default=None, instantiate=False) is None + assert s.getComponentByPosition(1, instantiate=False) is univ.noValue + assert s.getComponentByPosition(1) == [1] + def testGetComponentNoInstantiation(self): class Sequence(univ.Sequence): -- cgit v1.2.1