Skip to content

Commit 2260a54

Browse files
committed
Merge branch 'main' into fastmock
* main: Improve stats presentation for calls. (pythonGH-100274) Better stats for `LOAD_ATTR` and `STORE_ATTR` (pythonGH-100295) pythongh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (python#100277) Document that zipfile's pwd parameter is a `bytes` object (python#100209) pythongh-99767: mark `PyTypeObject.tp_watched` as internal use only in table (python#100271) Fix typo in introduction.rst (python#100266) pythongh-78997: AttributeError if loading fails in LibraryLoader.__getattr__ pythonGH-100234: Set a default value for random.expovariate() (pythonGH-100235) Remove uninformative itertools recipe (pythonGH-100253) pythonGH-99767: update PyTypeObject docs for type watchers (pythonGH-99928) Move stats for the method cache into the `Py_STAT` machinery (pythonGH-100255) pythonGH-100222: fix typo _py_set_opocde -> _py_set_opcode (pythonGH-100259) pythonGH-100000: Cleanup and polish various watchers code (pythonGH-99998) pythongh-90111: Minor Cleanup for Runtime-Global Objects (pythongh-100254)
2 parents 246772c + d4052d8 commit 2260a54

29 files changed

+448
-228
lines changed

Diff for: Doc/c-api/typeobj.rst

+9
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Quick Reference
147147
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
148148
| :c:member:`~PyTypeObject.tp_vectorcall` | :c:type:`vectorcallfunc` | | | | | |
149149
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
150+
| [:c:member:`~PyTypeObject.tp_watched`] | char | | | | | |
151+
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
150152

151153
.. [#slots]
152154
@@ -2090,6 +2092,13 @@ and :c:type:`PyType_Type` effectively act as defaults.)
20902092
.. versionadded:: 3.9 (the field exists since 3.8 but it's only used since 3.9)
20912093

20922094

2095+
.. c:member:: char PyTypeObject.tp_watched
2096+
2097+
Internal. Do not use.
2098+
2099+
.. versionadded:: 3.12
2100+
2101+
20932102
.. _static-types:
20942103

20952104
Static Types

Diff for: Doc/includes/typestruct.h

+3
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@ typedef struct _typeobject {
8080

8181
destructor tp_finalize;
8282
vectorcallfunc tp_vectorcall;
83+
84+
/* bitset of which type-watchers care about this type */
85+
char tp_watched;
8386
} PyTypeObject;

Diff for: Doc/library/itertools.rst

-7
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,6 @@ which incur interpreter overhead.
829829
"Count how many times the predicate is true"
830830
return sum(map(pred, iterable))
831831

832-
def pad_none(iterable):
833-
"Returns the sequence elements and then returns None indefinitely."
834-
return chain(iterable, repeat(None))
835-
836832
def ncycles(iterable, n):
837833
"Returns the sequence elements n times"
838834
return chain.from_iterable(repeat(tuple(iterable), n))
@@ -1193,9 +1189,6 @@ which incur interpreter overhead.
11931189
>>> take(5, map(int, repeatfunc(random.random)))
11941190
[0, 0, 0, 0, 0]
11951191

1196-
>>> list(islice(pad_none('abc'), 0, 6))
1197-
['a', 'b', 'c', None, None, None]
1198-
11991192
>>> list(ncycles('abc', 3))
12001193
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
12011194

Diff for: Doc/library/random.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,17 @@ be found in any statistics text.
320320
``beta > 0``. Returned values range between 0 and 1.
321321

322322

323-
.. function:: expovariate(lambd)
323+
.. function:: expovariate(lambd = 1.0)
324324

325325
Exponential distribution. *lambd* is 1.0 divided by the desired
326326
mean. It should be nonzero. (The parameter would be called
327327
"lambda", but that is a reserved word in Python.) Returned values
328328
range from 0 to positive infinity if *lambd* is positive, and from
329329
negative infinity to 0 if *lambd* is negative.
330330

331+
.. versionchanged:: 3.12
332+
Added the default value for ``lambd``.
333+
331334

332335
.. function:: gammavariate(alpha, beta)
333336

Diff for: Doc/library/zipfile.rst

+8-7
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ ZipFile Objects
273273
Access a member of the archive as a binary file-like object. *name*
274274
can be either the name of a file within the archive or a :class:`ZipInfo`
275275
object. The *mode* parameter, if included, must be ``'r'`` (the default)
276-
or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files.
276+
or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files as a
277+
:class:`bytes` object.
277278

278279
:meth:`~ZipFile.open` is also a context manager and therefore supports the
279280
:keyword:`with` statement::
@@ -325,7 +326,7 @@ ZipFile Objects
325326
must be its full name or a :class:`ZipInfo` object. Its file information is
326327
extracted as accurately as possible. *path* specifies a different directory
327328
to extract to. *member* can be a filename or a :class:`ZipInfo` object.
328-
*pwd* is the password used for encrypted files.
329+
*pwd* is the password used for encrypted files as a :class:`bytes` object.
329330

330331
Returns the normalized path created (a directory or new file).
331332

@@ -352,7 +353,7 @@ ZipFile Objects
352353
Extract all members from the archive to the current working directory. *path*
353354
specifies a different directory to extract to. *members* is optional and must
354355
be a subset of the list returned by :meth:`namelist`. *pwd* is the password
355-
used for encrypted files.
356+
used for encrypted files as a :class:`bytes` object.
356357

357358
.. warning::
358359

@@ -377,16 +378,16 @@ ZipFile Objects
377378

378379
.. method:: ZipFile.setpassword(pwd)
379380

380-
Set *pwd* as default password to extract encrypted files.
381+
Set *pwd* (a :class:`bytes` object) as default password to extract encrypted files.
381382

382383

383384
.. method:: ZipFile.read(name, pwd=None)
384385

385386
Return the bytes of the file *name* in the archive. *name* is the name of the
386387
file in the archive, or a :class:`ZipInfo` object. The archive must be open for
387-
read or append. *pwd* is the password used for encrypted files and, if specified,
388-
it will override the default password set with :meth:`setpassword`. Calling
389-
:meth:`read` on a ZipFile that uses a compression method other than
388+
read or append. *pwd* is the password used for encrypted files as a :class:`bytes`
389+
object and, if specified, overrides the default password set with :meth:`setpassword`.
390+
Calling :meth:`read` on a ZipFile that uses a compression method other than
390391
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
391392
:const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
392393
be raised if the corresponding compression module is not available.

Diff for: Doc/tutorial/introduction.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ Indices may also be negative numbers, to start counting from the right::
269269
Note that since -0 is the same as 0, negative indices start from -1.
270270

271271
In addition to indexing, *slicing* is also supported. While indexing is used
272-
to obtain individual characters, *slicing* allows you to obtain substring::
272+
to obtain individual characters, *slicing* allows you to obtain a substring::
273273

274274
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
275275
'Py'

Diff for: Include/cpython/code.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef union {
2828
#define _Py_OPARG(word) ((word).oparg)
2929

3030
static inline void
31-
_py_set_opocde(_Py_CODEUNIT *word, uint8_t opcode)
31+
_py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
3232
{
3333
word->opcode = opcode;
3434
}

Diff for: Include/internal/pycore_global_objects.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ extern "C" {
2828

2929
struct _Py_cached_objects {
3030
PyObject *str_replace_inf;
31+
32+
PyObject *interned_strings;
3133
};
3234

3335
#define _Py_GLOBAL_OBJECT(NAME) \
34-
_PyRuntime.global_objects.NAME
36+
_PyRuntime.static_objects.NAME
3537
#define _Py_SINGLETON(NAME) \
3638
_Py_GLOBAL_OBJECT(singletons.NAME)
3739

38-
struct _Py_global_objects {
40+
struct _Py_static_objects {
3941
struct {
4042
/* Small integers are preallocated in this array so that they
4143
* can be shared.
@@ -59,8 +61,6 @@ struct _Py_global_objects {
5961
PyHamtNode_Bitmap hamt_bitmap_node_empty;
6062
_PyContextTokenMissing context_token_missing;
6163
} singletons;
62-
63-
PyObject *interned;
6464
};
6565

6666
#define _Py_INTERP_CACHED_OBJECT(interp, NAME) \

Diff for: Include/internal/pycore_interp.h

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ struct _is {
142142
// Initialized to _PyEval_EvalFrameDefault().
143143
_PyFrameEvalFunction eval_frame;
144144

145-
PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];
146145
PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
147146
// One bit is set for each non-NULL entry in func_watchers
148147
uint8_t active_func_watchers;

Diff for: Include/internal/pycore_parser.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
#endif
1010

1111

12+
#include "pycore_ast.h" // struct _expr
13+
#include "pycore_global_strings.h" // _Py_DECLARE_STR()
1214
#include "pycore_pyarena.h" // PyArena
1315

1416

@@ -22,9 +24,22 @@ struct _parser_runtime_state {
2224
#else
2325
int _not_used;
2426
#endif
27+
struct _expr dummy_name;
2528
};
2629

27-
30+
_Py_DECLARE_STR(empty, "")
31+
#define _parser_runtime_state_INIT \
32+
{ \
33+
.dummy_name = { \
34+
.kind = Name_kind, \
35+
.v.Name.id = &_Py_STR(empty), \
36+
.v.Name.ctx = Load, \
37+
.lineno = 1, \
38+
.col_offset = 0, \
39+
.end_lineno = 1, \
40+
.end_col_offset = 0, \
41+
}, \
42+
}
2843

2944
extern struct _mod* _PyParser_ASTFromString(
3045
const char *str,

Diff for: Include/internal/pycore_runtime.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ typedef struct pyruntimestate {
163163

164164
/* All the objects that are shared by the runtime's interpreters. */
165165
struct _Py_cached_objects cached_objects;
166-
struct _Py_global_objects global_objects;
166+
struct _Py_static_objects static_objects;
167167

168168
/* The following fields are here to avoid allocation during init.
169169
The data is exposed through _PyRuntimeState pointer fields.

Diff for: Include/internal/pycore_runtime_init.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_object.h"
12+
#include "pycore_parser.h"
1213
#include "pycore_pymem_init.h"
1314
#include "pycore_obmalloc_init.h"
1415

@@ -32,6 +33,7 @@ extern "C" {
3233
until _PyInterpreterState_Enable() is called. */ \
3334
.next_id = -1, \
3435
}, \
36+
.parser = _parser_runtime_state_INIT, \
3537
.imports = { \
3638
.lock = { \
3739
.mutex = NULL, \
@@ -70,7 +72,7 @@ extern "C" {
7072
.types = { \
7173
.next_version_tag = 1, \
7274
}, \
73-
.global_objects = { \
75+
.static_objects = { \
7476
.singletons = { \
7577
.small_ints = _Py_small_ints_INIT, \
7678
.bytes_empty = _PyBytes_SIMPLE_INIT(0, 0), \

Diff for: Include/internal/pycore_typeobject.h

-6
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ struct type_cache_entry {
3636
};
3737

3838
#define MCACHE_SIZE_EXP 12
39-
#define MCACHE_STATS 0
4039

4140
struct type_cache {
4241
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
43-
#if MCACHE_STATS
44-
size_t hits;
45-
size_t misses;
46-
size_t collisions;
47-
#endif
4842
};
4943

5044
/* For now we hard-code this to a value for which we are confident

Diff for: Include/pystats.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern "C" {
88

99
#ifdef Py_STATS
1010

11-
#define SPECIALIZATION_FAILURE_KINDS 32
11+
#define SPECIALIZATION_FAILURE_KINDS 36
1212

1313
/* Stats for determining who is calling PyEval_EvalFrame */
1414
#define EVAL_CALL_TOTAL 0
@@ -65,8 +65,15 @@ typedef struct _object_stats {
6565
uint64_t dict_materialized_new_key;
6666
uint64_t dict_materialized_too_big;
6767
uint64_t dict_materialized_str_subclass;
68+
uint64_t type_cache_hits;
69+
uint64_t type_cache_misses;
70+
uint64_t type_cache_dunder_hits;
71+
uint64_t type_cache_dunder_misses;
72+
uint64_t type_cache_collisions;
6873
} ObjectStats;
6974

75+
#
76+
7077
typedef struct _stats {
7178
OpcodeStats opcode_stats[256];
7279
CallStats call_stats;

Diff for: Lib/ctypes/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ def __init__(self, dlltype):
444444
def __getattr__(self, name):
445445
if name[0] == '_':
446446
raise AttributeError(name)
447-
dll = self._dlltype(name)
447+
try:
448+
dll = self._dlltype(name)
449+
except OSError:
450+
raise AttributeError(name)
448451
setattr(self, name, dll)
449452
return dll
450453

0 commit comments

Comments
 (0)