Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a269d82

Browse files
committed
merge
2 parents 1d75382 + c7b6c50 commit a269d82

9 files changed

Lines changed: 90 additions & 33 deletions

File tree

Doc/library/codecs.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ It defines the following functions:
7878
reference (for encoding only)
7979
* ``'backslashreplace'``: replace with backslashed escape sequences (for
8080
encoding only)
81-
* ``'surrogateescape'``: replace with surrogate U+DCxx, see :pep:`383`
81+
* ``'surrogateescape'``: on decoding, replace with code points in the Unicode
82+
Private Use Area ranging from U+DC80 to U+DCFF. These private code
83+
points will then be turned back into the same bytes when the
84+
``surrogateescape`` error handler is used when encoding the data.
85+
(See :pep:`383` for more.)
8286

8387
as well as any other error handling name defined via :func:`register_error`.
8488

Doc/library/functions.rst

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -895,16 +895,36 @@ are always available. They are listed here in alphabetical order.
895895
the list of supported encodings.
896896

897897
*errors* is an optional string that specifies how encoding and decoding
898-
errors are to be handled--this cannot be used in binary mode. Pass
899-
``'strict'`` to raise a :exc:`ValueError` exception if there is an encoding
900-
error (the default of ``None`` has the same effect), or pass ``'ignore'`` to
901-
ignore errors. (Note that ignoring encoding errors can lead to data loss.)
902-
``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted
903-
where there is malformed data. When writing, ``'xmlcharrefreplace'``
904-
(replace with the appropriate XML character reference) or
905-
``'backslashreplace'`` (replace with backslashed escape sequences) can be
906-
used. Any other error handling name that has been registered with
907-
:func:`codecs.register_error` is also valid.
898+
errors are to be handled--this cannot be used in binary mode.
899+
A variety of standard error handlers are available, though any
900+
error handling name that has been registered with
901+
:func:`codecs.register_error` is also valid. The standard names
902+
are:
903+
904+
* ``'strict'`` to raise a :exc:`ValueError` exception if there is
905+
an encoding error. The default value of ``None`` has the same
906+
effect.
907+
908+
* ``'ignore'`` ignores errors. Note that ignoring encoding errors
909+
can lead to data loss.
910+
911+
* ``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted
912+
where there is malformed data.
913+
914+
* ``'surrogateescape'`` will represent any incorrect bytes as code
915+
points in the Unicode Private Use Area ranging from U+DC80 to
916+
U+DCFF. These private code points will then be turned back into
917+
the same bytes when the ``surrogateescape`` error handler is used
918+
when writing data. This is useful for processing files in an
919+
unknown encoding.
920+
921+
* ``'xmlcharrefreplace'`` is only supported when writing to a file.
922+
Characters not supported by the encoding are replaced with the
923+
appropriate XML character reference ``&#nnn;``.
924+
925+
* ``'backslashreplace'`` (also only supported when writing)
926+
replaces unsupported characters with Python's backslashed escape
927+
sequences.
908928

909929
.. index::
910930
single: universal newlines; open() built-in function

Lib/codecs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Codec:
105105
Python will use the official U+FFFD REPLACEMENT
106106
CHARACTER for the builtin Unicode codecs on
107107
decoding and '?' on encoding.
108+
'surrogateescape' - replace with private codepoints U+DCnn.
108109
'xmlcharrefreplace' - Replace with the appropriate XML
109110
character reference (only for encoding).
110111
'backslashreplace' - Replace with backslashed escape sequences

Lib/subprocess.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ def __init__(self, args, bufsize=-1, executable=None,
810810
if universal_newlines:
811811
self.stderr = io.TextIOWrapper(self.stderr)
812812

813+
self._closed_child_pipe_fds = False
813814
try:
814815
self._execute_child(args, executable, preexec_fn, close_fds,
815816
pass_fds, cwd, env,
@@ -826,19 +827,21 @@ def __init__(self, args, bufsize=-1, executable=None,
826827
except EnvironmentError:
827828
pass # Ignore EBADF or other errors.
828829

829-
# Make sure the child pipes are closed as well.
830-
to_close = []
831-
if stdin == PIPE:
832-
to_close.append(p2cread)
833-
if stdout == PIPE:
834-
to_close.append(c2pwrite)
835-
if stderr == PIPE:
836-
to_close.append(errwrite)
837-
for fd in to_close:
838-
try:
839-
os.close(fd)
840-
except EnvironmentError:
841-
pass
830+
if not self._closed_child_pipe_fds:
831+
to_close = []
832+
if stdin == PIPE:
833+
to_close.append(p2cread)
834+
if stdout == PIPE:
835+
to_close.append(c2pwrite)
836+
if stderr == PIPE:
837+
to_close.append(errwrite)
838+
if hasattr(self, '_devnull'):
839+
to_close.append(self._devnull)
840+
for fd in to_close:
841+
try:
842+
os.close(fd)
843+
except EnvironmentError:
844+
pass
842845

843846
raise
844847

@@ -1383,14 +1386,18 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
13831386
# be sure the FD is closed no matter what
13841387
os.close(errpipe_write)
13851388

1386-
if p2cread != -1 and p2cwrite != -1:
1389+
# self._devnull is not always defined.
1390+
devnull_fd = getattr(self, '_devnull', None)
1391+
if p2cread != -1 and p2cwrite != -1 and p2cread != devnull_fd:
13871392
os.close(p2cread)
1388-
if c2pwrite != -1 and c2pread != -1:
1393+
if c2pwrite != -1 and c2pread != -1 and c2pwrite != devnull_fd:
13891394
os.close(c2pwrite)
1390-
if errwrite != -1 and errread != -1:
1395+
if errwrite != -1 and errread != -1 and errwrite != devnull_fd:
13911396
os.close(errwrite)
1392-
if hasattr(self, '_devnull'):
1393-
os.close(self._devnull)
1397+
if devnull_fd is not None:
1398+
os.close(devnull_fd)
1399+
# Prevent a double close of these fds from __init__ on error.
1400+
self._closed_child_pipe_fds = True
13941401

13951402
# Wait for exec to fail or succeed; possibly raising an
13961403
# exception (limited in size)

Lib/test/test_curses.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,18 @@ def test_userptr_without_set(stdscr):
252252
except curses.panel.error:
253253
pass
254254

255+
def test_userptr_memory_leak(stdscr):
256+
w = curses.newwin(10, 10)
257+
p = curses.panel.new_panel(w)
258+
obj = object()
259+
nrefs = sys.getrefcount(obj)
260+
for i in range(100):
261+
p.set_userptr(obj)
262+
263+
p.set_userptr(None)
264+
if sys.getrefcount(obj) != nrefs:
265+
raise RuntimeError("set_userptr leaked references")
266+
255267
def test_resize_term(stdscr):
256268
if hasattr(curses, 'resizeterm'):
257269
lines, cols = curses.LINES, curses.COLS
@@ -317,6 +329,7 @@ def main(stdscr):
317329
module_funcs(stdscr)
318330
window_funcs(stdscr)
319331
test_userptr_without_set(stdscr)
332+
test_userptr_memory_leak(stdscr)
320333
test_resize_term(stdscr)
321334
test_issue6243(stdscr)
322335
test_unget_wch(stdscr)

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Core and Builtins
3232
Library
3333
-------
3434

35+
- subprocess: Prevent a possible double close of parent pipe fds when the
36+
subprocess exec runs into an error. Prevent a regular multi-close of the
37+
/dev/null fd when any of stdin, stdout and stderr was set to DEVNULL.
38+
3539
- Issue #16102: Make uuid._netbios_getnode() work again on Python 3.
3640

3741
- Issue #18109: os.uname() now decodes fields from the locale encoding, and
@@ -78,6 +82,9 @@ Library
7882
the default for linking if LDSHARED is not also overriden. This restores
7983
Distutils behavior introduced in 3.2.3 and inadvertently dropped in 3.3.0.
8084

85+
- Issue #18113: Fixed a refcount leak in the curses.panel module's
86+
set_userptr() method. Reported by Atsuo Ishimoto.
87+
8188
IDLE
8289
----
8390

Modules/_curses_panel.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
322322
static PyObject *
323323
PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj)
324324
{
325+
PyObject *oldobj;
326+
PyCursesInitialised;
327+
oldobj = (PyObject *) panel_userptr(self->pan);
328+
Py_XDECREF(oldobj);
325329
Py_INCREF(obj);
326330
return PyCursesCheckERR(set_panel_userptr(self->pan, (void*)obj),
327331
"set_panel_userptr");

Modules/_io/_iomodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ PyDoc_STRVAR(open_doc,
168168
"'strict' to raise a ValueError exception if there is an encoding error\n"
169169
"(the default of None has the same effect), or pass 'ignore' to ignore\n"
170170
"errors. (Note that ignoring encoding errors can lead to data loss.)\n"
171-
"See the documentation for codecs.register for a list of the permitted\n"
172-
"encoding error strings.\n"
171+
"See the documentation for codecs.register or run 'help(codecs.Codec)'\n"
172+
"for a list of the permitted encoding error strings.\n"
173173
"\n"
174174
"newline controls how universal newlines works (it only applies to text\n"
175175
"mode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It works as\n"

Modules/_io/textio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,9 @@ PyDoc_STRVAR(textiowrapper_doc,
642642
"encoding gives the name of the encoding that the stream will be\n"
643643
"decoded or encoded with. It defaults to locale.getpreferredencoding(False).\n"
644644
"\n"
645-
"errors determines the strictness of encoding and decoding (see the\n"
646-
"codecs.register) and defaults to \"strict\".\n"
645+
"errors determines the strictness of encoding and decoding (see\n"
646+
"help(codecs.Codec) or the documentation for codecs.register) and\n"
647+
"defaults to \"strict\".\n"
647648
"\n"
648649
"newline controls how line endings are handled. It can be None, '',\n"
649650
"'\\n', '\\r', and '\\r\\n'. It works as follows:\n"

0 commit comments

Comments
 (0)