Author: Armin Rigo <[email protected]>
Branch:
Changeset: r88506:de25d7d491be
Date: 2016-11-21 09:44 +0100
http://bitbucket.org/pypy/pypy/changeset/de25d7d491be/
Log: Issue #2425 bis: call encode() when we don't have the lock acquired
diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py
--- a/pypy/module/_file/interp_file.py
+++ b/pypy/module/_file/interp_file.py
@@ -303,27 +303,7 @@
size = space.r_longlong_w(w_size)
stream.truncate(size)
- def direct_write(self, w_data):
- space = self.space
- self.check_writable()
- if self.binary:
- data = space.getarg_w('s*', w_data).as_str()
- else:
- if space.isinstance_w(w_data, space.w_unicode):
- if self.encoding:
- w_encoding = space.wrap(self.encoding)
- else:
- w_encoding = getdefaultencoding(space)
- if self.errors:
- w_errors = space.wrap(self.errors)
- else:
- w_errors = space.wrap("strict")
- w_data = space.call_method(w_data, "encode",
- w_encoding, w_errors)
- data = space.charbuf_w(w_data)
- self.do_direct_write(data)
-
- def do_direct_write(self, data):
+ def direct_write_str(self, data):
self.softspace = 0
self.getstream().write(data)
@@ -353,7 +333,7 @@
_exposed_method_names = []
def _decl(class_scope, name, docstring,
- wrapresult="space.wrap(result)"):
+ wrapresult="space.wrap(result)", exposed=True):
# hack hack to build a wrapper around the direct_xxx methods.
# The wrapper adds lock/unlock calls and a space.wrap() on
# the result, conversion of stream errors to OperationErrors,
@@ -393,7 +373,8 @@
exec str(src) in globals(), class_scope
if unwrap_spec is not None:
class_scope['file_' + name].unwrap_spec = unwrap_spec
- class_scope['_exposed_method_names'].append(name)
+ if exposed:
+ class_scope['_exposed_method_names'].append(name)
_decl(locals(), "__init__", """Opens a file.""")
@@ -470,11 +451,8 @@
Size defaults to the current file position, as returned by tell().""")
- _decl(locals(), "write",
- """write(str) -> None. Write string str to file.
-
-Note that due to buffering, flush() or close() may be needed before
-the file on disk reflects the data written.""")
+ _decl(locals(), "write_str", "Interp-level only, see file_write()",
+ exposed=False)
_decl(locals(), "__iter__",
"""Iterating over files, as in 'for line in f:', returns each line of
@@ -505,6 +483,32 @@
else:
return space.str_w(space.repr(w_name))
+ def file_write(self, w_data):
+ """write(str) -> None. Write string str to file.
+
+Note that due to buffering, flush() or close() may be needed before
+the file on disk reflects the data written."""
+ space = self.space
+ self.check_writable()
+ if self.binary:
+ data = space.getarg_w('s*', w_data).as_str()
+ else:
+ if space.isinstance_w(w_data, space.w_unicode):
+ # note: "encode" is called before we acquire the lock
+ # for this file, which is done in file_write_str()
+ if self.encoding:
+ w_encoding = space.wrap(self.encoding)
+ else:
+ w_encoding = getdefaultencoding(space)
+ if self.errors:
+ w_errors = space.wrap(self.errors)
+ else:
+ w_errors = space.wrap("strict")
+ w_data = space.call_method(w_data, "encode",
+ w_encoding, w_errors)
+ data = space.charbuf_w(w_data)
+ self.file_write_str(data)
+
def file_writelines(self, w_lines):
"""writelines(sequence_of_strings) -> None. Write the strings to the
file.
@@ -616,6 +620,7 @@
cls=W_File,
doc="Support for 'print'."),
__repr__ = interp2app(W_File.file__repr__),
+ write = interp2app(W_File.file_write),
writelines = interp2app(W_File.file_writelines),
__exit__ = interp2app(W_File.file__exit__),
__weakref__ = make_weakref_descr(W_File),
diff --git a/pypy/module/marshal/interp_marshal.py
b/pypy/module/marshal/interp_marshal.py
--- a/pypy/module/marshal/interp_marshal.py
+++ b/pypy/module/marshal/interp_marshal.py
@@ -120,7 +120,7 @@
class DirectStreamWriter(StreamReaderWriter):
def write(self, data):
- self.file.do_direct_write(data)
+ self.file.direct_write_str(data)
class DirectStreamReader(StreamReaderWriter):
def read(self, n):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit