Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 497671e

Browse files
author
Moshe Zadka
committed
The one thing I love more then writing code is deleting code.
* Removed func_hash and func_compare, so they can be treated as immutable content-less objects (address hash and comparison) * Added tests to that affect to test_funcattrs (also testing func_code is writable) * Reverse meaning of tests in test_opcodes which checked identical code gets identical functions
1 parent 2beeb22 commit 497671e

3 files changed

Lines changed: 28 additions & 40 deletions

File tree

Lib/test/test_funcattrs.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,22 @@ def another():
154154
# This isn't specifically related to function attributes, but it does test a
155155
# core dump regression in funcobject.c
156156
del another.func_defaults
157+
158+
def foo():
159+
pass
160+
161+
def bar():
162+
pass
163+
164+
def temp():
165+
print 1
166+
167+
if foo==bar: raise TestFailed
168+
169+
d={}
170+
d[foo] = 1
171+
172+
foo.func_code = temp.func_code
173+
174+
d[foo]
175+

Lib/test/test_opcodes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def __init__(self, ignore):
5050

5151
try: raise AClass, b
5252
except BClass, v:
53-
if v != b: raise TestFailed
54-
else: raise TestFailed
53+
if v != b: raise TestFailed, "v!=b"
54+
else: raise TestFailed, "no exception"
5555

5656
try: raise b
5757
except AClass, v:
58-
if v != b: raise TestFailed
58+
if v != b: raise TestFailed, "v!=b AClass"
5959

6060
# not enough arguments
6161
try: raise BClass, a
@@ -64,21 +64,21 @@ def __init__(self, ignore):
6464
try: raise DClass, a
6565
except DClass, v:
6666
if not isinstance(v, DClass):
67-
raise TestFailed
67+
raise TestFailed, "v not DClass"
6868

6969
print '2.3 comparing function objects'
7070

7171
f = eval('lambda: None')
7272
g = eval('lambda: None')
73-
if f != g: raise TestFailed
73+
if f == g: raise TestFailed, "functions should not be same"
7474

7575
f = eval('lambda a: a')
7676
g = eval('lambda a: a')
77-
if f != g: raise TestFailed
77+
if f == g: raise TestFailed, "functions should not be same"
7878

7979
f = eval('lambda a=1: a')
8080
g = eval('lambda a=1: a')
81-
if f != g: raise TestFailed
81+
if f == g: raise TestFailed, "functions should not be same"
8282

8383
f = eval('lambda: 0')
8484
g = eval('lambda: 1')

Objects/funcobject.c

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -269,37 +269,6 @@ func_repr(PyFunctionObject *op)
269269
return PyString_FromString(buf);
270270
}
271271

272-
static int
273-
func_compare(PyFunctionObject *f, PyFunctionObject *g)
274-
{
275-
int c;
276-
if (f->func_globals != g->func_globals)
277-
return (f->func_globals < g->func_globals) ? -1 : 1;
278-
if (f->func_defaults != g->func_defaults) {
279-
if (f->func_defaults == NULL)
280-
return -1;
281-
if (g->func_defaults == NULL)
282-
return 1;
283-
c = PyObject_Compare(f->func_defaults, g->func_defaults);
284-
if (c != 0)
285-
return c;
286-
}
287-
return PyObject_Compare(f->func_code, g->func_code);
288-
}
289-
290-
static long
291-
func_hash(PyFunctionObject *f)
292-
{
293-
long h,x;
294-
h = PyObject_Hash(f->func_code);
295-
if (h == -1) return h;
296-
x = _Py_HashPointer(f->func_globals);
297-
if (x == -1) return x;
298-
h ^= x;
299-
if (h == -1) h = -2;
300-
return h;
301-
}
302-
303272
static int
304273
func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
305274
{
@@ -347,12 +316,12 @@ PyTypeObject PyFunction_Type = {
347316
0, /*tp_print*/
348317
0, /*tp_getattr*/
349318
0, /*tp_setattr*/
350-
(cmpfunc)func_compare, /*tp_compare*/
319+
0, /*tp_compare*/
351320
(reprfunc)func_repr, /*tp_repr*/
352321
0, /*tp_as_number*/
353322
0, /*tp_as_sequence*/
354323
0, /*tp_as_mapping*/
355-
(hashfunc)func_hash, /*tp_hash*/
324+
0, /*tp_hash*/
356325
0, /*tp_call*/
357326
0, /*tp_str*/
358327
(getattrofunc)func_getattro, /*tp_getattro*/

0 commit comments

Comments
 (0)