def raw_connection_from ( engine_or_conn ) : if hasattr ( engine_or_conn , 'cursor' ) : return engine_or_conn , False if hasattr ( engine_or_conn , 'connection' ) : return engine_or_conn . connection , False return engine_or_conn . raw_connection ( ) , True
how to check if sql connection is open in python
Extract a raw_connection and determine if it should be automatically closed .
def raw_connection_from(engine_or_conn):
    """Extract a raw_connection and determine if it should be automatically closed.

    Only connections opened by this package will be closed automatically.
    """
    if hasattr(engine_or_conn, 'cursor'):
        return engine_or_conn, False
    if hasattr(engine_or_conn, 'connection'):
        return engine_or_conn.connection, False
    return engine_or_conn.raw_connection(), True