Created
February 25, 2016 21:54
-
-
Save escapewindow/17ff4517f85dfe0cd20e to your computer and use it in GitHub Desktop.
this also fixes #154 but i'm not sure why this approach wasn't taken earlier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pgpy/pgp.py b/pgpy/pgp.py | |
index 89ff7f6..7d38f45 100644 | |
--- a/pgpy/pgp.py | |
+++ b/pgpy/pgp.py | |
@@ -852,11 +852,13 @@ class PGPMessage(PGPObject, Armorable): | |
:type sensitive: ``bool`` | |
:keyword compression: Set the compression algorithm for the new message. | |
Defaults to :py:obj:`CompressionAlgorithm.ZIP`. Ignored if cleartext is True. | |
+ :keyword encoding: if message is unicode or str, this is the encoding of the message | |
""" | |
cleartext = kwargs.pop('cleartext', False) | |
sensitive = kwargs.pop('sensitive', False) | |
compression = kwargs.pop('compression', CompressionAlgorithm.ZIP) | |
file = kwargs.pop('file', False) | |
+ encoding = kwargs.pop('encoding', 'utf-8') | |
filename = '' | |
mtime = datetime.utcnow() | |
@@ -878,7 +880,7 @@ class PGPMessage(PGPObject, Armorable): | |
else: | |
# load literal data | |
lit = LiteralData() | |
- lit._contents = bytearray(cls.text_to_bytes(message)) | |
+ lit._contents = bytearray(cls.text_to_bytes(message, encoding=encoding)) | |
lit.filename = '_CONSOLE' if sensitive else os.path.basename(filename) | |
lit.mtime = mtime | |
lit.format = 'b' | |
diff --git a/pgpy/types.py b/pgpy/types.py | |
index 2cc4320..a3ec5f0 100644 | |
--- a/pgpy/types.py | |
+++ b/pgpy/types.py | |
@@ -224,20 +224,14 @@ class PGPObject(six.with_metaclass(abc.ABCMeta, object)): | |
return i.to_bytes(blen, order) | |
@staticmethod | |
- def text_to_bytes(text): | |
- bin = bytearray() | |
- | |
- if text is None or isinstance(text, bytearray): | |
+ def text_to_bytes(text, encoding='utf-8'): | |
+ if text is None or isinstance(text, (six.binary_type, bytearray)): | |
return text | |
- | |
- for c in iter(ord(c) for c in text): | |
- if c < 256: | |
- bin.append(c) | |
- | |
- else: | |
- bin += PGPObject.int_to_bytes(c) | |
- | |
- return bytes(bin) | |
+ try: | |
+ binary_text = bytes(text) | |
+ except TypeError: | |
+ binary_text = bytes(text, encoding=encoding) | |
+ return binary_text | |
@abc.abstractmethod | |
def parse(self, packet): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment