By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,695 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,695 IT Pros & Developers. It's quick & easy.

Dynamically add class method causes "SystemError: ... bad argument to internal function"

P: n/a
Hi, group,
I am trying to dynamically add a method to class by following
this post:

http://groups-beta.google.com/group/...8e91be352c6bfc

But I got the Error like the below:

*******Error**********************
Traceback (most recent call last):
File "C:\Documents and Settings\cwu\Desktop\t1.py", line 36, in ?
C().f2(1,2,4,x='x')
File "C:\Documents and Settings\cwu\Desktop\t1.py", line 19, in newf2
self.f()
SystemError: C:\sf\python\dist\src-maint23\Objects\cellobject.c:22: bad
argument to internal function
*******Error**********************

Anybody has a thought about this problem?

My sample code is pasted as the below:

*********Code*******************************
##import new
##import sys
##
##def safe_tuple(seq):
## """Force func_code attributes to tuples of strings.
##
##
## Many of the func_code attributes must take tuples, not lists,
## and *cannot* accept unicode items--they must be coerced to strings
## or the interpreter will crash.
## """
## seq = map(str, seq)
## return tuple(seq)
##
##class C:
## def __init__(self):
## def _generic(*a,**k):
## print vars()
## self.f()
##
## def _newmethod(newname):
## fcode = _generic.func_code
## #code( argcount, nlocals, stacksize, flags, codestring,
constants, names, varnames, filename, name, firstlineno, lnotab)
## fcode_new = new.code(fcode.co_argcount, fcode.co_nlocals,
fcode.co_stacksize,
## fcode.co_flags, fcode.co_code,
tuple(fcode.co_consts), # Notice co_consts should *not* be
safe_tupled.
## safe_tuple(fcode.co_names),
safe_tuple(fcode.co_varnames),
## fcode.co_filename,
## newname,
## fcode.co_firstlineno,
fcode.co_lnotab)
## return new.function(fcode_new,globals())
## setattr(self,'f2',_newmethod('newf2'))
##
## def f():
## print 'from f'
##
##C().f2(1,2,4,x='x')
##
##
**********Code************************

*Sorry, I have to put comment chars before the code to preserve the
proper indentation.

Thanks a lot!
CW

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I have noticed that the exception was caused by the call of "self.f()"
within my "_generic" function. It will raise SystemError exception
whenever I refer to any method or attribute of "self" within "_generic"
function. Otherwise, dynamically adding method works fine. I cannot
figure out what's the reason there.

CW

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.