From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id QK+ODoju517aEgAA0tVLHw (envelope-from ) for ; Mon, 15 Jun 2020 21:56:24 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id kBV4Coju517YOgAA1q6Kng (envelope-from ) for ; Mon, 15 Jun 2020 21:56:24 +0000 Received: from arlo.cworth.org (arlo.cworth.org [50.126.95.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9C3C2940B0C for ; Mon, 15 Jun 2020 21:56:23 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 075226DE104A; Mon, 15 Jun 2020 14:56:16 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NR5nTFqBnJm8; Mon, 15 Jun 2020 14:56:16 -0700 (PDT) Received: from arlo.cworth.org (localhost [IPv6:::1]) by arlo.cworth.org (Postfix) with ESMTP id 7F54C6DE0F98; Mon, 15 Jun 2020 14:56:12 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id ED09E6DE0F80 for ; Mon, 15 Jun 2020 14:56:09 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id FNwN7K88Afap for ; Mon, 15 Jun 2020 14:56:09 -0700 (PDT) Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by arlo.cworth.org (Postfix) with ESMTPS id 420F86DE0F4A for ; Mon, 15 Jun 2020 14:56:09 -0700 (PDT) Received: by mail-ej1-f54.google.com with SMTP id dr13so19095437ejc.3 for ; Mon, 15 Jun 2020 14:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=//nvKHNc/FvHik0Kv/S5MHKr/q9n8bLLxpcyK4xjLB4=; b=YkAKf4B/eS7iHmcdlJrp3Nhb302ls9VDcd+j1zVQmy1pNfOQqS8diu8cQGOLrGr0sf Q9lMM3C93slIghCiwlrm+nddxHTcOScSi8wkw+69A5pKFVGDrn8/LQ1J8AKaFyTuq518 KAgd+gmpI+jSr8jLG/IgBZr/vDADIRQeY5uqhDotsUq1Yx0CNvErYEQ8g9n2G563cDPP gZwYTFMjR98U8+DZrhK43xaezQA+DpxC6meOr8tDJkKGUfx06dVSc81kKiGExLXU6U5n Q49dMmOposD7/S38Gl8bqZJL5E0XG7FSO25pD5YdI3iWFcYa5KKpkZ44VmY2m0AEbRlN v9Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=//nvKHNc/FvHik0Kv/S5MHKr/q9n8bLLxpcyK4xjLB4=; b=sZk5v8lUMNnOJTX6KZqxr/Z6m5sFIXrH6fTD8EJC0OmyYDG3xTfzTr0RMEYD2xTELX eNJB//dMaWmUpnCJGPoO5mshxCyRbGcQDYeIoWZaU28IgvxvNclRNipRPraIrZzvBiRX A5YBMT2hM8iLCAe9IObkE5/Cx9gfF1dbJ1FXa9iguwbf953UjoCS3ANQ+/nWGOvX7JLi 6dJFh1famoKVAxZYnlQyBJpjCnNNm2qcREmIPqIBLZBfQ3Svg5A8Cj3yMr+WwwXG/L63 /ld7z7H3J4D9JSpP0ntPMh2mNLv22cksf77YJLCrZzmw7iFTgd7640tWnBwxbQRn1EWl WNgA== X-Gm-Message-State: AOAM5301WJimQaHKrkaNyqoqBsx+jQh1EQuVlVtS/mS/lFokrwfxPZv1 FkEKHREfsJwU/oadwDvH7Q2oX0FZ X-Google-Smtp-Source: ABdhPJzg9hBxYEZe3S2x+jByrGSoAFIYLCwPBpyYQ8T+nyIjOcA8YGIVO+EM8pUXyH+O9L8rXBWzJA== X-Received: by 2002:a17:906:19c7:: with SMTP id h7mr26898918ejd.403.1592258167503; Mon, 15 Jun 2020 14:56:07 -0700 (PDT) Received: from powell.devork.be (2a02-8388-8480-1180-4c18-fc69-8d8c-22b5.cable.dynamic.v6.surfer.at. [2a02:8388:8480:1180:4c18:fc69:8d8c:22b5]) by smtp.gmail.com with ESMTPSA id qt19sm9769640ejb.14.2020.06.15.14.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 14:56:07 -0700 (PDT) Received: (nullmailer pid 220677 invoked by uid 1000); Mon, 15 Jun 2020 21:56:05 -0000 From: Floris Bruynooghe To: notmuch@notmuchmail.org Subject: [PATCH 2/2] python config access: fix style and KeyError bug Date: Mon, 15 Jun 2020 23:55:53 +0200 Message-Id: <20200615215553.220529-3-flub@devork.be> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200615215553.220529-1-flub@devork.be> References: <20200615215553.220529-1-flub@devork.be> MIME-Version: 1.0 X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: notmuch-bounces@notmuchmail.org Sender: "notmuch" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (body hash did not verify) header.d=gmail.com header.s=20161025 header.b=YkAKf4B/; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 50.126.95.6 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Spam-Score: 0.99 X-TUID: AO/2Iv9ly+HX This fixes some minor style/pep8 things and adds tests for the new config support. Also fixes a bug where KeyError was never raised on a missing key. --- bindings/python-cffi/notmuch2/_config.py | 9 ++-- bindings/python-cffi/tests/test_config.py | 56 +++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 bindings/python-cffi/tests/test_config.py diff --git a/bindings/python-cffi/notmuch2/_config.py b/bindings/python-cffi/notmuch2/_config.py index 58383c16..29de6495 100644 --- a/bindings/python-cffi/notmuch2/_config.py +++ b/bindings/python-cffi/notmuch2/_config.py @@ -4,9 +4,12 @@ import notmuch2._base as base import notmuch2._capi as capi import notmuch2._errors as errors + __all__ = ['ConfigMapping'] + class ConfigIter(base.NotmuchIter): + def __init__(self, parent, iter_p): super().__init__( parent, iter_p, @@ -19,6 +22,7 @@ class ConfigIter(base.NotmuchIter): item = super().__next__() return base.BinString.from_cffi(item) + class ConfigMapping(base.NotmuchObject, collections.abc.MutableMapping): """The config key/value pairs stored in the database. @@ -50,11 +54,10 @@ class ConfigMapping(base.NotmuchObject, collections.abc.MutableMapping): ret = capi.lib.notmuch_database_get_config(self._ptr(), key, val_pp) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) - if val_pp[0] == "": - capi.lib.free(val_pp[0]) - raise KeyError val = base.BinString.from_cffi(val_pp[0]) capi.lib.free(val_pp[0]) + if val == '': + raise KeyError return val def __setitem__(self, key, val): diff --git a/bindings/python-cffi/tests/test_config.py b/bindings/python-cffi/tests/test_config.py new file mode 100644 index 00000000..1b2695f5 --- /dev/null +++ b/bindings/python-cffi/tests/test_config.py @@ -0,0 +1,56 @@ +import collections.abc + +import pytest + +import notmuch2._database as dbmod + +import notmuch2._config as config + + +class TestIter: + + @pytest.fixture + def db(self, maildir): + with dbmod.Database.create(maildir.path) as db: + yield db + + def test_type(self, db): + assert isinstance(db.config, collections.abc.MutableMapping) + assert isinstance(db.config, config.ConfigMapping) + + def test_alive(self, db): + assert db.config.alive + + def test_set_get(self, maildir): + # Ensure get-set works from different db objects + with dbmod.Database.create(maildir.path) as db0: + db0.config['spam'] = 'ham' + with dbmod.Database(maildir.path) as db1: + assert db1.config['spam'] == 'ham' + + def test_get_keyerror(self, db): + with pytest.raises(KeyError): + val = db.config['not-a-key'] + print(repr(val)) + + def test_iter(self, db): + assert list(db.config) == [] + db.config['spam'] = 'ham' + db.config['eggs'] = 'bacon' + assert set(db.config) == {'spam', 'eggs'} + assert set(db.config.keys()) == {'spam', 'eggs'} + assert set(db.config.values()) == {'ham', 'bacon'} + assert set(db.config.items()) == {('spam', 'ham'), ('eggs', 'bacon')} + + def test_len(self, db): + assert len(db.config) == 0 + db.config['spam'] = 'ham' + assert len(db.config) == 1 + db.config['eggs'] = 'bacon' + assert len(db.config) == 2 + + def test_del(self, db): + db.config['spam'] = 'ham' + assert db.config.get('spam') == 'ham' + del db.config['spam'] + assert db.config.get('spam') is None -- 2.27.0