2 messages in com.googlegroups.sqlalchemy[sqlalchemy] sa0.5 __init__ replacement
FromSent OnAttachments
az...@svilendobrev.com28 Jun 2008 23:15 
jason kirtland29 Jun 2008 12:03 
Subject:[sqlalchemy] sa0.5 __init__ replacement
From:az...@svilendobrev.com (az@svilendobrev.com)
Date:06/28/2008 11:15:08 PM
List:com.googlegroups.sqlalchemy

hi i have

class X(object): ....

X.__init__ = setattr_kargs where def setattr_kargs( *args, **kargs): assert len(args)==1 x = args[0] for k,v in kargs.iteritems(): setattr( x, k, v)

when SA comes to play, it fails to find a 'self' in "__init__" arguments.

in format_argspec_plus(), this line self_arg = spec[0] and spec[0][0] or None should never be None: self_arg = spec[0] and spec[0][0] or spec[1]+'[0]' seems to work.

why not (args,vargs,kwargs,defaults) instead of spec+indexes? also, the empty defaults is tuple/() and not None, so spec[3] should not be compared to None but just bool of it. Something like:

def format_argspec_plus(fn, grouped=True): spec = args,vargs,kwargs,defaults = inspect.getargspec(fn) vkargs = inspect.formatargspec(*spec) self_arg = args and args[0] or vargs+'[0]' apply_pos = inspect.formatargspec( args,vargs,kwargs) defaulted_vals = defaults and args[-len(defaults):] or () apply_kw = inspect.formatargspec( args,vargs,kwargs, defaulted_vals, formatvalue=lambda x: '=' + x) if grouped: return dict(args=vkargs, self_arg=self_arg, apply_pos=apply_pos, apply_kw=apply_kw) else: return dict(args=vkargs[1:-1], self_arg=self_arg, apply_pos=apply_pos[1:-1], apply_kw=apply_kw[1:-1])

the log/printing of functext/funcvars will help a bit, as the functext is not very visible in a stacktrace.

and/or, maybe fix/hack with the co_filename and co_firstlineno code-attributes so inspect.getsource( myclass.__init__) "works"... maybe pointing to orm.attributes._generate_init function.

on a general note, this functext-generation+exec... is going to make troubles. i've been generating languages (like 3 level python1 -> python2 -> python3/C/corba/html/...) for years, but always exposed the full result and tried to avoid it as much as possible - if _can_ be done without generation+exec - or absolute possible minimum of it. it's a _very_ slippery slope, regardless how "attractive" it may look. debugging it is a nightmare.

for example, the query._generative decoration is not really readable, if something breaks there, would be very hard to understand what/why/where. Playing with user's __init__ may be unavoidable but these decorators over ~standartized SA code should work without stringtext+exec.

anyway ciao svilen