UnboundLocalError: local variable 'diff' referenced before assignment
My company currency "INR", i ahve created a price list with currency "USD" . when i try to create a sales order with that price list i am getting "UnboundLocalError: local variable 'diff' referenced before assignment" error
Question information
- Language:
- English Edit question
- Status:
- Answered
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Revision history for this message
![]() |
#1 |
I could help you, as this looks like a python problem (is it?), but I'd need to see the source code to help you, and if you're a company, I'm not sure you'd want me to see that. When I have issues like this, what I normally do is follow the code's patgh of execution, and see (in this case) if there any ways depending on options chosen, that this variable could be used before it's assigned to. If you can, assign to it as early on in the code as possible.
Hope this helps,
Hamish
Revision history for this message
![]() |
#2 |
Thank you Hamish,
I am using openerp version 7.0
I did not change any code in base module.
My company currency "INR", i ahve created a price list with currency "USD" . when i try to create a sales order with that price list i am getting "UnboundLocalError: local variable 'diff' referenced before assignment" error.
This error showing in file base/res/
Revision history for this message
![]() |
#3 |
So is this a python problem? If so, python 2 or 3? What is openerp? I can't really fix this unless I see your code, could you deliberately cause the error and show me the last 30 lines of output please?
Thanks,
Hamish
Revision history for this message
![]() |
#4 |
Thank you Hamish,
This is the total error i am getting.
OpenERP
OpenERP Server Error
Client Traceback (most recent call last):
File "/home/
response[
File "/home/
return req.session.
File "/home/
r = self.proxy(
File "/home/
result = self.session.
File "/home/
raise xmlrpclib.
Server Traceback (most recent call last):
File "/home/
return openerp.
File "/home/
result = ExportService.
File "/home/
res = fn(db, uid, *params)
File "/home/
return f(self, dbname, *args, **kwargs)
File "/home/
res = self.exec_
File "/home/
return object.
File "/home/
result[res_id] = wf_service.
File "/home/
res2 = instance.
File "/home/
workitem.
File "/home/
ok = _split_test(cr, workitem, activity[
File "/home/
_join_test(cr, t[0], t[1], ident, stack)
File "/home/
create(
File "/home/
process(cr, res, ident, stack=stack)
File "/home/
result = _execute(cr, workitem, activity, ident, stack)
File "/home/
returned_action = wkf_expr.
File "/home/
return _eval_expr(cr, ident, workitem, activity['action'])
File "/home/
ret = eval(line, env, nocopy=True)
File "/home/
return eval(test_
File "", line 1, in <module>
File "/home/
return attr(self._cr, self._uid, [self._id], *args, **kwargs)
File "/home/
total, total_currency, iml = self.compute_
File "/home/
context=
File "/home/
rate = self._get_
File "/home/
rate = super(res_
File "/home/
return diff
UnboundLocalError: local variable 'diff' referenced before assignment
Revision history for this message
![]() |
#6 |
Good, I use python 2 as well :) Okay, now we're really getting somewhere. Could you now show me the contents of /home/kptuser/
Thanks,
Hamish
Revision history for this message
![]() |
#7 |
Thank you very much helping us Hamish,
This is code of res_currency.py
# -*- coding: utf-8 -*-
#######
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://
#
#######
import re
import time
from openerp import tools
from openerp.osv import fields, osv
from openerp.tools import float_round, float_is_zero, float_compare
from openerp.
CURRENCY_
class res_currency(
def _current_rate(self, cr, uid, ids, name, arg, context=None):
return self._current_
def _current_
return self._current_
def _current_
if context is None:
context = {}
res = {}
if 'date' in context:
date = context['date']
else:
date = time.strftime(
date = date or time.strftime(
# Convert False values to None ...
# ... and use 'is NULL' instead of '= some-id'.
operator = '=' if currency_rate_type else 'is'
for id in ids:
if cr.rowcount:
id, rate = cr.fetchall()[0]
elif not raise_on_no_rate:
else:
return res
_name = "res.currency"
_description = "Currency"
_columns = {
# Note: 'code' column was removed as of v6.0, the 'name' should now hold the ISO code.
'name': fields.
'symbol': fields.
'rate': fields.
# Do not use for computation ! Same as rate field with silent failing
'rate_ids': fields.
'accuracy': fields.
'rounding': fields.
'active': fields.
'date': fields.
'base': fields.
'position': fields.
}
_defaults = {
'active': 1,
'position' : 'after',
'rounding': 0.01,
'accuracy': 4,
}
_sql_
# this constraint does not cover all cases due to SQL NULL handling for company_id,
# so it is complemented with a unique index (see below). The constraint and index
# share the same prefix so that IntegrityError triggered by the index will be caught
# and reported to the user with the constraint's error message.
]
_order = "name"
def init(self, cr):
# CONSTRAINT/UNIQUE INDEX on (name,company_id)
# /!\ The unique constraint 'unique_
# only support field names in constraint definitions, and we need a function here:
# we need to special-case company_id to treat all NULL company_id as equal, otherwise
# we would allow duplicate "global" currencies (all having company_id == NULL)
if not cr.fetchone():
def read(self, cr, user, ids, fields=None, context=None, load='_
res = super(res_currency, self).read(cr, user, ids, fields, context, load)
values = res
if not isinstance(values, list):
values = [values]
for r in values:
if r.__contains_
if rates:
return res
def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
if not args:
args = []
results = super(res_
if not results:
if name_match:
return results
def name_get(self, cr, uid, ids, context=None):
if not ids:
return []
if isinstance(ids, (int, long)):
ids = [ids]
reads = self.read(cr, uid, ids, ['name','symbol'], context=context, load='_
return [(x['id'], tools.ustr(
def round(self, cr, uid, currency, amount):
"""Return ``amount`` rounded according to ``currency``'s
rounding rules.
:param browse_record currency: currency for which we are rounding
:param float amount: the amount to round
:return: rounded float
"""
return float_round(amount, precision_
def compare_
"""Compare ``amount1`` and ``amount2`` after rounding them according to the
given currency's precision..
An amount is considered lower/greater than another amount if their rounded
value is different. This is not the same as having a non-zero difference!
For example 1.432 and 1.431 are equal at 2 digits precision,
so this method would return 0.
However 0.006 and 0.002 are considered different (returns 1) because
they respectively round to 0.01 and 0.0, even though
:param browse_record currency: currency for which we are rounding
:param float amount1: first amount to compare
:param float amount2: second amount to compare
:return: (resp.) -1, 0 or 1, if ``amount1`` is (resp.) lower than,
"""
return float_compare(
def is_zero(self, cr, uid, currency, amount):
"""Returns true if ``amount`` is small enough to be treated as
zero according to ``currency``'s rounding rules.
Warning: ``is_zero(
:param browse_record currency: currency for which we are rounding
:param float amount: amount to compare with currency's zero
"""
return float_is_
def _get_conversion
if context is None:
context = {}
ctx = context.copy()
to_currency = self.browse(cr, uid, to_currency.id, context=ctx)
if from_currency.rate == 0 or to_currency.rate == 0:
date = context.get('date', time.strftime(
if from_currency.rate == 0:
else:
raise osv.except_
return to_currency.
def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount,
if not context:
context = {}
if not from_currency_id:
if not to_currency_id:
xc = self.browse(cr, uid, [from_currency_
to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1]
if (to_currency_id == from_currency_id) and (currency_
if round:
else:
else:
rate = self._get_
if round:
else:
res_currency()
class res_currency_
_name = "res.currency.
_description = "Currency Rate Type"
_columns = {
'name': fields.char('Name', size=64, required=True, translate=True),
}
res_currency_
class res_currency_
_name = "res.currency.rate"
_description = "Currency Rate"
_columns = {
'name': fields.date('Date', required=True, select=True),
'rate': fields.
}
_defaults = {
'name': lambda *a: time.strftime(
}
_order = "name desc"
res_currency_rate()
# vim:expandtab:
Thank you,
sincerely,
Samba.
Revision history for this message
![]() |
#8 |
Okay, thanks, and you're welcome :) By the looks of it, this calculates conversion rates, but the way they've written is is hard to understand. Just so I can understand what it should do, could you add:
print to_currency.
just before:
return to_currency.
and show me the ouput from a successful operation?
It seems that the calculation to_currency.
Thanks,
Hamish
Revision history for this message
![]() |
#9 |
Could you also show me the output of lsb_release -a?
STATUS=NEEDSINFO
Thanks,
Hamish
On 12 June 2014 00:41:38 GMT+08:00, Hamish McIntyre-Bhatty <email address hidden> wrote:
>Question #249967 on Ubuntu changed:
>https:/
>
>Hamish McIntyre-Bhatty requested more information:
>Okay, thanks, and you're welcome :) By the looks of it, this calculates
>conversion rates, but the way they've written is is hard to understand.
>Just so I can understand what it should do, could you add:
>
>print to_currency.
>
>just before:
>
>return to_currency.
>
>and show me the ouput from a successful operation?
>
>It seems that the calculation to_currency.
>sometimes returns "diff", but it'll help to know what its supposed to
>result in. I'm having some difficulty understanding the code so far.
>Sometimes I think mine's messy, but it's nothing like this! Anyhow, it
>doesn't look like it's too complicated. Also, could you show me the
>changes you made in any files to add support for USD?
>
>Thanks,
>Hamish
>
>--
>You received this question notification because you are a direct
>subscriber of the question.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Can you help with this problem?
Provide an answer of your own, or ask sambasivarao for more information if necessary.