Strange error

W

WSobczuk

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
 
F

Fredrik Lundh

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-default-values-shared-between-objects

</F>
 
W

WSobczuk

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.
 
F

Fredrik Lundh

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>
 
B

bruno at modulix

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-default-values-shared-between-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 !-)
 
B

BartlebyScrivener

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 :

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top