[ZCM] [ZC] 1593/ 3 Accept "OFS/CopySupport _get_id() is buggy"

Collector: Zope Bugs, Features, and Patches ... zope-coders-admin at zope.org
Fri Nov 26 09:22:57 EST 2004


Issue #1593 Update (Accept) "OFS/CopySupport _get_id() is buggy"
 Status Accepted, Zope/bug+solution medium
To followup, visit:
  http://collector.zope.org/Zope/1593

==============================================================
= Accept - Entry #3 by ajung on Nov 26, 2004 9:22 am

 Supporters added: ajung

Applied patch for Zope 2.8a2, Zope 2.7.4
________________________________________
= Accept - Entry #2 by efge on Nov 26, 2004 6:57 am

 Status: Pending => Accepted

 Supporters added: efge


________________________________________
= Request - Entry #1 by Anonymous User on Nov 24, 2004 4:50 am

While I was reading the _get_id function, I felt it was really strange :

####
    def _get_id(self, id):
        # Allow containers to override the generation of
        # object copy id by attempting to call its _get_id
        # method, if it exists.
        n=0
        if (len(id) > 8) and (id[8:]=='copy_of_'):
            n=1
        orig_id=id
        while 1:
            if self._getOb(id, None) is None:
                return id
            id='copy%s_of_%s' % (n and n+1 or '', orig_id)
            n=n+1
####

currently, when copying and pasting an object, 'copy[0-9]*_of_' will be 
prepended to the id (if the id is already in use in the current 
folder). Thus, copying 'x' will create 'copy_of_x', 'copy2_of_x', etc. 
and copying 'copy_of_x' will create 'copy_of_copy_of_x', 
'copy2_of_copy_of_x', etc.

What I don't get, is what the two lines between 'n=0' and 'orig_id=id' 
are supposed to do. What they do is this : if an object id is 16 chars 
long, and it ends with 'copy_of_' (like 'XXXXXXXXcopy_of_'), its first 
copy will be called 'copy2_of_XXXXXXXXcopy_of_' instead of 
'copy_of_XXXXXXXXcopy_of_'...

What I think the author intended to do, is that, when you copy an object 
called 'copy_of_x', the copy will be named 'copy2_of_x' instead of 
'copy_of_copy_of_x'.

Thus, here's a function that does the job :

####
  import re
  copy_re=re.compile('^copy[0-9]*_of_')

  def _get_id(self, id):
      # Allow containers to override the generation of
      # object copy id by attempting to call its _get_id
      # method, if it exists.
      copy_match=self.copy_re.match(id)
      if (copy_match) and (copy_match.end() < len(id)):
          n=1
          orig_id=self.copy_re.sub('', id)
      else:
          n=0
          orig_id=id
      while 1:
          if self._getOb(id, None) is None:
              return id
          id='copy%s_of_%s' % (n and n+1 or '', orig_id)
          n=n+1
####
==============================================================



More information about the Zope-Collector-Monitor mailing list