cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

document.Document context manager ignore exceptions #427

Closed aogier closed 5 years ago

aogier commented 5 years ago

Bug Description

document.Document context manager ignores exceptions in inner block, making costly manual housekeeping/rollback necessary in case of errors. Using try/except blocks is indeed feasible but still expensive as I'd have to keep a pristine copy in memory (and I'd get a new _rev anyway because save() in __exit__).

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

import cloudant

client = cloudant.CouchDB('user', 'pass',
                          url='http://localhost:5984', connect=True)
db = client['db']

db.create_document({'_id': 'julia006', 'name': 'julia', 'age': 6})

with cloudant.document.Document(db, 'julia006') as doc:

    doc['age'] = 7
    0 / 0
    doc['prop'] = 'value'

# doc will be partly updated:
# {'_id': 'julia006',
#  '_rev': '2-492dc56da45c97bf6b17ff85e8d83f9d',
#  'name': 'julia', 'age': 7}

2. What you expected to happen

Document not updated because an error in context manager inner block.

3. What actually happened

Document get updated with all the changes made until the error happened.

aogier commented 5 years ago

solved in master