Skip to content

Commit

Permalink
Merge branch 'main' into fastmock
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
carljm committed Dec 16, 2022
2 parents 246772c + d4052d8 commit 2260a54
Show file tree
Hide file tree
Showing 29 changed files with 448 additions and 228 deletions.
9 changes: 9 additions & 0 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ Quick Reference
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
| :c:member:`~PyTypeObject.tp_vectorcall` | :c:type:`vectorcallfunc` | | | | | |
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
| [:c:member:`~PyTypeObject.tp_watched`] | char | | | | | |
+------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+

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


.. c:member:: char PyTypeObject.tp_watched
Internal. Do not use.

.. versionadded:: 3.12


.. _static-types:

Static Types
Expand Down
3 changes: 3 additions & 0 deletions Doc/includes/typestruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ typedef struct _typeobject {

destructor tp_finalize;
vectorcallfunc tp_vectorcall;

/* bitset of which type-watchers care about this type */
char tp_watched;
} PyTypeObject;
7 changes: 0 additions & 7 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,6 @@ which incur interpreter overhead.
"Count how many times the predicate is true"
return sum(map(pred, iterable))

def pad_none(iterable):
"Returns the sequence elements and then returns None indefinitely."
return chain(iterable, repeat(None))

def ncycles(iterable, n):
"Returns the sequence elements n times"
return chain.from_iterable(repeat(tuple(iterable), n))
Expand Down Expand Up @@ -1193,9 +1189,6 @@ which incur interpreter overhead.
>>> take(5, map(int, repeatfunc(random.random)))
[0, 0, 0, 0, 0]

>>> list(islice(pad_none('abc'), 0, 6))
['a', 'b', 'c', None, None, None]

>>> list(ncycles('abc', 3))
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

Expand Down
5 changes: 4 additions & 1 deletion Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,17 @@ be found in any statistics text.
``beta > 0``. Returned values range between 0 and 1.


.. function:: expovariate(lambd)
.. function:: expovariate(lambd = 1.0)

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

.. versionchanged:: 3.12
Added the default value for ``lambd``.


.. function:: gammavariate(alpha, beta)

Expand Down
15 changes: 8 additions & 7 deletions Doc/library/zipfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ ZipFile Objects
Access a member of the archive as a binary file-like object. *name*
can be either the name of a file within the archive or a :class:`ZipInfo`
object. The *mode* parameter, if included, must be ``'r'`` (the default)
or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files.
or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files as a
:class:`bytes` object.

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

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

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

.. warning::

Expand All @@ -377,16 +378,16 @@ ZipFile Objects

.. method:: ZipFile.setpassword(pwd)

Set *pwd* as default password to extract encrypted files.
Set *pwd* (a :class:`bytes` object) as default password to extract encrypted files.


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

Return the bytes of the file *name* in the archive. *name* is the name of the
file in the archive, or a :class:`ZipInfo` object. The archive must be open for
read or append. *pwd* is the password used for encrypted files and, if specified,
it will override the default password set with :meth:`setpassword`. Calling
:meth:`read` on a ZipFile that uses a compression method other than
read or append. *pwd* is the password used for encrypted files as a :class:`bytes`
object and, if specified, overrides the default password set with :meth:`setpassword`.
Calling :meth:`read` on a ZipFile that uses a compression method other than
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
:const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
be raised if the corresponding compression module is not available.
Expand Down
2 changes: 1 addition & 1 deletion Doc/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Indices may also be negative numbers, to start counting from the right::
Note that since -0 is the same as 0, negative indices start from -1.

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

>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
Expand Down
2 changes: 1 addition & 1 deletion Include/cpython/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef union {
#define _Py_OPARG(word) ((word).oparg)

static inline void
_py_set_opocde(_Py_CODEUNIT *word, uint8_t opcode)
_py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
{
word->opcode = opcode;
}
Expand Down
8 changes: 4 additions & 4 deletions Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ extern "C" {

struct _Py_cached_objects {
PyObject *str_replace_inf;

PyObject *interned_strings;
};

#define _Py_GLOBAL_OBJECT(NAME) \
_PyRuntime.global_objects.NAME
_PyRuntime.static_objects.NAME
#define _Py_SINGLETON(NAME) \
_Py_GLOBAL_OBJECT(singletons.NAME)

struct _Py_global_objects {
struct _Py_static_objects {
struct {
/* Small integers are preallocated in this array so that they
* can be shared.
Expand All @@ -59,8 +61,6 @@ struct _Py_global_objects {
PyHamtNode_Bitmap hamt_bitmap_node_empty;
_PyContextTokenMissing context_token_missing;
} singletons;

PyObject *interned;
};

#define _Py_INTERP_CACHED_OBJECT(interp, NAME) \
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ struct _is {
// Initialized to _PyEval_EvalFrameDefault().
_PyFrameEvalFunction eval_frame;

PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];
PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
// One bit is set for each non-NULL entry in func_watchers
uint8_t active_func_watchers;
Expand Down
17 changes: 16 additions & 1 deletion Include/internal/pycore_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C" {
#endif


#include "pycore_ast.h" // struct _expr
#include "pycore_global_strings.h" // _Py_DECLARE_STR()
#include "pycore_pyarena.h" // PyArena


Expand All @@ -22,9 +24,22 @@ struct _parser_runtime_state {
#else
int _not_used;
#endif
struct _expr dummy_name;
};


_Py_DECLARE_STR(empty, "")
#define _parser_runtime_state_INIT \
{ \
.dummy_name = { \
.kind = Name_kind, \
.v.Name.id = &_Py_STR(empty), \
.v.Name.ctx = Load, \
.lineno = 1, \
.col_offset = 0, \
.end_lineno = 1, \
.end_col_offset = 0, \
}, \
}

extern struct _mod* _PyParser_ASTFromString(
const char *str,
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ typedef struct pyruntimestate {

/* All the objects that are shared by the runtime's interpreters. */
struct _Py_cached_objects cached_objects;
struct _Py_global_objects global_objects;
struct _Py_static_objects static_objects;

/* The following fields are here to avoid allocation during init.
The data is exposed through _PyRuntimeState pointer fields.
Expand Down
4 changes: 3 additions & 1 deletion Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
#endif

#include "pycore_object.h"
#include "pycore_parser.h"
#include "pycore_pymem_init.h"
#include "pycore_obmalloc_init.h"

Expand All @@ -32,6 +33,7 @@ extern "C" {
until _PyInterpreterState_Enable() is called. */ \
.next_id = -1, \
}, \
.parser = _parser_runtime_state_INIT, \
.imports = { \
.lock = { \
.mutex = NULL, \
Expand Down Expand Up @@ -70,7 +72,7 @@ extern "C" {
.types = { \
.next_version_tag = 1, \
}, \
.global_objects = { \
.static_objects = { \
.singletons = { \
.small_ints = _Py_small_ints_INIT, \
.bytes_empty = _PyBytes_SIMPLE_INIT(0, 0), \
Expand Down
6 changes: 0 additions & 6 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ struct type_cache_entry {
};

#define MCACHE_SIZE_EXP 12
#define MCACHE_STATS 0

struct type_cache {
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
#if MCACHE_STATS
size_t hits;
size_t misses;
size_t collisions;
#endif
};

/* For now we hard-code this to a value for which we are confident
Expand Down
9 changes: 8 additions & 1 deletion Include/pystats.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern "C" {

#ifdef Py_STATS

#define SPECIALIZATION_FAILURE_KINDS 32
#define SPECIALIZATION_FAILURE_KINDS 36

/* Stats for determining who is calling PyEval_EvalFrame */
#define EVAL_CALL_TOTAL 0
Expand Down Expand Up @@ -65,8 +65,15 @@ typedef struct _object_stats {
uint64_t dict_materialized_new_key;
uint64_t dict_materialized_too_big;
uint64_t dict_materialized_str_subclass;
uint64_t type_cache_hits;
uint64_t type_cache_misses;
uint64_t type_cache_dunder_hits;
uint64_t type_cache_dunder_misses;
uint64_t type_cache_collisions;
} ObjectStats;

#

typedef struct _stats {
OpcodeStats opcode_stats[256];
CallStats call_stats;
Expand Down
5 changes: 4 additions & 1 deletion Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ def __init__(self, dlltype):
def __getattr__(self, name):
if name[0] == '_':
raise AttributeError(name)
dll = self._dlltype(name)
try:
dll = self._dlltype(name)
except OSError:
raise AttributeError(name)
setattr(self, name, dll)
return dll

Expand Down
Loading

0 comments on commit 2260a54

Please sign in to comment.