Skip to content

Commit 7be15b5

Browse files
Fix AttributeError for 'im_func', 'func_code' in Python 3 compatibility
Simplified the introspection logic for hpilo.Ilo methods by unifying Python 2 and 3 compatibility checks. This includes handling attributes __code__/func_code and __defaults__/func_defaults. This fix allows the module to function correctly in Python 2 and Python 3 environments.
1 parent 50b4e0a commit 7be15b5

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

hpilo_cli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ def hpilo_help(option, opt_str, value, parser, exitcode=0):
251251
else:
252252
if value in ilo_methods:
253253
import re, textwrap
254-
func = getattr(hpilo.Ilo, value).im_func
255-
code = func.func_code
254+
func = getattr(hpilo.Ilo, value) if PY3 else getattr(hpilo.Ilo, value).im_func
255+
code = func.__code__ if PY3 else func.func_code
256256
args = ''
257257
if code.co_argcount > 1:
258258
args = code.co_varnames[:code.co_argcount]
259-
defaults = func.func_defaults or []
259+
defaults = func.__defaults__ if PY3 else func.func_defaults or []
260260
args = ["%s=%s" % (x, x.upper()) for x in args[:len(args)-len(defaults)]] + \
261261
["[%s=%s]" % (x,str(y)) for x, y in zip(args[len(args)-len(defaults):], defaults) if x != 'progress']
262262
args = ' ' + ' '.join(args[1:])

0 commit comments

Comments
 (0)