From eb8d9190154a72d557674fcffb2a397bce5daed9 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Tue, 13 Jun 2017 17:17:45 +0200 Subject: [PATCH] [FIX] sql_db: port fix from psycopg/psycopg2#459 NUL characters must not be used in query parameters, as they will be ignored by libpq, being end-of-string characters. Preventing NULs avoids unexpected results from queries. It is only necessary with psycopg2 versions before 2.7, which includes the upstream fix. --- openerp/sql_db.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/openerp/sql_db.py b/openerp/sql_db.py index a3af4be677a..22bed4eabbc 100644 --- a/openerp/sql_db.py +++ b/openerp/sql_db.py @@ -63,6 +63,19 @@ psycopg2.extensions.register_type(psycopg2.extensions.new_type((700, 701, 1700,) import tools + +from tools import parse_version as pv +if pv(psycopg2.__version__) < pv('2.7'): + from psycopg2._psycopg import QuotedString + def adapt_string(adapted): + """Python implementation of psycopg/psycopg2#459 from v2.7""" + if '\x00' in adapted: + raise ValueError("A string literal cannot contain NUL (0x00) characters.") + return QuotedString(adapted) + + psycopg2.extensions.register_adapter(str, adapt_string) + psycopg2.extensions.register_adapter(unicode, adapt_string) + from tools.func import frame_codeinfo from datetime import datetime as mdt from datetime import timedelta