diff options
author | elie <elie> | 2013-11-30 20:31:16 +0000 |
---|---|---|
committer | elie <elie> | 2013-11-30 20:31:16 +0000 |
commit | 628883bb4f65196dd162de9fb3373447f02ff672 (patch) | |
tree | fec8ca179dc37438bac0d9620667b06d9ac147cf | |
parent | a33bc8fd20d726bd0e19b499f6775eb8e69c844a (diff) | |
download | pyasn1-628883bb4f65196dd162de9fb3373447f02ff672.tar.gz |
* the base.NoValue() class, that indicates uninitialized ASN.1 object,
made public.
* fixes to __repr__() implementation of many Scalar types to take into
account all of their initializers such as tagSet, subtypeSpec etc.
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | pyasn1/type/base.py | 18 | ||||
-rw-r--r-- | pyasn1/type/univ.py | 30 |
3 files changed, 41 insertions, 12 deletions
@@ -10,7 +10,12 @@ Revision 0.1.8 - Missing T61String,ISO646String character types and ObjectDescriptor useful type added. - Distribute is gone, switched to setuptools completely. +- Missing NamedValues.__repr__() added. +- The base.NoValue() class, that indicates uninitialized ASN.1 object, + made public. - Fix to NamedType.__repr__() to work properly. +- Fixes to __repr__() implementation of many Scalar types to take into + account all of their initializers such as tagSet, subtypeSpec etc. - String typed float initializer to REAL type now supported. - Float typed mantissa initializer to REAL type for base 2 added. - Encoding bases 8 and 16 support for REAL type binary encoder added. diff --git a/pyasn1/type/base.py b/pyasn1/type/base.py index 6c7eeec..19b037c 100644 --- a/pyasn1/type/base.py +++ b/pyasn1/type/base.py @@ -47,13 +47,14 @@ class Asn1ItemBase(Asn1Item): return self._tagSet.isSuperTagSetOf(other.getTagSet()) and \ self._subtypeSpec.isSuperTypeOf(other.getSubtypeSpec()) -class __NoValue: +class NoValue: def __getattr__(self, attr): raise error.PyAsn1Error('No value for %s()' % attr) def __getitem__(self, i): raise error.PyAsn1Error('No value') + def __repr__(self): return '%s()' % self.__class__.__name__ -noValue = __NoValue() +noValue = NoValue() # Base class for "simple" ASN.1 objects. These are immutable. class AbstractSimpleAsn1Item(Asn1ItemBase): @@ -72,10 +73,15 @@ class AbstractSimpleAsn1Item(Asn1ItemBase): self._len = None def __repr__(self): - if self._value is noValue: - return self.__class__.__name__ + '()' - else: - return self.__class__.__name__ + '(%s)' % (self.prettyOut(self._value),) + r = [] + if self._value is not self.defaultValue: + r.append(self.prettyOut(self._value)) + if self._tagSet is not self.tagSet: + r.append('tagSet=%r' % (self._tagSet,)) + if self._subtypeSpec is not self.subtypeSpec: + r.append('subtypeSpec=%r' % (self._subtypeSpec,)) + return '%s(%s)' % (self.__class__.__name__, ', '.join(r)) + def __str__(self): return str(self._value) def __eq__(self, other): return self is other and True or self._value == other diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py index f32b4fd..29f99fd 100644 --- a/pyasn1/type/univ.py +++ b/pyasn1/type/univ.py @@ -22,6 +22,12 @@ class Integer(base.AbstractSimpleAsn1Item): self, value, tagSet, subtypeSpec ) + def __repr__(self): + if self.__namedValues is not self.namedValues: + return '%s, %r)' % (base.AbstractSimpleAsn1Item.__repr__(self)[:-1], self.__namedValues) + else: + return base.AbstractSimpleAsn1Item.__repr__(self) + def __and__(self, value): return self.clone(self._value & value) def __rand__(self, value): return self.clone(value & self._value) def __or__(self, value): return self.clone(self._value | value) @@ -392,12 +398,24 @@ class OctetString(base.AbstractSimpleAsn1Item): return str(value) def __repr__(self): - if self._value is base.noValue: - return self.__class__.__name__ + '()' - if [ x for x in self.asNumbers() if x < 32 or x > 126 ]: - return self.__class__.__name__ + '(hexValue=\'' + ''.join([ '%.2x' % x for x in self.asNumbers() ])+'\')' - else: - return self.__class__.__name__ + '(\'' + self.prettyOut(self._value) + '\')' + r = [] + doHex = False + if self._value is not self.defaultValue: + for x in self.asNumbers(): + if x < 32 or x > 126: + doHex = True + break + if not doHex: + r.append('%r' % (self._value,)) + if self._tagSet is not self.tagSet: + r.append('tagSet=%r' % (self._tagSet,)) + if self._subtypeSpec is not self.subtypeSpec: + r.append('subtypeSpec=%r' % (self._subtypeSpec,)) + if self.encoding is not self._encoding: + r.append('encoding=%r' % (self._encoding,)) + if doHex: + r.append('hexValue=%r' % ''.join([ '%.2x' % x for x in self.asNumbers() ])) + return '%s(%s)' % (self.__class__.__name__, ', '.join(r)) if sys.version_info[0] <= 2: def __str__(self): return str(self._value) |