Fix callRemote unicode issues on Python 2 universally, using monkeypatching.

This commit is contained in:
Itamar Turner-Trauring 2020-10-01 10:48:09 -04:00
parent bdf2dcd796
commit 5899cfdabd
5 changed files with 50 additions and 4 deletions

0
newsfragments/3458.minor Normal file
View File

View File

@ -42,3 +42,9 @@ __full_version__ = __appname__ + '/' + str(__version__)
# Install Python 3 module locations in Python 2:
from future import standard_library
standard_library.install_aliases()
# Monkey-patch 3rd party libraries:
from ._monkeypatch import patch
patch()
del patch

View File

@ -0,0 +1,41 @@
"""
Monkey-patching of third party libraries.
Ported to Python 3.
"""
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
def patch():
"""Path third-party libraries to make Tahoe-LAFS work."""
# Make sure Foolscap always get native strings passed to method names in callRemote.
# This can be removed when any one of the following happens:
#
# 1. Tahoe-LAFS on Python 2 switches to version of Foolscap that fixes
# https://github.com/warner/foolscap/issues/72
# 2. Foolscap is dropped as a dependency.
# 3. Tahoe-LAFS drops Python 2 support.
if PY2:
# Only tested with this version; ensure correctness with new releases,
# and then either update the assert or hopefully drop the monkeypatch.
from foolscap import __version__
assert __version__ == "0.13.1", "Wrong version %s of Foolscap" % (__version__,)
from foolscap.referenceable import RemoteReference
original_getMethodInfo = RemoteReference._getMethodInfo
def _getMethodInfo(self, name):
if isinstance(name, str):
name = name.encode("utf-8")
return original_getMethodInfo(self, name)
RemoteReference._getMethodInfo = _getMethodInfo

View File

@ -6,7 +6,7 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2, native_str
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
@ -765,9 +765,7 @@ class Share(object):
level=log.WEIRD, umid="qZu0wg"))
def _send_request(self, start, length):
# For some reason tests fail on Python 2 if this is not a native
# string...
return self._rref.callRemote(native_str("read"), start, length)
return self._rref.callRemote("read", start, length)
def _got_data(self, data, start, length, block_ev, lp):
block_ev.finished(len(data), now())

View File

@ -24,6 +24,7 @@ if PY2:
# Keep these sorted alphabetically, to reduce merge conflicts:
PORTED_MODULES = [
"allmydata._monkeypatch",
"allmydata.codec",
"allmydata.crypto",
"allmydata.crypto.aes",