Very strange KeyError on 'PORT' in db/backends/mysql/base.py

27 views
Skip to first unread message

Peter Bengtsson

unread,
Nov 23, 2011, 1:39:37 PM11/23/11
to django...@googlegroups.com
(django version 1.3.1)

To explain my set up would be very hard as it's not just plain Django but there's a bunch of other third parties involved but we'll have to try.

I'm getting this traceback when running tests::

Traceback (most recent call last):
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/utils/unittest/case.py", line 339, in run
    testMethod()
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/apps/users/tests.py", line 310, in test_mozilla_ldap_backend_basic
    user, created = back.get_or_create_user('peter', ldap_user)
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/apps/users/auth/backends.py", line 146, in get_or_create_user
    .filter(email__iexact=ldap_user.attrs.get('mail')[0])):
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/models/query.py", line 107, in _result_iter
    self._fill_cache()
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/models/query.py", line 772, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/models/query.py", line 273, in iterator
    for row in compiler.results_iter():
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/models/sql/compiler.py", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/models/sql/compiler.py", line 734, in execute_sql
    cursor = self.connection.cursor()
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/backends/__init__.py", line 252, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/db/backends/mysql/base.py", line 318, in _cursor
    if settings_dict['PORT']:
KeyError: 'PORT'

So just above line 318 in db/backends/mysql/base.py I put in this::

 if 'PORT' not in settings_dict:  # my debugging
      from pprint import pprint;pprint(settings_dict)  # mydebugging
  if settings_dict['PORT']:
      kwargs['port'] = int(settings_dict['PORT'])

And then when printed out it prints this::

{'ENGINE': 'django.db.backends.mysql',
 'HOST': 'localhost',
 'NAME': 'test_pto',
 'OPTIONS': {'charset': 'utf8',
             'init_command': 'SET storage_engine=InnoDB',
             'use_unicode': True},
 'PASSWORD': XXXXXX,
 'TEST_CHARSET': 'utf8',
 'TEST_COLLATION': 'utf8_general_ci',
 'TEST_MIRROR': None,
 'TEST_NAME': None,
 'TIME_ZONE': 'America/Los_Angeles',
 'USER': 'root'}

As you can see it's a copy of settings.DATABASES but with the name having an added prefix of "test_". Except now it's missing 'PORT'.

My settings look like this::

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'pto',
        'USER': 'root',
        'PASSWORD': XXXX,
        'HOST': '',
        'PORT': '',
        'OPTIONS': {
            'init_command': 'SET storage_engine=InnoDB',
            'charset' : 'utf8',
            'use_unicode' : True,
        },
        'TEST_CHARSET': 'utf8',
        'TEST_COLLATION': 'utf8_general_ci',
    },
}

Having a blank 'PORT' key shouldn't be a problem. Removing the key doesn't help. However, setting it to '3306' *does* help but that's not getting to the root of the problem.

I know it's a very very hard problem to debug but by mentioning it perhaps other people can chip in some experience. It's a long shot.

A couple of other things:
* It fails on the first and every test
* Debugging self._connection['default'] a bit I find that it has 'PORT' for a while (I think whilst setting up fixtures and stuff) and then stops having it.
* I'm using github.com/jbalogh/test-utils but that hasn't changed and it used to work perfectly fine a couple of days ago.
* Switching test runner to TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner' solves the problem but I didn't need to do this before.
* Colleagues have been unable to reproduce this using very similar stacks which could mean it's OS related to threading or something really low-level. Sigh...


Ivo Brodien

unread,
Nov 23, 2011, 2:14:54 PM11/23/11
to django...@googlegroups.com


* Colleagues have been unable to reproduce this using very similar stacks which could mean it's OS related to threading or something really low-level. Sigh...

maybe I did a wrong search, but I did

grep -r '3306’ . 

inside the django directory and could not find a place where the default port is specified.

Shouldn’t it be there or is this specified in e.g. MySQLdb?


Ivo Brodien

unread,
Nov 23, 2011, 2:33:14 PM11/23/11
to django...@googlegroups.com
just another wild guess:

in django/db/utils.py Line 84:

for setting in ('NAME', 'USER', 'PASSWORD', 'HOST', 'PORT'):
conn.setdefault(setting, ‘’)

It looks like PORT is set to ‘’ instead of 3306 if missing. But still it should be in the dict.

really strange, yes.

Reply all
Reply to author
Forward
0 new messages