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

Strange error

P: n/a
I have encountered a very strange error and I'm hoping that some Python
hackers here could give me insight on this.

searchview.py file contains two functions:
def mysearch(indexname, request, c, page = 0, searchdburl = INDEX_URL,
query_add = {}, queries = [], form = True, limit = DEFAULT_LIMIT, tags
= {}, order = ''):
and
def search(findquery, path = None, page=0, tags = {}, order='', limit =
DEFAULT_LIMIT, queries=[], searchdburl = INDEX_URL):

I import and call both from various places. Now recently I discovered
that when I call:
mysearch('pubcomm', request, vars, page = page, query_add =
query_add)
from one place, then inside mysearch() (I test it at the start of the
function) the tags argument *has* a value leftover from apparently from
a previous call. When investigating I have also found that it
sometimes has the value of the search() function's tags parameter from,
apparently, some previous call.
After I changed the call to this:
mysearch('pubcomm', request, vars, page = page, query_add =
query_add, tags = {})
the error stopped occurring, so it seems that what I'm suspecting is
right.

The searchview.py file does not contain any global 'tags' variable, but
the search() function does process it's own tags argument like so:
tags = dict([(k, listify(v)) for k, v in tags.iteritems()])

The whole thing is happening on Python 2.4.2 on Linux (I tried it on
Fedora and Gentoo). It's under a heavy load (the whole thing happens
inside a Flup based FCGI backend), but I'm not using threads so there
is no concurrency involved - only sequential processing of requests.

I haven't isolated a test case yet, hoping that someone could give me a
hint on this. Maybe I'm making some stupid mistake or maybe it's some
namespace bug (or 'feature'). Any ideas?

Thanks,
Wojtek

May 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
WS******@gmail.com wrote:
I have encountered a very strange error and I'm hoping that some Python
hackers here could give me insight on this.

searchview.py file contains two functions:
def mysearch(indexname, request, c, page = 0, searchdburl = INDEX_URL,
query_add = {}, queries = [], form = True, limit = DEFAULT_LIMIT, tags
= {}, order = ''):
and
def search(findquery, path = None, page=0, tags = {}, order='', limit =
DEFAULT_LIMIT, queries=[], searchdburl = INDEX_URL):


default values are evaluated once, when the function object is created.
this is explained in the tutorial, in the language reference, and in
the FAQ:

http://pyfaq.infogami.com/why-are-de...etween-objects

</F>

May 18 '06 #2

P: n/a
Evaluation of default values seems to have nothing to do with the case
I described.
The default values are both tags = {}, and still inside mysearch() I
sometimes get some value from previous call inside tags, when the tags
keyword argument is not specified.

May 18 '06 #3

P: n/a
WS******@gmail.com wrote:
The default values are both tags = {}, and still inside mysearch() I
sometimes get some value from previous call inside tags, when the tags
keyword argument is not specified.


which is exactly what happens if you *update* the default argument. did
you even bother to read the FAQ entry?

</F>

May 18 '06 #4

P: n/a
WS******@gmail.com wrote:
Evaluation of default values seems to have nothing to do with the case
I described.
It does. Please *read* the faq:
http://pyfaq.infogami.com/why-are-de...etween-objects
The default values are both tags = {}, and still inside mysearch() I
sometimes get some value from previous call inside tags, when the tags
keyword argument is not specified.


def wtf(foo, args=[]):
args.append(foo)
print args

wtf('???')
wtf('???')
wtf('???')

You didn't post the actual code for mysearch(), but chances are your
code is modifying it !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 18 '06 #5

P: n/a
>> This type of bug commonly bites neophyte programmers.

That IS weird. I'm new. I read it, I see how it works, but how come in
between calls of the function, you can't access the values being
stored? Or I guess you can, but I don't see the way.

How come you can't do something like :
wtf.args


It doesn't give you the list with the values it's storing between
function calls.

May 18 '06 #6

P: n/a
Sorry,

Never mind.

wtf.func_defaults

Thanks for the examples.

Rick

May 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.