Benjamin-Loison / cpython

The Python programming language
https://www.python.org/
Other
0 stars 0 forks source link

Verify all occurrences `re.match` in my codes as very likely wanted `re.fullmatch` instead #43

Closed Benjamin-Loison closed 19 hours ago

Benjamin-Loison commented 1 day ago

On my phone, my laptop, my computer, LemnosLife VPS and OverClock3000.

https://docs.python.org/3.13/library/re.html#re.fullmatch

help(re.fullmatch)
Output: ``` Help on function fullmatch in module re: fullmatch(pattern, string, flags=0) Try to apply the pattern to all of the string, returning a Match object, or None if no match was found. ```

https://docs.python.org/3.13/library/re.html#re.match

help(re.match)
Output: ``` Help on function match in module re: match(pattern, string, flags=0) Try to apply the pattern at the start of the string, returning a Match object, or None if no match was found. ```

The only difference is at the start and to all of the string.

Pyzo states:

Python 3.12.3 (main, Sep 11 2024, 14:17:37) on linux (64 bits).

on my Linux Mint 22 Cinnamon Framework 13.

import re

re.match('\\d', '1 dollars')
<re.Match object; span=(0, 1), match='1'>
re.fullmatch('\\d', '1 dollars') is None
True

In my head as far as I remember there were re.match and re.findall/re.search, so by default all re.match should be replaced with re.fullmatch.

Quickly verifying for each occurrence if the replacement makes sense would be nice.

Benjamin-Loison commented 1 day ago

On my Linux Mint 22 Cinnamon Framework 13:

clear && time sudo grep -rn '\<match(' / --include '*.py' --color=always --exclude-dir={usr,lib,.local,.cache,.cargo,.sage,bitcoin-master,bitcoin,inaSpeechSegmenter,git,woob,yt-dlp,libraries,proc}
...
real    0m3.997s
user    0m0.006s
sys 0m0.008s
Benjamin-Loison commented 1 day ago
git commit -a
Replace `re.match` with `re.fullmatch`

See [Benjamin-Loison/cpython/issues/43](https://github.com/Benjamin-Loison/cpython/issues/43).

Would be nice to commit using the clipboard, see Benjamin_Loison/git/issues/84.

Should verify that only commit such replacements, not pending changes containing secrets for instance.

Benjamin-Loison commented 23 hours ago
# Should maybe be `re.fullmatch`, see [Benjamin-Loison/cpython/issues/43](https://github.com/Benjamin-Loison/cpython/issues/43).
Benjamin-Loison commented 20 hours ago

BFMTV_news_ticker_retrieval/src/commit/d6a54eab66dcceb44b7e44edcfc940ecd2ec5e12/main.py#L94

re.match('tes', 'test')
<re.Match object; span=(0, 3), match='tes'>
re.match('^tes$', 'test') is None
True
Benjamin-Loison commented 20 hours ago

On my Fairphone 4 Termux:

clear && time grep -rn '\<match(' ~ --include '*.py' --color=always --exclude-dir={.cargo,yt-dlp,woob,android_vendor_lineage,android_frameworks_base,google-api-python-client,matrix-commander,LibreTranslate}
...
real    0m0.777s
user    0m0.179s
sys 0m0.591s
Benjamin-Loison commented 19 hours ago

On Debian 12 GNOME Pegasus:

clear && time sudo grep -rn '\<match(' / --include '*.py' --color=always --exclude-dir={usr,lib,media,Mess,Mumble,GitHub,platform-tools_r29.0.6-windows,volatility-2.6,yt-dlp,Libs,volatility3-1.0.0,impacket,TTS-master,NBT-master,git,ic,bitcoin,android-studio,bitcoin-master,.cargo,tacotron-master,TTS-256ed6307ce04b1ccbe7c3591bdb6b564f000cf1,Homki}
...
real    0m2.827s
user    0m0.007s
sys 0m0.005s
Benjamin-Loison commented 19 hours ago

On OverClock3000:

clear && time sudo grep -rn '\<match(' / --include '*.py' --color=always --exclude-dir={var,opt,venv,.cargo,synapse,.local,.rustup,lib,.cache,node_modules,vbox,rustdesk,vcpkg,usr}
...
real    0m2.584s
user    0m1.050s
sys 0m1.381s
Benjamin-Loison commented 19 hours ago

On LemnosLife VPS:

clear && time sudo grep -rn '\<match(' / --include '*.py' --color=always --exclude-dir={usr,.local,venv}
...
real    0m0.792s
user    0m0.231s
sys 0m0.330s