'Creates a cursor, opening a connection if necessary.'

def cursor(self):

    self.validate_thread_sharing()
    if (self.use_debug_cursor or ((self.use_debug_cursor is None) and settings.DEBUG)):
        cursor = self.make_debug_cursor(self._cursor())
    else:
        cursor = util.CursorWrapper(self._cursor(), self)
    return cursor
