Browse Source

add some docs...

add the correct ending to the exception when everything isn't
consumed..

[git-p4: depot-paths = "//depot/python/pypasn1/main/": change = 1826]
python2
John-Mark Gurney 8 years ago
parent
commit
370505f336
1 changed files with 15 additions and 1 deletions
  1. +15
    -1
      pasn1.py

+ 15
- 1
pasn1.py View File

@@ -82,6 +82,10 @@ class ASN1Object:
self._tag = tag self._tag = tag


class ASN1Coder(object): class ASN1Coder(object):
'''A class that contains an PASN.1 encoder/decoder.

Exports two methods, loads and dumps.'''

def __init__(self): def __init__(self):
pass pass


@@ -305,6 +309,8 @@ class ASN1Coder(object):
return r, end return r, end


def dumps(self, obj): def dumps(self, obj):
'''Convert obj into a string.'''

tf = self._typemap[type(obj)] tf = self._typemap[type(obj)]
fun = getattr(self, 'enc_%s' % tf) fun = getattr(self, 'enc_%s' % tf)
return self._typetag[tf] + fun(obj) return self._typetag[tf] + fun(obj)
@@ -340,12 +346,20 @@ class ASN1Coder(object):
return datetime.datetime.strptime(ts, fstr), end return datetime.datetime.strptime(ts, fstr), end


def loads(self, data, pos=0, end=None, consume=False): def loads(self, data, pos=0, end=None, consume=False):
'''Load from data, starting at pos (option), and ending
at end (optional). If it is required to consume the
whole string (not the default), set consume to True, and
a ValueError will be raised if the string is not
completely consumed. The second item in ValueError will
be the possition that was the detected end.'''

if end is None: if end is None:
end = len(data) end = len(data)
r, e = self._loads(data, pos, end) r, e = self._loads(data, pos, end)


if consume and e != end: if consume and e != end:
raise ValueError('entire string not consumed')
raise ValueError('entire string not consumed', e)

return r return r


def deeptypecmp(obj, o): def deeptypecmp(obj, o):


Loading…
Cancel
Save