Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'NoneType' object has no attribute 'strip' exception in streaming.py #576

Closed
geransmith opened this issue Mar 16, 2015 · 52 comments
Closed
Labels
Bug This is regarding a bug with the library

Comments

@geransmith
Copy link
Contributor

streaming.py threw the following exception:

Traceback (most recent call last):
File "./script.py", line 93, in
stream.filter(track=['#programming'], languages=['en'])
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 428, in filter
self._start(async)
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 346, in _start
self._run()
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 286, in _run
raise exception
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 255, in _run
self._read_loop(resp)
File "/usr/local/lib/python3.4/dist-packages/tweepy/streaming.py", line 298, in _read_loop
line = buf.read_line().strip()
AttributeError: 'NoneType' object has no attribute 'strip'

@YukiDayDreamer
Copy link

I also met this problem few hours ago...

@struct78
Copy link

struct78 commented Apr 9, 2015

Also getting it here

@Gitju
Copy link

Gitju commented Apr 10, 2015

Got the same error.

Traceback (most recent call last):^M
File "twitter.py", line 25, in ^M
sapi.filter(track=['@' + TWITTER_ACCOUNT_NAME])^M
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 428, in filter^M
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 346, in _start^M
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 255, in _run^M
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 298, in _read_loop^M
AttributeError: 'NoneType' object has no attribute 'strip'^M

Seems like the error does not appear in former versions several months ago as they run stable for me.
https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py#L298

@danielforsyth
Copy link

Just got this today as well.

@nomeyer
Copy link

nomeyer commented Apr 17, 2015

Same issue here. So did you just roll back to a previous release to resolve the issue?

@nomeyer
Copy link

nomeyer commented Apr 20, 2015

I was running tweepy 3.3.0 (which is not a stable release). Rolling back to 3.2.0 resolved the issue for me.

@arinto
Copy link

arinto commented Apr 22, 2015

I encountered the same issue. Will downgrade to 3.2.0 as pointed by @nomeyer

geransmith added a commit to geransmith/DraenorSelfies that referenced this issue May 18, 2015
Working on getting the NoneType AttributeErrors handled. This is due to [an issue in Tweepy](tweepy/tweepy#576)
@YukiDayDreamer
Copy link

Hey Guys! I FOUND A SOLUTION!!! This problem is due to the program has streamed some problematic twitter data. To avoid this, you can add a AttributeError dectection part in related part of streaming.py in tweepy lib to pass this annoying problem. After doing this, I haven't got this problem in the latest 10 days.
You can use replace the _run function in streaming.py with mine. Have a try~

def _run(self):
# Authenticate
url = "https://%s%s" % (self.host, self.url)

    # Connect and process the stream
    error_counter = 0
    resp = None
    exception = None
    while self.running:
        if self.retry_count is not None:
            if error_counter > self.retry_count:
                # quit if error count greater than retry count
                break
        try:
            auth = self.auth.apply_auth()
            resp = self.session.request('POST',
                                        url,
                                        data=self.body,
                                        timeout=self.timeout,
                                        stream=True,
                                        auth=auth,
                                        verify=self.verify)
            if resp.status_code != 200:
                if self.listener.on_error(resp.status_code) is False:
                    break
                error_counter += 1
                if resp.status_code == 420:
                    self.retry_time = max(self.retry_420_start,
                                          self.retry_time)
                sleep(self.retry_time)
                self.retry_time = min(self.retry_time * 2,
                                      self.retry_time_cap)
            else:
                error_counter = 0
                self.retry_time = self.retry_time_start
                self.snooze_time = self.snooze_time_step
                self.listener.on_connect()
                self._read_loop(resp)
        except (Timeout, ssl.SSLError) as exc:
            # This is still necessary, as a SSLError can actually be
            # thrown when using Requests
            # If it's not time out treat it like any other exception
            if isinstance(exc, ssl.SSLError):
                if not (exc.args and 'timed out' in str(exc.args[0])):
                    exception = exc
                    break
            if self.listener.on_timeout() is False:
                break
            if self.running is False:
                break
            sleep(self.snooze_time)
            self.snooze_time = min(self.snooze_time + self.snooze_time_step,
                                   self.snooze_time_cap)
        except Exception as exc:
            exception = exc
            # any other exception is fatal, so kill loop
            break

    # cleanup
    self.running = False
    if resp:
        resp.close()

    self.new_session()

    if exception:
        # call a handler first so that the exception can be logged.
        self.listener.on_exception(exception)
        raise

@bartmika
Copy link

Thanks @dengjiaxian, I will test this out and see how it goes.

@huinamao
Copy link

@dengjiaxian I tried with your updated streaming.py. I have been streaming the geolocated tweets successfully for three days. But just couple of hours ago, there is another error raised. Does it happen to you? Any solution?

Traceback (most recent call last):
File "main_streaming_geotagg_US.py", line 69, in
stream.filter(locations=[-125.0011, 24.9493, -66.9326, 49.5904]) ## united states
File "build/bdist.linux-x86_64/egg/tweepy/streaming.py", line 434, in filter
File "build/bdist.linux-x86_64/egg/tweepy/streaming.py", line 350, in _start
File "build/bdist.linux-x86_64/egg/tweepy/streaming.py", line 286, in _run
TypeError: argument of type 'NoneType' is not iterable

I do not understand why there is the 'NoneType' error. I thought adding the following exception in _read_loop can capture such error.
except AttributeError: # this might throw an error when streaming a "nonetype" object
pass

@kodeine
Copy link

kodeine commented Feb 2, 2016

#698 had the same problem.

@Jugaadu
Copy link

Jugaadu commented Mar 16, 2016

Got same error in python 2.7 tweepy 3.5.0.. Let me know If anyone has solution on this

File "AIFW2016.py", line 33, in
stream.filter(track=["#AIFW2016", "#AIFW", "#AIFW16", "#IndiaModern"])
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 445, in filter
self._start(async)
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 361, in _start
self._run()
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 294, in _run
raise exception
AttributeError: 'NoneType' object has no attribute 'strip'

@micku
Copy link

micku commented Mar 18, 2016

I am having the same problem as @Jugaadu

@ealhazmi
Copy link

Me too!

@mriedl7
Copy link

mriedl7 commented Mar 23, 2016

Also having the same problem as @Jugaadu @micku @ealhazmi

@kurisubrooks
Copy link

Encountered this problem yesterday for the first time. Still no fix?

@bartmika
Copy link

+1

@srwareham
Copy link

+1. I have had this issue since I started using Tweepy ~ 3 weeks ago. Have always been using the most up to date version of Tweepy (currently 3.5 on python 2.7.11). It can be days between occurrences of the error, but it always invariably comes up and kills the process.

@rodolfomartinez
Copy link

+1

@eeevanbbb
Copy link

Same issue for me with Tweepy version 3.5.0, happens sporadically (every few days, generally):

AttributeError: 'NoneType' object has no attribute 'strip'

It's a problem in streaming.py, and the only time strip() is called in that file is on line 313:

line = buf.read_line().strip()

Perhaps read_line() is returning None? Could a check be added to make sure read_line() is not None before continuing?

@Jugaadu
Copy link

Jugaadu commented Mar 29, 2016

I have rolled back to previous version of tweepy 3.2.0, It doesn't throw this error now. Tweepy 3.5.0 have a bug. Please use previous version

@srwareham
Copy link

For those who just want a quick fix, this worked for me: f4bfce5

You simply need to modify the steaming.py file that is located in your python path. The hotfix I did, which has been working without error for > 10 days now went as follows:

  1. Find the path of streaming.py. You can do so by entering the following in your terminal: python -c 'import tweepy; print tweepy.streaming.__file__ (streaming.py should be in the same directory)
  2. Open up streaming.py (not .pyc) in your text editor of choice and add the line found in the commit linked above
  3. Delete streaming.pyc
  4. Enter in your terminal: python streaming.py

Done! Now we have a new streaming.pyc and when you import tweepy it will import the newly modifed version, which should not suffer from this bug. Not saying it is the best fix design-wise, but it has worked for me without error!

@remram44
Copy link
Contributor

remram44 commented Apr 25, 2016

I'm also affected by this, I applied f4bfce5, will report back if it crashes again. Please consider building a release 😄

@remram44
Copy link
Contributor

Got this today: AttributeError: 'int' object has no attribute 'strip'

@srwareham
Copy link

@remram44 Did you add
return '0'
or
return 0

Your error sounds a lot like the latter which is different than f4bfce5.

bchase77 pushed a commit to bchase77/GarageDoor that referenced this issue Oct 19, 2016
'NoneType' object has no attribute 'strip' exception in streaming.py

Source is from: tweepy/tweepy#576

Changes taken from: benfei/tweepy@d40ee87
@tejasshah17
Copy link

tejasshah17 commented Oct 22, 2016

Hi, I faced with the similar issue and found out a temporary solution. The following solution works for 3.5v of tweepy. Please update this code as temporary fix in the repository which might help other people like. Please find the solution below:

      while self.running and not resp.raw.closed:
            length = 0
            while not resp.raw.closed:
                line = buf.read_line()
                if line:
                    line = line.strip()
                #line = buf.read_line().strip()
                if not line:
                    self.listener.keep_alive()  # keep-alive new lines are expected
                elif line.isdigit():

@wknowles
Copy link

@tejasshah17 I tried your fix but I get:

TypeError: expected string or buffer

Im using 3.5v tweepy

@shaochengcheng
Copy link

I went through the stream.py, one of the hard work tweepy try to accomplishment is:

Docs quoted from ReadBuffer:

"""Buffer data from the response in a smarter way than httplib/requests can.
Tweets are roughly in the 2-12kb range, averaging around 3kb.
Requests/urllib3/httplib/socket all use socket.read, which blocks
until enough data is returned. On some systems (eg google appengine), socket
reads are quite slow. To combat this latency we can read big chunks,
but the blocking part means we won't get results until enough tweets
have arrived. That may not be a big deal for high throughput systems.
For low throughput systems we don't want to sacrafice latency, so we
use small chunks so it can read the length and the tweet in 2 read calls.
"""

As it is stated, tweepy define the ReadBuffer class to smartly read response raw content. Then
Stream._read_loop loop over to call ReadBuffer.read_line to generate tweet data.

However, I noticed that the HTTP connection dependency of tweepy is the package requests and requests itself provides a smart way to handle streaming data

Docs quoted from class requests.Response

iter_lines(chunk_size=512, decode_unicode=None, delimiter=None)

Iterates over the response data, one line at a time. When stream=True is set on the request, this avoids reading the content at once into memory for large responses.

I am not sure whether this method is smart enough to combat with ReadBuffer of tweepy. Since ReadBuffer is not quiet stable, can we use the function Request provides.

Sad thing is that tweepy is NO LONGER MAINTAINED!

@Aaron1011
Copy link
Contributor

@shaochengcheng: As stated here, I'll be continuing to work on Tweepy.

@remram44
Copy link
Contributor

That is awesome news! I will try to do some reviewing when I get the time. Though there is probably value in making all this simpler...

Also please update the tagline
tagline screenshot

@Aaron1011
Copy link
Contributor

Closed in c1eddf1.

@toshiro92
Copy link

Yeah great @Aaron1011 ! I will test it soon !

@swayson
Copy link

swayson commented Dec 21, 2016

This fix is not deployed to PyPi yet, right?

@remram44
Copy link
Contributor

There hasn't been a Tweepy release since November 2015, so no.

@Aaron1011
Copy link
Contributor

Sorry - I'm still unable to push new releases to PyPi. I will as soon as I'm granted access.

@BenderV
Copy link

BenderV commented Feb 20, 2017

@Aaron1011 do you have any idea when will you have PyPi access? It's sad to see such a trivial bugs not being fixed! Anyway, thanks for your hard work!

@remram44
Copy link
Contributor

@Aaron1011 If you don't see this happening, please consider getting tweepy4 or something similar on PyPI.

@Horatio-Blackwood
Copy link

Horatio-Blackwood commented Mar 1, 2017

Is this fix available in PyPi yet? Can I use pip to pull it down? Having this issue in my bot currently - also - thanks for the fix, Tweepy is a wonderful piece of software.

@Kannanravindran
Copy link

I face the same error too AttributeError: 'NoneType' object has no attribute 'strip'. Found this thread and decided to install the package from git using
pip install git+https://github.com/tweepy/tweepy/issues/576

In a couple of minutes got this error
TweepError: Expecting length, unexpected value found

Anyone else faced this issue?
Is version 3.6 from git stable?

@weijchen
Copy link

weijchen commented Jul 12, 2017

Run into same error today,

Traceback (most recent call last):
File ".\bot1.py", line 96, in
streamingAPI.filter(track=stocklist)
File "C:\Users\jimmyweicc\Anaconda3\lib\site-packages\tweepy\streaming.py", line 445, in filter
self._start(async)
File "C:\Users\jimmyweicc\Anaconda3\lib\site-packages\tweepy\streaming.py", line 361, in _start
self._run()
File "C:\Users\jimmyweicc\Anaconda3\lib\site-packages\tweepy\streaming.py", line 294, in _run
raise exception
File "C:\Users\jimmyweicc\Anaconda3\lib\site-packages\tweepy\streaming.py", line 263, in _run
self._read_loop(resp)
File "C:\Users\jimmyweicc\Anaconda3\lib\site-packages\tweepy\streaming.py", line 313, in _read_loop
line = buf.read_line().strip()
AttributeError: 'NoneType' object has no attribute 'strip'

Is there any solution to this?

@Jugaadu
Copy link

Jugaadu commented Aug 17, 2017

@weijchen Easy solution - rollback to tweepy 3.2.0

@weijchen
Copy link

@Jugaadu Thanks!

@byoung0589
Copy link

You should update requests requirement while you are in there.

@paldana
Copy link

paldana commented May 17, 2018

For v3.6, the same issue was mentioned in #1026 and "resolved" in #1027. Not sure why it's not yet merged to the master branch though.

@Harmon758
Copy link
Member

This specific fix (c1eddf1) was in the v3.6.0 release.
However, the error persisted (see #1026). This was then fixed with 32fa592 (#1027) in 3.7.0.

@Harmon758 Harmon758 added the Bug This is regarding a bug with the library label Apr 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This is regarding a bug with the library
Projects
None yet
Development

No branches or pull requests