Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

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

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:Python27libsite-packagespraw\__init__.py", line 1028, in __init__
    super(AuthenticatedReddit, self).__init__(*args, **kwargs)
  File "C:Python27libsite-packagespraw\__init__.py", line 502, in __init__
    super(OAuth2Reddit, self).__init__(*args, **kwargs)
  File "C:Python27libsite-packagespraw\__init__.py", line 615, in __init__
    super(UnauthenticatedReddit, self).__init__(*args, **kwargs)
  File "C:Python27libsite-packagespraw\__init__.py", line 280, in __init__
    self.handler = handler or DefaultHandler()
  File "C:Python27libsite-packagesprawhandlers.py", line 70, in __init__
    self.http = Session()  # Each instance should have its own session
  File "C:Python27libsite-packages
equestssessions.py", line 176, in __init__
    self.headers = default_headers()
  File "C:Python27libsite-packages
equestsutils.py", line 461, in default_headers
    'User-Agent': default_user_agent(),
  File "C:Python27libsite-packages
equestsutils.py", line 430, in default_user_agent
    _implementation = platform.python_implementation()
  File "C:Python27libplatform.py", line 1458, in python_implementation
    return _sys_version()[0]
  File "C:Python27libplatform.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

1As 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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...