← Back to team overview

c2c-oerpscenario team mailing list archive

Re: [Bug 802352] Re: [6.0] Inheriting _constraints "list index out of range" if original len(_constraint ) is bigger than the new one

 

On Tuesday 28 June 2011, you wrote:
> I propose one thing, this thing took from me at least 4 hours i think i can
> not be the only one that can have this "finger error", i propose for trunk
> merge the diff attached it verifiy that the typo is correct when you
> inherit a _constraint.
> 
> I don't know if the place for this verification is right but i propose
> the idea....

> https://bugs.launchpad.net/openobject-server/+bug/802352/+attachment/21831
> 00/+files/diff_error_controled.diff
> 

Well, in principle, you have a point about this diff.

The problem is that Python is a weakly typed language, and can propagate wrong 
types in parts of the code you'd never expect them. Not only they could cause 
exceptions, but any kind of Pythonic (in the Monty Python sense) behavior.

The solution is to patch Python to emulate what a strong language would do. 
Your patch does that, but I'd prefer to keep an even simpler (and compact) 
syntax:

   for c in some_list:
      assert isinstance(c, tuple), "Why do we get %r ?" % c

Which is an one-liner, blocks exactly what we want.. We could have assertions 
in many places, like the start of functions that need to ensure eg. that 'ids' 
is a list.

On the other hand, even this assertion will introduce some overhead in our 
code. Sometimes it's critical to save even that one line.


-- 
Say NO to spam and viruses. Stop using Microsoft Windows!

-- 
You received this bug notification because you are a member of C2C
OERPScenario, which is subscribed to the OpenERP Project Group.
https://bugs.launchpad.net/bugs/802352

Title:
  [6.0] Inheriting _constraints "list index out of range" if original
  len(_constraint ) is bigger than the new one

Status in OpenERP Server:
  Opinion

Bug description:
  branch: 6.0
  addons : 4677
  server: 3452
  My module example:
  class stock_move_constraint(osv.osv):   
      _inherit = 'stock.move'
      _columns = {}
      def _check_import_info(self, cr, uid, ids, context=None):
          print "I checked"
          return True
      _constraints = [
          (_check_import_info,'You must assign a track lot with import information for this product',['tracking_id'])],
  stock_move_constraint()

  When I try to update my module ($python openerp-server.py -u mymodule
  -d mydb) this traceback is received.

  Traceback (most recent call last):
    File "./openerp-server.py", line 121, in <module>
      db,pool = pooler.get_db_and_pool(dbname, update_module=tools.config['init'] or tools.config['update'], pooljobs=False)
    File "/home/nhomar/CD_curso/linux_and_sources/server/bin/pooler.py", line 39, in get_db_and_pool
      addons.load_modules(db, force_demo, status, update_module)
    File "/home/nhomar/CD_curso/linux_and_sources/server/bin/addons/__init__.py", line 883, in load_modules
      processed_modules.extend(load_module_graph(cr, graph, status, report=report, skip_modules=processed_modules))
    File "/home/nhomar/CD_curso/linux_and_sources/server/bin/addons/__init__.py", line 717, in load_module_graph
      modules = pool.instanciate(package.name, cr)
    File "/home/nhomar/CD_curso/linux_and_sources/server/bin/osv/osv.py", line 258, in instanciate
      res.append(klass.createInstance(self, module, cr))
    File "/home/nhomar/CD_curso/linux_and_sources/server/bin/osv/osv.py", line 354, in createInstance
      if new[c2][2]==c[2] and (new[c2][0] == c[0] \
  IndexError: list index out of range

  I printed variables of this method (check patch it is corrected) and
  some kind of bad logic is used, the server is trying to check as a
  list an integer....

  Original problem
  ~~~~LINE 348 on osv.py

                          if s=='_constraints':
                              for c in cls.__dict__.get(s, []):
                                  exist = False
                                  for c2 in range(len(new)):
   ####THIS IS MY PRINT it is an integer,,,,, 
  #on the range(len(new)) always will return an index in c2 of the original class never, and if the original 
  #_constraint list in class is bigger than the new one it will broke.
                                       print c2 
                                       #For _constraints, we should check field and methods as well
                                       if new[c2][2]==c[2] and (new[c2][0] == c[0] \
                                              or getattr(new[c2][0],'__name__', True) == \
                                                  getattr(c[0],'__name__', False)):
                                          # If new class defines a constraint with
                                          # same function name, we let it override
                                          # the old one.
                                          new[c2] = c
                                          exist = True
                                          break
                                  if not exist:
                                      new.append(c)

To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-server/+bug/802352/+subscriptions


References