| 142 | |
| 143 | |
| 144 | class SetEncoder(encoder.SequenceEncoder): |
| 145 | @staticmethod |
| 146 | def _componentSortKey(componentAndType): |
| 147 | """Sort SET components by tag |
| 148 | |
| 149 | Sort regardless of the Choice value (static sort) |
| 150 | """ |
| 151 | component, asn1Spec = componentAndType |
| 152 | |
| 153 | if asn1Spec is None: |
| 154 | asn1Spec = component |
| 155 | |
| 156 | if asn1Spec.typeId == univ.Choice.typeId and not asn1Spec.tagSet: |
| 157 | if asn1Spec.tagSet: |
| 158 | return asn1Spec.tagSet |
| 159 | else: |
| 160 | return asn1Spec.componentType.minTagSet |
| 161 | else: |
| 162 | return asn1Spec.tagSet |
| 163 | |
| 164 | def encodeValue(self, value, asn1Spec, encodeFun, **options): |
| 165 | |
| 166 | substrate = b'' |
| 167 | |
| 168 | comps = [] |
| 169 | compsMap = {} |
| 170 | |
| 171 | if asn1Spec is None: |
| 172 | # instance of ASN.1 schema |
| 173 | inconsistency = value.isInconsistent |
| 174 | if inconsistency: |
| 175 | raise error.PyAsn1Error( |
| 176 | f"ASN.1 object {value.__class__.__name__} is inconsistent") |
| 177 | |
| 178 | namedTypes = value.componentType |
| 179 | |
| 180 | for idx, component in enumerate(value.values()): |
| 181 | if namedTypes: |
| 182 | namedType = namedTypes[idx] |
| 183 | |
| 184 | if namedType.isOptional and not component.isValue: |
| 185 | continue |
| 186 | |
| 187 | if namedType.isDefaulted and component == namedType.asn1Object: |
| 188 | continue |
| 189 | |
| 190 | compsMap[id(component)] = namedType |
| 191 | |
| 192 | else: |
| 193 | compsMap[id(component)] = None |
| 194 | |
| 195 | comps.append((component, asn1Spec)) |
| 196 | |
| 197 | else: |
| 198 | # bare Python value + ASN.1 schema |
| 199 | for idx, namedType in enumerate(asn1Spec.componentType.namedTypes): |
| 200 | |
| 201 | try: |