Skip to content

Commit

Permalink
Grep for PY3 and drop Python 2 support. (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Sep 12, 2019
1 parent d2bbf33 commit 2c8c48b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 62 deletions.
3 changes: 1 addition & 2 deletions src/OFS/DTMLMethod.py
Expand Up @@ -14,7 +14,6 @@
"""
import re

from six import PY3
from six import binary_type
from six.moves.urllib.parse import quote

Expand Down Expand Up @@ -423,7 +422,7 @@ def safe_file_data(data):
# Helper to convert upload file content into a safe value for saving
if hasattr(data, 'read'):
data = data.read()
if PY3 and isinstance(data, binary_type):
if isinstance(data, binary_type):
data = data.decode('utf-8')
return data

Expand Down
8 changes: 3 additions & 5 deletions src/OFS/FindSupport.py
Expand Up @@ -12,8 +12,6 @@
##############################################################################
"""Find support
"""
import six

from AccessControl import ClassSecurityInfo
from AccessControl.class_init import InitializeClass
from AccessControl.Permission import getPermissionIdentifier
Expand Down Expand Up @@ -129,19 +127,19 @@ def ZopeFindAndApply(self, obj, obj_ids=None, obj_metatypes=None,
if obj_searchterm:
if isinstance(obj_searchterm, TaintedString):
obj_searchterm = str(obj_searchterm)
if six.PY3 and not isinstance(obj_searchterm, str):
if not isinstance(obj_searchterm, str):
obj_searchterm = obj_searchterm.decode(
default_encoding)
if hasattr(ob, 'PrincipiaSearchSource'):
pss = ob.PrincipiaSearchSource()
if six.PY3 and not isinstance(pss, str):
if not isinstance(pss, str):
try:
pss = pss.decode(default_encoding)
except UnicodeDecodeError:
pss = ''
if hasattr(ob, 'SearchableText'):
st = ob.SearchableText()
if six.PY3 and not isinstance(st, str):
if not isinstance(st, str):
try:
st = st.decode(default_encoding)
except UnicodeDecodeError:
Expand Down
4 changes: 1 addition & 3 deletions src/OFS/tests/testFileAndImage.py
Expand Up @@ -6,7 +6,6 @@
from io import BytesIO

import six
from six import PY3

import OFS.Image
import Testing.testbrowser
Expand Down Expand Up @@ -271,8 +270,7 @@ def testIndexHtmlWithString(self):

def testPrincipiaSearchSource_not_text(self):
data = ''.join([chr(x) for x in range(256)])
if PY3:
data = data.encode('utf-8')
data = data.encode('utf-8')
self.file.manage_edit('foobar', 'application/octet-stream',
filedata=data)
self.assertEqual(self.file.PrincipiaSearchSource(), b'')
Expand Down
14 changes: 3 additions & 11 deletions src/OFS/tests/testSimpleItem.py
@@ -1,7 +1,5 @@
import unittest

import six


class TestItem(unittest.TestCase):

Expand Down Expand Up @@ -110,7 +108,6 @@ def test_title_and_id_nonascii(self):
unencoded_id = u'\xfc\xe4\xee\xe9\xdf_id'
encoded_id = unencoded_id.encode('UTF-8')
unencoded_title = u'\xfc\xe4\xee\xe9\xdf Title'
encoded_title = unencoded_title.encode('UTF-8')
item = self._makeOne()

item.id = unencoded_id
Expand All @@ -120,15 +117,10 @@ def test_title_and_id_nonascii(self):
self.assertIn(unencoded_id, item.title_and_id())
self.assertIn(unencoded_title, item.title_and_id())

# Now mix encoded and unencoded. The combination is a native
# string, meaning encoded on Python 2 and unencoded on Python 3
# Now mix encoded and unencoded. The combination is a native string:
item.id = encoded_id
if six.PY3:
self.assertIn(unencoded_id, item.title_and_id())
self.assertIn(unencoded_title, item.title_and_id())
else:
self.assertIn(encoded_id, item.title_and_id())
self.assertIn(encoded_title, item.title_and_id())
self.assertIn(unencoded_id, item.title_and_id())
self.assertIn(unencoded_title, item.title_and_id())

def test_standard_error_message_is_called(self):
from zExceptions import BadRequest
Expand Down
7 changes: 1 addition & 6 deletions src/Products/PageTemplates/tests/test_pagetemplate.py
@@ -1,8 +1,6 @@
import os
import unittest

from six import PY3

from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Testing.ZopeTestCase import ZopeTestCase

Expand Down Expand Up @@ -78,10 +76,7 @@ def test_secure(self):
self.fail("Expected unauthorized.")

from AccessControl.SecurityInfo import allow_module
if PY3:
allow_module('html')
else:
allow_module('cgi')
allow_module('html')
result = template(soup=soup)
self.assertTrue('<foo></bar>' in result)

Expand Down
9 changes: 2 additions & 7 deletions src/Products/PageTemplates/tests/util.py
Expand Up @@ -16,8 +16,6 @@
import sys
import unittest

import six

from ExtensionClass import Base


Expand Down Expand Up @@ -123,11 +121,8 @@ def normalize_xml(s):


def _open(filename, mode):
if six.PY3:
# Define explicit encoding for windows platform
return open(filename, mode, encoding='utf-8')
else:
return open(filename, mode)
# Define explicit encoding for windows platform
return open(filename, mode, encoding='utf-8')


def read_input(filename):
Expand Down
31 changes: 14 additions & 17 deletions src/ZPublisher/HTTPRequest.py
Expand Up @@ -15,14 +15,14 @@
"""

import codecs
import html
import os
import random
import re
import time
from cgi import FieldStorage
from copy import deepcopy

from six import PY3
from six import binary_type
from six import string_types
from six import text_type
Expand All @@ -49,11 +49,6 @@
from ZPublisher.utils import basic_auth_decode


if PY3:
from html import escape
else:
from cgi import escape

# This may get overwritten during configuration
default_encoding = 'utf-8'

Expand Down Expand Up @@ -491,9 +486,7 @@ def processInputs(

meth = None
fs_kw = {}
if PY3:
# In Python 3 we need the proper encoding to parse the input.
fs_kw['encoding'] = self.charset
fs_kw['encoding'] = self.charset

fs = ZopeFieldStorage(
fp=fp, environ=environ, keep_blank_values=1, **fs_kw)
Expand Down Expand Up @@ -637,7 +630,7 @@ def processInputs(
if should_be_tainted(attr):
raise ValueError(
"%s is not a valid record attribute name" %
escape(attr, True))
html.escape(attr, True))

# defer conversion
if flags & CONVERTED:
Expand Down Expand Up @@ -1465,37 +1458,41 @@ def __str__(self):
result = "<h3>form</h3><table>"
row = '<tr valign="top" align="left"><th>%s</th><td>%s</td></tr>'
for k, v in _filterPasswordFields(self.form.items()):
result = result + row % (escape(k, False), escape(repr(v), False))
result = result + row % (
html.escape(k, False), html.escape(repr(v), False))
result = result + "</table><h3>cookies</h3><table>"
for k, v in _filterPasswordFields(self.cookies.items()):
result = result + row % (escape(k, False), escape(repr(v), False))
result = result + row % (
html.escape(k, False), html.escape(repr(v), False))
result = result + "</table><h3>lazy items</h3><table>"
for k, v in _filterPasswordFields(self._lazies.items()):
result = result + row % (escape(k, False), escape(repr(v), False))
result = result + row % (
html.escape(k, False), html.escape(repr(v), False))
result = result + "</table><h3>other</h3><table>"
for k, v in _filterPasswordFields(self.other.items()):
if k in ('PARENTS', 'RESPONSE'):
continue
result = result + row % (escape(k, False), escape(repr(v), False))
result = result + row % (
html.escape(k, False), html.escape(repr(v), False))

for n in "0123456789":
key = "URL%s" % n
try:
result = result + row % (key, escape(self[key], False))
result = result + row % (key, html.escape(self[key], False))
except KeyError:
pass
for n in "0123456789":
key = "BASE%s" % n
try:
result = result + row % (key, escape(self[key], False))
result = result + row % (key, html.escape(self[key], False))
except KeyError:
pass

result = result + "</table><h3>environ</h3><table>"
for k, v in self.environ.items():
if k not in hide_key:
result = result + row % (
escape(k, False), escape(repr(v), False))
html.escape(k, False), html.escape(repr(v), False))
return result + "</table>"

def __repr__(self):
Expand Down
3 changes: 1 addition & 2 deletions src/ZPublisher/WSGIPublisher.py
Expand Up @@ -19,7 +19,6 @@
from io import BytesIO
from io import IOBase

from six import PY3
from six import reraise
from six.moves._thread import allocate_lock

Expand Down Expand Up @@ -286,7 +285,7 @@ def publish_module(environ, start_response,
result = ()

path_info = environ.get('PATH_INFO')
if path_info and PY3:
if path_info:
# BIG Comment, see discussion at
# https://github.com/zopefoundation/Zope/issues/575
#
Expand Down
6 changes: 2 additions & 4 deletions src/ZPublisher/tests/test_Converters.py
Expand Up @@ -13,7 +13,6 @@

import unittest

from six import PY3
from six import text_type


Expand Down Expand Up @@ -211,9 +210,8 @@ def test_field2lines_with_string_with_newlines(self):
def test_field2text_with_string_with_newlines(self):
from ZPublisher.Converters import field2text
to_convert = 'abc\r\ndef\r\nghi'
if PY3:
expected = 'abc\ndef\nghi'
self.assertEqual(field2text(to_convert), expected)
expected = 'abc\ndef\nghi'
self.assertEqual(field2text(to_convert), expected)

def test_field2ulines_with_list(self):
from ZPublisher.Converters import field2ulines
Expand Down
7 changes: 2 additions & 5 deletions src/ZPublisher/utils.py
Expand Up @@ -14,7 +14,6 @@
import base64
import logging

from six import PY3
from six import binary_type
from six import text_type

Expand Down Expand Up @@ -89,8 +88,7 @@ def basic_auth_encode(user, password=None):
if password is not None:
value = value + ':' + password
header = b'Basic ' + base64.b64encode(value.encode('latin-1'))
if PY3:
header = header.decode('latin-1')
header = header.decode('latin-1')
return header


Expand All @@ -102,7 +100,6 @@ def basic_auth_decode(token):
return None
value = token.split()[-1] # Strip 'Basic '
plain = base64.b64decode(value)
if PY3:
plain = plain.decode('latin-1')
plain = plain.decode('latin-1')
user, password = plain.split(':', 1) # Split at most once
return (user, password)

0 comments on commit 2c8c48b

Please sign in to comment.