bug-global
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: make string raw so escape sequence r'\s' is valid


From: Shigio YAMAGUCHI
Subject: Re: make string raw so escape sequence r'\s' is valid
Date: Wed, 5 Jun 2024 18:22:08 +0900

Hello,
> the two character sequence r'\s' is an escape sequence, isn't it?

I did a little research.
It seems that Python 3.6 and later considers '\s' an invalid escape sequence
(DeprecationWarnings). But DeprecationWarnings are silenced by default.
Python 3.12 and later displays it as a SyntaxWarning by default.

[test.py]
+-------------
|print('\s')
+-------------

$ python3.11 test.py
\s
$ python3.12 test.py
/***/***/test.py:1: SyntaxWarning: invalid escape sequence '\s'
  print('\s')
\s

I realized that '\s' should be considered an (invalid) escape sequence.
Thanks to this, I learned a lot.

Regards,
Shigio

On Wed, Jun 5, 2024 at 4:09 PM daniel watson <ozzloy@each.do> wrote:
>
>
>
> > Though these strings does not include escape sequences,
>
> i am confused by this.
>
> the two character sequence r'\s' is an escape sequence, isn't it?
>
> in a non-prefixed python string, it's an invalid escape sequence.
>
> #+begin_src python :results output
>   print('\s')
> #+end_src
>
> #+RESULTS:
> : \s
>
> #+STDERR:
> : <stdin>:3: SyntaxWarning: invalid escape sequence '\s'
> : [ Babel evaluation exited with code 0 ]
>
>
>
> with a double backslash in the source code,
>
> #+begin_src python :results output
>   print('\\s')
> #+end_src
>
> #+RESULTS:
> : \s
>
> no stderr for this block.
>
>
> in a regex, r'\s' is a valid escape sequence for the whitespace
> character class.
>
>
>
> > it seems that it is recommended to use 'r' prefix for all regular
> > expressions.
>
> i think it's useful to use 'r' prefix to avoid double escaping.
> for example,
>
> #+begin_src python :results output
>   print('\\s' == r'\s')
> #+end_src
>
> #+RESULTS:
> : True
>
> no stderr
>
>
> let me know if this isn't quite right.
>
>
> Shigio YAMAGUCHI <shigio@gnu.org> writes:
>
> > Hello,
> >> here's the disclaim,
> >
> > This is only in the case of copyright issues.
> >
> >> these two strings had the escape sequence r'\s', and were not prefixed
> >> with 'r'.  this patch adds the 'r' prefix, making the sequence valid.
> >
> > Though these strings does not include escape sequences, it seems that
> > it is recommended to use 'r' prefix for all regular expressions.
> > I will incorporate your patch into the repository.
> >
> > Thank you.
> >
> > Regards,
> > Shigio
> >
> > On Tue, Jun 4, 2024 at 2:11 PM daniel watson <ozzloy@each.do> wrote:
> >>
> >>
> >> not sure how to submit patches. open to redoing if you want.
> >>
> >> here's the disclaim,
> >> ======================================================================
> >>
> >>     I, Daniel Watson, hereby disclaim all copyright interest in my
> >>     changes and enhancements to Global (herein called
> >>     the "Program").
> >>
> >>     I affirm that I have no other intellectual property interest that
> >>     would undermine this release, or the use of the Program, and will
> >>     do nothing to undermine it in the future.  I represent that the
> >>     changes and enhancements are my own and not a copy of someone
> >>     else's work.
> >>
> >>     Daniel Watson, 2024-06-03 21:05:55 -0700
> >>
> >> ======================================================================
> >>
> >> do you want me to print a piece of paper with this on it and sign it,
> >> scan it, and send the result?
> >>
> >>
> >> commit message and patch below the line
> >>
> >> ======================================================================
> >>
> >>
> >> these two strings had the escape sequence r'\s', and were not prefixed
> >> with 'r'.  this patch adds the 'r' prefix, making the sequence valid.
> >>
> >>
> >> diff --git a/plugin-factory/pygments_parser.py.in 
> >> b/plugin-factory/pygments_parser.py.in
> >> index 6017d4b..9c35951 100644
> >> --- a/plugin-factory/pygments_parser.py.in
> >> +++ b/plugin-factory/pygments_parser.py.in
> >> @@ -89,7 +89,7 @@ class PygmentsParser:
> >>                      # we can assume index are delivered in ascending order
> >>                      while self.lines_index[cur_line] <= index:
> >>                          cur_line += 1
> >> -                    tag = re.sub('\s+', '', tag)    # remove newline and 
> >> spaces
> >> +                    tag = re.sub(r'\s+', '', tag)    # remove newline and 
> >> spaces
> >>                      if self.options.strip_punctuation:
> >>                          tag = tag.strip(PUNCTUATION_CHARACTERS)
> >>                      if tag:
> >> @@ -158,7 +158,10 @@ class CtagsParser:
> >>              line = self.child_stdout.readline()
> >>              if not line or line.startswith(TERMINATOR):
> >>                  break
> >> -            match = re.search(r'(\S+)\s+(\d+)\s+' + re.escape(path) + 
> >> '\s+(.*)$', line)
> >> +            match = re.search(r'(\S+)\s+(\d+)\s+'
> >> +                              + re.escape(path)
> >> +                              + r'\s+(.*)$',
> >> +                              line)
> >>              if match:
> >>                  (tag, lnum, image) = match.groups()
> >>                  if self.options.strip_punctuation:
> >>
>


-- 
Shigio YAMAGUCHI <shigio@gnu.org>
PGP fingerprint:
26F6 31B4 3D62 4A92 7E6F  1C33 969C 3BE3 89DD A6EB



reply via email to

[Prev in Thread] Current Thread [Next in Thread]