[FIX] fields: `digits()` computation

Ensure we always have an valid cursor when determining `digits()`.

fixes #6605, fixes #6650
This commit is contained in:
Christophe Simonis 2015-05-11 18:32:28 +02:00
parent c0deb1f726
commit eeedd2d9f5
2 changed files with 20 additions and 7 deletions

View File

@ -1054,7 +1054,8 @@ class Float(Field):
@property
def digits(self):
if callable(self._digits):
return self._digits(fields.get_cursor())
with fields._get_cursor() as cr:
return self._digits(cr)
else:
return self._digits

View File

@ -43,6 +43,7 @@ import pytz
import re
import xmlrpclib
from operator import itemgetter
from contextlib import contextmanager
from psycopg2 import Binary
import openerp
@ -52,13 +53,22 @@ from openerp.tools import float_repr, float_round, frozendict, html_sanitize
import simplejson
from openerp import SUPERUSER_ID, registry
def get_cursor():
# retrieve a valid cursor from any environment
@contextmanager
def _get_cursor():
# yield a valid cursor from any environment or create a new one if none found
from openerp.api import Environment
from openerp.http import request
try:
request.env # force request's env to be computed
except RuntimeError:
pass # ignore if not in a request
for env in Environment.envs:
if not env.cr.closed:
return env.cr
raise RuntimeError("No valid cursor found")
yield env.cr
break
else:
with registry().cursor() as cr:
yield cr
EMPTY_DICT = frozendict()
@ -396,7 +406,8 @@ class float(_column):
@property
def digits(self):
if self._digits_compute:
return self._digits_compute(get_cursor())
with _get_cursor() as cr:
return self._digits_compute(cr)
else:
return self._digits
@ -1316,7 +1327,8 @@ class function(_column):
@property
def digits(self):
if self._digits_compute:
return self._digits_compute(get_cursor())
with _get_cursor() as cr:
return self._digits_compute(cr)
else:
return self._digits