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

Skip to content

Commit f357d3e

Browse files
committed
This module is now completely obsolete.
Noted this in the XXX comments. Also, changed all three functions to use the attributes if they exist.
1 parent c444865 commit f357d3e

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

Lib/codehack.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# XXX The functions getcodename() and getfuncname() are now obsolete
1111
# XXX as code and function objects now have a name attribute --
1212
# XXX co.co_name and f.func_name.
13+
# XXX getlineno() is now also obsolete because of the new attribute
14+
# XXX of code objects, co.co_firstlineno.
1315

1416
# Extract the function or class name from a code object.
1517
# This is a bit of a hack, since a code object doesn't contain
@@ -34,6 +36,10 @@
3436
_namecache = {} # The cache
3537

3638
def getcodename(co):
39+
try:
40+
return co.co_name
41+
except AttributeError:
42+
pass
3743
key = `co` # arbitrary but uniquely identifying string
3844
if _namecache.has_key(key): return _namecache[key]
3945
filename = co.co_filename
@@ -55,11 +61,19 @@ def getcodename(co):
5561
# Use the above routine to find a function's name.
5662

5763
def getfuncname(func):
64+
try:
65+
return func.func_name
66+
except AttributeError:
67+
pass
5868
return getcodename(func.func_code)
5969

6070
# A part of the above code to extract just the line number from a code object.
6171

6272
def getlineno(co):
73+
try:
74+
return co.co_firstlineno
75+
except AttributeError:
76+
pass
6377
code = co.co_code
6478
if ord(code[0]) == SET_LINENO:
6579
return ord(code[1]) | ord(code[2]) << 8

Lib/lib-old/codehack.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# XXX The functions getcodename() and getfuncname() are now obsolete
1111
# XXX as code and function objects now have a name attribute --
1212
# XXX co.co_name and f.func_name.
13+
# XXX getlineno() is now also obsolete because of the new attribute
14+
# XXX of code objects, co.co_firstlineno.
1315

1416
# Extract the function or class name from a code object.
1517
# This is a bit of a hack, since a code object doesn't contain
@@ -34,6 +36,10 @@
3436
_namecache = {} # The cache
3537

3638
def getcodename(co):
39+
try:
40+
return co.co_name
41+
except AttributeError:
42+
pass
3743
key = `co` # arbitrary but uniquely identifying string
3844
if _namecache.has_key(key): return _namecache[key]
3945
filename = co.co_filename
@@ -55,11 +61,19 @@ def getcodename(co):
5561
# Use the above routine to find a function's name.
5662

5763
def getfuncname(func):
64+
try:
65+
return func.func_name
66+
except AttributeError:
67+
pass
5868
return getcodename(func.func_code)
5969

6070
# A part of the above code to extract just the line number from a code object.
6171

6272
def getlineno(co):
73+
try:
74+
return co.co_firstlineno
75+
except AttributeError:
76+
pass
6377
code = co.co_code
6478
if ord(code[0]) == SET_LINENO:
6579
return ord(code[1]) | ord(code[2]) << 8

0 commit comments

Comments
 (0)