Skip to content

Commit c86a295

Browse files
committed
BUG19584116: Fix extra signal causing runtime error in Django
Using connection_created signal defined in django.db.backends.signals causes maximum recursion depth reached runtime error. We fix this by not sending the extra signal after the connection to the database is setup. Unit tests are updated.
1 parent 55490d7 commit c86a295

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/mysql/connector/django/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ def get_new_connection(self, conn_params):
584584
cnx = mysql.connector.connect(**conn_params)
585585
self.server_version = cnx.get_server_version()
586586
cnx.set_converter_class(DjangoMySQLConverter)
587-
connection_created.send(sender=self.__class__, connection=self)
588587

589588
return cnx
590589

@@ -609,6 +608,7 @@ def create_cursor(self):
609608
def _connect(self):
610609
"""Setup the connection with MySQL"""
611610
self.connection = self.get_new_connection(self.get_connection_params())
611+
connection_created.send(sender=self.__class__, connection=self)
612612
self.init_connection_state()
613613

614614
def _cursor(self):

tests/test_django.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import datetime
2828
import sys
29+
import unittest
2930

3031
import tests
3132

@@ -82,6 +83,7 @@
8283
import django.db # pylint: disable=W0611
8384
if tests.DJANGO_VERSION >= (1, 6):
8485
from django.db.backends import FieldInfo
86+
from django.db.backends.signals import connection_created
8587

8688
import mysql.connector
8789
from mysql.connector.django.base import (DatabaseWrapper, DatabaseOperations,
@@ -207,6 +209,23 @@ def test__init__(self):
207209
exp = self.conn.converter._time_to_mysql(value)
208210
self.assertEqual(exp, self.cnx.ops.value_to_db_time(value))
209211

212+
def test_signal(self):
213+
from django.db import connection
214+
215+
def conn_setup(*args, **kwargs):
216+
conn = kwargs['connection']
217+
cur = conn.cursor()
218+
cur.execute("SET @xyz=10")
219+
cur.close()
220+
221+
connection_created.connect(conn_setup)
222+
cursor = connection.cursor()
223+
cursor.execute("SELECT @xyz")
224+
225+
self.assertEqual((10,), cursor.fetchone())
226+
cursor.close()
227+
self.cnx.close()
228+
210229

211230
class DjangoDatabaseOperations(tests.MySQLConnectorTests):
212231

0 commit comments

Comments
 (0)