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

[W605] invalid escape sequence ‘x’ #66

Closed
SigureMo opened this issue Oct 3, 2022 · 7 comments · Fixed by PaddlePaddle/Paddle#46752
Closed

[W605] invalid escape sequence ‘x’ #66

SigureMo opened this issue Oct 3, 2022 · 7 comments · Fixed by PaddlePaddle/Paddle#46752
Assignees

Comments

@SigureMo
Copy link
Member

SigureMo commented Oct 3, 2022

此前统计 172 个,虽然看起来较多,但往往一个字符串里会出现多个该问题,实际上需要修复的位置并不是特别多,建议手动修复

该问题主要是一些忘记加自动转义标志 r 的字符串,如正则、含 LaTeX 数学公式的 docstring 等,这些直接在字符串前加 r 即可

# 正则例子
- 'PD_REGISTER_KERNEL\(.*?\).*?\{.*?\}'
+ r'PD_REGISTER_KERNEL\(.*?\).*?\{.*?\}'

# 公式例子
-         """Variance of distribution.
+         r"""Variance of distribution.

          The variance is
  
          .. math::
              variance = 2 * \sigma^2
          In the above equation:
  
          * :math:`scale = \sigma`: is the scale parameter.
  
          Returns:
              Tensor: The variance value.
          """

因为对于错误的转义序列,如 '\l',Python 会自动修正为 '\\l',不需要担心加 r 后会出现问题

image

当然,可能需要辨别下字符串里是否同时混用正确转义序列和错误转义序列,如 '\l\n' 会被认为 '\\l\n',如果直接加 r 是不对的,如果遇到这种情况需要手动修改为 '\\l\n'

image

此外一些 docstring 会用图来表示 fuse 的 pass,如

-     """
+     r"""
      x_var   f_var(persistable)
        \       /
           conv2d
             |
          conv2d_var    y_var(persistable)
              \          /
             elementwise_add
                  |
           elementwise_add_var
                  |
                 act
                  |
                act_var
      """

这些同样直接加 r 即可

@caolonghao
Copy link

具体地看了一下,除了忘记在代码中添加转义,还有这样与系统命令相关的地方也出现了同样的问题,这些地方该如何修改呢?

def get_all_uts(rootPath):
    all_uts_paddle = '%s/build/all_uts_paddle' % rootPath
    os.system(
        'cd %s/build && ctest -N -V | grep -Ei "Test[ \t]+#" | grep -oEi "\w+$" > %s'
        % (rootPath, all_uts_paddle))

@SigureMo
Copy link
Member Author

SigureMo commented Oct 5, 2022

这种情况其实就是上述的混用情况

当然,可能需要辨别下字符串里是否同时混用正确转义序列和错误转义序列,如 '\l\n' 会被认为 '\\l\n',如果直接加 r 是不对的,如果遇到这种情况需要手动修改为 '\\l\n'

这里 '\t' 是有效转义序列,而 '\w' 则不是,因此可将 '\w' 改为 '\\w' 可保证对现有代码没有任何影响

# 这在 Python runtime 没有任何影响
>>> 'cd %s/build && ctest -N -V | grep -Ei "Test[ \t]+#" | grep -oEi "\w+$" > %s' == \
... 'cd %s/build && ctest -N -V | grep -Ei "Test[ \t]+#" | grep -oEi "\\w+$" > %s'
True

不过在 grep 开启 -E 选项情况下(启用扩展正则),'\t''\\t' 应该是一样的效果,因此这里的话更建议在整个字符串上加 r 自动转义,但这是一个特殊情况,不是所有混用情况都可以这样处理的,其它情况需要根据具体情形进行处理

# 这在 Python runtime 会被认为是不同的字符串
>>> 'cd %s/build && ctest -N -V | grep -Ei "Test[ \t]+#" | grep -oEi "\w+$" > %s' == \
... r'cd %s/build && ctest -N -V | grep -Ei "Test[ \t]+#" | grep -oEi "\w+$" > %s'
False
# 但这对 grep 而言是等价的
# 需要预先创建 build 目录编译并 cd build
# test.py
import subprocess

if __name__ == "__main__":
    a = subprocess.run('ctest -N -V | grep -Ei "Test[ \t]+#"',
                       shell=True,
                       stdout=subprocess.PIPE)
    b = subprocess.run(r'ctest -N -V | grep -Ei "Test[ \t]+#"',
                       shell=True,
                       stdout=subprocess.PIPE)
    print(a.stdout == b.stdout)

# shell 执行以下命令
# python test.py
# 输出如下
# True

还有其他类似的 os.system 命令或者 subprocess 命令的情况吗?

@caolonghao
Copy link

另外就是在 ./tools/jetson_infer_op.pyset_diff_value() 函数中出现了,具体如下:

def set_diff_value(file, atol="1e-5", inplace_atol="1e-7"):
    """
    :param file: refer to op_test.py
    :param atol: refer to op_test.py
    :param inplace_atol:
    :return:
    """
    os.system("sed -i 's/self.check_output(/self\.check_output\(atol=" + atol +
              ",inplace_atol=" + inplace_atol + ",/g\' " + file)

但这里直接添加 r 进行转义在 Python Runtime 中被认为是相同字符串,所以应该不存在问题。其余可能产生疑问的我都在Python Runtime中进行了比较均为相同字符串,所以应该直接添加 r 进行转义即可。

@caolonghao
Copy link

另外就是在 python/paddle/fluid/tests 文件夹中全部都是形如

    r'''
    conv_input   conv_weight_var(persistable)
      \       /
         conv_op
          |
      conv_out_var
    '''

这样的更新,但是在 pre-commit 检查的 copy_checker 中,出现了下列报错,可能是什么原因呢?

Image

@SigureMo
Copy link
Member Author

SigureMo commented Oct 5, 2022

这应该是 Windows 上默认编码为 'gbk' 导致的问题,copyright_checker 是一个本地的 hook,源码见 tools/codestyle/copyright.hook,这里第 70 行没有指定编码方式,因此使用了默认的编码方式('gbk')来读取文件,导致解码失败

因此也许第 70 行需要修改成下面这样

-         with open(path) as f:
+         with open(path, encoding='utf-8') as f:

不过我不太清楚这个问题是否稳定复现,因此需要之后调研一下再确定是否要修改(因为 Paddle 应该也是有不少 Windows 开发者的,貌似也没有遇到这个问题?具体复现方式需要进一步确定下)

现在的话建议先在 yapf 等其余 hook 都运行过后直接 git commit -m 'xxx' --no-verify 跳过 pre-commit 的验证

@SigureMo
Copy link
Member Author

SigureMo commented Oct 5, 2022

我怀疑是修改包含中文的文件才会引发这样的问题?我在 PaddlePaddle/Paddle#46751 做了一下修复,这个 merge 后应该可以彻底解决问题

@caolonghao
Copy link

有道理,应该只有在windows下并且修改包含中文的文件才会出现这个问题。

@caolonghao caolonghao moved this from Todo to In Progress in Flake8 错误码修复 Oct 5, 2022
Repository owner moved this from In Progress to Done in Flake8 错误码修复 Oct 20, 2022
Repository owner moved this from Todo to Done in Flake8 少量存量,手动修复 Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

Successfully merging a pull request may close this issue.

2 participants