Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
A
astor
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
astor
Commits
c7297cde
Kaydet (Commit)
c7297cde
authored
Nis 22, 2017
tarafından
Patrick Maupin
Kaydeden (comit)
GitHub
Nis 22, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Merge pull request #67 from berkerpeksag/unicode_literals
Add support for __future__ unicode_literals
üst
31fe3980
b9da032a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
2 deletions
+20
-2
code_gen.py
astor/code_gen.py
+8
-1
string_repr.py
astor/string_repr.py
+4
-1
test_code_gen.py
tests/test_code_gen.py
+8
-0
No files found.
astor/code_gen.py
Dosyayı görüntüle @
c7297cde
...
@@ -125,6 +125,8 @@ class SourceGenerator(ExplicitNodeVisitor):
...
@@ -125,6 +125,8 @@ class SourceGenerator(ExplicitNodeVisitor):
"""
"""
using_unicode_literals
=
False
def
__init__
(
self
,
indent_with
,
add_line_information
=
False
,
def
__init__
(
self
,
indent_with
,
add_line_information
=
False
,
pretty_string
=
pretty_string
):
pretty_string
=
pretty_string
):
self
.
result
=
[]
self
.
result
=
[]
...
@@ -264,6 +266,10 @@ class SourceGenerator(ExplicitNodeVisitor):
...
@@ -264,6 +266,10 @@ class SourceGenerator(ExplicitNodeVisitor):
self
.
statement
(
node
,
'from '
,
node
.
level
*
'.'
,
self
.
statement
(
node
,
'from '
,
node
.
level
*
'.'
,
node
.
module
or
''
,
' import '
)
node
.
module
or
''
,
' import '
)
self
.
comma_list
(
node
.
names
)
self
.
comma_list
(
node
.
names
)
# Goofy stuff for Python 2.7 _pyio module
if
node
.
module
==
'__future__'
and
'unicode_literals'
in
(
x
.
name
for
x
in
node
.
names
):
self
.
using_unicode_literals
=
True
def
visit_Import
(
self
,
node
):
def
visit_Import
(
self
,
node
):
self
.
statement
(
node
,
'import '
)
self
.
statement
(
node
,
'import '
)
...
@@ -497,7 +503,8 @@ class SourceGenerator(ExplicitNodeVisitor):
...
@@ -497,7 +503,8 @@ class SourceGenerator(ExplicitNodeVisitor):
# Cheesy way to force a flush
# Cheesy way to force a flush
self
.
write
(
'foo'
)
self
.
write
(
'foo'
)
result
.
pop
()
result
.
pop
()
result
.
append
(
self
.
pretty_string
(
node
.
s
,
embedded
,
result
))
result
.
append
(
self
.
pretty_string
(
node
.
s
,
embedded
,
result
,
uni_lit
=
self
.
using_unicode_literals
))
def
visit_JoinedStr
(
self
,
node
,
def
visit_JoinedStr
(
self
,
node
,
# constants
# constants
...
...
astor/string_repr.py
Dosyayı görüntüle @
c7297cde
...
@@ -76,7 +76,8 @@ def _prep_triple_quotes(s, mysplit=mysplit, replacements=replacements):
...
@@ -76,7 +76,8 @@ def _prep_triple_quotes(s, mysplit=mysplit, replacements=replacements):
return
''
.
join
(
s
)
return
''
.
join
(
s
)
def
pretty_string
(
s
,
embedded
,
current_output
,
min_trip_str
=
20
,
max_line
=
100
):
def
pretty_string
(
s
,
embedded
,
current_output
,
min_trip_str
=
20
,
max_line
=
100
,
uni_lit
=
False
):
"""There are a lot of reasons why we might not want to or
"""There are a lot of reasons why we might not want to or
be able to return a triple-quoted string. We can always
be able to return a triple-quoted string. We can always
punt back to the default normal string.
punt back to the default normal string.
...
@@ -87,6 +88,8 @@ def pretty_string(s, embedded, current_output, min_trip_str=20, max_line=100):
...
@@ -87,6 +88,8 @@ def pretty_string(s, embedded, current_output, min_trip_str=20, max_line=100):
# Punt on abnormal strings
# Punt on abnormal strings
if
(
isinstance
(
s
,
special_unicode
)
or
not
isinstance
(
s
,
basestring
)):
if
(
isinstance
(
s
,
special_unicode
)
or
not
isinstance
(
s
,
basestring
)):
return
default
return
default
if
uni_lit
and
isinstance
(
s
,
bytes
):
return
'b'
+
default
len_s
=
len
(
default
)
len_s
=
len
(
default
)
current_line
=
_get_line
(
current_output
)
current_line
=
_get_line
(
current_output
)
...
...
tests/test_code_gen.py
Dosyayı görüntüle @
c7297cde
...
@@ -417,6 +417,14 @@ class CodegenTestCase(unittest.TestCase):
...
@@ -417,6 +417,14 @@ class CodegenTestCase(unittest.TestCase):
ast2
=
astor
.
parsefile
(
__file__
)
ast2
=
astor
.
parsefile
(
__file__
)
self
.
assertEqual
(
astor
.
to_source
(
ast1
),
astor
.
codegen
.
to_source
(
ast2
))
self
.
assertEqual
(
astor
.
to_source
(
ast1
),
astor
.
codegen
.
to_source
(
ast2
))
def
test_unicode_literals
(
self
):
source
=
"""
from __future__ import (print_function, unicode_literals)
x = b'abc'
y = u'abc'
"""
self
.
assertAstEqual
(
source
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
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