From: William S F. <ws...@fu...> - 2007-12-12 19:26:43
|
Nicholas Oxhøj wrote: > Dustin J. Mitchell wrote: > > I've boiled this down to its barest essentials. Here goes: > > > > 1 %module "aa" > > 2 > > 3 %inline %{ > > 4 char *aa_string = "Is mah string"; > > 5 > > 6 char *fn(char *in) { > > 7 return "ok"; > > 8 } > > 9 %} > > > > perl -Maa -e 'print aa::fn($aa::aa_string), "\n";' gives: > > TypeError in method 'fn', argument 1 of type 'char *' > > while perl -Maa -e 'print aa::fn($aa::aa_string.""), "\n";' gives the > > expected "ok". > > > > I'm fairly comfortable with XS and the stuff in perlguts, so I can > > trace this down to here: > > > > 1464 SWIGINTERN int > > 1465 SWIG_AsCharPtrAndSize(SV *obj, char** cptr, size_t* psize, int > *alloc) > > 1466 { > > 1467 if (SvPOK(obj)) { > > > > where 'obj' is the SV that results from $aa:aa_string. This check > > fails, causing SWIG to try and fail to cast 'obj' to a pchar, and > > ultimately give the error message above. > > > > I added printfs to both _wrap_aa_string_get() and _wrap_fn(), and the > > results were: > > > > _wrap_fn: SvPVOK(0x627620): 0 > > _wrap_aa_string_get: SvPVOK(0x627620): 262144 > > _wrap_aa_string_get: SvPVOK(0x627620): 262144 > > > > Which suggests that _wrap_fn is being called *before* a value is > > fetched for $aa_string. This suggests that the magic used for global > > variable access is confusing the heck out of the wrapper for fn(). > > Basically, the SvPOK is too simplistic a check, because while 'obj' is > > *not* a PV yet, a call to SvPV will still yield a string. As > > evidence, "commenting out" the SvPOK: > > 1467 if (1 || SvPOK(obj)) { > > causes the script to produce the expected "ok". > > > > Is there any way around this? I suppose the obvious answer is, "don't > > use global variables," and I can do that -- but if there's a better > > solution, I'd like to know :) > > > > Dustin > It looks like you hit the same bug, I did a couple of months ago. I > don't think it is just a problem related to global variables, but rather > related to argument conversion of any value that hasn't yet been used as > a Perl string. > > First I tried asking on swig-devel > (http://sourceforge.net/mailarchive/message.php?msg_id=E1IYLMV-0000fp-Qx%40sc8-sf-web21.sourceforge.net), > > but got no answers, so I finally submitted a detailed bug-report on it > (https://sourceforge.net/tracker/?func=detail&atid=101645&aid=1798728&group_id=1645), > > including what I thought would be the correct fix. But unfortunately for > you (and me), nothing has apperently been done to correct it, although > it seems a very simple fix- not even one line, just one character :-) > We don't have an active Perl maintainer, but Nicholas' suggestion looks reasonable, so I've applied it. Please try the svn version. William |