general: remove six.PY[23] usage (#40006)

This commit is contained in:
Frédéric Péters 2020-02-16 20:34:47 +01:00
parent d82a02dfee
commit ca91df122a
22 changed files with 54 additions and 203 deletions

View File

@ -4979,10 +4979,7 @@ def test_data_sources_view(pub):
data_source.store()
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
assert 'Preview' in resp.text
if six.PY2:
assert 'error: not a string' in resp.text
else:
assert 'Domicilié' in resp.text
assert 'Domicilié' in resp.text
# check json
json_file_path = os.path.join(pub.app_dir, 'test.json')

View File

@ -3027,10 +3027,7 @@ def test_backoffice_wscall_error_email(http_requests, pub, emails):
assert 'Error during webservice call' in resp.text
# check email box
if six.PY2:
error_email = emails.get('[ERROR] [WSCALL] ValueError: No JSON object could be decoded')
else:
error_email = emails.get('[ERROR] [WSCALL] json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)')
error_email = emails.get('[ERROR] [WSCALL] json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)')
assert '/form-title/%s/' % number31.id in error_email['payload']
assert error_email['msg']['References']
@ -4651,14 +4648,9 @@ def test_inspect_page(pub, local_user):
assert (pq('[title="form_var_foo_non_unicode_convertible"]')
.parents('li').children('div.value span')
.text().startswith('ok '))
if six.PY2:
assert (pq('[title="form_var_foo_str_but_non_utf8"]')
.parents('li').children('div.value span')
.text() == '\'\\xed\\xa0\\x00\'')
else:
assert (pq('[title="form_var_foo_str_but_non_utf8"]')
.parents('li').children('div.value span')
.text() == "b'\\xed\\xa0\\x00' (<class 'bytes'>)")
assert (pq('[title="form_var_foo_str_but_non_utf8"]')
.parents('li').children('div.value span')
.text() == "b'\\xed\\xa0\\x00' (<class 'bytes'>)")
# don't show «unusable» variables
assert 'form_f1' not in resp.text

View File

@ -289,10 +289,7 @@ def test_json_datasource_bad_url(http_requests, caplog):
datasource = {'type': 'json', 'value': 'http://remote.example.net/xml'}
assert data_sources.get_items(datasource) == []
assert 'Error reading JSON data source output' in caplog.records[-1].message
if six.PY2:
assert 'No JSON object could be decoded' in caplog.records[-1].message
else:
assert 'Expecting value:' in caplog.records[-1].message
assert 'Expecting value:' in caplog.records[-1].message
datasource = {'type': 'json', 'value': 'http://remote.example.net/connection-error'}
assert data_sources.get_items(datasource) == []

View File

@ -3851,10 +3851,7 @@ def test_formdata_generated_document_odt_to_pdf_download_push_to_portfolio(pub,
http_post_request.return_value = None, 200, 'null', None
resp = resp.form.submit('button_export_to')
assert http_post_request.call_count == 1
if six.PY2:
assert ("file 'template.pdf' pushed to portfolio of 'Foo Bar\\xc3\\xa9'"
== caplog.records[-1].message)
elif locale.getpreferredencoding() == 'UTF-8':
if locale.getpreferredencoding() == 'UTF-8':
assert ("file 'template.pdf' pushed to portfolio of 'Foo Baré'"
== caplog.records[-1].message)
else: # Python < 3.7

View File

@ -456,10 +456,6 @@ def test_get_formdefs_of_all_kinds(pub):
def test_pickle_2to3_conversion(pub):
if six.PY2:
pytest.skip('only relevant for Python 3')
return
FormDef.wipe()
Workflow.wipe()

View File

@ -111,8 +111,6 @@ def test_simplify_apostrophes():
def test_simplify_accented():
assert simplify(u'cliché') == 'cliche'
if six.PY2:
assert simplify(u'cliché'.encode('iso-8859-1')) == 'cliche'
def test_simplify_remove():
@ -130,9 +128,6 @@ def test_json_str_decoder():
'lst': [{'a': 'b'}, 1, 2],
'bla': u'éléphant'
})
if six.PY2:
assert type(list(json.loads(json_str).keys())[0]) is unicode
assert type(json.loads(json_str)['lst'][0]['a']) is unicode
assert type(list(json_loads(json_str).keys())[0]) is str
assert type(json_loads(json_str)['lst'][0]['a']) is str

View File

@ -189,9 +189,6 @@ def test_import_config_zip():
pub.import_zip(c)
assert pub.cfg['language'] == {'language': 'en'}
assert pub.cfg['sp'] == {'what': 'ever'}
if six.PY2:
assert not isinstance(pub.cfg['language'], unicode)
assert not isinstance(pub.cfg['whatever2'][-1]['c'], unicode)
def test_cron_command():

View File

@ -1404,10 +1404,7 @@ def test_email_attachments(pub, emails):
assert emails.emails['foobar']['msg'].get_payload()[0].get_content_type() == 'text/html'
assert emails.emails['foobar']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
assert emails.emails['foobar']['msg'].get_payload()[2].get_content_type() == 'text/plain'
if six.PY2:
assert emails.emails['foobar']['msg'].get_payload()[2].get_payload() == 'blah'
else:
assert base64.decodestring(force_bytes(emails.emails['foobar']['msg'].get_payload()[2].get_payload())) == b'blah'
assert base64.decodestring(force_bytes(emails.emails['foobar']['msg'].get_payload()[2].get_payload())) == b'blah'
assert len(emails.emails['foobar']['msg'].get_payload()) == 3
emails.empty()
@ -1635,10 +1632,7 @@ def test_webservice_call(http_requests, pub):
item.record_errors = True
with pytest.raises(AbortActionException):
item.perform(formdata)
if six.PY2:
assert formdata.evolution[-1].parts[-1].summary == 'ValueError: No JSON object could be decoded\n'
else:
assert formdata.evolution[-1].parts[-1].summary == 'json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n'
assert formdata.evolution[-1].parts[-1].summary == 'json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n'
assert formdata.workflow_data.get('xxx_status') == 200
assert formdata.workflow_data.get('xxx_time')
assert 'xxx_error_response' not in formdata.workflow_data

View File

@ -186,11 +186,7 @@ def graphviz(workflow, url_prefix='', select=None, svg=True,
done[next_id] = True
label = item.get_jump_label(target_id=next_id)
label = label.replace('"', '\\"')
if six.PY2:
label = label.decode('utf8')
label = textwrap.fill(label, 20, break_long_words=False)
if six.PY2:
label = label.encode('utf8')
label = label.replace('\n', '\\n')
print('[label="%s"' % label, end=' ', file=out)
print(',URL="%s%s"]' % (url_prefix, url), file=out)

View File

@ -119,26 +119,10 @@ class CompatHTTPRequest(HTTPRequest):
v = self.django_request.POST[k]
if k.endswith('[]'):
v = [x for x in self.django_request.POST.getlist(k)]
if six.PY2:
if k.endswith('[]'):
v = [x.encode(site_charset) for x in v]
else:
v = self.django_request.POST[k]
if isinstance(v, unicode):
v = v.encode(site_charset)
if isinstance(k, unicode):
k = k.encode(site_charset)
self.form[k] = v
for k, upload_file in self.django_request.FILES.items():
if six.PY2:
upload = Upload(upload_file.name.encode('utf-8'),
upload_file.content_type.encode('utf-8'),
upload_file.charset)
if isinstance(k, unicode):
k = k.encode(site_charset)
else:
upload = Upload(upload_file.name,
upload = Upload(upload_file.name,
upload_file.content_type,
upload_file.charset)
upload.fp = upload_file.file

View File

@ -409,16 +409,15 @@ class Field(object):
self.display_locations.append('listings')
changed = True
self.in_listing = None
if six.PY3:
# repair dictionary attributes that may have been kept as bytes in
# the initial python 2 -> 3 conversion.
from wcs.qommon.storage import deep_bytes2str
for key in ('prefill', 'data_source'):
value = getattr(self, key, None)
if not value:
continue
if b'type' in value:
setattr(self, key, deep_bytes2str(getattr(self, key)))
# repair dictionary attributes that may have been kept as bytes in
# the initial python 2 -> 3 conversion.
from wcs.qommon.storage import deep_bytes2str
for key in ('prefill', 'data_source'):
value = getattr(self, key, None)
if not value:
continue
if b'type' in value:
setattr(self, key, deep_bytes2str(getattr(self, key)))
return changed
def evaluate_condition(self, dict_vars, formdef, condition):

View File

@ -33,19 +33,10 @@ except ImportError:
# force_str: compatibility function to produce an encoded string in Python 2
# and a proper unicode string in Python 3
if six.PY2:
def force_str(s):
if isinstance(s, str):
return s
return s.encode('utf-8')
else:
force_str = force_text
force_str = force_text
if six.PY2:
PICKLE_KWARGS = {}
else:
# unpickle python2 strings as bytes
PICKLE_KWARGS = {'encoding': 'bytes', 'fix_imports': True}
# unpickle python2 strings as bytes
PICKLE_KWARGS = {'encoding': 'bytes', 'fix_imports': True}
def _(message):
@ -134,47 +125,25 @@ class _MainThread(TenantAwareThread):
self._Thread__delete()
if six.PY2:
class _DummyThread(TenantAwareThread):
class _DummyThread(TenantAwareThread):
def __init__(self):
super(_DummyThread, self).__init__(name=threading._newname("Dummy-%d"))
def __init__(self):
super(_DummyThread, self).__init__(name=threading._newname("Dummy-%d"), daemon=True)
# Thread.__block consumes an OS-level locking primitive, which
# can never be used by a _DummyThread. Since a _DummyThread
# instance is immortal, that's bad, so release this resource.
del self._Thread__block
self._started.set()
self._set_ident()
with threading._active_limbo_lock:
threading._active[self._ident] = self
self._Thread__started.set()
self._set_ident()
with threading._active_limbo_lock:
threading._active[threading._get_ident()] = self
def _stop(self):
pass
def _set_daemon(self):
return True
def is_alive(self):
assert not self._is_stopped and self._started.is_set()
return True
def join(self, timeout=None):
assert False, "cannot join a dummy thread"
else:
class _DummyThread(TenantAwareThread):
def __init__(self):
super(_DummyThread, self).__init__(name=threading._newname("Dummy-%d"), daemon=True)
self._started.set()
self._set_ident()
with threading._active_limbo_lock:
threading._active[self._ident] = self
def _stop(self):
pass
def is_alive(self):
assert not self._is_stopped and self._started.is_set()
return True
def join(self, timeout=None):
assert False, "cannot join a dummy thread"
def join(self, timeout=None):
assert False, "cannot join a dummy thread"
class AppConfig(django.apps.AppConfig):

View File

@ -538,8 +538,7 @@ class TextWidget(quixote.form.TextWidget):
except (TypeError, ValueError):
maxlength = 0
if maxlength:
uvalue = self.value if six.PY3 else self.value.decode(get_publisher().site_charset)
if len(uvalue) > maxlength:
if len(self.value) > maxlength:
self.error = _('too many characters (limit is %d)') % maxlength
if self.validation_function:
try:
@ -1068,8 +1067,6 @@ class DateWidget(StringWidget):
def parse(self, request=None):
StringWidget.parse(self, request=request)
if six.PY2 and type(self.value) is unicode:
self.value = self.value.encode(get_publisher().site_charset)
return self.value
@classmethod
@ -1513,8 +1510,6 @@ class WysiwygTextWidget(TextWidget):
if self.value:
if _sanitizeHTML:
self.value = _sanitizeHTML(self.value, get_request().charset, 'text/html')
elif six.PY2 and isinstance(self.value, unicode):
self.value = self.value.encode(get_publisher().site_charset)
if self.value.startswith('<br />'):
self.value = self.value[6:]
if self.value.endswith('<br />'):

View File

@ -135,19 +135,12 @@ class HTTPRequest(quixote.http_request.HTTPRequest):
if ctype == 'application/json':
from .misc import json_loads
length = int(self.environ.get('CONTENT_LENGTH') or '0')
if six.PY3: # quixote will have consumed the body
self.stdin.seek(0)
self.stdin.seek(0) # quixote will have consumed the body
payload = self.stdin.read(length)
try:
self._json = json_loads(payload)
except ValueError as e:
raise RequestError('invalid json payload (%s)' % str(e))
if six.PY2:
# Make sure request.form doesn't contain unicode strings, converting
# them all to strings encoded in the site charset.
self.form = dict(
(str(k), v.encode(self.charset) if isinstance(v, unicode) else v)
for k, v in self.form.items())
# remove characters that are not valid XML so it doesn't have to happen
# down the chain.
illegal_xml_chars = re.compile(u'[\x00-\x08\x0b\x0c\x0e-\x1f\ud800-\udfff\ufffe\uffff]')

View File

@ -172,8 +172,6 @@ def simplify(s, space='-'):
s = force_text(unicodedata.normalize('NFKD', s).encode('ascii', 'ignore'))
s = re.sub(r'[^\w\s\'%s]' % space, '', s).strip().lower()
s = re.sub(r'[\s\'%s]+' % space, space, s)
if six.PY2:
return s.encode('ascii')
return s
@ -536,7 +534,7 @@ class JSONEncoder(json.JSONEncoder):
if isinstance(obj, decimal.Decimal):
return str(obj)
if six.PY3 and isinstance(obj, bytes):
if isinstance(obj, bytes):
return obj.decode('ascii')
if hasattr(obj, 'base_filename'):
@ -552,27 +550,12 @@ class JSONEncoder(json.JSONEncoder):
def json_encode_helper(d, charset):
'''Encode a JSON structure into local charset'''
if six.PY3:
# in python 3 we keep strings, no conversion to bytes
return d
if isinstance(d, unicode):
return d.encode(charset)
elif isinstance(d, list):
return [json_encode_helper(e, charset) for e in d]
elif isinstance(d, dict):
new_d = {}
for k, v in d.items():
new_d[json_encode_helper(k, charset)] = json_encode_helper(v, charset)
return new_d
else:
return d
# since Python 3 strings are unicode, no need to convert anything
return d
def json_loads(value, charset=None):
if six.PY3:
return json.loads(force_text(value))
charset = (get_publisher() and get_publisher().site_charset) or 'utf-8'
return json_encode_helper(json.loads(value), charset)
return json.loads(force_text(value))
def json_response(data):

View File

@ -188,9 +188,6 @@ class QommonPublisher(Publisher, object):
def _generate_plaintext_error(self, request, original_response,
exc_type, exc_value, tb, limit = None):
if exc_value and six.PY2:
# do not fail on exception strings using non-ascii chars
exc_value = force_text(str(exc_value), errors='ignore').encode('ascii')
if not self.USE_LONG_TRACES:
if not request:
# this happens when an exception is raised by an afterjob
@ -274,9 +271,6 @@ class QommonPublisher(Publisher, object):
error_summary = traceback.format_exception_only(exc_type, exc_value)
error_summary = error_summary[0][0:-1] # de-listify and strip newline
if six.PY2:
# make sure no unicode is emitted in traces (#11005)
error_summary = unicode(str(error_summary), errors='ignore').encode('ascii')
plain_error_msg = self._generate_plaintext_error(request,
original_response,
@ -711,10 +705,7 @@ class QommonPublisher(Publisher, object):
def reload_cfg(self):
filename = os.path.join(self.app_dir, 'config.pck')
try:
if six.PY3:
self.cfg = cPickle.load(open(filename, 'rb'), encoding='utf-8')
else:
self.cfg = cPickle.load(open(filename, 'rb'))
self.cfg = cPickle.load(open(filename, 'rb'), encoding='utf-8')
except:
self.cfg = {}

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import copyreg
import errno
import operator
import os
@ -34,12 +35,9 @@ from .vendor import locket
from quixote import get_publisher
from . import PICKLE_KWARGS, force_str
if six.PY3:
import copyreg
# add compatibility names in case those were stored in pickles
sys.modules['copy_reg'] = copyreg
sys.modules['__builtin__'] = builtins
# add compatibility names in case those were stored in pickles
sys.modules['copy_reg'] = copyreg
sys.modules['__builtin__'] = builtins
def cache_umask():
@ -109,8 +107,6 @@ def deep_bytes2str(obj, seen=None):
# Convert obj loaded by unpickle(encoding='bytes') to a proper object using
# strings; this is required as encoding='utf-8' is not possible when there
# are pickled datetime objects. <https://bugs.python.org/issue22005>
if six.PY2:
return obj
if seen is None:
seen = {}
if obj is None or isinstance(obj, (int, float, str, time.struct_time, type(Ellipsis))):
@ -488,7 +484,7 @@ class StorableObject(object):
return None
raise KeyError()
o.__class__ = cls
if six.PY3 and any((isinstance(k, bytes) for k in o.__dict__)):
if any((isinstance(k, bytes) for k in o.__dict__)):
pickle_2to3_conversion(o)
if not ignore_migration:
o.id = str(o.id) # makes sure 'id' is a string

View File

@ -533,8 +533,6 @@ def variable_resolve(self, context):
except UnicodeEncodeError:
# don't crash on non-ascii variable names
return context.template.engine.string_if_invalid
if six.PY2 and isinstance(value, SafeString):
return SafeText(value, 'utf-8')
if isinstance(value, str):
return force_text(value, 'utf-8')
return value

View File

@ -70,8 +70,7 @@ def pickle_loads(value):
if hasattr(value, 'tobytes'):
value = value.tobytes()
obj = UnpicklerClass(BytesIO(force_bytes(value)), **PICKLE_KWARGS).load()
if six.PY3:
obj = deep_bytes2str(obj)
obj = deep_bytes2str(obj)
return obj
@ -282,10 +281,6 @@ def parse_clause(clause):
def str_encode(value):
if isinstance(value, list):
return [str_encode(x) for x in value]
if six.PY2:
if not isinstance(value, unicode):
return value
return value.encode(get_publisher().site_charset)
return value
@ -1204,10 +1199,7 @@ class SqlMixin(object):
if value is not None:
if field.key in ('ranked-items', 'password'):
# turn {'poire': 2, 'abricot': 1, 'pomme': 3} into an array
if six.PY2:
value = [[x, site_unicode(y).encode('utf-8')] for x, y in value.items()]
else:
value = [[force_str(x), force_str(y)] for x, y in value.items()]
value = [[force_str(x), force_str(y)] for x, y in value.items()]
elif sql_type == 'varchar':
assert isinstance(value, six.string_types)
elif sql_type == 'date':

View File

@ -469,13 +469,10 @@ class ExportToModel(WorkflowStatusItem):
2 * ('<nsa:line-break xmlns:nsa="%(ns)s"/>' % {'ns': OO_TEXT_NS}))
as_node = ET.fromstring(as_str)
node.text = as_node.text
if six.PY2:
node._children = as_node._children
else:
for child in node.getchildren():
node.remove(child)
for child in as_node.getchildren():
node.append(child)
for child in node.getchildren():
node.remove(child)
for child in as_node.getchildren():
node.append(child)
node.tail = current_tail
outstream = BytesIO()

View File

@ -75,8 +75,6 @@ class JournalWsCallErrorPart: #pylint: disable=C1001
r += htmltext('<ul>')
for attr in ('err', 'err_class', 'err_desc', 'reason'):
if attr in json_data:
if six.PY2 and isinstance(json_data[attr], unicode):
json_data[attr] = force_str(json_data[attr])
r += htmltext('<li>%s: %s</li>\n' ) % (
labels.get(attr), json_data[attr])
r += htmltext('</ul>')

View File

@ -2621,11 +2621,6 @@ def get_formdata_template_context(formdata=None):
ctx['after'] = ctx.get('form_status')
ctx['evolution'] = ctx.get('form_evolution')
if six.PY2:
charset = get_publisher().site_charset
for k, v in ctx.items():
if isinstance(v, unicode):
ctx[k] = v.encode(charset, 'ignore')
return ctx