Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Praw "failed to parse CPython sys.version" when creating Reddit object

Tags:

python

I'm getting this error when creating a reddit object. Here's the code:

import praw, requests, ctypes
r = praw.Reddit(user_agent="Wallpaper downloader")

Here's the error:

Traceback (most recent call last):

File "C:/Python27/background.py", line 3, in <module>
    r = praw.Reddit(user_agent="Wallpaper downloader")
  File "C:\Python27\lib\site-packages\praw\__init__.py", line 1028, in __init__
    super(AuthenticatedReddit, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\praw\__init__.py", line 502, in __init__
    super(OAuth2Reddit, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\praw\__init__.py", line 615, in __init__
    super(UnauthenticatedReddit, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\praw\__init__.py", line 280, in __init__
    self.handler = handler or DefaultHandler()
  File "C:\Python27\lib\site-packages\praw\handlers.py", line 70, in __init__
    self.http = Session()  # Each instance should have its own session
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 176, in __init__
    self.headers = default_headers()
  File "C:\Python27\lib\site-packages\requests\utils.py", line 461, in default_headers
    'User-Agent': default_user_agent(),
  File "C:\Python27\lib\site-packages\requests\utils.py", line 430, in default_user_agent
    _implementation = platform.python_implementation()
  File "C:\Python27\lib\platform.py", line 1458, in python_implementation
    return _sys_version()[0]
  File "C:\Python27\lib\platform.py", line 1423, in _sys_version
    repr(sys_version))
ValueError: failed to parse CPython sys.version: '2.7.3 |EPD_free 7.3-2 (32-bit)| (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)]'

I installed praw using pip, and I'm using Windows. Any idea why I got this error?

like image 747
Augusto Dias Noronha Avatar asked Jan 13 '23 06:01

Augusto Dias Noronha


2 Answers

This looks to be a bug in EPD.

What's going on:

platforms.py attemps, in the function _sys_version (which is called by python_implementation, as your stack trace shows) to, in some cases, parse sys.version with a regex. In your case, it thinks you're running CPython (are you? CPython is the normal Python version, not anything like Jython or IronPython), and that is the case in which the regex gets run. The regex:

_sys_version_parser = re.compile(
    r'([\w.+]+)\s*'
    '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
    '\[([^\]]+)\]?')

And the code that runs it:

else:
    # CPython
    match = _sys_version_parser.match(sys_version)
    if match is None:
        raise ValueError(
            'failed to parse CPython sys.version: %s' %
            repr(sys_version))
    version, buildno, builddate, buildtime, compiler = \
          match.groups()
    name = 'CPython'
    builddate = builddate + ' ' + buildtime

The code is fairly simple: it's raising the error because the regex didn't match. Looking at the regex:

r'([\w.+]+)\s*'
'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
'\[([^\]]+)\]?')

That first part, ([\w.+]+)\s*, matches space-separated chunks of [a-zA-Z0-9_.+] — the function hints that this is the version number. That's probably matching the "2.7.3" correctly.

The second part is much more interesting. The code hints that it's looking for a buildno, and the regex seems to¹ indicate that it's looking for a literal paren ((). We see this later in your string: (default, Apr 12 2012, 14:30:37)

But this part is in the way: |EPD_free 7.3-2 (32-bit)|. My guess is that the regex isn't expecting that, and that's what's causing it to choke.

How to fix it:

In the short term, to test this theory, try removing it in Python. Just assign to sys.version, something like,

# This raises an exception for you:
platform.python_implementation()

# Try this:
sys.version = '2.7.3 (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)]'
# Hopefully, this no longer raises.
platform.python_implementation()

If that does fix it, you'll probably want to get rid of it long term. I'm assuming this is Enthought Python Distribution Free — you'll probably have to file a bug there, as this is probably something they've done.

¹As an aside, there's some weirdness here. The literal should have those backslashes doubled, or be a raw string. (The r from the first literal doesn't, I believe, carry over, however, an unknown escape ends up being slash-whatever, so it still works.)

like image 103
Thanatos Avatar answered Jan 25 '23 04:01

Thanatos


I've been experiencing the same issue. I just submitted a bug fix for it.

like image 33
madman2890 Avatar answered Jan 25 '23 03:01

madman2890