[Patches] code_compare

Vladimir Marangozov Vladimir.Marangozov@inrialpes.fr
Sat, 8 Apr 2000 18:18:55 +0200 (CEST)


Guido van Rossum wrote:
> 
> If you compare the name first, sorting a list of code objects would
> sort them by name first -- whgich is kind of nice.
> 
> Also, strictly speaking, the hash function should be updated.
> 

Right -- done.

--

Add the co_name field comparison to differentiate named code objects,
even when their code is the same. This caused side effects for -O
because of constant folding, like this one:

>>> class C:
...     def foo(self): pass
...     def bar(self): pass    
... 
>>> C.bar
<unbound method C.foo>
>>> 

-- 
       Vladimir MARANGOZOV          | Vladimir.Marangozov@inrialpes.fr
http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252

--
I confirm that, to the best of my knowledge and belief, this contribution is
free of any claims of third parties under copyright, patent or other rights
or interests ("claims").  To the extent that I have any such claims, I
hereby grant to CNRI a nonexclusive, irrevocable, royalty-free, worldwide
license to reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part of the
Python software and its related documentation, or any derivative versions
thereof, at no cost to CNRI or its licensed users, and to authorize others
to do so.

I acknowledge that CNRI may, at its sole discretion, decide whether or not
to incorporate this contribution in the Python software and its related
documentation.  I further grant CNRI permission to use my name and other
identifying information provided to CNRI by me for use in connection with
the Python software and its related documentation.

-------------------------------[ cut here ]---------------------------  
*** Python/compile.c-orig	Sat Apr  8 11:39:23 2000
--- Python/compile.c	Sat Apr  8 18:06:10 2000
***************
*** 140,145 ****
--- 140,147 ----
  	PyCodeObject *co, *cp;
  {
  	int cmp;
+ 	cmp = PyObject_Compare(co->co_name, cp->co_name);
+ 	if (cmp) return cmp;
  	cmp = co->co_argcount - cp->co_argcount;
  	if (cmp) return cmp;
  	cmp = co->co_nlocals - cp->co_nlocals;
***************
*** 160,175 ****
  code_hash(co)
  	PyCodeObject *co;
  {
! 	long h, h1, h2, h3, h4;
! 	h1 = PyObject_Hash(co->co_code);
  	if (h1 == -1) return -1;
! 	h2 = PyObject_Hash(co->co_consts);
  	if (h2 == -1) return -1;
! 	h3 = PyObject_Hash(co->co_names);
  	if (h3 == -1) return -1;
! 	h4 = PyObject_Hash(co->co_varnames);
  	if (h4 == -1) return -1;
! 	h = h1 ^ h2 ^ h3 ^ h4 ^
  		co->co_argcount ^ co->co_nlocals ^ co->co_flags;
  	if (h == -1) h = -2;
  	return h;
--- 162,179 ----
  code_hash(co)
  	PyCodeObject *co;
  {
! 	long h, h1, h2, h3, h4, h5;
! 	h1 = PyObject_Hash(co->co_name);
  	if (h1 == -1) return -1;
! 	h2 = PyObject_Hash(co->co_code);
  	if (h2 == -1) return -1;
! 	h3 = PyObject_Hash(co->co_consts);
  	if (h3 == -1) return -1;
! 	h4 = PyObject_Hash(co->co_names);
  	if (h4 == -1) return -1;
! 	h5 = PyObject_Hash(co->co_varnames);
! 	if (h5 == -1) return -1;
! 	h = h1 ^ h2 ^ h3 ^ h4 ^ h5 ^
  		co->co_argcount ^ co->co_nlocals ^ co->co_flags;
  	if (h == -1) h = -2;
  	return h;