Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.0] GettextAlias._get_cr may choose a closed cr #76559

Open
ap-wtioit opened this issue Sep 15, 2021 · 2 comments
Open

[13.0] GettextAlias._get_cr may choose a closed cr #76559

ap-wtioit opened this issue Sep 15, 2021 · 2 comments

Comments

@ap-wtioit
Copy link
Contributor

ap-wtioit commented Sep 15, 2021

Impacted versions:
13.0

Steps to reproduce:

if some_errors:
    with registry(self._cr.dbname).cursor() as cr:
        persist_logs(some_errors, cr)
    raise UserError(_(some_errors[0]))

Current behavior:
GettextAlias._get_cr picks the first cr found in the frame/stack

Expected behavior:
GettextAlias._get_cr picks the first unclosed cr found in the frame/stack

Video/Screenshot link (optional):

Support ticket number submitted via odoo.com/help (optional):

FYI @wt-io-it

@wtaferner
Copy link
Contributor

@mart-e
What do you think?

@mart-e
Copy link
Contributor

mart-e commented Sep 16, 2021

Indeed, we try to retrieve keywords like cr, context, self,.. when calling the _() method to be able to retrieve the ir.translation records.
If your cursor is closed GettextAlias will fail when using it. To fix your issue, you must open a new cursor.

For instance, when translating an error message after an SQL error, we explicitly open a new cursor.

# We open a *new* cursor here, one reason is that failed SQL
# queries (as in IntegrityError) will invalidate the current one.
with closing(odoo.sql_db.db_connect(dbname).cursor()) as cr:
if ttype == 'sql_constraint':
res = translate_sql_constraint(cr, key=key, lang=lang)
else:
res = translate(cr, name=False, source_type=ttype,
lang=lang, source=src)
return res or src

raise UserError(_(some_errors[0]))

This won't work by the way, you should only pass static strings to _() if you want the export to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants