LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Python 2.7 - AttributeError: 'NoneType' object has no attribute 'strip' (https://www.linuxquestions.org/questions/slackware-14/python-2-7-attributeerror-nonetype-object-has-no-attribute-strip-4175533445/)

glupa4e 02-08-2015 01:12 PM

Python 2.7 - AttributeError: 'NoneType' object has no attribute 'strip'
 
Hello,

i am trying to decode one stream and push it to a player instead of watching it in a browser. I am using livestreamer for this purpose. Unfortunately I am getting error when i try it out:

Code:

bash-4.2$ livestreamer hlsvariant://http://domain/path_to_playlist/playlist.m3u8 best
[cli][info] Found matching plugin stream for URL hlsvariant://http://domain/path_to_playlist/playlist.m3u8
[cli][info] Available streams: 142k (worst), 180p, 272p, 360p, 720p (best)
[cli][info] Opening stream: 720p (hls)
Traceback (most recent call last):
  File "/usr/bin/livestreamer", line 9, in <module>
    load_entry_point('livestreamer==1.11.1', 'console_scripts', 'livestreamer')()
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 851, in main
    handle_url()
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 470, in handle_url
    handle_stream(plugin, streams, stream_name)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 357, in handle_stream
    success = output_stream(stream)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 227, in output_stream
    stream_fd, prebuffer = open_stream(stream)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 204, in open_stream
    stream_fd = stream.open()
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 254, in open
    reader.open()
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/segmented.py", line 185, in open
    self.worker = self.__worker__(self)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 121, in __init__
    self.reload_playlist()
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 134, in reload_playlist
    playlist = hls_playlist.load(res.text, res.url)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 275, in load
    return parser(base_uri).parse(data)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 241, in parse
    self.parse_line(lineno, line)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 193, in parse_line
    self.m3u8.allow_cache = self.parse_tag(line, self.parse_bool)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 143, in parse_tag
    tag, value = self.split_tag(line)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 102, in split_tag
    return match.group("tag"), match.group("value").strip()
AttributeError: 'NoneType' object has no attribute 'strip'

I do not know now if there is a problem with the stream itself, or it is the livestreamer or python that generates that error. I know that this is not Slackware specific issue but if anyone has got experience with such things might share their knowledge.

Thanks!

Richard Cranium 02-08-2015 03:20 PM

Well, it's really a bug in the livestreamer code.

They test to see if a playlist line matches...
Code:

match = re.match("#(?P<tag>[\w-]+)(:(?P<value>.+))?", line)
However, the "value" expression can happen 0 or 1 times. You've got a case of 0 times. Since there's no match for the "value" expression, you get a None object (essentially a null pointer) instead of a string. None objects don't have a strip method but string objects do. The bug is that they call strip() instead of checking to see if the match is None.

IMO, the line
Code:

return match.group("tag"), match.group("value").strip()
should be replaced with
Code:

val = match.group("value")
return match.group("tag"), val.strip() if val else None

Welcome to dynamic scripting languages. :)

glupa4e 02-09-2015 01:57 PM

hello Richard Cranium,

thank you for your post. I think i got what the problem is. What i did not grasp is - should i by myself modify something in the code or not? And if yes, where and how should i do that? Or just wait and be patient until the livestreamer developers correct this error?

thanks

Richard Cranium 02-09-2015 09:59 PM

I'd send them the traceback along with a copy of your playlist (assuming that you don't mind someone else seeing it).

You could also change your playlist to match the pattern they *really* expect. As a last resort, you could change your copy of /usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py to match what I wrote above. (I'm not certain that returning None for the tag's value would really work; it could just cause a problem somewhere else.)

glupa4e 02-10-2015 02:05 PM

Hello again Richard Cranium,

thank you for posting. Of course i do not mind, you sending them my playlist.
By the way i made the changes you had proposed but i got another error:
Code:

Traceback (most recent call last):
  File "/usr/bin/livestreamer", line 9, in <module>
    load_entry_point('livestreamer==1.11.1', 'console_scripts', 'livestreamer')()
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 851, in main
    handle_url()
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 450, in handle_url
    streams = fetch_streams(plugin)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 367, in fetch_streams
    sorting_excludes=args.stream_sorting_excludes)
  File "/usr/lib64/python2.7/site-packages/livestreamer/plugin/plugin.py", line 313, in get_streams
    return self.streams(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/livestreamer/plugin/plugin.py", line 227, in streams
    ostreams = self._get_streams()
  File "/usr/lib64/python2.7/site-packages/livestreamer/plugins/stream.py", line 73, in _get_streams
    streams = cls(self.session, urlnoproto, **params)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 278, in parse_variant_playlist
    parser = hls_playlist.load(res.text, base_uri=res.url)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 281, in load
    return parser(base_uri).parse(data)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 247, in parse
    self.parse_line(lineno, line)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 201, in parse_line
    self.state["streaminf"] = self.parse_tag(line, self.parse_attributes)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 152, in parse_tag
    value = transform(value)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 116, in parse_attributes
    attr = re.findall(ATTRIBUTE_REGEX, value)
  File "/usr/lib64/python2.7/re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

That is why i made a roll back so now i am getting the first error.

Thank you for your cooperation!

Richard Cranium 02-10-2015 06:09 PM

It doesn't surprise me that my code change suggestion moved the error somewhere else. :)
Please note that I don't work on livestreamer code; I've just done a lot of python programming.

glupa4e 08-20-2016 10:27 AM

Slackware 14.2 Upgrade
 
Hello,

i installed Slackware 14.2 and all optional and mandatory packages for livestreamer from Slackbuilds repository. I still get the error:
Code:

[cli][info] Available streams: 142k (worst), 180p, 272p, 360p, 720p (best)                                                       
[cli][info] Opening stream: 720p (hls)                                                                                           
Traceback (most recent call last):                                                                                               
  File "/usr/bin/livestreamer", line 9, in <module>
    load_entry_point('livestreamer==1.12.2', 'console_scripts', 'livestreamer')()
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 886, in main
    handle_url()
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 491, in handle_url
    handle_stream(plugin, streams, stream_name)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 378, in handle_stream
    success = output_stream(stream)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 245, in output_stream
    stream_fd, prebuffer = open_stream(stream)
  File "/usr/lib64/python2.7/site-packages/livestreamer_cli/main.py", line 222, in open_stream
    stream_fd = stream.open()
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 258, in open
    reader.open()
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/segmented.py", line 185, in open
    self.worker = self.__worker__(self)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 125, in __init__
    self.reload_playlist()
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls.py", line 138, in reload_playlist
    playlist = hls_playlist.load(res.text, res.url)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 279, in load
    return parser(base_uri).parse(data)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 245, in parse
    self.parse_line(lineno, line)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 197, in parse_line
    self.m3u8.allow_cache = self.parse_tag(line, self.parse_bool)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 147, in parse_tag
    tag, value = self.split_tag(line)
  File "/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py", line 102, in split_tag
    return match.group("tag"), match.group("value").strip()
AttributeError: 'NoneType' object has no attribute 'strip'

Could this error be fixed anyhow?

Richard Cranium 08-20-2016 11:57 AM

Did you submit a bug report to the livestream developers the first time around? You should give them the traceback and your playlist so they can reproduce the problem.

I'll stress again that I don't work on livestream, nor do I use it. I will not submit a bug report on this.

I looked closer at the livestreamer codebase, and I don't believe that the developers have seen a playlist with the entries that you have. (I think the livestream code base could use some unit tests to verify that the code can handle None return values in various places, but that's merely my opinion.)

You can try the following, but I don't think that it will work:

Edit the file /usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py. On line 99, you will find the line..
Code:

        match = re.match("#(?P<tag>[\w-]+)(:(?P<value>.+))?", line)
Delete the last question mark on that line and save the file.


The stuff marked out won't work and will simply result in the same error (moved to different line numbers) as the last time you tried.

dugan 08-20-2016 05:36 PM

glupa4e, please submit an new issue on the livestreamer github page and tell them what you just told us.

This is a bug in livestreamer and it needs to be fixed by the developers.

glupa4e 08-21-2016 07:06 AM

Hi Guys,

i have still not submitted an issue to Livestreamer. I thought one should be a developer to submit such issues but i am not. But if no one else wants to do that, i should create an account and try to describe the issue.

Richard Cranium 08-21-2016 05:49 PM

Quote:

Originally Posted by glupa4e (Post 5593850)
Hi Guys,

i have still not submitted an issue to Livestreamer. I thought one should be a developer to submit such issues but i am not. But if no one else wants to do that, i should create an account and try to describe the issue.

If only that were true. It would certainly cut down on the number of bug reports that I get in the Day Job. ;)

dugan 08-21-2016 07:10 PM

Quote:

Originally Posted by glupa4e (Post 5593850)
Hi Guys,

i have still not submitted an issue to Livestreamer. I thought one should be a developer to submit such issues but i am not. But if no one else wants to do that, i should create an account and try to describe the issue.

Your description of the issue here is perfect. Just tell them what you told us.

glupa4e 08-23-2016 02:26 AM

Hi Guys,

thanks for your piece of advice. I submitted an issue to the Livestreamer developers.

Richard Cranium 08-23-2016 10:43 AM

Quote:

Originally Posted by glupa4e (Post 5594693)
Hi Guys,

thanks for your piece of advice. I submitted an issue to the Livestreamer developers.

Thanks! Despite my whining, code doesn't get better if nobody knows about problems.

glupa4e 08-29-2016 01:35 PM

Thanks to GitHub developers
 
Changing Line 99 in the file
Code:

/usr/lib64/python2.7/site-packages/livestreamer/stream/hls_playlist.py
to
Code:

match = re.match("#(?P[\w-]+)(:(?P.+))", line)
solved my problem.


All times are GMT -5. The time now is 01:38 AM.