Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
698280df
Kaydet (Commit)
698280df
authored
Eyl 10, 2008
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #3756: make re.escape() handle bytes as well as str.
Patch by Andrew McNamara, reviewed and tweaked by myself.
üst
92f8f3e0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
15 deletions
+47
-15
re.py
Lib/re.py
+31
-15
test_re.py
Lib/test/test_re.py
+14
-0
NEWS
Misc/NEWS
+2
-0
No files found.
Lib/re.py
Dosyayı görüntüle @
698280df
...
@@ -211,23 +211,38 @@ def template(pattern, flags=0):
...
@@ -211,23 +211,38 @@ def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object"
"Compile a template pattern, returning a pattern object"
return
_compile
(
pattern
,
flags
|
T
)
return
_compile
(
pattern
,
flags
|
T
)
_alphanum
=
{}
_alphanum
_str
=
frozenset
(
for
c
in
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890'
:
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
)
_alphanum
[
c
]
=
1
_alphanum_bytes
=
frozenset
(
del
c
b
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
)
def
escape
(
pattern
):
def
escape
(
pattern
):
"Escape all non-alphanumeric characters in pattern."
"Escape all non-alphanumeric characters in pattern."
s
=
list
(
pattern
)
if
isinstance
(
pattern
,
str
):
alphanum
=
_alphanum
alphanum
=
_alphanum_str
for
i
in
range
(
len
(
pattern
)):
s
=
list
(
pattern
)
c
=
pattern
[
i
]
for
i
in
range
(
len
(
pattern
)):
if
c
not
in
alphanum
:
c
=
pattern
[
i
]
if
c
==
"
\000
"
:
if
c
not
in
alphanum
:
s
[
i
]
=
"
\\
000"
if
c
==
"
\000
"
:
s
[
i
]
=
"
\\
000"
else
:
s
[
i
]
=
"
\\
"
+
c
return
""
.
join
(
s
)
else
:
alphanum
=
_alphanum_bytes
s
=
[]
esc
=
ord
(
b
"
\\
"
)
for
c
in
pattern
:
if
c
in
alphanum
:
s
.
append
(
c
)
else
:
else
:
s
[
i
]
=
"
\\
"
+
c
if
c
==
0
:
return
pattern
[:
0
]
.
join
(
s
)
s
.
extend
(
b
"
\\
000"
)
else
:
s
.
append
(
esc
)
s
.
append
(
c
)
return
bytes
(
s
)
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# internals
# internals
...
@@ -248,7 +263,8 @@ def _compile(*key):
...
@@ -248,7 +263,8 @@ def _compile(*key):
pattern
,
flags
=
key
pattern
,
flags
=
key
if
isinstance
(
pattern
,
_pattern_type
):
if
isinstance
(
pattern
,
_pattern_type
):
if
flags
:
if
flags
:
raise
ValueError
(
'Cannot process flags argument with a compiled pattern'
)
raise
ValueError
(
"Cannot process flags argument with a compiled pattern"
)
return
pattern
return
pattern
if
not
sre_compile
.
isstring
(
pattern
):
if
not
sre_compile
.
isstring
(
pattern
):
raise
TypeError
(
"first argument must be string or compiled pattern"
)
raise
TypeError
(
"first argument must be string or compiled pattern"
)
...
@@ -325,7 +341,7 @@ class Scanner:
...
@@ -325,7 +341,7 @@ class Scanner:
if
i
==
j
:
if
i
==
j
:
break
break
action
=
self
.
lexicon
[
m
.
lastindex
-
1
][
1
]
action
=
self
.
lexicon
[
m
.
lastindex
-
1
][
1
]
if
hasattr
(
action
,
'__call__'
):
if
hasattr
(
action
,
"__call__"
):
self
.
match
=
m
self
.
match
=
m
action
=
action
(
self
,
m
.
group
())
action
=
action
(
self
,
m
.
group
())
if
action
is
not
None
:
if
action
is
not
None
:
...
...
Lib/test/test_re.py
Dosyayı görüntüle @
698280df
...
@@ -416,6 +416,7 @@ class ReTests(unittest.TestCase):
...
@@ -416,6 +416,7 @@ class ReTests(unittest.TestCase):
def
test_re_escape
(
self
):
def
test_re_escape
(
self
):
p
=
""
p
=
""
self
.
assertEqual
(
re
.
escape
(
p
),
p
)
for
i
in
range
(
0
,
256
):
for
i
in
range
(
0
,
256
):
p
=
p
+
chr
(
i
)
p
=
p
+
chr
(
i
)
self
.
assertEqual
(
re
.
match
(
re
.
escape
(
chr
(
i
)),
chr
(
i
))
is
not
None
,
self
.
assertEqual
(
re
.
match
(
re
.
escape
(
chr
(
i
)),
chr
(
i
))
is
not
None
,
...
@@ -426,6 +427,19 @@ class ReTests(unittest.TestCase):
...
@@ -426,6 +427,19 @@ class ReTests(unittest.TestCase):
self
.
assertEqual
(
pat
.
match
(
p
)
is
not
None
,
True
)
self
.
assertEqual
(
pat
.
match
(
p
)
is
not
None
,
True
)
self
.
assertEqual
(
pat
.
match
(
p
)
.
span
(),
(
0
,
256
))
self
.
assertEqual
(
pat
.
match
(
p
)
.
span
(),
(
0
,
256
))
def
test_re_escape_byte
(
self
):
p
=
b
""
self
.
assertEqual
(
re
.
escape
(
p
),
p
)
for
i
in
range
(
0
,
256
):
b
=
bytes
([
i
])
p
+=
b
self
.
assertEqual
(
re
.
match
(
re
.
escape
(
b
),
b
)
is
not
None
,
True
)
self
.
assertEqual
(
re
.
match
(
re
.
escape
(
b
),
b
)
.
span
(),
(
0
,
1
))
pat
=
re
.
compile
(
re
.
escape
(
p
))
self
.
assertEqual
(
pat
.
match
(
p
)
is
not
None
,
True
)
self
.
assertEqual
(
pat
.
match
(
p
)
.
span
(),
(
0
,
256
))
def
pickle_test
(
self
,
pickle
):
def
pickle_test
(
self
,
pickle
):
oldpat
=
re
.
compile
(
'a(?:b|(c|e){1,2}?|d)+?(.)'
)
oldpat
=
re
.
compile
(
'a(?:b|(c|e){1,2}?|d)+?(.)'
)
s
=
pickle
.
dumps
(
oldpat
)
s
=
pickle
.
dumps
(
oldpat
)
...
...
Misc/NEWS
Dosyayı görüntüle @
698280df
...
@@ -96,6 +96,8 @@ C API
...
@@ -96,6 +96,8 @@ C API
Library
Library
-------
-------
- Issue #3756: make re.escape() handle bytes as well as str.
- Issue #3800: fix filter() related bug in formatter.py.
- Issue #3800: fix filter() related bug in formatter.py.
- Issue #874900: fix behaviour of threading module after a fork.
- Issue #874900: fix behaviour of threading module after a fork.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment