bugy / script-server

Web UI for your scripts with execution management
Other
1.59k stars 247 forks source link

SyntaxWarning: invalid escape sequence #759

Closed ClarkeAC closed 1 month ago

ClarkeAC commented 1 month ago

Running with python = 3.12 (And it's probably that python version greater than 3.12 will have this problem) will raise SyntaxWarning: invalid escape sequence. Does not affect use. But it doesn't seem to appear on every run.

/app # python launcher.py 
/app/src/migrations/migrate.py:138: SyntaxWarning: invalid escape sequence '\d'  match = re.fullmatch('(.+)_([^_]+)_((\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d))', filename)
/app/src/migrations/migrate.py:325: SyntaxWarning: invalid escape sequence '\s'  space_matches = re.findall('^\s+', old_content, flags=re.MULTILINE)
/app/src/execution/logging.py:292: SyntaxWarning: invalid escape sequence '\w'  match = re.fullmatch('([\w_]+):(.*\r?\n)', line)
/app/src/model/model_helper.py:36: SyntaxWarning: invalid escape sequence '\w'  pattern = re.escape(ENV_VAR_PREFIX) + '\w+'
/app/src/model/model_helper.py:227: SyntaxWarning: invalid escape sequence '\.'  return re.sub('^\.', '', extension).lower()
/app/src/utils/file_utils.py:230: SyntaxWarning: invalid escape sequence '\w'  file_name_regex = '([\w.-]|(\\\ ))*'
/app/src/execution/executor.py:117: SyntaxWarning: invalid escape sequence '\w'  value_pattern = '((?<!\w)|^)' + re.escape(element_string) + '((?!\w)|$)'
/app/src/execution/executor.py:117: SyntaxWarning: invalid escape sequence '\w'  value_pattern = '((?<!\w)|^)' + re.escape(element_string) + '((?!\w)|$)'
/app/src/config/script/list_values.py:57: SyntaxWarning: invalid escape sequence '\$'  pattern = re.compile('\${([^}]+)\}')
/app/src/model/template_property.py:15: SyntaxWarning: invalid escape sequence '\$'  pattern = re.compile('\${([^}]+)\}')
/app/src/features/file_download_feature.py:261: SyntaxWarning: invalid escape sequence '\d'  group_number_matches = re.findall('^#\d+#', output_pattern[regex_start:])
/app/src/features/file_download_feature.py:281: SyntaxWarning: invalid escape sequence '\W'  regex_pattern = '(([^\W\d_]:)|~)' + regex_pattern
/app/src/features/file_download_feature.py:283: SyntaxWarning: invalid escape sequence '\w'  regex_pattern = regex_pattern.replace('#any_path', '(' + separator + '([\w.\-]|(\\\ ))+)+')
/app/src/files/user_file_storage.py:53: SyntaxWarning: invalid escape sequence '\d'  if not re.match('\d+', timed_folder):
2024-08-15 03:30:05,627 [root.INFO] Starting Script Server, v1.18.0
2024-08-15 03:30:05,628 [migrations.INFO] Applying migration add_execution_info_to_log_files
2024-08-15 03:30:05,628 [migrations.INFO] Applying migration introduce_access_config
2024-08-15 03:30:05,628 [migrations.INFO] Applying migration migrate_bash_formatting_to_output_format
2024-08-15 03:30:05,629 [migrations.INFO] Applying migration add_user_id_to_log_files
2024-08-15 03:30:05,629 [migrations.INFO] Applying migration migrate_repeat_param_and_same_arg_param
2024-08-15 03:30:05,630 [migrations.INFO] Applying migration migrate_output_files_parameters_substitution
2024-08-15 03:30:05,635 [asyncio.DEBUG] Using selector: EpollSelector
Server is running on: http://0.0.0.0:5000

I think it should be caused by this.

Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the result. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences only recognized in string literals fall into the category of unrecognized escapes for bytes literals.

Changed in version 3.6: Unrecognized escape sequences produce a DeprecationWarning. Changed in version 3.12: Unrecognized escape sequences produce a SyntaxWarning. In a future Python version they will be eventually a SyntaxError.

Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.

So maybe adding r to the regular expression section would work? e.g.

# /app/src/migrations/migrate.py:138
# before
match = re.fullmatch('(.+)_([^_]+)_((\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d))', filename) 
# after
match = re.fullmatch(r'(.+)_([^_]+)_((\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d))', filename)

Will this cause unintended errors? For example a low version of python (e.g. py36), or regex works not as expected?

bugy commented 1 month ago

Hi @ClarkeAC thanks for reporting. I think r prefix should work

ClarkeAC commented 1 month ago

Thanks for your reply @bugy , I tested it again and there were 11 more warnings, so I made changes to all of them. Here is the pull request #760.

ClarkeAC commented 1 month ago

@bugy I'm very sorry to bother you, I accidentally used my work email address and when I commit earlier, is it possible to change the commit record? I think it may require the following command

git rebase -i 8c6818545e8a6342e2688893f38bd6d92da3b5a6^
# edit 8c68185 fixed invalid escape sequence error, 11 more
git commit --amend --author="ClarkeAC <120437484+ClarkeAC@users.noreply.github.com>"
git rebase --continue
git push --force

Please let me know if you’re comfortable with this or if there’s any other process I should follow.

bugy commented 1 month ago

Hi @ClarkeAC done

(I used wrong commit hash, because I'm blind and couldn't find commit hash specified by you, and deleted my yesterday commit)

ClarkeAC commented 1 month ago

Thank you!