This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inconsistent Python find() behavior
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brian.curtin, georg.brandl, jcea, juan.gonzalez
Priority: normal Keywords:

Created on 2011-07-04 21:48 by juan.gonzalez, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (9)
msg139810 - (view) Author: Juan Gonzalez (juan.gonzalez) Date: 2011-07-04 21:48
Something really weird going on in python find() string function.  When I call <string>.find() and python returns -1 it crashes when compared against 0 using the ">" operator.

The statement in which crash condition occurs is the following:

    if url.find(str) > 0:
        print "RSS Item:", url
	break; 

However, if I change the statement to be "<" instead it does not crash.
The error that the python compiler reports is:

AttributeError: 'int' object has no attribute 'find'

My version of python is:

tony@ubuntu:~/auto/sel/scripts$ python -V
Python 2.7.1+
msg139823 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2011-07-05 02:20
Python 2.7.2 (default, Jun 16 2011, 01:46:46) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "hola".find("q") > 0
False
>>> "hola".find("q") < 0
True

I don't see the problem. Please, send a complete testcase.
msg139824 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2011-07-05 02:21
Note, anyway, that your python is not a real release. where is it coming from?.
msg139830 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-07-05 06:37
I suspect this is a problem where "url" is reassigned to an integer somewhere in code that isn't shown to us.

Please post the whole function and the whole traceback if you still think this is a valid bug.
msg139887 - (view) Author: Juan Gonzalez (juan.gonzalez) Date: 2011-07-05 16:03
Today I tried to use parse() instead of find() and I found out the following response:


tony@ubuntu:~/auto/sel/scripts$ python wtfibmdom
Traceback (most recent call last):
  File "wtfibmdom", line 22, in <module>
    if url.parse(str) > 0:
AttributeError: 'str' object has no attribute 'parse'
tony@ubuntu:~/auto/sel/scripts$ python wtfibmdom
Title: j3-dcsled-prd-validation passed Fri, 01 Jul 2011 14:03:59 -0500
Description: Build passed
Traceback (most recent call last):
  File "wtfibmdom", line 22, in <module>
    if url.find(str) > 0:
AttributeError: 'int' object has no attribute 'find'


I think this behavior is inconsistent since the compiler is treating the url variable as int and string at the same time.
msg139888 - (view) Author: Juan Gonzalez (juan.gonzalez) Date: 2011-07-05 16:06
Hi Georg,

This is the python code listing:

from RSS import ns, CollectionChannel, TrackingChannel

#Create a tracking channel, which is a data structure that
#Indexes RSS data by item URL
tc = TrackingChannel()
str = 'j3-nspire-prd-validation'
index = 0
#Returns the RSSParser instance used, which can usually be ignored
#tc.parse("http://www.python.org/channews.rdf")
tc.parse("http://pdt-california.eps.ti.com:8080/dashboard/rss.xml")   

RSS10_TITLE = (ns.rss10, 'title')
RSS10_DESC = (ns.rss10, 'description')

#You can also use tc.keys()
items = tc.listItems()
for item in items:
    #Each item is a (url, order_index) tuple
    url = item[index]
    #print "RSS Item:", 
    #str.find(str, beg=0 end=len(string))
    if url.find(str) > 0:
         print "RSS Item:", url
	 break; 
    #Get all the data for the item as a Python dictionary
    index = index + 1
    item_data = tc.getItem(item)
    print "Title:", item_data.get(RSS10_TITLE, "(none)")
    print "Description:", item_data.get(RSS10_DESC, "(none)")
msg139890 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2011-07-05 16:08
Can you post some example code or a test case?
msg139893 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2011-07-05 16:16
Put the failing code inside a "try", and wrote in the "except": "print repr(url)". I am pretty sure your "url" can be, actually, a number.

Or print "url" just before the 'faulty' line. I guess you will be surprised.
msg139896 - (view) Author: Juan Gonzalez (juan.gonzalez) Date: 2011-07-05 16:24
I print 1 before the faulty line and like Jesús says I'm surprised I get a 1

Description: Build passed
1
Traceback (most recent call last):
  File "wtfibmdom", line 23, in <module>
    if url.find(str) > 0:
AttributeError: 'int' object has no attribute 'find'
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56701
2011-07-05 16:27:11brian.curtinsetstatus: open -> closed
type: crash -> behavior
stage: resolved
2011-07-05 16:24:47juan.gonzalezsetmessages: + msg139896
2011-07-05 16:16:42jceasetmessages: + msg139893
2011-07-05 16:08:05brian.curtinsetnosy: + brian.curtin
messages: + msg139890
2011-07-05 16:06:24juan.gonzalezsetmessages: + msg139888
2011-07-05 16:03:47juan.gonzalezsetstatus: pending -> open

messages: + msg139887
2011-07-05 06:37:55georg.brandlsetstatus: open -> pending

nosy: + georg.brandl
messages: + msg139830

resolution: not a bug
2011-07-05 02:21:44jceasetmessages: + msg139824
2011-07-05 02:20:34jceasetnosy: + jcea
messages: + msg139823
2011-07-04 21:48:37juan.gonzalezcreate