Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13798 -- Added connection argument to the connection_created s…

…ignal. Thanks to liangent for the report, and Alex Gaynor for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13672 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6909c22663ec12f95501622bba643504f63d3143 1 parent a5c80a2
Russell Keith-Magee authored August 30, 2010
2  django/contrib/gis/db/backends/spatialite/base.py
@@ -51,7 +51,7 @@ def _cursor(self):
51 51
             self.connection.create_function("django_extract", 2, _sqlite_extract)
52 52
             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
53 53
             self.connection.create_function("regexp", 2, _sqlite_regexp)
54  
-            connection_created.send(sender=self.__class__)
  54
+            connection_created.send(sender=self.__class__, connection=self)
55 55
 
56 56
             ## From here on, customized for GeoDjango ##
57 57
 
2  django/db/backends/mysql/base.py
@@ -297,7 +297,7 @@ def _cursor(self):
297 297
             self.connection = Database.connect(**kwargs)
298 298
             self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
299 299
             self.connection.encoders[SafeString] = self.connection.encoders[str]
300  
-            connection_created.send(sender=self.__class__)
  300
+            connection_created.send(sender=self.__class__, connection=self)
301 301
         cursor = CursorWrapper(self.connection.cursor())
302 302
         return cursor
303 303
 
2  django/db/backends/oracle/base.py
@@ -384,7 +384,7 @@ def _cursor(self):
384 384
                 # Django docs specify cx_Oracle version 4.3.1 or higher, but
385 385
                 # stmtcachesize is available only in 4.3.2 and up.
386 386
                 pass
387  
-            connection_created.send(sender=self.__class__)
  387
+            connection_created.send(sender=self.__class__, connection=self)
388 388
         if not cursor:
389 389
             cursor = FormatStylePlaceholderCursor(self.connection)
390 390
         return cursor
5  django/db/backends/postgresql/base.py
@@ -135,8 +135,9 @@ def _cursor(self):
135 135
             if settings_dict['PORT']:
136 136
                 conn_string += " port=%s" % settings_dict['PORT']
137 137
             self.connection = Database.connect(conn_string, **settings_dict['OPTIONS'])
138  
-            self.connection.set_isolation_level(1) # make transactions transparent to all cursors
139  
-            connection_created.send(sender=self.__class__)
  138
+            # make transactions transparent to all cursors
  139
+            self.connection.set_isolation_level(1)
  140
+            connection_created.send(sender=self.__class__, connection=self)
140 141
         cursor = self.connection.cursor()
141 142
         if new_connection:
142 143
             if set_tz:
2  django/db/backends/postgresql_psycopg2/base.py
@@ -136,7 +136,7 @@ def _cursor(self):
136 136
             self.connection = Database.connect(**conn_params)
137 137
             self.connection.set_client_encoding('UTF8')
138 138
             self.connection.set_isolation_level(self.isolation_level)
139  
-            connection_created.send(sender=self.__class__)
  139
+            connection_created.send(sender=self.__class__, connection=self)
140 140
         cursor = self.connection.cursor()
141 141
         cursor.tzinfo_factory = None
142 142
         if new_connection:
2  django/db/backends/signals.py
... ...
@@ -1,3 +1,3 @@
1 1
 from django.dispatch import Signal
2 2
 
3  
-connection_created = Signal()
  3
+connection_created = Signal(providing_args=["connection"])
2  django/db/backends/sqlite3/base.py
@@ -176,7 +176,7 @@ def _cursor(self):
176 176
             self.connection.create_function("django_extract", 2, _sqlite_extract)
177 177
             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
178 178
             self.connection.create_function("regexp", 2, _sqlite_regexp)
179  
-            connection_created.send(sender=self.__class__)
  179
+            connection_created.send(sender=self.__class__, connection=self)
180 180
         return self.connection.cursor(factory=SQLiteCursorWrapper)
181 181
 
182 182
     def close(self):
65  tests/regressiontests/backends/tests.py
@@ -8,6 +8,7 @@
8 8
 from django.core.management.color import no_style
9 9
 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
10 10
 from django.db.backends.signals import connection_created
  11
+from django.db.backends.postgresql import version as pg_version
11 12
 from django.test import TestCase
12 13
 
13 14
 from regressiontests.backends import models
@@ -154,46 +155,34 @@ def test_generic_relation(self):
154 155
         obj = models.Post.objects.create(name='New post', text='goodbye world')
155 156
         self.assertTrue(obj.pk > 10)
156 157
 
  158
+class PostgresVersionTest(TestCase):
  159
+    def assert_parses(self, version_string, version):
  160
+        self.assertEqual(pg_version._parse_version(version_string), version)
157 161
 
158  
-def connection_created_test(sender, **kwargs):
159  
-    print 'connection_created signal'
160  
-
161  
-__test__ = {'API_TESTS': """
162  
-# Check Postgres version parsing
163  
->>> from django.db.backends.postgresql import version as pg_version
164  
-
165  
->>> pg_version._parse_version("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)")
166  
-(8, 3, 1)
167  
-
168  
->>> pg_version._parse_version("PostgreSQL 8.3.6")
169  
-(8, 3, 6)
170  
-
171  
->>> pg_version._parse_version("PostgreSQL 8.3")
172  
-(8, 3, None)
173  
-
174  
->>> pg_version._parse_version("EnterpriseDB 8.3")
175  
-(8, 3, None)
176  
-
177  
->>> pg_version._parse_version("PostgreSQL 8.3 beta4")
178  
-(8, 3, None)
179  
-
180  
->>> pg_version._parse_version("PostgreSQL 8.4beta1")
181  
-(8, 4, None)
182  
-
183  
-"""}
  162
+    def test_parsing(self):
  163
+        self.assert_parses("PostgreSQL 8.3 beta4", (8, 3, None))
  164
+        self.assert_parses("PostgreSQL 8.3", (8, 3, None))
  165
+        self.assert_parses("EnterpriseDB 8.3", (8, 3, None))
  166
+        self.assert_parses("PostgreSQL 8.3.6", (8, 3, 6))
  167
+        self.assert_parses("PostgreSQL 8.4beta1", (8, 4, None))
  168
+        self.assert_parses("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)", (8, 3, 1))
184 169
 
185 170
 # Unfortunately with sqlite3 the in-memory test database cannot be
186 171
 # closed, and so it cannot be re-opened during testing, and so we
187 172
 # sadly disable this test for now.
188  
-if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
189  
-    __test__['API_TESTS'] += """
190  
->>> connection_created.connect(connection_created_test)
191  
->>> connection.close() # Ensure the connection is closed
192  
->>> cursor = connection.cursor()
193  
-connection_created signal
194  
->>> connection_created.disconnect(connection_created_test)
195  
->>> cursor = connection.cursor()
196  
-"""
197  
-
198  
-if __name__ == '__main__':
199  
-    unittest.main()
  173
+if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] != "django.db.backends.sqlite3":
  174
+    class ConnectionCreatedSignalTest(TestCase):
  175
+        def test_signal(self):
  176
+            data = {}
  177
+            def receiver(sender, connection, **kwargs):
  178
+                data["connection"] = connection
  179
+
  180
+            connection_created.connect(receiver)
  181
+            connection.close()
  182
+            cursor = connection.cursor()
  183
+            self.assertTrue(data["connection"] is connection)
  184
+
  185
+            connection_created.disconnect(receiver)
  186
+            data.clear()
  187
+            cursor = connection.cursor()
  188
+            self.assertTrue(data == {})

0 notes on commit 6909c22

Please sign in to comment.
Something went wrong with that request. Please try again.