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
bc95973b
Kaydet (Commit)
bc95973b
authored
Eki 08, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fix handling on negative numbers in ast.literal_eval().
üst
4f3abb0f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
8 deletions
+20
-8
ast.py
Lib/ast.py
+13
-7
test_ast.py
Lib/test/test_ast.py
+3
-1
NEWS
Misc/NEWS
+4
-0
No files found.
Lib/ast.py
Dosyayı görüntüle @
bc95973b
...
...
@@ -65,19 +65,25 @@ def literal_eval(node_or_string):
elif
isinstance
(
node
,
Name
):
if
node
.
id
in
_safe_names
:
return
_safe_names
[
node
.
id
]
elif
isinstance
(
node
,
UnaryOp
)
and
\
isinstance
(
node
.
op
,
(
UAdd
,
USub
))
and
\
isinstance
(
node
.
operand
,
(
Num
,
UnaryOp
,
BinOp
)):
operand
=
_convert
(
node
.
operand
)
if
isinstance
(
node
.
op
,
UAdd
):
return
+
operand
else
:
return
-
operand
elif
isinstance
(
node
,
BinOp
)
and
\
isinstance
(
node
.
op
,
(
Add
,
Sub
))
and
\
isinstance
(
node
.
right
,
Num
)
and
\
isinstance
(
node
.
right
.
n
,
complex
)
and
\
isinstance
(
node
.
left
,
Num
)
and
\
isinstance
(
node
.
left
.
n
,
(
int
,
float
)):
left
=
node
.
left
.
n
right
=
node
.
right
.
n
isinstance
(
node
.
right
,
(
Num
,
UnaryOp
,
BinOp
))
and
\
isinstance
(
node
.
left
,
(
Num
,
UnaryOp
,
BinOp
)):
left
=
_convert
(
node
.
left
)
right
=
_convert
(
node
.
right
)
if
isinstance
(
node
.
op
,
Add
):
return
left
+
right
else
:
return
left
-
right
raise
ValueError
(
'malformed
string'
)
raise
ValueError
(
'malformed
node or string: '
+
repr
(
node
)
)
return
_convert
(
node_or_string
)
...
...
Lib/test/test_ast.py
Dosyayı görüntüle @
bc95973b
...
...
@@ -288,12 +288,14 @@ class ASTHelpers_Test(unittest.TestCase):
self
.
assertEqual
(
ast
.
literal_eval
(
'{1, 2, 3}'
),
{
1
,
2
,
3
})
self
.
assertEqual
(
ast
.
literal_eval
(
'b"hi"'
),
b
"hi"
)
self
.
assertRaises
(
ValueError
,
ast
.
literal_eval
,
'foo()'
)
self
.
assertEqual
(
ast
.
literal_eval
(
'-6'
),
-
6
)
self
.
assertEqual
(
ast
.
literal_eval
(
'-6j+3'
),
3
-
6
j
)
self
.
assertEqual
(
ast
.
literal_eval
(
'3.25'
),
3.25
)
def
test_literal_eval_issue4907
(
self
):
self
.
assertEqual
(
ast
.
literal_eval
(
'2j'
),
2
j
)
self
.
assertEqual
(
ast
.
literal_eval
(
'10 + 2j'
),
10
+
2
j
)
self
.
assertEqual
(
ast
.
literal_eval
(
'1.5 - 2j'
),
1.5
-
2
j
)
self
.
assertRaises
(
ValueError
,
ast
.
literal_eval
,
'2 + (3 + 4j)'
)
def
test_main
():
...
...
Misc/NEWS
Dosyayı görüntüle @
bc95973b
...
...
@@ -10,6 +10,10 @@ What's New in Python 3.2 Alpha 3?
Core and Builtins
-----------------
- ast.literal_eval() can now handle negative numbers. It is also a little
more liberal in what it accepts without compromising the safety of the
evaluation. For example, 3j+4 and 3+4+5 are both accepted.
- Issue #10006: type.__abstractmethods__ now raises an AttributeError. As a
result metaclasses can now be ABCs (see #9533).
...
...
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