remove more Python2: unicode -> str, long -> int

This commit is contained in:
Alexandre Detiste 2024-02-28 00:53:49 +01:00
parent 53084f76ce
commit 0e5b6daa38
4 changed files with 14 additions and 20 deletions

View File

@ -1,8 +1,6 @@
"""Ported to Python 3. """Ported to Python 3.
""" """
from past.builtins import unicode
from zope.interface import implementer from zope.interface import implementer
from allmydata.interfaces import ICheckResults, ICheckAndRepairResults, \ from allmydata.interfaces import ICheckResults, ICheckAndRepairResults, \
IDeepCheckResults, IDeepCheckAndRepairResults, IURI, IDisplayableServer IDeepCheckResults, IDeepCheckAndRepairResults, IURI, IDisplayableServer
@ -64,7 +62,7 @@ class CheckResults(object):
# unicode. # unicode.
if isinstance(summary, bytes): if isinstance(summary, bytes):
summary = unicode(summary, "utf-8") summary = unicode(summary, "utf-8")
assert isinstance(summary, unicode) # should be a single string assert isinstance(summary, str) # should be a single string
self._summary = summary self._summary = summary
assert not isinstance(report, str) # should be list of strings assert not isinstance(report, str) # should be list of strings
self._report = report self._report = report

View File

@ -5,7 +5,6 @@ Ported to Python 3.
from __future__ import annotations from __future__ import annotations
from future.utils import native_str from future.utils import native_str
from past.builtins import long, unicode
from six import ensure_str from six import ensure_str
import os, time, weakref, itertools import os, time, weakref, itertools
@ -57,7 +56,7 @@ from eliot import (
_TOTAL_SHARES = Field.for_types( _TOTAL_SHARES = Field.for_types(
u"total_shares", u"total_shares",
[int, long], [int, int],
u"The total number of shares desired.", u"The total number of shares desired.",
) )
@ -104,7 +103,7 @@ _HAPPINESS_MAPPINGS = Field(
_HAPPINESS = Field.for_types( _HAPPINESS = Field.for_types(
u"happiness", u"happiness",
[int, long], [int, int],
u"The computed happiness of a certain placement.", u"The computed happiness of a certain placement.",
) )
@ -142,7 +141,7 @@ GET_SHARE_PLACEMENTS = MessageType(
_EFFECTIVE_HAPPINESS = Field.for_types( _EFFECTIVE_HAPPINESS = Field.for_types(
u"effective_happiness", u"effective_happiness",
[int, long], [int, int],
u"The computed happiness value of a share placement map.", u"The computed happiness value of a share placement map.",
) )
@ -1622,7 +1621,7 @@ class AssistedUploader(object):
# abbreviated), so if we detect old results, just clobber them. # abbreviated), so if we detect old results, just clobber them.
sharemap = upload_results.sharemap sharemap = upload_results.sharemap
if any(isinstance(v, (bytes, unicode)) for v in sharemap.values()): if any(isinstance(v, (bytes, str)) for v in sharemap.values()):
upload_results.sharemap = None upload_results.sharemap = None
def _build_verifycap(self, helper_upload_results): def _build_verifycap(self, helper_upload_results):
@ -1701,7 +1700,7 @@ class BaseUploadable(object):
def set_default_encoding_parameters(self, default_params): def set_default_encoding_parameters(self, default_params):
assert isinstance(default_params, dict) assert isinstance(default_params, dict)
for k,v in default_params.items(): for k,v in default_params.items():
precondition(isinstance(k, (bytes, unicode)), k, v) precondition(isinstance(k, (bytes, str)), k, v)
precondition(isinstance(v, int), k, v) precondition(isinstance(v, int), k, v)
if "k" in default_params: if "k" in default_params:
self.default_encoding_param_k = default_params["k"] self.default_encoding_param_k = default_params["k"]

View File

@ -24,13 +24,12 @@ def print(*args, **kwargs):
encoding error handler and then write the result whereas builtin print encoding error handler and then write the result whereas builtin print
uses the "strict" encoding error handler. uses the "strict" encoding error handler.
""" """
from past.builtins import unicode
out = kwargs.pop("file", None) out = kwargs.pop("file", None)
if out is None: if out is None:
out = _sys_stdout out = _sys_stdout
encoding = out.encoding or "ascii" encoding = out.encoding or "ascii"
def ensafe(o): def ensafe(o):
if isinstance(o, unicode): if isinstance(o, str):
return o.encode(encoding, errors="replace").decode(encoding) return o.encode(encoding, errors="replace").decode(encoding)
return o return o
return _print( return _print(

View File

@ -7,8 +7,6 @@ Methods ending in to_string() are actually to_bytes(), possibly should be fixed
in follow-up port. in follow-up port.
""" """
from past.builtins import unicode, long
import re import re
from typing import Type from typing import Type
@ -91,7 +89,7 @@ class CHKFileURI(_BaseURI):
def to_string(self): def to_string(self):
assert isinstance(self.needed_shares, int) assert isinstance(self.needed_shares, int)
assert isinstance(self.total_shares, int) assert isinstance(self.total_shares, int)
assert isinstance(self.size, (int,long)) assert isinstance(self.size, int)
return (b'URI:CHK:%s:%s:%d:%d:%d' % return (b'URI:CHK:%s:%s:%d:%d:%d' %
(base32.b2a(self.key), (base32.b2a(self.key),
@ -147,7 +145,7 @@ class CHKFileVerifierURI(_BaseURI):
def to_string(self): def to_string(self):
assert isinstance(self.needed_shares, int) assert isinstance(self.needed_shares, int)
assert isinstance(self.total_shares, int) assert isinstance(self.total_shares, int)
assert isinstance(self.size, (int,long)) assert isinstance(self.size, int)
return (b'URI:CHK-Verifier:%s:%s:%d:%d:%d' % return (b'URI:CHK-Verifier:%s:%s:%d:%d:%d' %
(si_b2a(self.storage_index), (si_b2a(self.storage_index),
@ -742,7 +740,7 @@ ALLEGED_IMMUTABLE_PREFIX = b'imm.'
def from_string(u, deep_immutable=False, name=u"<unknown name>"): def from_string(u, deep_immutable=False, name=u"<unknown name>"):
"""Create URI from either unicode or byte string.""" """Create URI from either unicode or byte string."""
if isinstance(u, unicode): if isinstance(u, str):
u = u.encode("utf-8") u = u.encode("utf-8")
if not isinstance(u, bytes): if not isinstance(u, bytes):
raise TypeError("URI must be unicode string or bytes: %r" % (u,)) raise TypeError("URI must be unicode string or bytes: %r" % (u,))
@ -844,7 +842,7 @@ def is_uri(s):
return False return False
def is_literal_file_uri(s): def is_literal_file_uri(s):
if isinstance(s, unicode): if isinstance(s, str):
s = s.encode("utf-8") s = s.encode("utf-8")
if not isinstance(s, bytes): if not isinstance(s, bytes):
return False return False
@ -853,7 +851,7 @@ def is_literal_file_uri(s):
s.startswith(ALLEGED_IMMUTABLE_PREFIX + b'URI:LIT:')) s.startswith(ALLEGED_IMMUTABLE_PREFIX + b'URI:LIT:'))
def has_uri_prefix(s): def has_uri_prefix(s):
if isinstance(s, unicode): if isinstance(s, str):
s = s.encode("utf-8") s = s.encode("utf-8")
if not isinstance(s, bytes): if not isinstance(s, bytes):
return False return False
@ -895,9 +893,9 @@ def pack_extension(data):
pieces = [] pieces = []
for k in sorted(data.keys()): for k in sorted(data.keys()):
value = data[k] value = data[k]
if isinstance(value, (int, long)): if isinstance(value, int):
value = b"%d" % value value = b"%d" % value
if isinstance(k, unicode): if isinstance(k, str):
k = k.encode("utf-8") k = k.encode("utf-8")
assert isinstance(value, bytes), k assert isinstance(value, bytes), k
assert re.match(br'^[a-zA-Z_\-]+$', k) assert re.match(br'^[a-zA-Z_\-]+$', k)