← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-gpg-doctests-bytes into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-gpg-doctests-bytes into launchpad:master.

Commit message:
Fix GPG doctests to pass input as bytes

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/397830
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-gpg-doctests-bytes into launchpad:master.
diff --git a/lib/lp/archivepublisher/tests/archive-signing.txt b/lib/lp/archivepublisher/tests/archive-signing.txt
index d61bd69..52a0f3a 100644
--- a/lib/lp/archivepublisher/tests/archive-signing.txt
+++ b/lib/lp/archivepublisher/tests/archive-signing.txt
@@ -167,7 +167,7 @@ to test the export funtions
     ...         self.secret = secret
     ...         self.fingerprint = 'fpr'
     ...     def export(self):
-    ...         return "Secret %s" % self.secret
+    ...         return six.ensure_binary("Secret %s" % self.secret)
 
 exportSecretKey() raises an error if given a public key.
 
@@ -336,7 +336,8 @@ printing the context the key is generated.
     >>> def mock_key_generator(name, logger=None):
     ...     print 'Generating:', name
     ...     key_path = os.path.join(gpgkeysdir, 'sign.only@xxxxxxxxxxxxxxxxx')
-    ...     return gpghandler.importSecretKey(open(key_path).read())
+    ...     with open(key_path, 'rb') as f:
+    ...         return gpghandler.importSecretKey(f.read())
 
     >>> from zope.security.proxy import removeSecurityProxy
     >>> naked_gpghandler = removeSecurityProxy(gpghandler)
@@ -426,20 +427,23 @@ keyserver.
     >>> retrieved_key = gpghandler.retrieveKey(
     ...    signing_key.fingerprint)
 
-    >>> signature = gpghandler.getVerifiedSignature(
-    ...     content=open(release_path).read(),
-    ...     signature=open(release_path + '.gpg').read())
+    >>> with open(release_path, 'rb') as release_file:
+    ...     with open(release_path + '.gpg', 'rb') as signature_file:
+    ...         signature = gpghandler.getVerifiedSignature(
+    ...             content=release_file.read(),
+    ...             signature=signature_file.read())
 
     >>> expected_fingerprint = (
     ...     archive_signing_key.archive.signing_key.fingerprint)
     >>> signature.fingerprint == expected_fingerprint
     True
 
-    >>> inline_signature = gpghandler.getVerifiedSignature(
-    ...     content=open(inline_release_path).read())
+    >>> with open(inline_release_path, 'rb') as inline_release_file:
+    ...     inline_signature = gpghandler.getVerifiedSignature(
+    ...         content=inline_release_file.read())
     >>> inline_signature.fingerprint == expected_fingerprint
     True
-    >>> print inline_signature.plain_data
+    >>> print(inline_signature.plain_data.decode('UTF-8'))
     This is a fake release file.
     <BLANKLINE>
 
diff --git a/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt b/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt
index a816a5e..d705ecd 100644
--- a/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt
+++ b/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt
@@ -287,7 +287,8 @@ A419AE861E88BC9E04B9C26FBA2B9389DFD20543:
 If they sign the text correctly, they are redirected to their home page.
 
     >>> login(ANONYMOUS)
-    >>> good = gpghandler.signContent(bytes(verification_content), key, 'test')
+    >>> good = gpghandler.signContent(
+    ...     six.ensure_binary(verification_content), key, 'test')
     >>> logout()
 
     >>> browser.getControl('Signed text').value = good
diff --git a/lib/lp/services/gpg/doc/gpg-encryption.txt b/lib/lp/services/gpg/doc/gpg-encryption.txt
index d453e14..7bef091 100644
--- a/lib/lp/services/gpg/doc/gpg-encryption.txt
+++ b/lib/lp/services/gpg/doc/gpg-encryption.txt
@@ -92,7 +92,7 @@ Decrypt a unicode content:
 
 What about a message encrypted for an unknown key.
 
-    >>> cipher = """-----BEGIN PGP MESSAGE-----
+    >>> cipher = b"""-----BEGIN PGP MESSAGE-----
     ... Version: GnuPG v1.2.5 (GNU/Linux)
     ...
     ... hQEOAxkWbKKY/feTEAP+NoZRhb+/2OOd4f0FGRe7XP1HMvsRZ7X0uk/EjCv1CVnn
diff --git a/lib/lp/services/gpg/doc/gpg-signatures.txt b/lib/lp/services/gpg/doc/gpg-signatures.txt
index 2f98077..50c807b 100644
--- a/lib/lp/services/gpg/doc/gpg-signatures.txt
+++ b/lib/lp/services/gpg/doc/gpg-signatures.txt
@@ -29,7 +29,7 @@ OpenPGP Signature Verification
 
 The text below was "clear signed" by 0xDFD20543 master key:
 
-    >>> content = """-----BEGIN PGP SIGNED MESSAGE-----
+    >>> content = b"""-----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
     ... Test Message.
@@ -48,7 +48,7 @@ The text below was "clear signed" by 0xDFD20543 master key:
 
 The text below was "clear signed" by a 0x02BA5EF6, a subkey of 0xDFD20543
 
-    >>> content = """-----BEGIN PGP SIGNED MESSAGE-----
+    >>> content = b"""-----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
     ... Test Message.
@@ -72,7 +72,7 @@ The text below was "clear signed" by a 0x02BA5EF6, a subkey of 0xDFD20543
 
 The text below was "clear signed" by 0xDFD20543 master key but tampered with:
 
-    >>> content = """-----BEGIN PGP SIGNED MESSAGE-----
+    >>> content = b"""-----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
     ... Test Message
@@ -93,7 +93,7 @@ The text below was "clear signed" by 0xDFD20543 master key but tampered with:
 
 If no signed content is found, an exception is raised:
 
-    >>> content = """-----BEGIN PGP SIGNED MESSAGE-----
+    >>> content = b"""-----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
     ... Test Message
@@ -112,7 +112,7 @@ The text below contains two clear signed sections.  As there are two
 signing keys involved here, we raise a verification error, since the
 signed text can not be attributed solely to either key:
 
-    >>> content = """
+    >>> content = b"""
     ... -----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
@@ -157,7 +157,7 @@ The text below was signed by  key that's is not part of the
 imported keyring. Note that we have extra debug information containing
 the GPGME error codes (they may be helpful).
 
-    >>> content = """-----BEGIN PGP SIGNED MESSAGE-----
+    >>> content = b"""-----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
     ... Text Message
@@ -198,7 +198,7 @@ with information about the error.  These come directly from the gpgme module
 itself.
 
     >>> from lp.services.gpg.interfaces import GPGVerificationError
-    >>> content = """
+    >>> content = b"""
     ... -----BEGIN PGP SIGNED MESSAGE-----
     ... Hash: SHA1
     ...
diff --git a/lib/lp/services/gpg/doc/gpghandler.txt b/lib/lp/services/gpg/doc/gpghandler.txt
index 0fabddf..7778775 100644
--- a/lib/lp/services/gpg/doc/gpghandler.txt
+++ b/lib/lp/services/gpg/doc/gpghandler.txt
@@ -38,7 +38,7 @@ unit tests somewhere else at some point. -- Guilherme Salgado, 2006-08-23
 
 A GPGKeyNotFoundError is raised if we try to import an empty content.
 
-    >>> key = gpghandler.importPublicKey('')
+    >>> key = gpghandler.importPublicKey(b'')
     ... # doctest: +IGNORE_EXCEPTION_MODULE_IN_PYTHON2
     Traceback (most recent call last):
     ...
@@ -46,7 +46,7 @@ A GPGKeyNotFoundError is raised if we try to import an empty content.
 
 The same happens for bogus content.
 
-    >>> key = gpghandler.importPublicKey('XXXXXXXXX')
+    >>> key = gpghandler.importPublicKey(b'XXXXXXXXX')
     ... # doctest: +IGNORE_EXCEPTION_MODULE_IN_PYTHON2
     Traceback (most recent call last):
     ...
@@ -57,7 +57,8 @@ Let's recover some coherent data and verify if it works as expected:
     >>> import os
     >>> from lp.testing.gpgkeys import gpgkeysdir
     >>> filepath = os.path.join(gpgkeysdir, 'test@xxxxxxxxxxxxxxxxx')
-    >>> pubkey = open(filepath).read()
+    >>> with open(filepath, 'rb') as f:
+    ...     pubkey = f.read()
     >>> key = gpghandler.importPublicKey(pubkey)
 
     >>> verifyObject(IPymeKey, key)
@@ -83,7 +84,7 @@ Let's recover some coherent data and verify if it works as expected:
 
 Public keys can be exported in ASCII-armored format.
 
-    >>> print key.export()
+    >>> print(six.ensure_text(key.export()))
     -----BEGIN PGP PUBLIC KEY BLOCK-----
     ...
     -----END PGP PUBLIC KEY BLOCK-----
@@ -93,7 +94,8 @@ Now, try to import a secret key, which will cause a
 SecretGPGKeyImportDetected exception to be raised.
 
     >>> filepath = os.path.join(gpgkeysdir, 'test@xxxxxxxxxxxxxxxxx')
-    >>> seckey = open(filepath).read()
+    >>> with open(filepath, 'rb') as f:
+    ...     seckey = f.read()
     >>> key = gpghandler.importPublicKey(seckey)
     ... # doctest: +IGNORE_EXCEPTION_MODULE_IN_PYTHON2
     Traceback (most recent call last):
@@ -104,8 +106,9 @@ Now, try to import two public keys, causing a MoreThanOneGPGKeyFound
 exception to be raised.
 
     >>> filepath = os.path.join(gpgkeysdir, 'foo.bar@xxxxxxxxxxxxxxxxx')
-    >>> pubkey2 = open(filepath).read()
-    >>> key = gpghandler.importPublicKey('\n'.join([pubkey, pubkey2]))
+    >>> with open(filepath, 'rb') as f:
+    ...     pubkey2 = f.read()
+    >>> key = gpghandler.importPublicKey(b'\n'.join([pubkey, pubkey2]))
     ... # doctest: +IGNORE_EXCEPTION_MODULE_IN_PYTHON2
     Traceback (most recent call last):
     ...
@@ -146,7 +149,8 @@ does exactly the same job performed by importPublicKey() but
 supporting only ASCII-armored secret keys.
 
     >>> filepath = os.path.join(gpgkeysdir, 'test@xxxxxxxxxxxxxxxxx')
-    >>> seckey = open(filepath).read()
+    >>> with open(filepath, 'rb') as f:
+    ...     seckey = f.read()
     >>> key = gpghandler.importSecretKey(seckey)
 
     >>> verifyObject(IPymeKey, key)
@@ -172,7 +176,7 @@ supporting only ASCII-armored secret keys.
 
 Secret keys can be exported in ASCII-armored format.
 
-    >>> print key.export()
+    >>> print(six.ensure_text(key.export()))
     -----BEGIN PGP PRIVATE KEY BLOCK-----
     ...
     -----END PGP PRIVATE KEY BLOCK-----
@@ -193,7 +197,8 @@ We will set up and use the test-keyserver.
 Import a test key.
 
     >>> filepath = os.path.join(gpgkeysdir, 'ppa-sample@xxxxxxxxxxxxxxxxx')
-    >>> seckey = open(filepath).read()
+    >>> with open(filepath, 'rb') as f:
+    ...     seckey = f.read()
     >>> new_key = gpghandler.importSecretKey(seckey)
 
 Upload the just-generated key to the keyserver so that we can reset