Skip to content

Commit 02b68e0

Browse files
authored
Merge branch 'main' into remove_unused_consts
2 parents f03b6a7 + 3a1dde8 commit 02b68e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+726
-1154
lines changed

Doc/library/stdtypes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,9 @@ expression support in the :mod:`re` module).
16171617
range [*start*, *end*]. Optional arguments *start* and *end* are
16181618
interpreted as in slice notation.
16191619

1620+
If *sub* is empty, returns the number of empty strings between characters
1621+
which is the length of the string plus one.
1622+
16201623

16211624
.. method:: str.encode(encoding="utf-8", errors="strict")
16221625

@@ -2698,6 +2701,9 @@ arbitrary binary data.
26982701
The subsequence to search for may be any :term:`bytes-like object` or an
26992702
integer in the range 0 to 255.
27002703

2704+
If *sub* is empty, returns the number of empty slices between characters
2705+
which is the length of the bytes object plus one.
2706+
27012707
.. versionchanged:: 3.3
27022708
Also accept an integer in the range 0 to 255 as the subsequence.
27032709

Lib/asyncio/sslproto.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,6 @@ def get_read_buffer_size(self):
199199
"""Return the current size of the read buffer."""
200200
return self._ssl_protocol._get_read_buffer_size()
201201

202-
def get_write_buffer_limits(self):
203-
"""Get the high and low watermarks for write flow control.
204-
Return a tuple (low, high) where low and high are
205-
positive number of bytes."""
206-
return self._ssl_protocol._transport.get_write_buffer_limits()
207-
208202
@property
209203
def _protocol_paused(self):
210204
# Required for sendfile fallback pause_writing/resume_writing logic

Lib/test/test_getpath.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,37 @@ def test_venv_changed_name_posix(self):
382382
actual = getpath(ns, expected)
383383
self.assertEqual(expected, actual)
384384

385+
def test_venv_changed_name_copy_posix(self):
386+
"Test a venv --copies layout on *nix that lacks a distributed 'python'"
387+
ns = MockPosixNamespace(
388+
argv0="python",
389+
PREFIX="/usr",
390+
ENV_PATH="/venv/bin:/usr/bin",
391+
)
392+
ns.add_known_xfile("/usr/bin/python9")
393+
ns.add_known_xfile("/venv/bin/python")
394+
ns.add_known_file("/usr/lib/python9.8/os.py")
395+
ns.add_known_dir("/usr/lib/python9.8/lib-dynload")
396+
ns.add_known_file("/venv/pyvenv.cfg", [
397+
r"home = /usr/bin"
398+
])
399+
expected = dict(
400+
executable="/venv/bin/python",
401+
prefix="/usr",
402+
exec_prefix="/usr",
403+
base_executable="/usr/bin/python9",
404+
base_prefix="/usr",
405+
base_exec_prefix="/usr",
406+
module_search_paths_set=1,
407+
module_search_paths=[
408+
"/usr/lib/python98.zip",
409+
"/usr/lib/python9.8",
410+
"/usr/lib/python9.8/lib-dynload",
411+
],
412+
)
413+
actual = getpath(ns, expected)
414+
self.assertEqual(expected, actual)
415+
385416
def test_symlink_normal_posix(self):
386417
"Test a 'standard' install layout via symlink on *nix"
387418
ns = MockPosixNamespace(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix calculation of :data:`sys._base_executable` when inside a POSIX virtual
2+
environment using copies of the python binary when the base installation does
3+
not provide the executable name used by the venv. Calculation will fall back to
4+
alternative names ("python<MAJOR>", "python<MAJOR>.<MINOR>").
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue that could potentially cause incorrect error handling for some
2+
bytecode instructions.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove older version of ``_SSLProtocolTransport.get_write_buffer_limits`` in :mod:`!asyncio.sslproto`

Modules/getpath.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,25 @@ def search_up(prefix, *landmarks, test=isfile):
375375
pass
376376
if not base_executable:
377377
base_executable = joinpath(executable_dir, basename(executable))
378+
# It's possible "python" is executed from within a posix venv but that
379+
# "python" is not available in the "home" directory as the standard
380+
# `make install` does not create it and distros often do not provide it.
381+
#
382+
# In this case, try to fall back to known alternatives
383+
if os_name != 'nt' and not isfile(base_executable):
384+
base_exe = basename(executable)
385+
for candidate in (DEFAULT_PROGRAM_NAME, f'python{VERSION_MAJOR}.{VERSION_MINOR}'):
386+
candidate += EXE_SUFFIX if EXE_SUFFIX else ''
387+
if base_exe == candidate:
388+
continue
389+
candidate = joinpath(executable_dir, candidate)
390+
# Only set base_executable if the candidate exists.
391+
# If no candidate succeeds, subsequent errors related to
392+
# base_executable (like FileNotFoundError) remain in the
393+
# context of the original executable name
394+
if isfile(candidate):
395+
base_executable = candidate
396+
break
378397
break
379398
else:
380399
venv_prefix = None

Modules/zlibmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,7 @@ ZlibDecompressor__new__(PyTypeObject *cls,
17181718
PyObject *kwargs)
17191719
{
17201720
static char *keywords[] = {"wbits", "zdict", NULL};
1721-
static char *format = "|iO:_ZlibDecompressor";
1721+
static const char * const format = "|iO:_ZlibDecompressor";
17221722
int wbits = MAX_WBITS;
17231723
PyObject *zdict = NULL;
17241724
zlibstate *state = PyType_GetModuleState(cls);

Objects/abstract.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ PyObject_Type(PyObject *o)
4545
}
4646

4747
v = (PyObject *)Py_TYPE(o);
48-
Py_INCREF(v);
49-
return v;
48+
return Py_NewRef(v);
5049
}
5150

5251
Py_ssize_t
@@ -722,9 +721,7 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
722721
return -1;
723722
}
724723

725-
view->obj = obj;
726-
if (obj)
727-
Py_INCREF(obj);
724+
view->obj = Py_XNewRef(obj);
728725
view->buf = buf;
729726
view->len = len;
730727
view->readonly = readonly;
@@ -776,8 +773,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
776773
/* Fast path for common types. */
777774
if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
778775
if (PyUnicode_CheckExact(obj)) {
779-
Py_INCREF(obj);
780-
return obj;
776+
return Py_NewRef(obj);
781777
}
782778
if (PyLong_CheckExact(obj)) {
783779
return PyObject_Str(obj);
@@ -1405,8 +1401,7 @@ _PyNumber_Index(PyObject *item)
14051401
}
14061402

14071403
if (PyLong_Check(item)) {
1408-
Py_INCREF(item);
1409-
return item;
1404+
return Py_NewRef(item);
14101405
}
14111406
if (!_PyIndex_Check(item)) {
14121407
PyErr_Format(PyExc_TypeError,
@@ -1520,8 +1515,7 @@ PyNumber_Long(PyObject *o)
15201515
}
15211516

15221517
if (PyLong_CheckExact(o)) {
1523-
Py_INCREF(o);
1524-
return o;
1518+
return Py_NewRef(o);
15251519
}
15261520
m = Py_TYPE(o)->tp_as_number;
15271521
if (m && m->nb_int) { /* This should include subclasses of int */
@@ -2045,8 +2039,7 @@ PySequence_Tuple(PyObject *v)
20452039
a tuple *subclass* instance as-is, hence the restriction
20462040
to exact tuples here. In contrast, lists always make
20472041
a copy, so there's no need for exactness below. */
2048-
Py_INCREF(v);
2049-
return v;
2042+
return Py_NewRef(v);
20502043
}
20512044
if (PyList_CheckExact(v))
20522045
return PyList_AsTuple(v);
@@ -2144,8 +2137,7 @@ PySequence_Fast(PyObject *v, const char *m)
21442137
}
21452138

21462139
if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
2147-
Py_INCREF(v);
2148-
return v;
2140+
return Py_NewRef(v);
21492141
}
21502142

21512143
it = PyObject_GetIter(v);

Objects/boolobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ PyObject *PyBool_FromLong(long ok)
2323
result = Py_True;
2424
else
2525
result = Py_False;
26-
Py_INCREF(result);
27-
return result;
26+
return Py_NewRef(result);
2827
}
2928

3029
/* We define bool_new to always return either Py_True or Py_False */

Objects/bytesobject.c

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ static inline PyObject* bytes_get_empty(void)
5353
// Return a strong reference to the empty bytes string singleton.
5454
static inline PyObject* bytes_new_empty(void)
5555
{
56-
Py_INCREF(EMPTY);
57-
return (PyObject *)EMPTY;
56+
return Py_NewRef(EMPTY);
5857
}
5958

6059

@@ -126,8 +125,7 @@ PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
126125
}
127126
if (size == 1 && str != NULL) {
128127
op = CHARACTER(*str & 255);
129-
Py_INCREF(op);
130-
return (PyObject *)op;
128+
return Py_NewRef(op);
131129
}
132130
if (size == 0) {
133131
return bytes_new_empty();
@@ -162,8 +160,7 @@ PyBytes_FromString(const char *str)
162160
}
163161
else if (size == 1) {
164162
op = CHARACTER(*str & 255);
165-
Py_INCREF(op);
166-
return (PyObject *)op;
163+
return Py_NewRef(op);
167164
}
168165

169166
/* Inline PyObject_NewVar */
@@ -527,14 +524,12 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
527524
if (PyBytes_Check(v)) {
528525
*pbuf = PyBytes_AS_STRING(v);
529526
*plen = PyBytes_GET_SIZE(v);
530-
Py_INCREF(v);
531-
return v;
527+
return Py_NewRef(v);
532528
}
533529
if (PyByteArray_Check(v)) {
534530
*pbuf = PyByteArray_AS_STRING(v);
535531
*plen = PyByteArray_GET_SIZE(v);
536-
Py_INCREF(v);
537-
return v;
532+
return Py_NewRef(v);
538533
}
539534
/* does it support __bytes__? */
540535
func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
@@ -1411,13 +1406,11 @@ bytes_concat(PyObject *a, PyObject *b)
14111406

14121407
/* Optimize end cases */
14131408
if (va.len == 0 && PyBytes_CheckExact(b)) {
1414-
result = b;
1415-
Py_INCREF(result);
1409+
result = Py_NewRef(b);
14161410
goto done;
14171411
}
14181412
if (vb.len == 0 && PyBytes_CheckExact(a)) {
1419-
result = a;
1420-
Py_INCREF(result);
1413+
result = Py_NewRef(a);
14211414
goto done;
14221415
}
14231416

@@ -1458,8 +1451,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
14581451
}
14591452
size = Py_SIZE(a) * n;
14601453
if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
1461-
Py_INCREF(a);
1462-
return (PyObject *)a;
1454+
return Py_NewRef(a);
14631455
}
14641456
nbytes = (size_t)size;
14651457
if (nbytes + PyBytesObject_SIZE <= nbytes) {
@@ -1625,8 +1617,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
16251617
else if (start == 0 && step == 1 &&
16261618
slicelength == PyBytes_GET_SIZE(self) &&
16271619
PyBytes_CheckExact(self)) {
1628-
Py_INCREF(self);
1629-
return (PyObject *)self;
1620+
return Py_NewRef(self);
16301621
}
16311622
else if (step == 1) {
16321623
return PyBytes_FromStringAndSize(
@@ -1696,8 +1687,7 @@ bytes___bytes___impl(PyBytesObject *self)
16961687
/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/
16971688
{
16981689
if (PyBytes_CheckExact(self)) {
1699-
Py_INCREF(self);
1700-
return (PyObject *)self;
1690+
return Py_NewRef(self);
17011691
}
17021692
else {
17031693
return PyBytes_FromStringAndSize(self->ob_sval, Py_SIZE(self));
@@ -1922,8 +1912,7 @@ do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
19221912
PyBuffer_Release(&vsep);
19231913

19241914
if (i == 0 && j == len && PyBytes_CheckExact(self)) {
1925-
Py_INCREF(self);
1926-
return (PyObject*)self;
1915+
return Py_NewRef(self);
19271916
}
19281917
else
19291918
return PyBytes_FromStringAndSize(s+i, j-i);
@@ -1952,8 +1941,7 @@ do_strip(PyBytesObject *self, int striptype)
19521941
}
19531942

19541943
if (i == 0 && j == len && PyBytes_CheckExact(self)) {
1955-
Py_INCREF(self);
1956-
return (PyObject*)self;
1944+
return Py_NewRef(self);
19571945
}
19581946
else
19591947
return PyBytes_FromStringAndSize(s+i, j-i);
@@ -2152,8 +2140,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
21522140
}
21532141
if (!changed && PyBytes_CheckExact(input_obj)) {
21542142
Py_DECREF(result);
2155-
Py_INCREF(input_obj);
2156-
return input_obj;
2143+
return Py_NewRef(input_obj);
21572144
}
21582145
/* Fix the size of the resulting byte string */
21592146
if (inlen > 0)
@@ -2245,8 +2232,7 @@ bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix)
22452232
}
22462233

22472234
if (PyBytes_CheckExact(self)) {
2248-
Py_INCREF(self);
2249-
return (PyObject *)self;
2235+
return Py_NewRef(self);
22502236
}
22512237

22522238
return PyBytes_FromStringAndSize(self_start, self_len);
@@ -2284,8 +2270,7 @@ bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix)
22842270
}
22852271

22862272
if (PyBytes_CheckExact(self)) {
2287-
Py_INCREF(self);
2288-
return (PyObject *)self;
2273+
return Py_NewRef(self);
22892274
}
22902275

22912276
return PyBytes_FromStringAndSize(self_start, self_len);
@@ -2844,8 +2829,7 @@ PyBytes_FromObject(PyObject *x)
28442829
}
28452830

28462831
if (PyBytes_CheckExact(x)) {
2847-
Py_INCREF(x);
2848-
return x;
2832+
return Py_NewRef(x);
28492833
}
28502834

28512835
/* Use the modern buffer interface */
@@ -3269,8 +3253,7 @@ bytes_iter(PyObject *seq)
32693253
if (it == NULL)
32703254
return NULL;
32713255
it->it_index = 0;
3272-
Py_INCREF(seq);
3273-
it->it_seq = (PyBytesObject *)seq;
3256+
it->it_seq = (PyBytesObject *)Py_NewRef(seq);
32743257
_PyObject_GC_TRACK(it);
32753258
return (PyObject *)it;
32763259
}

Objects/call.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,7 @@ _PyStack_UnpackDict(PyThreadState *tstate,
10001000

10011001
/* Copy positional arguments */
10021002
for (Py_ssize_t i = 0; i < nargs; i++) {
1003-
Py_INCREF(args[i]);
1004-
stack[i] = args[i];
1003+
stack[i] = Py_NewRef(args[i]);
10051004
}
10061005

10071006
PyObject **kwstack = stack + nargs;
@@ -1013,10 +1012,8 @@ _PyStack_UnpackDict(PyThreadState *tstate,
10131012
unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
10141013
while (PyDict_Next(kwargs, &pos, &key, &value)) {
10151014
keys_are_strings &= Py_TYPE(key)->tp_flags;
1016-
Py_INCREF(key);
1017-
Py_INCREF(value);
1018-
PyTuple_SET_ITEM(kwnames, i, key);
1019-
kwstack[i] = value;
1015+
PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key));
1016+
kwstack[i] = Py_NewRef(value);
10201017
i++;
10211018
}
10221019

0 commit comments

Comments
 (0)