Skip to content

Commit 71f8ff2

Browse files
committed
Now String is mapped to PyUnicode on ClassicPyObjectAdapter (instead of mapping it to PyString). Thus, for APIs which really need to return bytestrings (such as PyFile) we now have to explicitely declare the return type as PyString (and on code written in Python, use StringUtil.asPyString(String)). Fixes #1128
1 parent 796be6c commit 71f8ff2

21 files changed

Lines changed: 131 additions & 105 deletions

Lib/javapath.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from java.lang import System
2121
import os
2222

23+
from org.python.core.util.StringUtil import asPyString
24+
2325

2426
def _tostr(s, method):
2527
if isinstance(s, basestring):
@@ -40,7 +42,7 @@ def _type_name(obj):
4042
def dirname(path):
4143
"""Return the directory component of a pathname"""
4244
path = _tostr(path, "dirname")
43-
result = File(path).getParent()
45+
result = asPyString(File(path).getParent())
4446
if not result:
4547
if isabs(path):
4648
result = path # Must be root
@@ -51,7 +53,7 @@ def dirname(path):
5153
def basename(path):
5254
"""Return the final component of a pathname"""
5355
path = _tostr(path, "basename")
54-
return File(path).getName()
56+
return asPyString(File(path).getName())
5557

5658
def split(path):
5759
"""Split a pathname.
@@ -128,7 +130,7 @@ def join(path, *args):
128130
if a == "":
129131
a = os.sep
130132
f = File(f, a)
131-
return f.getPath()
133+
return asPyString(f.getPath())
132134

133135
def normcase(path):
134136
"""Normalize case of pathname.
@@ -137,7 +139,7 @@ def normcase(path):
137139
138140
"""
139141
path = _tostr(path, "normcase")
140-
return File(path).getPath()
142+
return asPyString(File(path).getPath())
141143

142144
def commonprefix(m):
143145
"Given a list of pathnames, return the longest common leading component"
@@ -197,7 +199,7 @@ def expanduser(path):
197199
if not c:
198200
return gethome()
199201
if c == os.sep:
200-
return File(gethome(), path[2:]).getPath()
202+
return asPyString(File(gethome(), path[2:]).getPath())
201203
return path
202204

203205
def getuser():
@@ -252,7 +254,7 @@ def abspath(path):
252254
def _abspath(path):
253255
# Must use normpath separately because getAbsolutePath doesn't normalize
254256
# and getCanonicalPath would eliminate symlinks.
255-
return normpath(File(sys.getPath(path)).getAbsolutePath())
257+
return normpath(asPyString(File(sys.getPath(path)).getAbsolutePath()))
256258

257259
def realpath(path):
258260
"""Return an absolute path normalized and symbolic links eliminated"""
@@ -261,7 +263,7 @@ def realpath(path):
261263

262264
def _realpath(path):
263265
try:
264-
return File(sys.getPath(path)).getCanonicalPath()
266+
return asPyString(File(sys.getPath(path)).getCanonicalPath())
265267
except java.io.IOException:
266268
return _abspath(path)
267269

Lib/os.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from java.io import File
4949
from org.python.core import PyFile
5050
from org.python.core.io import FileDescriptors, FileIO, IOBase
51+
from org.python.core.util.StringUtil import asPyString
5152

5253
# Mapping of: os._name: [name list, shell command list]
5354
_os_map = dict(nt=[
@@ -264,7 +265,7 @@ def listdir(path):
264265
l = File(sys.getPath(path)).list()
265266
if l is None:
266267
raise OSError(0, 'No such directory', path)
267-
return list(l)
268+
return [asPyString(entry) for entry in l]
268269

269270
def chmod(path, mode):
270271
"""chmod(path, mode)
@@ -629,7 +630,7 @@ def read(fd, buffersize):
629630
from org.python.core.util import StringUtil
630631
rawio = FileDescriptors.get(fd)
631632
buf = _handle_oserror(rawio.read, buffersize)
632-
return str(StringUtil.fromBytes(buf))
633+
return asPyString(StringUtil.fromBytes(buf))
633634

634635
def write(fd, string):
635636
"""write(fd, string) -> byteswritten

Lib/zlib.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import jarray, binascii
22

33
from java.util.zip import Adler32, Deflater, Inflater
4-
from java.lang import Long, String, StringBuffer
4+
from java.lang import Long, String
5+
6+
from cStringIO import StringIO
57

68
class error(Exception):
79
pass
@@ -132,18 +134,19 @@ def flush(self, length=None):
132134

133135
def _get_deflate_data(deflater):
134136
buf = jarray.zeros(1024, 'b')
135-
sb = StringBuffer()
137+
s = StringIO()
136138
while not deflater.finished():
137139
l = deflater.deflate(buf)
138140
if l == 0:
139141
break
140-
sb.append(String(buf, 0, 0, l))
141-
return sb.toString()
142+
s.write(String(buf, 0, 0, l))
143+
s.seek(0)
144+
return s.read()
142145

143146

144147
def _get_inflate_data(inflater, max_length=0):
145148
buf = jarray.zeros(1024, 'b')
146-
sb = StringBuffer()
149+
s = StringIO()
147150
total = 0
148151
while not inflater.finished():
149152
if max_length:
@@ -154,7 +157,8 @@ def _get_inflate_data(inflater, max_length=0):
154157
break
155158

156159
total += l
157-
sb.append(String(buf, 0, 0, l))
160+
s.write(String(buf, 0, 0, l))
158161
if max_length and total == max_length:
159162
break
160-
return sb.toString()
163+
s.seek(0)
164+
return s.read()

src/com/ziclix/python/sql/DataHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
177177

178178
case Types.LONGVARCHAR:
179179
if (object instanceof PyFile) {
180-
object = new PyString(((PyFile) object).read());
180+
object = ((PyFile) object).read();
181181
}
182182

183183
String varchar = (String) object.__tojava__(String.class);
@@ -192,7 +192,7 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
192192

193193
default :
194194
if (object instanceof PyFile) {
195-
object = new PyString(((PyFile) object).read());
195+
object = ((PyFile) object).read();
196196
}
197197

198198
stmt.setObject(index, object.__tojava__(Object.class), type);

src/com/ziclix/python/sql/JDBC20DataHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
6262

6363
case Types.CLOB:
6464
if (object instanceof PyFile) {
65-
object = new PyString(((PyFile) object).read());
65+
object = ((PyFile) object).read();
6666
}
6767

6868
String clob = (String) object.__tojava__(String.class);

src/com/ziclix/python/sql/Jython22DataHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
163163

164164
case Types.LONGVARCHAR:
165165
if (object instanceof PyFile) {
166-
object = new PyString(((PyFile) object).read());
166+
object = ((PyFile) object).read();
167167
}
168168

169169
String varchar = (String) object.__tojava__(String.class);
@@ -178,7 +178,7 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
178178

179179
default :
180180
if (object instanceof PyFile) {
181-
object = new PyString(((PyFile) object).read());
181+
object = ((PyFile) object).read();
182182
}
183183

184184
stmt.setObject(index, object.__tojava__(Object.class), type);

src/com/ziclix/python/sql/handler/MySQLDataHandler.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
5454
switch (type) {
5555

5656
case Types.LONGVARCHAR:
57-
String varchar;
57+
// XXX: Only works with ASCII data!
58+
byte[] bytes;
5859
if (object instanceof PyFile) {
59-
varchar = ((PyFile) object).read();
60+
bytes = ((PyFile) object).read().toBytes();
6061
} else {
61-
varchar = (String) object.__tojava__(String.class);
62+
String varchar = (String) object.__tojava__(String.class);
63+
bytes = StringUtil.toBytes(varchar);
6264
}
63-
InputStream stream = new ByteArrayInputStream(StringUtil.toBytes(varchar));
65+
InputStream stream = new ByteArrayInputStream(bytes);
6466

6567
stream = new BufferedInputStream(stream);
6668

67-
stmt.setAsciiStream(index, stream, varchar.length());
69+
stmt.setAsciiStream(index, stream, bytes.length);
6870
break;
6971

7072
default :

src/com/ziclix/python/sql/handler/PostgresqlDataHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, in
111111
String varchar;
112112
// Postgresql driver can't handle the setCharacterStream() method so use setObject() instead
113113
if (object instanceof PyFile) {
114-
varchar = ((PyFile) object).read();
114+
varchar = ((PyFile) object).read().asString();
115115
} else {
116116
varchar = (String) object.__tojava__(String.class);
117117
}

src/org/python/core/PyFile.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,16 @@ private String parseMode(String mode) {
286286
}
287287

288288
@ExposedMethod(defaults = {"-1"})
289-
final synchronized String file_read(int n) {
289+
final synchronized PyString file_read(int n) {
290290
checkClosed();
291-
return file.read(n);
291+
return new PyString(file.read(n));
292292
}
293293

294-
public String read(int n) {
294+
public PyString read(int n) {
295295
return file_read(n);
296296
}
297297

298-
public String read() {
298+
public PyString read() {
299299
return file_read(-1);
300300
}
301301

@@ -310,16 +310,16 @@ public int readinto(PyObject buf) {
310310
}
311311

312312
@ExposedMethod(defaults = {"-1"})
313-
final synchronized String file_readline(int max) {
313+
final synchronized PyString file_readline(int max) {
314314
checkClosed();
315-
return file.readline(max);
315+
return new PyString(file.readline(max));
316316
}
317317

318-
public String readline(int max) {
318+
public PyString readline(int max) {
319319
return file_readline(max);
320320
}
321321

322-
public String readline() {
322+
public PyString readline() {
323323
return file_readline(-1);
324324
}
325325

src/org/python/core/PyFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ static final PyObject function___new__(PyNewWrapper new_, boolean init, PyType s
143143
}
144144

145145
@ExposedGet(name = "func_name")
146-
public String getFuncName() {
147-
return __name__;
146+
public PyString getFuncName() {
147+
return new PyString(__name__);
148148
}
149149

150150
@ExposedSet(name = "func_name")
151-
public void setFuncName(String func_name) {
152-
__name__ = func_name;
151+
public void setFuncName(PyString func_name) {
152+
__name__ = func_name.asString();
153153
}
154154

155155
@ExposedGet(name = "func_doc")

0 commit comments

Comments
 (0)