Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Allow docstrings given as raw unicode (ur""") #119

Merged
merged 2 commits into from
Jun 22, 2015

Conversation

jbeezley
Copy link
Contributor

Fixes #116.

@jbeezley jbeezley force-pushed the raw-unicode-docstrings branch from 0d967d4 to 9524fb4 Compare May 31, 2015 19:27
@jbeezley jbeezley force-pushed the raw-unicode-docstrings branch from 9524fb4 to 6039dc6 Compare May 31, 2015 19:29
@Nurdok
Copy link
Member

Nurdok commented Jun 3, 2015

This PR looks good, but I'd like to see some tests for this.

@jbeezley
Copy link
Contributor Author

jbeezley commented Jun 3, 2015

I originally put in some tests, but ur is a syntax error in python 3, so there needs to be some mechanism for adding a test file that is only executed on python 2. Do you have any suggestions for how to do this?

@Nurdok
Copy link
Member

Nurdok commented Jun 19, 2015

@jbeezley It's possible to check if the current Python is 2.x or 3.x. I think the "Pythonic" way is to check this specific syntax for SyntaxError, like so:

try:
   eval('ur"foo"')
except SyntaxError:
    print "Python 3"
else:
    print "Python 2"

@jbeezley
Copy link
Contributor Author

You're right, you can catch syntax errors in an eval, but I tried various iterations of it without success.

These raise missing docstring.

def raw_unicode():
    pass
raw_unicode.__doc__ = eval(r'''ur"""Test unicode \xe9 and raw \\."""''')
def raw_unicode():
    eval(r'''ur"""Test unicode \xe9 and raw \\."""''')

and you can't eval a function definition like this.

eval(r'''def raw_unicode(): ur"""Test unicode \xe9 and raw \\."""''')

You can exec a definition, but that doesn't work with your tests. I.e.

exec('def no_docstring(): pass')

doesn't raise an error.

@Nurdok
Copy link
Member

Nurdok commented Jun 21, 2015

Instead of adding a test in test.py, I suggest you do it in test_pep257.py, where you can do something like this:

def test_unicode_raw():
    if sys.version_info[0] >= 3:
        return
    with Pep257Env() as env:
        with env.open('example.py', 'wt') as example:
            example.write(textwrap.dedent("""\
                def foo():
                    ur"""docstring"""
                    pass
            """))

            # assert stuff

@jbeezley jbeezley force-pushed the raw-unicode-docstrings branch from 4c8208d to 98c230b Compare June 22, 2015 16:17
@jbeezley
Copy link
Contributor Author

@Nurdok Thanks for the hint. I added a test.

Nurdok added a commit that referenced this pull request Jun 22, 2015
Allow docstrings given as raw unicode (ur""")
@Nurdok Nurdok merged commit dda1584 into PyCQA:master Jun 22, 2015
@Nurdok
Copy link
Member

Nurdok commented Jun 22, 2015

@jbeezley Merged! Thank you so much!

@jquast
Copy link

jquast commented Jun 22, 2015

Thank you @jbeezley !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support ur"""docstrings"""
3 participants