Changeset 2f0a5d9 in trunk


Ignore:
Timestamp:
2020-10-27T20:58:46Z (5 years ago)
Author:
GitHub <noreply@…>
Branches:
master
Children:
91c055a
Parents:
874bfa72 (diff), b79504a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Itamar Turner-Trauring <itamar@…> (2020-10-27 20:58:46)
git-committer:
GitHub <noreply@…> (2020-10-27 20:58:46)
Message:

Merge pull request #877 from tahoe-lafs/3485.backported-configparser-for-py-2

Backported configparser for Python 2

Fixes ticket:3485

Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified nix/tahoe-lafs.nix

    r874bfa72 r2f0a5d9  
    55, service-identity, pyyaml, magic-wormhole, treq, appdirs
    66, beautifulsoup4, eliot, autobahn, cryptography, netifaces
    7 , html5lib, pyutil, distro
     7, html5lib, pyutil, distro, configparser
    88}:
    99python.pkgs.buildPythonPackage rec {
     
    4747    service-identity pyyaml magic-wormhole treq
    4848    eliot autobahn cryptography netifaces setuptools
    49     future pyutil distro
     49    future pyutil distro configparser
    5050  ];
    5151
  • TabularUnified setup.py

    r874bfa72 r2f0a5d9  
    135135    # Linux distribution detection:
    136136    "distro >= 1.4.0",
     137
     138    # Backported configparser for Python 2:
     139    "configparser ; python_version < '3.0'",
    137140]
    138141
  • TabularUnified src/allmydata/client.py

    r874bfa72 r2f0a5d9  
    33from functools import partial
    44from errno import ENOENT, EPERM
    5 try:
    6     from ConfigParser import NoSectionError
    7 except ImportError:
    8     from configparser import NoSectionError
     5
     6# On Python 2 this will be the backported package:
     7from configparser import NoSectionError
    98
    109from foolscap.furl import (
     
    3635    yamlutil, configutil,
    3736)
    38 from allmydata.util.encodingutil import (get_filesystem_encoding,
    39                                          from_utf8_or_none)
     37from allmydata.util.encodingutil import get_filesystem_encoding
    4038from allmydata.util.abbreviate import parse_abbreviated_size
    4139from allmydata.util.time_format import parse_duration, parse_date
     
    720718    def init_stats_provider(self):
    721719        gatherer_furl = self.config.get_config("client", "stats_gatherer.furl", None)
     720        if gatherer_furl:
     721            # FURLs should be bytes:
     722            gatherer_furl = gatherer_furl.encode("utf-8")
    722723        self.stats_provider = StatsProvider(self, gatherer_furl)
    723724        self.stats_provider.setServiceParent(self)
     
    807808        config_storedir = self.get_config(
    808809            "storage", "storage_dir", self.STOREDIR,
    809         ).decode('utf-8')
     810        )
    810811        storedir = self.config.get_config_path(config_storedir)
    811812
     
    935936        if helper_furl in ("None", ""):
    936937            helper_furl = None
     938
     939        # FURLs need to be bytes:
     940        if helper_furl is not None:
     941            helper_furl = helper_furl.encode("utf-8")
    937942
    938943        DEP = self.encoding_params
     
    10441049        from allmydata.webish import WebishServer
    10451050        nodeurl_path = self.config.get_config_path("node.url")
    1046         staticdir_config = self.config.get_config("node", "web.static", "public_html").decode("utf-8")
     1051        staticdir_config = self.config.get_config("node", "web.static", "public_html")
    10471052        staticdir = self.config.get_config_path(staticdir_config)
    10481053        ws = WebishServer(self, webport, nodeurl_path, staticdir)
     
    10511056    def init_ftp_server(self):
    10521057        if self.config.get_config("ftpd", "enabled", False, boolean=True):
    1053             accountfile = from_utf8_or_none(
    1054                 self.config.get_config("ftpd", "accounts.file", None))
     1058            accountfile = self.config.get_config("ftpd", "accounts.file", None)
    10551059            if accountfile:
    10561060                accountfile = self.config.get_config_path(accountfile)
     
    10641068    def init_sftp_server(self):
    10651069        if self.config.get_config("sftpd", "enabled", False, boolean=True):
    1066             accountfile = from_utf8_or_none(
    1067                 self.config.get_config("sftpd", "accounts.file", None))
     1070            accountfile = self.config.get_config("sftpd", "accounts.file", None)
    10681071            if accountfile:
    10691072                accountfile = self.config.get_config_path(accountfile)
    10701073            accounturl = self.config.get_config("sftpd", "accounts.url", None)
    10711074            sftp_portstr = self.config.get_config("sftpd", "port", "8022")
    1072             pubkey_file = from_utf8_or_none(self.config.get_config("sftpd", "host_pubkey_file"))
    1073             privkey_file = from_utf8_or_none(self.config.get_config("sftpd", "host_privkey_file"))
     1075            pubkey_file = self.config.get_config("sftpd", "host_pubkey_file")
     1076            privkey_file = self.config.get_config("sftpd", "host_privkey_file")
    10741077
    10751078            from allmydata.frontends import sftpd
  • TabularUnified src/allmydata/frontends/ftpd.py

    r874bfa72 r2f0a5d9  
     1from six import ensure_str
    12
    23from types import NoneType
     
    334335
    335336        f = ftp.FTPFactory(p)
     337        # strports requires a native string.
     338        ftp_portstr = ensure_str(ftp_portstr)
    336339        s = strports.service(ftp_portstr, f)
    337340        s.setServiceParent(self)
  • TabularUnified src/allmydata/node.py

    r874bfa72 r2f0a5d9  
    44"""
    55from past.builtins import unicode
     6from six import ensure_str
    67
    78import datetime
     
    1011import types
    1112import errno
    12 from io import StringIO
    1313import tempfile
    1414from base64 import b32decode, b32encode
    1515
    16 # Python 2 compatibility
    17 from six.moves import configparser
    18 from future.utils import PY2
    19 if PY2:
    20     from io import BytesIO as StringIO  # noqa: F811
     16# On Python 2 this will be the backported package.
     17import configparser
    2118
    2219from twisted.python import log as twlog
     
    188185    # (try to) read the main config file
    189186    config_fname = os.path.join(basedir, "tahoe.cfg")
    190     parser = configparser.SafeConfigParser()
    191187    try:
    192188        parser = configutil.get_config(config_fname)
     
    194190        if e.errno != errno.ENOENT:
    195191            raise
     192        # The file is missing, just create empty ConfigParser.
     193        parser = configutil.get_config_from_string(u"")
    196194
    197195    configutil.validate_config(config_fname, parser, _valid_config)
     
    210208        _valid_config = _common_valid_config()
    211209
     210    if isinstance(config_str, bytes):
     211        config_str = config_str.decode("utf-8")
     212
    212213    # load configuration from in-memory string
    213     parser = configparser.SafeConfigParser()
    214     parser.readfp(StringIO(config_str))
     214    parser = configutil.get_config_from_string(config_str)
    215215
    216216    fname = "<in-memory>"
     
    640640    location = ",".join(new_locations)
    641641
     642    # Lacking this, Python 2 blows up in Foolscap when it is confused by a
     643    # Unicode FURL.
     644    location = location.encode("utf-8")
     645
    642646    return tubport, location
    643647
     
    687691            else:
    688692                port_or_endpoint = port
     693            # Foolscap requires native strings:
     694            if isinstance(port_or_endpoint, (bytes, unicode)):
     695                port_or_endpoint = ensure_str(port_or_endpoint)
    689696            tub.listenOn(port_or_endpoint)
    690697        tub.setLocation(location)
     
    840847        if lgfurl:
    841848            # this is in addition to the contents of log-gatherer-furlfile
     849            lgfurl = lgfurl.encode("utf-8")
    842850            self.log_tub.setOption("log-gatherer-furl", lgfurl)
    843851        self.log_tub.setOption("log-gatherer-furlfile",
  • TabularUnified src/allmydata/scripts/common.py

    r874bfa72 r2f0a5d9  
    99if PY2:
    1010    from future.builtins import str  # noqa: F401
    11 from six.moves.configparser import NoSectionError
     11
     12# On Python 2 this will be the backported package:
     13from configparser import NoSectionError
    1214
    1315from twisted.python import usage
  • TabularUnified src/allmydata/storage_client.py

    r874bfa72 r2f0a5d9  
    3232
    3333import re, time, hashlib
    34 try:
    35     from ConfigParser import (
    36         NoSectionError,
    37     )
    38 except ImportError:
    39     from configparser import NoSectionError
     34
     35# On Python 2 this will be the backport.
     36from configparser import NoSectionError
     37
    4038import attr
    4139from zope.interface import (
  • TabularUnified src/allmydata/test/test_configutil.py

    r874bfa72 r2f0a5d9  
    155155        )
    156156        self.assertIn("section [node] contains unknown option 'invalid'", str(e))
     157
     158    def test_duplicate_sections(self):
     159        """
     160        Duplicate section names are merged.
     161        """
     162        fname = self.create_tahoe_cfg('[node]\na = foo\n[node]\n b = bar\n')
     163        config = configutil.get_config(fname)
     164        self.assertEqual(config.get("node", "a"), "foo")
     165        self.assertEqual(config.get("node", "b"), "bar")
  • TabularUnified src/allmydata/test/test_connections.py

    r874bfa72 r2f0a5d9  
    169169            tor_provider.get_tor_handler()
    170170        self.assertIn(
    171             "invalid literal for int() with base 10: 'kumquat'",
     171            "invalid literal for int()",
     172            str(ctx.exception)
     173        )
     174        self.assertIn(
     175            "kumquat",
    172176            str(ctx.exception)
    173177        )
  • TabularUnified src/allmydata/test/test_node.py

    r874bfa72 r2f0a5d9  
    152152
    153153        config = read_config(basedir, "")
    154         self.failUnlessEqual(config.get_config("node", "nickname").decode('utf-8'),
     154        self.failUnlessEqual(config.get_config("node", "nickname"),
    155155                             u"\u2621")
    156156
  • TabularUnified src/allmydata/util/configutil.py

    r874bfa72 r2f0a5d9  
    22Read/write config files.
    33
    4 Configuration is returned as native strings.
     4Configuration is returned as Unicode strings.
    55
    66Ported to Python 3.
     
    1313from future.utils import PY2
    1414if PY2:
    15     # We don't do open(), because we want files to read/write native strs when
    16     # we do "r" or "w".
    17     from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, pow, round, super, bytes, dict, list, object, range, str, max, min  # noqa: F401
     15    from 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
    1816
    19 if PY2:
    20     # In theory on Python 2 configparser also works, but then code gets the
    21     # wrong exceptions and they don't get handled. So just use native parser
    22     # for now.
    23     from ConfigParser import SafeConfigParser
    24 else:
    25     from configparser import SafeConfigParser
     17# On Python 2 we use the backport package; that means we always get unicode
     18# out.
     19from configparser import ConfigParser
    2620
    2721import attr
     
    3731
    3832def get_config(tahoe_cfg):
    39     """Load the config, returning a SafeConfigParser.
     33    """Load the config, returning a ConfigParser.
    4034
    41     Configuration is returned as native strings.
     35    Configuration is returned as Unicode strings.
    4236    """
    43     config = SafeConfigParser()
    44     with open(tahoe_cfg, "r") as f:
    45         # On Python 2, where we read in bytes, skip any initial Byte Order
    46         # Mark. Since this is an ordinary file, we don't need to handle
    47         # incomplete reads, and can assume seekability.
    48         if PY2 and f.read(3) != b'\xEF\xBB\xBF':
    49             f.seek(0)
    50         config.readfp(f)
    51     return config
     37    # Byte Order Mark is an optional garbage code point you sometimes get at
     38    # the start of UTF-8 encoded files. Especially on Windows. Skip it by using
     39    # utf-8-sig. https://en.wikipedia.org/wiki/Byte_order_mark
     40    with open(tahoe_cfg, "r", encoding="utf-8-sig") as f:
     41        cfg_string = f.read()
     42    return get_config_from_string(cfg_string)
     43
     44
     45def get_config_from_string(tahoe_cfg_string):
     46    """Load the config from a string, return the ConfigParser.
     47
     48    Configuration is returned as Unicode strings.
     49    """
     50    parser = ConfigParser(strict=False)
     51    parser.read_string(tahoe_cfg_string)
     52    return parser
     53
    5254
    5355def set_config(config, section, option, value):
  • TabularUnified src/allmydata/webish.py

    r874bfa72 r2f0a5d9  
     1from six import ensure_str
     2
    13import re, time
    24
     
    187189        if re.search(r'^\d', webport):
    188190            webport = "tcp:"+webport # twisted warns about bare "0" or "3456"
     191        # strports must be native strings.
     192        webport = ensure_str(webport)
    189193        s = strports.service(webport, self.site)
    190194        s.setServiceParent(self)
Note: See TracChangeset for help on using the changeset viewer.