summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2014-06-13 08:45:49 +0000
committerelie <elie>2014-06-13 08:45:49 +0000
commit599c9614614b48a45b6d51d9f7288c44b791b5c2 (patch)
tree6a544f0fc8a47565d6092931a258c69e51d8c86c
parentfa9bdec43d966aca2b5dc5d42bff6ddc904a19a2 (diff)
downloadpyasn1-599c9614614b48a45b6d51d9f7288c44b791b5c2.tar.gz
* new prettyPrintType() abstract method implemented to base pyasn1 types
to facilitate encoding errors analisys. * the __str__() method implemented to Tag, TagSet and TagMap classes to ease encoding errors troubleshooting.
-rw-r--r--CHANGES5
-rw-r--r--pyasn1/codec/ber/decoder.py16
-rw-r--r--pyasn1/codec/ber/encoder.py4
-rw-r--r--pyasn1/type/base.py3
-rw-r--r--pyasn1/type/tag.py8
-rw-r--r--pyasn1/type/tagmap.py18
-rw-r--r--pyasn1/type/univ.py19
7 files changed, 60 insertions, 13 deletions
diff --git a/CHANGES b/CHANGES
index 4a71a3a..13b7588 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,11 @@ Revision 0.1.8
- ObjectIdentifier codec fixed to work properly with arc 0 and arc 2 values.
- Explicit limit on ObjectIdentifier arc value size removed.
- Unicode initializer support added to OctetString type and derivatives.
+- New prettyPrintType() abstract method implemented to base pyasn1 types
+ to facilitate encoding errors analisys.
+- The __str__() method implemented to Tag, TagSet and TagMap classes to
+ ease encoding errors troubleshooting.
+ easing encoding errors
- Fix to SEQUENCE and SET types to give them their private componentTypes
collection (which is a NamedTypes object) so that they won't collide in
a MT execution environment.
diff --git a/pyasn1/codec/ber/decoder.py b/pyasn1/codec/ber/decoder.py
index d0c9814..505d00f 100644
--- a/pyasn1/codec/ber/decoder.py
+++ b/pyasn1/codec/ber/decoder.py
@@ -18,7 +18,7 @@ class AbstractSimpleDecoder(AbstractDecoder):
tagFormats = (tag.tagFormatSimple,)
def _createComponent(self, asn1Spec, tagSet, value=None):
if tagSet[0][1] not in self.tagFormats:
- raise error.PyAsn1Error('Invalid tag format %r for %r' % (tagSet[0], self.protoComponent,))
+ raise error.PyAsn1Error('Invalid tag format %s for %s' % (tagSet[0], self.protoComponent.prettyPrintType()))
if asn1Spec is None:
return self.protoComponent.clone(value, tagSet)
elif value is None:
@@ -30,7 +30,7 @@ class AbstractConstructedDecoder(AbstractDecoder):
tagFormats = (tag.tagFormatConstructed,)
def _createComponent(self, asn1Spec, tagSet, value=None):
if tagSet[0][1] not in self.tagFormats:
- raise error.PyAsn1Error('Invalid tag format %r for %r' % (tagSet[0], self.protoComponent,))
+ raise error.PyAsn1Error('Invalid tag format %s for %s' % (tagSet[0], self.protoComponent.prettyPrintType()))
if asn1Spec is None:
return self.protoComponent.clone(tagSet)
else:
@@ -660,7 +660,7 @@ class Decoder:
else:
tagSet = lastTag + tagSet
state = stDecodeLength
- debug.logger and debug.logger & debug.flagDecoder and debug.logger('tag decoded into %r, decoding length' % tagSet)
+ debug.logger and debug.logger & debug.flagDecoder and debug.logger('tag decoded into %s, decoding length' % tagSet)
if state == stDecodeLength:
# Decode length
if not substrate:
@@ -745,12 +745,12 @@ class Decoder:
if debug.logger and debug.logger & debug.flagDecoder:
debug.logger('candidate ASN.1 spec is a map of:')
for t, v in asn1Spec.getPosMap().items():
- debug.logger(' %r -> %s' % (t, v.__class__.__name__))
+ debug.logger(' %s -> %s' % (t, v.__class__.__name__))
if asn1Spec.getNegMap():
debug.logger('but neither of: ')
for t, v in asn1Spec.getNegMap().items():
- debug.logger(' %r -> %s' % (t, v.__class__.__name__))
- debug.logger('new candidate ASN.1 spec is %s, chosen by %r' % (__chosenSpec is None and '<none>' or __chosenSpec.__class__.__name__, tagSet))
+ debug.logger(' %s -> %s' % (t, v.__class__.__name__))
+ debug.logger('new candidate ASN.1 spec is %s, chosen by %s' % (__chosenSpec is None and '<none>' or __chosenSpec.prettyPrintType(), tagSet))
else:
__chosenSpec = asn1Spec
debug.logger and debug.logger & debug.flagDecoder and debug.logger('candidate ASN.1 spec is %s' % asn1Spec.__class__.__name__)
@@ -768,7 +768,7 @@ class Decoder:
elif baseTagSet in self.__tagMap:
# base type or tagged subtype
concreteDecoder = self.__tagMap[baseTagSet]
- debug.logger and debug.logger & debug.flagDecoder and debug.logger('value decoder chosen by base %r' % (baseTagSet,))
+ debug.logger and debug.logger & debug.flagDecoder and debug.logger('value decoder chosen by base %s' % (baseTagSet,))
else:
concreteDecoder = None
if concreteDecoder:
@@ -818,7 +818,7 @@ class Decoder:
debug.logger and debug.logger & debug.flagDecoder and debug.logger('codec %s yields type %s, value:\n%s\n...remaining substrate is: %s' % (concreteDecoder.__class__.__name__, value.__class__.__name__, value.prettyPrint(), substrate and debug.hexdump(substrate) or '<none>'))
if state == stErrorCondition:
raise error.PyAsn1Error(
- '%r not in asn1Spec: %r' % (tagSet, asn1Spec)
+ '%s not in asn1Spec: %s' % (tagSet, asn1Spec)
)
if debug.logger and debug.logger & debug.flagDecoder:
debug.scope.pop()
diff --git a/pyasn1/codec/ber/encoder.py b/pyasn1/codec/ber/encoder.py
index 410d129..ff215ac 100644
--- a/pyasn1/codec/ber/encoder.py
+++ b/pyasn1/codec/ber/encoder.py
@@ -401,7 +401,7 @@ class Encoder:
self.__typeMap = typeMap
def __call__(self, value, defMode=1, maxChunkSize=0):
- debug.logger & debug.flagEncoder and debug.logger('encoder called in %sdef mode, chunk size %s for type %s, value:\n%s' % (not defMode and 'in' or '', maxChunkSize, value.__class__.__name__, value.prettyPrint()))
+ debug.logger & debug.flagEncoder and debug.logger('encoder called in %sdef mode, chunk size %s for type %s, value:\n%s' % (not defMode and 'in' or '', maxChunkSize, value.prettyPrintType(), value.prettyPrint()))
tagSet = value.getTagSet()
if len(tagSet) > 1:
concreteEncoder = explicitlyTaggedItemEncoder
@@ -416,7 +416,7 @@ class Encoder:
concreteEncoder = self.__tagMap[tagSet]
else:
raise Error('No encoder for %s' % (value,))
- debug.logger & debug.flagEncoder and debug.logger('using value codec %s chosen by %r' % (concreteEncoder.__class__.__name__, tagSet))
+ debug.logger & debug.flagEncoder and debug.logger('using value codec %s chosen by %s' % (concreteEncoder.__class__.__name__, tagSet))
substrate = concreteEncoder.encode(
self, value, defMode, maxChunkSize
)
diff --git a/pyasn1/type/base.py b/pyasn1/type/base.py
index 78d316f..600ae98 100644
--- a/pyasn1/type/base.py
+++ b/pyasn1/type/base.py
@@ -140,6 +140,9 @@ class AbstractSimpleAsn1Item(Asn1ItemBase):
# XXX Compatibility stub
def prettyPrinter(self, scope=0): return self.prettyPrint(scope)
+ def prettyPrintType(self, scope=0):
+ return '%s -> %s' % (self.getTagSet(), self.__class__.__name__)
+
#
# Constructed types:
# * There are five of them: Sequence, SequenceOf/SetOf, Set and Choice
diff --git a/pyasn1/type/tag.py b/pyasn1/type/tag.py
index 18b6559..7471a9b 100644
--- a/pyasn1/type/tag.py
+++ b/pyasn1/type/tag.py
@@ -24,6 +24,9 @@ class Tag:
self.uniq = (tagClass, tagId)
self.__hashedUniqTag = hash(self.uniq)
+ def __str__(self):
+ return '[%s:%s:%s]' % self.__tag
+
def __repr__(self):
return '%s(tagClass=%s, tagFormat=%s, tagId=%s)' % (
(self.__class__.__name__,) + self.__tag
@@ -62,7 +65,10 @@ class TagSet:
_uniq = _uniq + t.uniq
self.uniq = _uniq
self.__lenOfSuperTags = len(superTags)
-
+
+ def __str__(self):
+ return self.__superTags and '+'.join([str(x) for x in self.__superTags]) or '[untagged]'
+
def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
diff --git a/pyasn1/type/tagmap.py b/pyasn1/type/tagmap.py
index 7cec3a1..feb91ae 100644
--- a/pyasn1/type/tagmap.py
+++ b/pyasn1/type/tagmap.py
@@ -21,9 +21,23 @@ class TagMap:
raise KeyError()
def __repr__(self):
- s = '%r/%r' % (self.__posMap, self.__negMap)
+ s = self.__class__.__name__ + '('
+ if self.__posMap:
+ s = s + 'posMap=%r, ' % (self.__posMap,)
+ if self.__negMap:
+ s = s + 'negMap=%r, ' % (self.__negMap,)
if self.__defType is not None:
- s = s + '/%r' % (self.__defType,)
+ s = s + 'defType=%r' % (self.__defType,)
+ return s + ')'
+
+ def __str__(self):
+ s = self.__class__.__name__ + ':\n'
+ if self.__posMap:
+ s = s + 'posMap:\n%s, ' % ',\n '.join([ x.prettyPrintType() for x in self.__posMap.values()])
+ if self.__negMap:
+ s = s + 'negMap:\n%s, ' % ',\n '.join([ x.prettyPrintType() for x in self.__negMap.values()])
+ if self.__defType is not None:
+ s = s + 'defType:\n%s, ' % self.__defType.prettyPrintType()
return s
def clone(self, parentType, tagMap, uniq=False):
diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py
index ad9b84a..8638ad2 100644
--- a/pyasn1/type/univ.py
+++ b/pyasn1/type/univ.py
@@ -770,6 +770,14 @@ class SetOf(base.AbstractConstructedAsn1Item):
r = r + self._componentValues[idx].prettyPrint(scope)
return r
+ def prettyPrintType(self, scope=0):
+ scope = scope + 1
+ r = '%s -> %s {\n' % (self.getTagSet(), self.__class__.__name__)
+ if self._componentType is not None:
+ r = r + ' '*scope
+ r = r + self._componentType.prettyPrintType(scope)
+ return r + '\n' + ' '*(scope-1) + '}'
+
class SequenceOf(SetOf):
tagSet = baseTagSet = tag.initTagSet(
tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10)
@@ -912,6 +920,17 @@ class SequenceAndSetBase(base.AbstractConstructedAsn1Item):
)
return r
+ def prettyPrintType(self, scope=0):
+ scope = scope + 1
+ r = '%s -> %s {\n' % (self.getTagSet(), self.__class__.__name__)
+ for idx in range(len(self.componentType)):
+ r = r + ' '*scope
+ r = r + '"%s"' % self.componentType.getNameByPosition(idx)
+ r = '%s = %s\n' % (
+ r, self._componentType.getTypeByPosition(idx).prettyPrintType(scope)
+ )
+ return r + '\n' + ' '*(scope-1) + '}'
+
class Sequence(SequenceAndSetBase):
tagSet = baseTagSet = tag.initTagSet(
tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10)