Many parts of the code raise None exception rather than original exception, that can not find the real error。

Issue #2357 resolved
Former user created an issue

Many parts of the code raise None exception rather than original exception, that can not find the real error。

For example: SQLAlchemy-0.7.1/sqlalchemy/orm/session.py, in line 1561, in _flush

Because i use eventlet.db_pool without charset option, I got TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType, but actually encoding errors。

Comments (3)

  1. Mike Bayer repo owner

    Need detailed information about the specific issue, description is too vague (note the guidelines for posting bugs at http://www.sqlalchemy.org/participate.html#bugs request that a full, succinct, reproducing test case for the bug is attached).

    Line 1561 of _session.py does not handle user input or by itself raise any exception:

    flush_context = UOWTransaction(self)
    

    SQLAlchemy exceptions derive from SQLAlchemyError, which in turn derives from Python's base Exception class (line 17, exc.py):

    class SQLAlchemyError(Exception):
        """Generic error class."""
    

    They don't extend from NoneType, which is not even possible in Python.

    Per Python's own documentation, inheriting from Exception is the correct approach. See #2160. Citing http://docs.python.org/library/exceptions.html:

    The built-in exception classes can be sub-classed to define new exceptions; programmers are encouraged to at least **derive new exceptions from the Exception class and not BaseException**. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions.
    

    And directly below that:

    exception BaseException
    
    The base class for all built-in exceptions. **It is not meant to be directly inherited by user-defined classes (for that, use Exception)**.
    

    I'm not familiar with eventlet.db_pool, though SQLAlchemy does not currently have supported integration paths with asynchronous systems. Certainly if it requires that exceptions are implemented as old style classes and not extending Exception, that's a bug in eventlet, not SQLAlchemy.

  2. Log in to comment