Opened 6 years ago

Closed 6 years ago

#28853 closed Cleanup/optimization (fixed)

Use database cursor as a context manager internally

Reported by: Jon Dufresne Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While reading the code Django code, I noticed some uses of connection.cursor() weren't being used as a context manager. For example:

https://github.com/django/django/blob/4f5526e346861c0b2ffa2ea7229747c883e14432/django/test/testcases.py#L855-L857

def _reset_sequences(self, db_name):
    ...
    cursor = conn.cursor()
    for sql in sql_list:
        cursor.execute(sql)

This pattern exists multiple time throughout Django. Using a context manager helps ensure resources are closed deterministically and that errors aren't hidden.

Change History (4)

comment:1 by Jon Dufresne, 6 years ago

Has patch: set

comment:2 by Mariusz Felisiak, 6 years ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted

comment:3 by Mariusz Felisiak, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In 7a6fbf3:

Fixed #28853 -- Updated connection.cursor() uses to use a context manager.

Note: See TracTickets for help on using tickets.
Back to Top