Opened 15 years ago

Closed 8 years ago

#13161 closed Cleanup/optimization (wontfix)

Avoid global registration of psycopg2 adapters/extensions

Reported by: spoksss Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: psycopg2, unicode
Cc: Carl Meyer, Florian Apolloner Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

I have some problem while I use Django connection based on psycopg2 in the same time while using some other database application.
Because Django changes default psycopg2 settings (psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)),
I get "psycopg2.InterfaceError: can't decode into unicode string from SQLASCII", when I try use my external database application which works with SQLASCII after import django settings.

I think it isn't good way when Django changes default, global settings of psycopg2, because other application can have problems.
I have read documentation of psycopg2, and it is possible to set that psycopg2.extensions.UNICODE just for custom connection or cursor, so I think it is good idea to use that feature in Django.
http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.register_type

I have tested my changes with psycopg 2.0.8 and Django-1.2-beta-1 and everything work correctly.

Attachments (4)

base.py (8.1 KB ) - added by spoksss 15 years ago.
Small usefull change.
patch-r12807.diff (1.6 KB ) - added by spoksss 15 years ago.
diff file
project_with_test_ticket_13161.tar.gz (3.7 KB ) - added by spoksss 14 years ago.
Small project with test to raise psycopg2 error.
base.py.diff13161 (1.0 KB ) - added by Piotr Maślanka 12 years ago.
git patch fixing the problem

Download all attachments as: .zip

Change History (22)

by spoksss, 15 years ago

Attachment: base.py added

Small usefull change.

comment:1 by Alex Gaynor, 15 years ago

Changes should be uploaded as diffs from the root of the source tree.

by spoksss, 15 years ago

Attachment: patch-r12807.diff added

diff file

comment:2 by Russell Keith-Magee, 15 years ago

Component: UncategorizedDatabase layer (models, ORM)
Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted

Strictly, this looks to be a duplicate of #5171, but I'll keep this open given that we have a patch that seems to address the implementation dead-end on that ticket.

This is going to need a test before it gets to trunk.

comment:3 by spoksss, 14 years ago

What could I do to test it?
This solution works correctly in my environment,
and it will be nice if I can help somehow and this patch will be add to trunk.

comment:4 by Russell Keith-Magee, 14 years ago

You write a test that does exactly the thing that causes the problem to occur -- that is, write a test that accesses psycopg directly as well as using Django's ORM, demonstrating that the way that Django's ORM sets the default value is problematic. The test should be integrated as part of Djanog's test suite -- the regressiontests.backend module would be an appropriate location; the test should fail using current trunk, and pass once your patch is applied.

comment:5 by Carl Meyer, 14 years ago

Cc: Carl Meyer added

by spoksss, 14 years ago

Small project with test to raise psycopg2 error.

comment:6 by spoksss, 14 years ago

Hi,

I created some project just to show what is a problem, I attached it in this comment.

Set settings, syncdb and then try to run below test:

python manage.py test myapp.MyModelTestCase

comment:7 by Luke Plant, 14 years ago

Type: Bug

comment:8 by Luke Plant, 14 years ago

Severity: Normal

comment:9 by spoksss, 14 years ago

Easy pickings: unset
UI/UX: unset

Hi,
what is status of this bug?
It is possible that the patch will be applied to trunk?

regards,

comment:10 by anonymous, 12 years ago

Version: 1.2-beta1.4

Now (version 1.4 of Django) there is only one supported backend (psycopg2) for postgresql, so it is possible, that this patch will be in trunk.
What else can be done to commit it to trunk?

in reply to:  10 comment:11 by Aymeric Augustin, 12 years ago

Replying to anonymous:

What else can be done to commit it to trunk?

The patch is still marked as needing tests.

comment:12 by Piotr Maślanka, 12 years ago

psycopg version "2.4.5 (dt dec pq3 ext)" fails to respect the per-connection settings, but respects the per-cursor setting just fine. Patch would be best served if setting command was moved to cursor creation (DatabaseWrapper._cursor()).

by Piotr Maślanka, 12 years ago

Attachment: base.py.diff13161 added

git patch fixing the problem

comment:13 by Piotr Maślanka, 12 years ago

Needs tests: unset

comment:14 by Ramiro Morales, 12 years ago

Needs tests: set

comment:15 by Florian Apolloner, 12 years ago

Cc: Florian Apolloner added

What about register_adapter in the most recent patch, isn't that "global" too? If yes we probable should fix all issues and not just the unicode issue.

comment:16 by Anssi Kääriäinen, 12 years ago

I guess the SafeString adapters aren't much of an issue as they don't apply to anything else than the SafeString types. But yeah, global they are.

I'd like to confirm that creating a cursor isn't slowed down too much by the change.

comment:17 by Tim Graham, 9 years ago

Summary: Ticked 2514 and 5171, solution for using Django with other psycopg2 applications.Avoid global registration of psycopg2 adapters/extensions
Type: BugCleanup/optimization
Version: 1.4master

comment:18 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed

The unicode type registration will be dropped with Python 2. There are some other extensions/types registered but I'm not sure they're causing any problems. Feel free to reopen if someone sees a reason to change things here.

Note: See TracTickets for help on using tickets.
Back to Top