(self, value)
| 632 | return value |
| 633 | |
| 634 | def prettyIn(self, value): |
| 635 | if isinstance(value, SizedInteger): |
| 636 | return value |
| 637 | elif isinstance(value, str): |
| 638 | if not value: |
| 639 | return SizedInteger(0).setBitLength(0) |
| 640 | |
| 641 | elif value[0] == '\'': # "'1011'B" -- ASN.1 schema representation (deprecated) |
| 642 | if value[-2:] == '\'B': |
| 643 | return self.fromBinaryString(value[1:-2], internalFormat=True) |
| 644 | elif value[-2:] == '\'H': |
| 645 | return self.fromHexString(value[1:-2], internalFormat=True) |
| 646 | else: |
| 647 | raise error.PyAsn1Error( |
| 648 | 'Bad BIT STRING value notation %s' % (value,) |
| 649 | ) |
| 650 | |
| 651 | elif self.namedValues and not value.isdigit(): # named bits like 'Urgent, Active' |
| 652 | names = [x.strip() for x in value.split(',')] |
| 653 | |
| 654 | try: |
| 655 | |
| 656 | bitPositions = [self.namedValues[name] for name in names] |
| 657 | |
| 658 | except KeyError: |
| 659 | raise error.PyAsn1Error('unknown bit name(s) in %r' % (names,)) |
| 660 | |
| 661 | rightmostPosition = max(bitPositions) |
| 662 | |
| 663 | number = 0 |
| 664 | for bitPosition in bitPositions: |
| 665 | number |= 1 << (rightmostPosition - bitPosition) |
| 666 | |
| 667 | return SizedInteger(number).setBitLength(rightmostPosition + 1) |
| 668 | |
| 669 | elif value.startswith('0x'): |
| 670 | return self.fromHexString(value[2:], internalFormat=True) |
| 671 | |
| 672 | elif value.startswith('0b'): |
| 673 | return self.fromBinaryString(value[2:], internalFormat=True) |
| 674 | |
| 675 | else: # assume plain binary string like '1011' |
| 676 | return self.fromBinaryString(value, internalFormat=True) |
| 677 | |
| 678 | elif isinstance(value, (tuple, list)): |
| 679 | return self.fromBinaryString(''.join([b and '1' or '0' for b in value]), internalFormat=True) |
| 680 | |
| 681 | elif isinstance(value, BitString): |
| 682 | return SizedInteger(value).setBitLength(len(value)) |
| 683 | |
| 684 | elif isinstance(value, int): |
| 685 | return SizedInteger(value) |
| 686 | |
| 687 | else: |
| 688 | raise error.PyAsn1Error( |
| 689 | 'Bad BitString initializer type \'%s\'' % (value,) |
| 690 | ) |
| 691 |
no test coverage detected