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

textwrap.dedent doesn't work properly with embedded strings containing linefeeds #67685

Closed
mdear mannequin opened this issue Feb 22, 2015 · 2 comments
Closed

textwrap.dedent doesn't work properly with embedded strings containing linefeeds #67685

mdear mannequin opened this issue Feb 22, 2015 · 2 comments

Comments

@mdear
Copy link
Mannequin

mdear mannequin commented Feb 22, 2015

BPO 23497
Nosy @ned-deily

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-02-22.01:19:25.975>
created_at = <Date 2015-02-22.00:57:55.125>
labels = ['invalid']
title = "textwrap.dedent doesn't work properly with embedded strings containing linefeeds"
updated_at = <Date 2015-02-22.01:19:25.834>
user = 'https://bugs.python.org/mdear'

bugs.python.org fields:

activity = <Date 2015-02-22.01:19:25.834>
actor = 'ned.deily'
assignee = 'none'
closed = True
closed_date = <Date 2015-02-22.01:19:25.975>
closer = 'ned.deily'
components = []
creation = <Date 2015-02-22.00:57:55.125>
creator = 'mdear'
dependencies = []
files = []
hgrepos = []
issue_num = 23497
keywords = []
message_count = 2.0
messages = ['236396', '236398']
nosy_count = 2.0
nosy_names = ['ned.deily', 'mdear']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue23497'
versions = []

@mdear
Copy link
Mannequin Author

mdear mannequin commented Feb 22, 2015

The textwrap.dedent function does not work when the string to dedent itself contains embedded strings that contain newline characters.

https://docs.python.org/3.4/library/textwrap.html?highlight=dedent#textwrap.dedent states that this function "can be used to make triple-quoted strings line up with the left edge of the display".

It just so happens that my triple-quoted string itself contains a single-quoted string with newline characters. I would have expected textwrap.dedent to ignore these newline characters inside single or double quoted strings contained in the larger triple-quoted string.

The semantics of this bug may be slightly different than https://bugs.python.org/issue19479 "textwrap.dedent doesn't work properly with strings containing CRLF", so I'm raising a new issue.

I am seeing this in a module I'm writing that emits synthetic python code that is subsequently passed back into the interpreter via "exec".

Python 3.4.1 (default, Nov 12 2014, 13:34:29) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from textwrap import dedent
>>> s = '''
...     'my_key' : r'my value which contains \n character'
... '''
>>> s
"\n    'my_key' : r'my value which contains \n character'\n"
>>> dedent(s)
"\n   'my_key' : r'my value which contains \ncharacter'\n"
>>>

@ned-deily
Copy link
Member

If you do not want the \n to be interpreted as a linefeed, you need to use a raw string literal, like:

a = r'''
...     'my_key' : r'my value which contains \n character'
... '''
>>> print(dedent(a))

'my_key' : r'my value which contains \n character'

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant