Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38010

RE: (any)dbm module lacks a context manager

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nick.cash@npcinternational.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'context': 0.05; '(defined': 0.09; 'dict': 0.09; 'subject:module': 0.09; 'to:addr:python.list': 0.09; 'to:addr:tim.thechases.com': 0.09; 'do,': 0.15; 'slightly': 0.15; '-like': 0.16; '>>>': 0.18; 'module': 0.19; 'to:name:python-list@python.org': 0.20; 'trying': 0.21; 'import': 0.21; '"",': 0.22; 'libraries': 0.22; 'to:2**1': 0.23; 'received:169.254': 0.24; 'header:In-Reply-To:1': 0.25; '(most': 0.27; "doesn't": 0.28; 'subject:: (': 0.29; 'wrap': 0.29; 'case,': 0.29; 'objects': 0.29; 'received:169': 0.29; 'error': 0.30; 'header:Received:8': 0.30; 'code': 0.31; 'implement': 0.32; 'file': 0.32; 'like:': 0.33; 'traceback': 0.33; 'to:addr:python- list': 0.33; 'protocol': 0.35; 'received:bigfish.com': 0.35; 'something': 0.35; 'actions': 0.36; 'useful': 0.36; 'charset:us- ascii': 0.36; 'does': 0.37; 'object': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'you.': 0.61; 'real': 0.61; 'close': 0.63; 'enhancement.': 0.84; 'subject:any': 0.84
X-Forefront-Antispam-Report CIP:157.56.240.117; KIP:(null); UIP:(null); IPV:NLI; H:BL2PRD0610HT004.namprd06.prod.outlook.com; RD:none; EFVD:NLI
X-SpamScore -1
X-BigFish PS-1(zz1432Izz1ee6h1de0h1202h1e76h1d1ah1d2ahzzz2fh2a8h668h839h944hd25hf0ah1220h1288h12a5h12a9h12bdh137ah13b6h1441h1504h1537h153bh15d0h162dh1631h1758h18e1h1946h1155h)
Received-SPF pass (mail49-co9: domain of npcinternational.com designates 157.56.240.117 as permitted sender) client-ip=157.56.240.117; envelope-from=nick.cash@npcinternational.com; helo=BL2PRD0610HT004.namprd06.prod.outlook.com ; .outlook.com ;
From Nick Cash <nick.cash@npcinternational.com>
To "python.list@tim.thechases.com" <python.list@tim.thechases.com>, "python-list@python.org" <python-list@python.org>
Subject RE: (any)dbm module lacks a context manager
Thread-Topic (any)dbm module lacks a context manager
Thread-Index AQHN/+WIfpkAO3FUd06HC4HltZstGJhj0XTw
Date Thu, 31 Jan 2013 19:34:16 +0000
References <20130131130305.40fc78a8@bigbox.christie.dr>
In-Reply-To <20130131130305.40fc78a8@bigbox.christie.dr>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [70.166.238.194]
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-OriginatorOrg npcinternational.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1259.1359664495.2939.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359664495 news.xs4all.nl 6905 [2001:888:2000:d::a6]:50343
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38010

Show key headers only | View raw


> >>> import dbm
> >>> with dbm.open("mydb", 'c') as d:
> ...     d["hello"] = "world"
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: '_dbm.dbm' object has no attribute '__exit__'

This error message is somewhat misleading... it actually means you're trying to use an object as a context manager, and it doesn't implement the context manager protocol (defined __enter__ and __exit__ methods). In this case, db.open() returns a dict -like object that is not a context manager. You'd need to refactor your code to something like:

import dbm
d = dbm.open("mydb", 'c')
d["hello"] = "world"
d.close() 

You may want to wrap any actions on d with a try-except-finally so you can always close the db. If dbm objects were real context managers, they would do this for you. 
This does seem like a useful enhancement. It might be slightly involved to do, as the dbm module has multiple implementations depending on what libraries are available on the OS.

-Nick Cash

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

RE: (any)dbm module lacks a context manager Nick Cash <nick.cash@npcinternational.com> - 2013-01-31 19:34 +0000

csiph-web