_ translate function blows up if used in method with a parameter named 'args': AttributeError: 'list' object has no attribute 'get' in lang = args[-1].get('lang',False)

Bug #487251 reported by Raphaël Valyi - http://www.akretion.com
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Odoo Server (MOVED TO GITHUB)
Fix Released
Medium
OpenERP's Framework R&D

Bug Description

Hello,

since some 1 week and 3 weeks at the very max, when using openerp-server 5.0 head revisions, I have this error when I try to connect me to the server:

No LSB modules are available.
[2009-11-23 20:19:35,840] ERROR:web-services:[01]:
[2009-11-23 20:19:35,840] ERROR:web-services:[02]: Environment Information :
[2009-11-23 20:19:35,840] ERROR:web-services:[03]: System : Linux-2.6.28-11-server-x86_64-with-Ubuntu-9.04-jaunty
[2009-11-23 20:19:35,840] ERROR:web-services:[04]: OS Name : posix
[2009-11-23 20:19:35,840] ERROR:web-services:[05]: Distributor ID: Ubuntu
[2009-11-23 20:19:35,841] ERROR:web-services:[06]: Description: Ubuntu 9.04
[2009-11-23 20:19:35,841] ERROR:web-services:[07]: Release: 9.04
[2009-11-23 20:19:35,841] ERROR:web-services:[08]: Codename: jaunty
[2009-11-23 20:19:35,841] ERROR:web-services:[09]: Operating System Release : 2.6.28-11-server
[2009-11-23 20:19:35,841] ERROR:web-services:[10]: Operating System Version : #42-Ubuntu SMP Fri Apr 17 02:45:36 UTC 2009
[2009-11-23 20:19:35,841] ERROR:web-services:[11]: Operating System Architecture : 64bit
[2009-11-23 20:19:35,841] ERROR:web-services:[12]: Operating System Locale : en_US.UTF8
[2009-11-23 20:19:35,841] ERROR:web-services:[13]: Python Version : 2.6.2
[2009-11-23 20:19:35,841] ERROR:web-services:[14]: OpenERP-Server Version : 5.0.6
[2009-11-23 20:19:35,842] ERROR:web-services:[15]: Last revision No. & ID : 1878 <email address hidden>
[2009-11-23 20:19:35,842] ERROR:web-services:[16]:
[2009-11-23 20:19:35,842] ERROR:web-services:[17]: Traceback (most recent call last):
[2009-11-23 20:19:35,842] ERROR:web-services:[18]: File "/opt/openerp5.0_test/server/bin/osv/osv.py", line 61, in wrapper
[2009-11-23 20:19:35,842] ERROR:web-services:[19]: return f(self, dbname, *args, **kwargs)
[2009-11-23 20:19:35,842] ERROR:web-services:[20]: File "/opt/openerp5.0_test/server/bin/osv/osv.py", line 120, in execute
[2009-11-23 20:19:35,842] ERROR:web-services:[21]: res = pool.execute_cr(cr, uid, obj, method, *args, **kw)
[2009-11-23 20:19:35,842] ERROR:web-services:[22]: File "/opt/openerp5.0_test/server/bin/osv/osv.py", line 112, in execute_cr
[2009-11-23 20:19:35,843] ERROR:web-services:[23]: return getattr(object, method)(cr, uid, *args, **kw)
[2009-11-23 20:19:35,843] ERROR:web-services:[24]: File "/opt/openerp5.0_test/addons/magentoerpconnect/magerp_core.py", line 56, in core_sync
[2009-11-23 20:19:35,843] ERROR:web-services:[25]: self.pool.get('external.shop.group').mage_import_base(cr, uid,core_imp_conn, inst.id, defaults={'referential_id':inst.id})
[2009-11-23 20:19:35,843] ERROR:web-services:[26]: File "/opt/openerp5.0_test/addons/magentoerpconnect/magerp_osv.py", line 246, in mage_import_base
[2009-11-23 20:19:35,843] ERROR:web-services:[27]: data = conn.call(list_method, context['ids_or_filter'])
[2009-11-23 20:19:35,843] ERROR:web-services:[28]: File "/opt/openerp5.0_test/addons/magentoerpconnect/magerp_osv.py", line 74, in call
[2009-11-23 20:19:35,843] ERROR:web-services:[29]: self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 1 second before next attempt"))
[2009-11-23 20:19:35,843] ERROR:web-services:[30]: File "/opt/openerp5.0_test/server/bin/tools/translate.py", line 139, in __call__
[2009-11-23 20:19:35,843] ERROR:web-services:[31]: lang = args[-1].get('lang',False)
[2009-11-23 20:19:35,844] ERROR:web-services:[32]: AttributeError: 'list' object has no attribute 'get'

I use to make it start and see no bug with the following patch:

=== modified file 'bin/tools/translate.py'
--- bin/tools/translate.py 2009-11-17 12:45:11 +0000
+++ bin/tools/translate.py 2009-11-18 14:21:07 +0000
@@ -135,7 +135,7 @@

         if not (cr and lang):
             args = frame.f_locals.get('args',False)
- if args:
+ if args and type(args[-1]) == dict:
                 lang = args[-1].get('lang',False)
                 if frame.f_globals.get('pooler',False):
                     cr = pooler.get_db(frame.f_globals['pooler'].pool_dic.keys()[0]).cursor()

HOWEVER, I MIGHT NOT HAVE TESTED ALL THE FEATURES AND CONSIDER THAT A UGLY WORKAROUND. SO PLEASE DON'T JUST COMMIT THAT PATCH WITHOUT FINDING THE REAL BUG CAUSE/REAL SOLUTION.

Hope this helps

Revision history for this message
Anup(SerpentCS) (anup-serpent) wrote :

Hello Raphael,

   Can you please tell us when do you get this error ?. And I would like to request you to debug it and print args[-1] what it actually comes. Generally, it is a dictionary which is the argument to the method so please notify us about the thing.

Thank you.

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Hello Raphel,

Can you focus on this issue please?

As said by Anup(OpenERP), arguments of methods come as dictionaries.

Better if you can debug and check by prints.

Thank you.

Changed in openobject-server:
status: New → Incomplete
Revision history for this message
Raphaël Valyi - http://www.akretion.com (rvalyi) wrote : Re: [Bug 487251] Re: 5.0 regression: AttributeError: 'list' object has no attribute 'get' in lang = args[-1].get('lang', False)
Download full text (5.3 KiB)

Hello,

no rush for that one, I reported it to see if anyone confirms. But it seems
we are the only one having the issue, or more exactly it seems it could be
related to our new Magento - OpenERP connector we are using, we will
investigate a bit more and let you know. Thanks.

On Tue, Dec 1, 2009 at 10:11 AM, Jay (Open ERP) <email address hidden> wrote:

> Hello Raphel,
>
> Can you focus on this issue please?
>
> As said by Anup(OpenERP), arguments of methods come as dictionaries.
>
> Better if you can debug and check by prints.
>
> Thank you.
>
> ** Changed in: openobject-server
> Status: New => Incomplete
>
> --
> 5.0 regression: AttributeError: 'list' object has no attribute 'get' in
> lang = args[-1].get('lang',False)
> https://bugs.launchpad.net/bugs/487251
> You received this bug notification because you are a direct subscriber
> of the bug.
>
> Status in OpenObject Server: Incomplete
>
> Bug description:
> Hello,
>
> since some 1 week and 3 weeks at the very max, when using openerp-server
> 5.0 head revisions, I have this error when I try to connect me to the
> server:
>
> No LSB modules are available.
> [2009-11-23 20:19:35,840] ERROR:web-services:[01]:
> [2009-11-23 20:19:35,840] ERROR:web-services:[02]: Environment Information
> :
> [2009-11-23 20:19:35,840] ERROR:web-services:[03]: System :
> Linux-2.6.28-11-server-x86_64-with-Ubuntu-9.04-jaunty
> [2009-11-23 20:19:35,840] ERROR:web-services:[04]: OS Name : posix
> [2009-11-23 20:19:35,840] ERROR:web-services:[05]: Distributor ID:
> Ubuntu
> [2009-11-23 20:19:35,841] ERROR:web-services:[06]: Description: Ubuntu 9.04
> [2009-11-23 20:19:35,841] ERROR:web-services:[07]: Release: 9.04
> [2009-11-23 20:19:35,841] ERROR:web-services:[08]: Codename: jaunty
> [2009-11-23 20:19:35,841] ERROR:web-services:[09]: Operating System Release
> : 2.6.28-11-server
> [2009-11-23 20:19:35,841] ERROR:web-services:[10]: Operating System Version
> : #42-Ubuntu SMP Fri Apr 17 02:45:36 UTC 2009
> [2009-11-23 20:19:35,841] ERROR:web-services:[11]: Operating System
> Architecture : 64bit
> [2009-11-23 20:19:35,841] ERROR:web-services:[12]: Operating System Locale
> : en_US.UTF8
> [2009-11-23 20:19:35,841] ERROR:web-services:[13]: Python Version : 2.6.2
> [2009-11-23 20:19:35,841] ERROR:web-services:[14]: OpenERP-Server Version :
> 5.0.6
> [2009-11-23 20:19:35,842] ERROR:web-services:[15]: Last revision No. & ID :
> 1878 <email address hidden>
> [2009-11-23 20:19:35,842] ERROR:web-services:[16]:
> [2009-11-23 20:19:35,842] ERROR:web-services:[17]: Traceback (most recent
> call last):
> [2009-11-23 20:19:35,842] ERROR:web-services:[18]: File
> "/opt/openerp5.0_test/server/bin/osv/osv.py", line 61, in wrapper
> [2009-11-23 20:19:35,842] ERROR:web-services:[19]: return f(self,
> dbname, *args, **kwargs)
> [2009-11-23 20:19:35,842] ERROR:web-services:[20]: File
> "/opt/openerp5.0_test/server/bin/osv/osv.py", line 120, in execute
> [2009-11-23 20:19:35,842] ERROR:web-services:[21]: res =
> pool.execute_cr(cr, uid, obj, method, *args, **kw)
> [2009-11-23 20:19:35,842] ERROR:web-services:[22]: File
> "/opt/openerp5.0_test/server/bin/osv/osv.py", line 112, in execut...

Read more...

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote : Re: 5.0 regression: AttributeError: 'list' object has no attribute 'get' in lang = args[-1].get('lang',False)

Thank you very much for your good response.

Reopen the bug if you meet this again.

It could be a method-argument issue.

Thank you for your interest.

Changed in openobject-server:
status: Incomplete → Invalid
Revision history for this message
Xavier Fernandez http://www.smile.fr (xav-fernandez) wrote :

Hello,

this problem occurs every time you use the _() function from translate (from tools.translate import _) in a function containing an argument or local variable named 'args'.

args = frame.f_locals.get('args',False) checks if this variable (or agument) exists and if it does, it expects it to be a list (or dictionnary with "-1" key) which last item is a dictionnary (args[-1].get('lang',False))

In the OpenERP-magento connector: def call(self, method, *args):
this is not infortunatly not the case.

I had the same issue for a custom development.
Most of my webservices expected an args argument (def create_booking(self, cr, uid, args, context={}):)

A easy solution is to rename the 'args' argument into something else (like arguments or args2 or whatever_feels_right). Same goes for local variables.

But I think the _() should also be improved in order to deal with 'args' local variables that are not list which last item is a dictionnary...

Changed in openobject-server:
status: Invalid → Confirmed
importance: Undecided → Wishlist
Changed in openobject-server:
importance: Wishlist → Medium
summary: - 5.0 regression: AttributeError: 'list' object has no attribute 'get' in
- lang = args[-1].get('lang',False)
+ _ translate function blows up if used in method with a parameter named
+ 'arg': AttributeError: 'list' object has no attribute 'get' in lang =
+ args[-1].get('lang',False)
summary: _ translate function blows up if used in method with a parameter named
- 'arg': AttributeError: 'list' object has no attribute 'get' in lang =
+ 'args': AttributeError: 'list' object has no attribute 'get' in lang =
args[-1].get('lang',False)
Changed in openobject-server:
assignee: nobody → Anup (Open ERP) (ach-openerp)
status: Confirmed → In Progress
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Hello Raphael and Xavier,

We would like to request you to check with the attached patch.

Kindly let us know about the outcome of this patch.

Thank you.

Revision history for this message
Anup(SerpentCS) (anup-serpent) wrote :

Hello Raphael,

I have attached a better solution for it. would you please check it and notify us.

Thanks

Revision history for this message
Anup(SerpentCS) (anup-serpent) wrote :

Hello Raphael and Xavier,

  IMHO we need a concrete solution for _() method and it should deal with 'args' in all cases whether it's a list,tuple or anything else. The _() should not crash the system. I think i have got such solution. Here I am attaching a refined patch which solves many issues with translations. Would you please check the patch and notify?

Thanks.

Revision history for this message
xrg (xrg) wrote : Re: [Bug 487251] Re: _ translate function blows up if used in method with a parameter named 'args': AttributeError: 'list' object has no attribute 'get' in lang = args[-1].get('lang', False)

On Friday 27 August 2010, you wrote:
> Hello Raphael and Xavier,
>
> IMHO we need a concrete solution for _() method and it should deal
> with 'args' in all cases whether it's a list,tuple or anything else. The
> _() should not crash the system. I think i have got such solution. Here
> I am attaching a refined patch which solves many issues with
> translations. Would you please check the patch and notify?
>
> Thanks.
>
> ** Patch added: "refined_translation_patch_for_all.diff"
>
> https://bugs.launchpad.net/openobject-server/+bug/487251/+attachment/15229
> 26/+files/refined_translation_patch_for_all.diff

This whole inspection thing of _( ) is a bit weird, but cannot be done better
in that (Python) language[1].
Your patch seems ok, but I would add a little more logic to it: First look for
"kwargs", a dict, and then the "args", because it is safer to look for the
dictionary first.

Reading the manual: http://docs.python.org/library/inspect.html , there is
always some room for improvement ;)

Revision history for this message
Anup(SerpentCS) (anup-serpent) wrote :

Hello Panos,

    I have updated the code with kwargs. Would you please check the attached patch and notify?

Thanks.

Revision history for this message
Anup(SerpentCS) (anup-serpent) wrote :

Hello Panos,

    We can also have a solution for this omitting the args, as taking args[-1] is not feasible it can crash the system or it can also give false result too for e.g. if the last argument is a dictionary but not context . So I have a better solution for translate._() method. Please check the attached patch and give your views.

Thanks.

Revision history for this message
xrg (xrg) wrote :

On Thursday 30 September 2010, you wrote:
> Hello Panos,
>
> We can also have a solution for this omitting the args, as taking
> args[-1] is not feasible it can crash the system or it can also give
> false result too for e.g. if the last argument is a dictionary but not
> context . So I have a better solution for translate._() method. Please
> check the attached patch and give your views.
>
> Thanks.
>
> ** Patch added: "translate_patch.diff"
>
> https://bugs.launchpad.net/openobject-server/+bug/487251/+attachment/16564
> 80/+files/translate_patch.diff

Please remember that the _() must be a fast function, may be called much more
often than we really want it (because of bad code, in fact). So it is
important to keep the code as simple (and "cheap") as possible. Every stack
frame we open, every loop or variable instantiation will add some "weight" to
the function, which we should ideally avoid.

Changed in openobject-server:
assignee: Anup (OpenERP) (ach-openerp) → OpenERP's Framework R&D (openerp-dev-framework)
Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

This was in fact fixed a few weeks ago with the various improvements to the gettext function in trunk.

Changed in openobject-server:
status: In Progress → Fix Released
milestone: none → 6.0-rc2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.