diff options
author | elie <elie> | 2013-08-03 19:52:22 +0000 |
---|---|---|
committer | elie <elie> | 2013-08-03 19:52:22 +0000 |
commit | ea6cda4014d4db683ee42c0d101bbf93ff678cf0 (patch) | |
tree | 809c915cd624a0cc51a7203515cac69c253bcc2d | |
parent | da66cd7d7da700019f3e057c76e5e5034451f0e2 (diff) | |
download | pyasn1-ea6cda4014d4db683ee42c0d101bbf93ff678cf0.tar.gz |
unicode initializer support added to OctetString type and derivatives
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | pyasn1/type/univ.py | 20 | ||||
-rw-r--r-- | test/type/test_univ.py | 6 |
3 files changed, 24 insertions, 3 deletions
@@ -1,6 +1,7 @@ Revision 0.1.8 -------------- +- Unicode initializer support added to OctetString type and derivatives. - 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/type/univ.py b/pyasn1/type/univ.py index 58548b4..0ae98be 100644 --- a/pyasn1/type/univ.py +++ b/pyasn1/type/univ.py @@ -304,19 +304,33 @@ class OctetString(base.AbstractSimpleAsn1Item): def prettyIn(self, value): if isinstance(value, str): return value + elif isinstance(value, unicode): + try: + return value.encode(self._encoding) + except UnicodeEncodeError: + raise error.PyAsn1Error( + 'Can\'t encode string \'%s\' with \'%s\' codec' % (value, self._encoding) + ) elif isinstance(value, (tuple, list)): try: return ''.join([ chr(x) for x in value ]) except ValueError: raise error.PyAsn1Error( 'Bad OctetString initializer \'%s\'' % (value,) - ) + ) else: return str(value) else: def prettyIn(self, value): if isinstance(value, bytes): return value + elif isinstance(value, str): + try: + return value.encode(self._encoding) + except UnicodeEncodeError: + raise error.PyAsn1Error( + 'Can\'t encode string \'%s\' with \'%s\' codec' % (value, self._encoding) + ) elif isinstance(value, OctetString): return value.asOctets() elif isinstance(value, (tuple, list, map)): @@ -325,14 +339,14 @@ class OctetString(base.AbstractSimpleAsn1Item): except ValueError: raise error.PyAsn1Error( 'Bad OctetString initializer \'%s\'' % (value,) - ) + ) else: try: return str(value).encode(self._encoding) except UnicodeEncodeError: raise error.PyAsn1Error( 'Can\'t encode string \'%s\' with \'%s\' codec' % (value, self._encoding) - ) + ) def fromBinaryString(self, value): diff --git a/test/type/test_univ.py b/test/type/test_univ.py index 4c7027d..4ad2af3 100644 --- a/test/type/test_univ.py +++ b/test/type/test_univ.py @@ -114,6 +114,12 @@ class OctetStringTestCase(unittest.TestCase): assert univ.OctetString(hexValue="FA9823C43E43510DE3422") == ints2octs((250, 152, 35, 196, 62, 67, 81, 13, 227, 66, 32)), 'hex init fails' def testTuple(self): assert univ.OctetString((1,2,3,4,5)) == ints2octs((1,2,3,4,5)), 'tuple init failed' + if version_info[0] <= 2: + def testUnicode(self): + assert univ.OctetString(u'q') == 'q', 'unicode init fails' + else: + def testUnicode(self): + assert univ.OctetString('q') == str2octs('q'), 'unicode init fails' def testStr(self): assert str(univ.OctetString('q')) == 'q', '__str__() fails' def testSeq(self): |