From 831ced93f0691eac16e84f76fe9f99a46c7fcc71 Mon Sep 17 00:00:00 2001 From: Andrew Jung <13521727+andrew-jung@users.noreply.github.com> Date: Wed, 1 Sep 2021 10:25:33 -0400 Subject: [PATCH] Use six to make utils/services Python 3 compatible (#69) * Use ensure_binary to make xml data Python 3 compatible * Add ensure_text to string concat in _parse_envelopes * Add ensure_text for stdout * Sort event_types for consistency --- exchangelib/services.py | 7 ++++--- exchangelib/util.py | 7 +++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/exchangelib/services.py b/exchangelib/services.py index 9b86bfa..f17c0a2 100644 --- a/exchangelib/services.py +++ b/exchangelib/services.py @@ -21,7 +21,7 @@ from sys import stdout import traceback -from six import text_type +from six import ensure_text, text_type from . import errors from .errors import EWSWarning, TransportError, SOAPError, ErrorTimeoutExpired, ErrorBatchProcessingStopped, \ @@ -270,7 +270,7 @@ def _write_xml(self, ftype, req_id, xml_str): elif ftype == 'streaming-response': stdout.write(u'STREAMING RESPONSE {} <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< {}\n'.format(req_id, now)) - stdout.write(PrettyXmlHandler.prettify_xml(xml_str) + b'\n') + stdout.write(ensure_text(PrettyXmlHandler.prettify_xml(xml_str) + b'\n')) def _parse_envelopes(self, response): try: @@ -279,6 +279,7 @@ def _parse_envelopes(self, response): for chunk in response.iter_content(): if not chunk: continue + chunk = ensure_text(chunk) curr_envelope += chunk index = curr_envelope.find(envelope_str) if index == -1: @@ -1577,7 +1578,7 @@ def get_payload(self, folders, event_types): assert event_type in CONCRETE_EVENT_TYPES deduped_event_types.add(event_type.ELEMENT_NAME) - for event_type_name in deduped_event_types: + for event_type_name in sorted(deduped_event_types): if event_type_name == 'StatusEvent': continue event_type_elem = create_element('t:EventType') diff --git a/exchangelib/util.py b/exchangelib/util.py index 4ae0c18..4f8574f 100644 --- a/exchangelib/util.py +++ b/exchangelib/util.py @@ -22,7 +22,7 @@ import requests.auth import requests.exceptions from requests import Request -from six import text_type, string_types +from six import ensure_binary, string_types, text_type from .errors import TransportError, RateLimitError, RedirectError, RelativeRedirect, CASError, UnauthorizedError, \ InvalidTokenError, ErrorInvalidSchemaVersionForMailboxVersion @@ -296,8 +296,7 @@ def parse_bytes(xml_bytes): @classmethod def prettify_xml(cls, xml_bytes): # Re-formats an XML document to a consistent style - if isinstance(xml_bytes, unicode): - xml_bytes = xml_bytes.encode('utf-8') + xml_bytes = ensure_binary(xml_bytes) return tostring( cls.parse_bytes(xml_bytes), xml_declaration=True, @@ -512,7 +511,7 @@ def post_ratelimited(protocol, session, url, headers, data, allow_redirects=Fals # Always create a dummy response for logging purposes, in case we fail in the following r = DummyResponse(url=url, headers={}, request_headers=headers) try: - data = data.encode('utf-8') + data = ensure_binary(data) except UnicodeDecodeError: try: data = data.decode('utf-8').encode('utf-8')