Skip to content

Commit 0052b6a

Browse files
committed
fix utf8 issue,asuming input as utf8 encoding,make all string io as unicode
1 parent b803756 commit 0052b6a

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

bin/j2py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ This is all very ordinary. We import the package bits, open and read
66
a file, translate it, and write it out.
77
88
"""
9+
from __future__ import unicode_literals
910
import sys
1011
from argparse import ArgumentParser, ArgumentTypeError
1112
from collections import defaultdict
1213
from logging import _levelNames as logLevels, exception, warning, info, basicConfig
1314
from os import path, makedirs
1415
from time import time
16+
from io import open
1517

1618
from java2python.compiler import Module, buildAST, transformAST
1719
from java2python.config import Config
@@ -107,7 +109,7 @@ def runTransform(options):
107109

108110
try:
109111
if filein != '-':
110-
source = open(filein).read()
112+
source = open(filein, encoding='utf-8').read()
111113
else:
112114
source = sys.stdin.read()
113115
except (IOError, ), exc:
@@ -157,9 +159,10 @@ def runTransform(options):
157159
if fileout == filedefault:
158160
output = sys.stdout
159161
else:
160-
output = open(fileout, 'w')
162+
output = open(fileout, 'w',encoding='utf-8')
161163
module.name = path.splitext(filein)[0] if filein != '-' else '<stdin>'
162-
print >> output, source
164+
# print >> output, source will raise TypeError: must be unicode, not str
165+
output.write(source)
163166

164167
if not options.skipcompile:
165168
try:

java2python/compiler/template.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# the compiler subpackage into multiple modules. So-called patterns
1313
# are usually a sign of a bad design and/or language limitations, and
1414
# this case is no exception.
15-
16-
from cStringIO import StringIO
15+
from __future__ import unicode_literals
16+
from StringIO import StringIO
1717
from functools import partial
1818
from itertools import chain, ifilter, imap
1919

@@ -73,7 +73,6 @@ def __init__(cls, name, bases, namespace):
7373
except (AttributeError, ):
7474
pass
7575

76-
7776
class Base(object):
7877
""" Base -> base class for formatting Python output.
7978
@@ -141,6 +140,9 @@ def __repr__(self):
141140
return ' '.join(parts)
142141

143142
def __str__(self):
143+
return unicode(self).encode('utf-8')
144+
145+
def __unicode__(self):
144146
""" Returns the Python source code representation of this template. """
145147
handlers = self.configHandlers('Output')
146148
return reduce(lambda v, func:func(self, v), handlers, self.dumps(-1))
@@ -320,7 +322,7 @@ def __repr__(self):
320322
parts.append(colors.white('tail:') + colors.black(self.tail))
321323
return ' '.join(parts)
322324

323-
def __str__(self):
325+
def __unicode__(self):
324326
""" Returns the Python source code representation of this template. """
325327
return self.fs.format(left=self.left, right=self.right) + self.tail
326328

java2python/lang/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
# instances.
4848
#
4949

50-
from cStringIO import StringIO
50+
from StringIO import StringIO
5151

5252
from antlr3 import ANTLRStringStream as StringStream, CommonTokenStream as TokenStream
5353
from antlr3.tree import CommonTreeAdaptor, CommonTree

java2python/lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
# java2python.lib -> common library bits.
4-
4+
from __future__ import unicode_literals
55
from functools import partial
66

77

0 commit comments

Comments
 (0)