Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed. when mysql fetch result None crash #369

Closed
wants to merge 1 commit into from

Conversation

Lupino
Copy link

@Lupino Lupino commented Jun 7, 2017

when mysql fetch result is None getitem crash

    item = self.rowproxy[index]
TypeError: 'NoneType' object has no attribute '__getitem__'

    item = self.rowproxy[index]
TypeError: 'NoneType' object has no attribute '__getitem__'
@zzzeek
Copy link
Owner

zzzeek commented Jun 7, 2017

hi there -

row.fetchone() will never be None, and if it is, that's a bug elsewhere. silently skipping here will just cause more failures.

can you please illustrate a complete test case that shows how this value becomes None?

@Lupino
Copy link
Author

Lupino commented Jun 8, 2017

error cause on mysql version 5.7.18

Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> engine = sqlalchemy.create_engine('mysql://root:@localhost/traffic?charset=utf8')
>>> conn = engine.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2091, in connect
    return self._connection_cls(self, **kwargs)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 90, in __init__
    if connection is not None else engine.raw_connection()
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2177, in raw_connection
    self.pool.unique_connection, _connection)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 328, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
    self._dec_overflow()
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
    return self._create_connection()
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/pool.py", line 661, in __connect
    exec_once(self.connection, self)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 246, in exec_once
    self(*args, **kw)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
    fn(*args, **kw)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 1331, in go
    return once_fn(*arg, **kw)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 181, in first_connect
    dialect.initialize(c)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1698, in initialize
    self._detect_ansiquotes(connection)
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1975, in _detect_ansiquotes
    mode = row[1] or ''
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2062, in __getitem__
    item = self.rowproxy[index]
TypeError: 'NoneType' object has no attribute '__getitem__'
>>>

@Lupino
Copy link
Author

Lupino commented Jun 8, 2017

Fixed. the _compat_fist and test the _compat_fetchone

mysql> select * from `instance_status_history`;
Empty set (0.00 sec)

mysql>
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> engine = sqlalchemy.create_engine('mysql://root:@localhost/traffic?charset=utf8')
>>> conn = engine.connect()
>>> rp = conn.execute('select * from `instance_status_history`')
>>> row = rp.dialect._compat_fetchone(rp)
>>> row[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lmj/repo/nupic/venv-test/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2064, in __getitem__
    item = self.rowproxy[index]
TypeError: 'NoneType' object has no attribute '__getitem__'
>>>

@zzzeek
Copy link
Owner

zzzeek commented Jun 8, 2017

mmmm still need to see what MySQL Python version you have, run this:

python -c "import MySQLdb; print(MySQLdb.__version__)"

Also go onto your database and show me the output of this:


MariaDB [(none)]> show variables like 'sql_mode';
+---------------+--------------------------------------------+
| Variable_name | Value                                      |
+---------------+--------------------------------------------+
| sql_mode      | NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------+
1 row in set (0.00 sec)

"sql_mode" is always present, if something weird is going on on your database where this is not coming back, we have to fix this elsewhere (e.g. we should not be calling _compat_first()).

@Lupino
Copy link
Author

Lupino commented Jun 10, 2017

$ python -c "import MySQLdb; print(MySQLdb.__version__)"
1.2.5
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4269
Server version: 5.7.18 Homebrew

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'sql_mode';
Empty set (0.00 sec)

mysql>

@zzzeek
Copy link
Owner

zzzeek commented Jun 12, 2017

OK. can you show me the output of "SHOW VARIABLES" just plain? also log in as root, and do "SHOW GRANTS FOR " with the user you're logging in as? if theres a case where a logged in user can't see VARIABLES that is a new bug report.

@zzzeek
Copy link
Owner

zzzeek commented Jun 12, 2017

issue created at https://bitbucket.org/zzzeek/sqlalchemy/issues/4007/mysql-57-may-have-made-show-variables as this looks like a regression due to changes in MySQL.

@Lupino
Copy link
Author

Lupino commented Jun 12, 2017

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9857
Server version: 5.7.18 Homebrew

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW VARIABLES;
Empty set (0.00 sec)

mysql> SHOW GRANTS FOR;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql>

@zzzeek
Copy link
Owner

zzzeek commented Jun 16, 2017

resolved in 5650a0c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants