Skip to content

Commit 4ec38d5

Browse files
authored
Merge pull request #449 from python/master
Sync Fork from Upstream Repo
2 parents 5dc2991 + f97dc80 commit 4ec38d5

Some content is hidden

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

41 files changed

+1064
-765
lines changed

Doc/c-api/code.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ bound into a function.
5151
Return a new empty code object with the specified filename,
5252
function name, and first line number. It is illegal to
5353
:func:`exec` or :func:`eval` the resulting code object.
54+
55+
.. c:function:: int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
56+
57+
Return the line number of the instruction that occurs on or before ``byte_offset`` and ends after it.
58+
If you just need the line number of a frame, use :c:func:`PyFrame_GetLineNumber` instead.
59+
60+
For efficiently iterating over the line numbers in a code object, use `the API described in PEP 626
61+
<https://www.python.org/dev/peps/pep-0626/#out-of-process-debuggers-and-profilers>`_.

Doc/library/array.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ The following data items and methods are also supported:
178178
array of some other type.
179179

180180

181-
.. method:: array.index(x)
181+
.. method:: array.index(x[, start[, stop]])
182182

183183
Return the smallest *i* such that *i* is the index of the first occurrence of
184-
*x* in the array.
184+
*x* in the array. The optional arguments *start* and *stop* can be
185+
specified to search for *x* within a subsection of the array. Raise
186+
:exc:`ValueError` if *x* is not found.
185187

188+
.. versionchanged:: 3.10
189+
Added optional *start* and *stop* parameters.
186190

187191
.. method:: array.insert(i, x)
188192

Doc/reference/import.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ a list containing the portion.
904904
``find_loader()`` in preference to ``find_module()``.
905905

906906
.. versionchanged:: 3.10
907-
Calls to :meth:`~importlib.abc.PathEntryFinder.find_module` by the import
907+
Calls to :meth:`~importlib.abc.PathEntryFinder.find_module` and
908+
:meth:`~importlib.abc.PathEntryFinder.find_loader` by the import
908909
system will raise :exc:`ImportWarning`.
909910

910911

Doc/whatsnew/3.10.rst

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,13 @@ argparse
633633
Misleading phrase "optional arguments" was replaced with "options" in argparse help. Some tests might require adaptation if they rely on exact output match.
634634
(Contributed by Raymond Hettinger in :issue:`9694`.)
635635
636+
array
637+
-----
638+
639+
The :meth:`~array.array.index` method of :class:`array.array` now has
640+
optional *start* and *stop* parameters.
641+
(Contributed by Anders Lorentsen and Zackery Spytz in :issue:`31956`.)
642+
636643
base64
637644
------
638645
@@ -1043,7 +1050,13 @@ Deprecated
10431050
:meth:`importlib.abc.PathEntryFinder.find_spec`
10441051
are preferred, respectively. You can use
10451052
:func:`importlib.util.spec_from_loader` to help in porting.
1046-
(Contributed by Brett Cannon in :issue:`42134`.)
1053+
(Contributed by Brett Cannon in :issue:`42134`.)
1054+
1055+
* The use of :meth:`importlib.abc.PathEntryFinder.find_loader` by the import
1056+
system now triggers an :exc:`ImportWarning` as
1057+
:meth:`importlib.abc.PathEntryFinder.find_spec` is preferred. You can use
1058+
:func:`importlib.util.spec_from_loader` to help in porting.
1059+
(Contributed by Brett Cannon in :issue:`43672`.)
10471060
10481061
* The import system now uses the ``__spec__`` attribute on modules before
10491062
falling back on :meth:`~importlib.abc.Loader.module_repr` for a module's
@@ -1315,6 +1328,18 @@ New Features
13151328
to simulate.
13161329
(Contributed by Antoine Pitrou in :issue:`43356`.)
13171330
1331+
* The limited C API is now supported if Python is built in debug mode (if the
1332+
``Py_DEBUG`` macro is defined). In the limited C API, the :c:func:`Py_INCREF`
1333+
and :c:func:`Py_DECREF` functions are now implemented as opaque function
1334+
calls, rather than accessing directly the :c:member:`PyObject.ob_refcnt`
1335+
member, if Python is built in debug mode and the ``Py_LIMITED_API`` macro
1336+
targets Python 3.10 or newer. It became possible to support the limited C API
1337+
in debug mode because the :c:type:`PyObject` structure is the same in release
1338+
and debug mode since Python 3.8 (see :issue:`36465`).
1339+
1340+
The limited C API is still not supported in the ``--with-trace-refs`` special
1341+
build (``Py_TRACE_REFS`` macro).
1342+
(Contributed by Victor Stinner in :issue:`43688`.)
13181343
13191344
Porting to Python 3.10
13201345
----------------------

Include/internal/pycore_long.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ static inline PyObject* __PyLong_GetSmallInt_internal(int value)
1818
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
1919
size_t index = _PY_NSMALLNEGINTS + value;
2020
PyObject *obj = (PyObject*)interp->small_ints[index];
21-
// _PyLong_GetZero() and _PyLong_GetOne() must not be called
22-
// before _PyLong_Init() nor after _PyLong_Fini()
21+
// _PyLong_GetZero(), _PyLong_GetOne() and get_small_int() must not be
22+
// called before _PyLong_Init() nor after _PyLong_Fini().
2323
assert(obj != NULL);
2424
return obj;
2525
}

Include/internal/pycore_pylifecycle.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);
5050
/* Various one-time initializers */
5151

5252
extern PyStatus _PyUnicode_Init(PyInterpreterState *interp);
53+
extern PyStatus _PyUnicode_InitTypes(void);
5354
extern PyStatus _PyBytes_Init(PyInterpreterState *interp);
5455
extern int _PyStructSequence_Init(void);
5556
extern int _PyLong_Init(PyInterpreterState *interp);
57+
extern int _PyLong_InitTypes(void);
5658
extern PyStatus _PyTuple_Init(PyInterpreterState *interp);
5759
extern PyStatus _PyFaulthandler_Init(int enable);
5860
extern int _PyTraceMalloc_Init(int enable);
@@ -64,9 +66,10 @@ extern PyStatus _PySys_ReadPreinitWarnOptions(PyWideStringList *options);
6466
extern PyStatus _PySys_ReadPreinitXOptions(PyConfig *config);
6567
extern int _PySys_UpdateConfig(PyThreadState *tstate);
6668
extern PyStatus _PyExc_Init(PyInterpreterState *interp);
67-
extern PyStatus _PyErr_Init(void);
69+
extern PyStatus _PyErr_InitTypes(void);
6870
extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod);
69-
extern int _PyFloat_Init(void);
71+
extern void _PyFloat_Init(void);
72+
extern int _PyFloat_InitTypes(void);
7073
extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
7174

7275
extern PyStatus _PyTypes_Init(void);

Include/object.h

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ whose size is determined when the object is allocated.
5454

5555
/* Py_DEBUG implies Py_REF_DEBUG. */
5656
#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
57-
#define Py_REF_DEBUG
57+
# define Py_REF_DEBUG
5858
#endif
5959

60-
#if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG)
61-
#error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG
60+
#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
61+
# error Py_LIMITED_API is incompatible with Py_TRACE_REFS
6262
#endif
6363

6464
/* PyTypeObject structure is defined in cpython/object.h.
@@ -74,8 +74,8 @@ typedef struct _typeobject PyTypeObject;
7474
#define _PyObject_EXTRA_INIT 0, 0,
7575

7676
#else
77-
#define _PyObject_HEAD_EXTRA
78-
#define _PyObject_EXTRA_INIT
77+
# define _PyObject_HEAD_EXTRA
78+
# define _PyObject_EXTRA_INIT
7979
#endif
8080

8181
/* PyObject_HEAD defines the initial segment of every PyObject. */
@@ -427,21 +427,46 @@ PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
427427

428428
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
429429

430+
/*
431+
These are provided as conveniences to Python runtime embedders, so that
432+
they can have object code that is not dependent on Python compilation flags.
433+
*/
434+
PyAPI_FUNC(void) Py_IncRef(PyObject *);
435+
PyAPI_FUNC(void) Py_DecRef(PyObject *);
436+
437+
// Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL.
438+
// Private functions used by Py_INCREF() and Py_DECREF().
439+
PyAPI_FUNC(void) _Py_IncRef(PyObject *);
440+
PyAPI_FUNC(void) _Py_DecRef(PyObject *);
441+
430442
static inline void _Py_INCREF(PyObject *op)
431443
{
444+
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
445+
// Stable ABI for Python 3.10 built in debug mode.
446+
_Py_IncRef(op);
447+
#else
448+
// Non-limited C API and limited C API for Python 3.9 and older access
449+
// directly PyObject.ob_refcnt.
432450
#ifdef Py_REF_DEBUG
433451
_Py_RefTotal++;
434452
#endif
435453
op->ob_refcnt++;
454+
#endif
436455
}
437456
#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
438457

439458
static inline void _Py_DECREF(
440-
#ifdef Py_REF_DEBUG
459+
#if defined(Py_REF_DEBUG) && !(defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000)
441460
const char *filename, int lineno,
442461
#endif
443462
PyObject *op)
444463
{
464+
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
465+
// Stable ABI for Python 3.10 built in debug mode.
466+
_Py_DecRef(op);
467+
#else
468+
// Non-limited C API and limited C API for Python 3.9 and older access
469+
// directly PyObject.ob_refcnt.
445470
#ifdef Py_REF_DEBUG
446471
_Py_RefTotal--;
447472
#endif
@@ -455,8 +480,9 @@ static inline void _Py_DECREF(
455480
else {
456481
_Py_Dealloc(op);
457482
}
483+
#endif
458484
}
459-
#ifdef Py_REF_DEBUG
485+
#if defined(Py_REF_DEBUG) && !(defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000)
460486
# define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
461487
#else
462488
# define Py_DECREF(op) _Py_DECREF(_PyObject_CAST(op))
@@ -525,13 +551,6 @@ static inline void _Py_XDECREF(PyObject *op)
525551

526552
#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
527553

528-
/*
529-
These are provided as conveniences to Python runtime embedders, so that
530-
they can have object code that is not dependent on Python compilation flags.
531-
*/
532-
PyAPI_FUNC(void) Py_IncRef(PyObject *);
533-
PyAPI_FUNC(void) Py_DecRef(PyObject *);
534-
535554
// Create a new strong reference to an object:
536555
// increment the reference count of the object and return the object.
537556
PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);

Lib/bdb.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def __init__(self, skip=None):
3434
self.fncache = {}
3535
self.frame_returning = None
3636

37+
self._load_breaks()
38+
3739
def canonic(self, filename):
3840
"""Return canonical form of filename.
3941
@@ -365,6 +367,12 @@ def set_quit(self):
365367
# Call self.get_*break*() to see the breakpoints or better
366368
# for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
367369

370+
def _add_to_breaks(self, filename, lineno):
371+
"""Add breakpoint to breaks, if not already there."""
372+
bp_linenos = self.breaks.setdefault(filename, [])
373+
if lineno not in bp_linenos:
374+
bp_linenos.append(lineno)
375+
368376
def set_break(self, filename, lineno, temporary=False, cond=None,
369377
funcname=None):
370378
"""Set a new breakpoint for filename:lineno.
@@ -377,12 +385,21 @@ def set_break(self, filename, lineno, temporary=False, cond=None,
377385
line = linecache.getline(filename, lineno)
378386
if not line:
379387
return 'Line %s:%d does not exist' % (filename, lineno)
380-
list = self.breaks.setdefault(filename, [])
381-
if lineno not in list:
382-
list.append(lineno)
388+
self._add_to_breaks(filename, lineno)
383389
bp = Breakpoint(filename, lineno, temporary, cond, funcname)
384390
return None
385391

392+
def _load_breaks(self):
393+
"""Apply all breakpoints (set in other instances) to this one.
394+
395+
Populates this instance's breaks list from the Breakpoint class's
396+
list, which can have breakpoints set by another Bdb instance. This
397+
is necessary for interactive sessions to keep the breakpoints
398+
active across multiple calls to run().
399+
"""
400+
for (filename, lineno) in Breakpoint.bplist.keys():
401+
self._add_to_breaks(filename, lineno)
402+
386403
def _prune_breaks(self, filename, lineno):
387404
"""Prune breakpoints for filename:lineno.
388405
@@ -681,6 +698,12 @@ def __init__(self, file, line, temporary=False, cond=None, funcname=None):
681698
else:
682699
self.bplist[file, line] = [self]
683700

701+
@staticmethod
702+
def clearBreakpoints():
703+
Breakpoint.next = 1
704+
Breakpoint.bplist = {}
705+
Breakpoint.bpbynumber = [None]
706+
684707
def deleteMe(self):
685708
"""Delete the breakpoint from the list associated to a file:line.
686709

Lib/importlib/_bootstrap_external.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,13 @@ def _legacy_get_spec(cls, fullname, finder):
13231323
# This would be a good place for a DeprecationWarning if
13241324
# we ended up going that route.
13251325
if hasattr(finder, 'find_loader'):
1326+
msg = (f"{_bootstrap._object_name(finder)}.find_spec() not found; "
1327+
"falling back to find_loader()")
1328+
_warnings.warn(msg, ImportWarning)
13261329
loader, portions = finder.find_loader(fullname)
13271330
else:
13281331
msg = (f"{_bootstrap._object_name(finder)}.find_spec() not found; "
1329-
"falling back to find_module()")
1332+
"falling back to find_module()")
13301333
_warnings.warn(msg, ImportWarning)
13311334
loader = finder.find_module(fullname)
13321335
portions = []

Lib/test/test_array.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,17 @@ def test_index(self):
918918
self.assertRaises(ValueError, a.index, None)
919919
self.assertRaises(ValueError, a.index, self.outside)
920920

921+
a = array.array('i', [-2, -1, 0, 0, 1, 2])
922+
self.assertEqual(a.index(0), 2)
923+
self.assertEqual(a.index(0, 2), 2)
924+
self.assertEqual(a.index(0, -4), 2)
925+
self.assertEqual(a.index(-2, -10), 0)
926+
self.assertEqual(a.index(0, 3), 3)
927+
self.assertEqual(a.index(0, -3), 3)
928+
self.assertEqual(a.index(0, 3, 4), 3)
929+
self.assertEqual(a.index(0, -3, -2), 3)
930+
self.assertRaises(ValueError, a.index, 2, 0, -10)
931+
921932
def test_count(self):
922933
example = 2*self.example
923934
a = array.array(self.typecode, example)

0 commit comments

Comments
 (0)