Hello, Another batch of py3 porting patches. With these, the only thing to fix to get ipapython tests passing will be handling encoding/decoding for stdin/stdout/stderr for ipautil.run().
-- Petr Viktorin
From 2dea9db1915ba493a9e214feea6bd0c7d77e4cae Mon Sep 17 00:00:00 2001 From: Petr Viktorin <[email protected]> Date: Tue, 20 Oct 2015 19:01:23 +0200 Subject: [PATCH] ipapython.nsslib, ipalib.rpc: Remove code for Python 2.6 and below IPA hasn't supported these pythons for a while now. --- ipalib/rpc.py | 33 ++++++++++++--------------------- ipapython/nsslib.py | 8 +------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/ipalib/rpc.py b/ipalib/rpc.py index 6a7f6982b632f965515a66369c9ea302db0fa448..02281d404938ed92e5b509c23b74ce049bf605f7 100644 --- a/ipalib/rpc.py +++ b/ipalib/rpc.py @@ -480,12 +480,9 @@ def get_connection_dbdir(self): def make_connection(self, host): host, self._extra_headers, x509 = self.get_host_info(host) - # Python 2.7 changed the internal class used in xmlrpclib from - # HTTP to HTTPConnection. We need to use the proper subclass - if sys.version_info >= (2, 7): - if self._connection and host == self._connection[0]: - return self._connection[1] + if self._connection and host == self._connection[0]: + return self._connection[1] dbdir = getattr(context, 'nss_dir', paths.IPA_NSSDB_DIR) connection_dbdir = self.get_connection_dbdir() @@ -500,20 +497,15 @@ def make_connection(self, host): # need to re-initialize. no_init = dbdir == ipapython.nsslib.current_dbdir - if sys.version_info < (2, 7): - conn = NSSHTTPS(host, 443, dbdir=dbdir, no_init=no_init) - else: - conn = NSSConnection(host, 443, dbdir=dbdir, no_init=no_init, - tls_version_min=api.env.tls_version_min, - tls_version_max=api.env.tls_version_max) + conn = NSSConnection(host, 443, dbdir=dbdir, no_init=no_init, + tls_version_min=api.env.tls_version_min, + tls_version_max=api.env.tls_version_max) self.dbdir=dbdir conn.connect() - if sys.version_info < (2, 7): - return conn - else: - self._connection = host, conn - return self._connection[1] + + self._connection = host, conn + return self._connection[1] class KerbTransport(SSLTransport): @@ -925,11 +917,10 @@ def create_connection(self, ccache=None, verbose=0, fallback=True, return serverproxy def destroy_connection(self): - if sys.version_info >= (2, 7): - conn = getattr(context, self.id, None) - if conn is not None: - conn = conn.conn._ServerProxy__transport - conn.close() + conn = getattr(context, self.id, None) + if conn is not None: + conn = conn.conn._ServerProxy__transport + conn.close() def _call_command(self, command, params): """Call the command with given params""" diff --git a/ipapython/nsslib.py b/ipapython/nsslib.py index 0d02e768ba664a0bdbcdcab349a6f41c364dbe25..52cf63f03ec2ca5cdeed5636434fff677664ac79 100644 --- a/ipapython/nsslib.py +++ b/ipapython/nsslib.py @@ -291,13 +291,7 @@ def endheaders(self, message=None): """ try: # FIXME: httplib uses old-style classes so super doesn't work - # Python 2.7 changed the API for endheaders. This is an attempt - # to work across versions - (major, minor, micro, releaselevel, serial) = sys.version_info - if major == 2 and minor < 7: - httplib.HTTPConnection.endheaders(self) - else: - httplib.HTTPConnection.endheaders(self, message) + httplib.HTTPConnection.endheaders(self, message) except NSPRError as e: self.close() raise e -- 2.1.0
From d408999b3b57db96fd5b9faf2ece9bd45db37eb1 Mon Sep 17 00:00:00 2001 From: Petr Viktorin <[email protected]> Date: Tue, 20 Oct 2015 18:50:13 +0200 Subject: [PATCH] ipapython.nsslib: Remove NSSHTTPS This workaround is unused in Python 2.7+. --- ipalib/rpc.py | 2 +- ipapython/nsslib.py | 68 ----------------------------------------------------- 2 files changed, 1 insertion(+), 69 deletions(-) diff --git a/ipalib/rpc.py b/ipalib/rpc.py index 02281d404938ed92e5b509c23b74ce049bf605f7..e291e54b175f3ed22e0c3f07edce752f73b5cb3a 100644 --- a/ipalib/rpc.py +++ b/ipalib/rpc.py @@ -60,7 +60,7 @@ from ipapython.dnsutil import DNSName from ipalib.text import _ import ipapython.nsslib -from ipapython.nsslib import NSSHTTPS, NSSConnection +from ipapython.nsslib import NSSConnection from ipalib.krb_utils import KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN, KRB5KRB_AP_ERR_TKT_EXPIRED, \ KRB5_FCC_PERM, KRB5_FCC_NOFILE, KRB5_CC_FORMAT, \ KRB5_REALM_CANT_RESOLVE, KRB5_CC_NOTFOUND, get_principal diff --git a/ipapython/nsslib.py b/ipapython/nsslib.py index 52cf63f03ec2ca5cdeed5636434fff677664ac79..5ae79b65c38ccf4748d3155f2ff7a91d19fca7ef 100644 --- a/ipapython/nsslib.py +++ b/ipapython/nsslib.py @@ -295,71 +295,3 @@ def endheaders(self, message=None): except NSPRError as e: self.close() raise e - - -class NSSHTTPS(httplib.HTTP): - # We would like to use HTTP 1.1 not the older HTTP 1.0 but xmlrpc.client - # and httplib do not play well together. httplib when the protocol - # is 1.1 will add a host header in the request. But xmlrpc.client - # always adds a host header irregardless of the HTTP protocol - # version. That means the request ends up with 2 host headers, - # but Apache freaks out if it sees 2 host headers, a known Apache - # issue. httplib has a mechanism to skip adding the host header - # (i.e. skip_host in HTTPConnection.putrequest()) but xmlrpc.client - # doesn't use it. Oh well, back to 1.0 :-( - # - #_http_vsn = 11 - #_http_vsn_str = 'HTTP/1.1' - - _connection_class = NSSConnection - - def __init__(self, host='', port=None, strict=None, dbdir=None, no_init=False): - # provide a default host, pass the X509 cert info - - # urf. compensate for bad input. - if port == 0: - port = None - self._setup(self._connection_class(host, port, strict, dbdir=dbdir, no_init=no_init)) - - def getreply(self): - """ - Override so we can close duplicated file connection on non-200 - responses. This was causing nss_shutdown() to fail with a busy - error. - """ - (status, reason, msg) = httplib.HTTP.getreply(self) - if status != 200: - self.file.close() - return (status, reason, msg) - -#------------------------------------------------------------------------------ - -if __name__ == "__main__": - standard_logging_setup('nsslib.log', debug=True, filemode='a') - root_logger.info("Start") - - if False: - conn = NSSConnection("www.verisign.com", 443, dbdir=paths.NSS_DB_DIR) - conn.set_debuglevel(1) - conn.connect() - conn.request("GET", "/") - response = conn.getresponse() - print(response.status) - #print response.msg - print(response.getheaders()) - data = response.read() - #print data - conn.close() - - if True: - h = NSSHTTPS("www.verisign.com", 443, dbdir=paths.NSS_DB_DIR) - h.connect() - h.putrequest('GET', '/') - h.endheaders() - http_status, http_reason, headers = h.getreply() - print("status = %s %s" % (http_status, http_reason)) - print("headers:\n%s" % headers) - f = h.getfile() - data = f.read() # Get the raw HTML - f.close() - #print data -- 2.1.0
From 97b9d3ffee6fafd661c8e85f56a4c7fbfb727457 Mon Sep 17 00:00:00 2001 From: Petr Viktorin <[email protected]> Date: Tue, 20 Oct 2015 18:14:14 +0200 Subject: [PATCH] ipapython.secrets: Port to Python 3 StringIO was renamed in Python 3. The import was was unused, so remove it. Files need to be opened in binary mode if bytes are written to them. (For Python 2: on Linux, there's no practical difference between text and binary mode) --- ipapython/secrets/store.py | 1 - ipatests/test_ipapython/test_secrets.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ipapython/secrets/store.py b/ipapython/secrets/store.py index 8f2d7982639018477bbf71351aeff6c9354e8406..26dcc468863dd758154c820cc4365418abe3eca8 100644 --- a/ipapython/secrets/store.py +++ b/ipapython/secrets/store.py @@ -11,7 +11,6 @@ import os import shutil import sys -import StringIO import tempfile diff --git a/ipatests/test_ipapython/test_secrets.py b/ipatests/test_ipapython/test_secrets.py index d88659e6ff1877317f78224933aecdde196ab5b8..9fbf825d2e2ec1fd365c2a3d57cc9a1793315a2a 100644 --- a/ipatests/test_ipapython/test_secrets.py +++ b/ipatests/test_ipapython/test_secrets.py @@ -31,7 +31,7 @@ def setUpClass(cls): cls.cert2db = os.path.join(testdir, 'cert2db') os.mkdir(cls.cert2db) seedfile = os.path.join(testdir, 'seedfile') - with open(seedfile, 'w') as f: + with open(seedfile, 'wb') as f: seed = os.urandom(1024) f.write(seed) subprocess.call(['certutil', '-d', cls.certdb, '-N', '-f', pwfile]) -- 2.1.0
From acccbcb4b251ccf02db6489cb4131a964ba59faa Mon Sep 17 00:00:00 2001 From: Petr Viktorin <[email protected]> Date: Thu, 8 Oct 2015 15:39:14 +0200 Subject: [PATCH] test_parameters: Alias long to int under Python 3 In py3, the two types are unified under the name "int". --- ipatests/test_ipalib/test_parameters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ipatests/test_ipalib/test_parameters.py b/ipatests/test_ipalib/test_parameters.py index fbbb0a27f46a04e662a9394d4851dd6e542643b4..c1ebf900dbb0d14a7ef79b356f0e591da99a5c04 100644 --- a/ipatests/test_ipalib/test_parameters.py +++ b/ipatests/test_ipalib/test_parameters.py @@ -44,6 +44,7 @@ if six.PY3: unicode = str + long = int NULLS = (None, b'', u'', tuple(), []) -- 2.1.0
From 6348fc1df273d29b3658f0d53018cda4f0115407 Mon Sep 17 00:00:00 2001 From: Petr Viktorin <[email protected]> Date: Thu, 17 Sep 2015 18:48:30 +0200 Subject: [PATCH] ipalib.rpc: Update for Python 3 The client XML-RPC implementation is tied to rpclib internals, so with a change in Python it needs to be updated. And rpclib changed in Python 3. --- ipalib/rpc.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/ipalib/rpc.py b/ipalib/rpc.py index e291e54b175f3ed22e0c3f07edce752f73b5cb3a..2accdc5f0799a1cadab995212db59c081715e29f 100644 --- a/ipalib/rpc.py +++ b/ipalib/rpc.py @@ -38,6 +38,7 @@ import base64 import json import socket +import gzip import gssapi from dns import resolver, rdatatype @@ -602,19 +603,24 @@ def _auth_complete(self, response): return True def single_request(self, host, handler, request_body, verbose=0): - # Based on xmlrpc.lient.Transport.single_request + # Based on Python 2.7's xmllib.Transport.single_request try: h = SSLTransport.make_connection(self, host) + if verbose: h.set_debuglevel(1) while True: - self.send_request(h, handler, request_body) - self.send_host(h, host) - self.send_user_agent(h) - self.send_content(h, request_body) + if six.PY2: + self.send_request(h, handler, request_body) + self.send_host(h, host) + self.send_user_agent(h) + self.send_content(h, request_body) + response = h.getresponse(buffering=True) + else: + self.__send_request(h, host, handler, request_body, verbose) + response = h.getresponse() - response = h.getresponse(buffering=True) if response.status != 200: if (response.getheader("content-length", 0)): response.read() @@ -637,6 +643,23 @@ def single_request(self, host, handler, request_body, verbose=0): finally: self.close() + if six.PY3: + def __send_request(self, connection, host, handler, request_body, debug): + # Based on xmlrpc.client.Transport.send_request + headers = self._extra_headers[:] + if debug: + connection.set_debuglevel(1) + if self.accept_gzip_encoding and gzip: + connection.putrequest("POST", handler, skip_accept_encoding=True) + connection.putheader("Accept-Encoding", "gzip") + headers.append(("Accept-Encoding", "gzip")) + else: + connection.putrequest("POST", handler) + headers.append(("User-Agent", self.user_agent)) + self.send_headers(connection, headers) + self.send_content(connection, request_body) + return connection + def store_session_cookie(self, cookie_header): ''' Given the contents of a Set-Cookie header scan the header and -- 2.1.0
-- Manage your subscription for the Freeipa-devel mailing list: https://www.redhat.com/mailman/listinfo/freeipa-devel Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code
