Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into new-symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Feb 28, 2024
2 parents c355bc6 + 0a61e23 commit 5362360
Show file tree
Hide file tree
Showing 35 changed files with 1,829 additions and 370 deletions.
1 change: 1 addition & 0 deletions .github/workflows/reusable-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
HOMEBREW_NO_ANALYTICS: 1
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_NO_INSTALL_CLEANUP: 1
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
PYTHONSTRICTEXTENSIONBUILD: 1
strategy:
fail-fast: false
Expand Down
20 changes: 15 additions & 5 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,20 @@ are always available. They are listed here in alphabetical order.

.. function:: eval(expression, globals=None, locals=None)

The arguments are a string and optional globals and locals. If provided,
*globals* must be a dictionary. If provided, *locals* can be any mapping
object.
:param expression:
A Python expression.
:type expression: :class:`str` | :ref:`code object <code-objects>`

:param globals:
The global namespace (default: ``None``).
:type globals: :class:`dict` | ``None``

:param locals:
The local namespace (default: ``None``).
:type locals: :term:`mapping` | ``None``

:returns: The result of the evaluated expression.
:raises: Syntax errors are reported as exceptions.

The *expression* argument is parsed and evaluated as a Python expression
(technically speaking, a condition list) using the *globals* and *locals*
Expand All @@ -545,8 +556,7 @@ are always available. They are listed here in alphabetical order.
:term:`nested scopes <nested scope>` (non-locals) in the enclosing
environment.

The return value is the result of
the evaluated expression. Syntax errors are reported as exceptions. Example:
Example:

>>> x = 1
>>> eval('x+1')
Expand Down
4 changes: 2 additions & 2 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Extending :class:`JSONEncoder`::
... if isinstance(obj, complex):
... return [obj.real, obj.imag]
... # Let the base class default method raise the TypeError
... return json.JSONEncoder.default(self, obj)
... return super().default(obj)
...
>>> json.dumps(2 + 1j, cls=ComplexEncoder)
'[2.0, 1.0]'
Expand Down Expand Up @@ -504,7 +504,7 @@ Encoders and Decoders
else:
return list(iterable)
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, o)
return super().default(o)


.. method:: encode(o)
Expand Down
77 changes: 42 additions & 35 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,74 +55,81 @@ typedef struct _Py_DebugOffsets {
uint64_t version;
// Runtime state offset;
struct _runtime_state {
off_t finalizing;
off_t interpreters_head;
uint64_t finalizing;
uint64_t interpreters_head;
} runtime_state;

// Interpreter state offset;
struct _interpreter_state {
off_t next;
off_t threads_head;
off_t gc;
off_t imports_modules;
off_t sysdict;
off_t builtins;
off_t ceval_gil;
off_t gil_runtime_state_locked;
off_t gil_runtime_state_holder;
uint64_t next;
uint64_t threads_head;
uint64_t gc;
uint64_t imports_modules;
uint64_t sysdict;
uint64_t builtins;
uint64_t ceval_gil;
uint64_t gil_runtime_state_locked;
uint64_t gil_runtime_state_holder;
} interpreter_state;

// Thread state offset;
struct _thread_state{
off_t prev;
off_t next;
off_t interp;
off_t current_frame;
off_t thread_id;
off_t native_thread_id;
uint64_t prev;
uint64_t next;
uint64_t interp;
uint64_t current_frame;
uint64_t thread_id;
uint64_t native_thread_id;
} thread_state;

// InterpreterFrame offset;
struct _interpreter_frame {
off_t previous;
off_t executable;
off_t instr_ptr;
off_t localsplus;
off_t owner;
uint64_t previous;
uint64_t executable;
uint64_t instr_ptr;
uint64_t localsplus;
uint64_t owner;
} interpreter_frame;

// CFrame offset;
struct _cframe {
off_t current_frame;
off_t previous;
uint64_t current_frame;
uint64_t previous;
} cframe;

// Code object offset;
struct _code_object {
off_t filename;
off_t name;
off_t linetable;
off_t firstlineno;
off_t argcount;
off_t localsplusnames;
off_t localspluskinds;
off_t co_code_adaptive;
uint64_t filename;
uint64_t name;
uint64_t linetable;
uint64_t firstlineno;
uint64_t argcount;
uint64_t localsplusnames;
uint64_t localspluskinds;
uint64_t co_code_adaptive;
} code_object;

// PyObject offset;
struct _pyobject {
off_t ob_type;
uint64_t ob_type;
} pyobject;

// PyTypeObject object offset;
struct _type_object {
off_t tp_name;
uint64_t tp_name;
} type_object;

// PyTuple object offset;
struct _tuple_object {
off_t ob_item;
uint64_t ob_item;
} tuple_object;

// Unicode object offset;
struct _unicode_object {
uint64_t state;
uint64_t length;
size_t asciiobject_size;
} unicode_object;
} _Py_DebugOffsets;

/* Full Python runtime state */
Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ extern PyTypeObject _PyExc_MemoryError;
.tuple_object = { \
.ob_item = offsetof(PyTupleObject, ob_item), \
}, \
.unicode_object = { \
.state = offsetof(PyUnicodeObject, _base._base.state), \
.length = offsetof(PyUnicodeObject, _base._base.length), \
.asciiobject_size = sizeof(PyASCIIObject), \
}, \
}, \
.allocators = { \
.standard = _pymem_allocators_standard_INIT(runtime), \
Expand Down
6 changes: 4 additions & 2 deletions Include/internal/pycore_unionobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

extern PyTypeObject _PyUnion_Type;
// For extensions created by test_peg_generator
PyAPI_DATA(PyTypeObject) _PyUnion_Type;
PyAPI_FUNC(PyObject *) _Py_union_type_or(PyObject *, PyObject *);

#define _PyUnion_Check(op) Py_IS_TYPE((op), &_PyUnion_Type)
extern PyObject *_Py_union_type_or(PyObject *, PyObject *);

#define _PyGenericAlias_Check(op) PyObject_TypeCheck((op), &Py_GenericAliasType)
extern PyObject *_Py_subs_parameters(PyObject *, PyObject *, PyObject *, PyObject *);
Expand Down
2 changes: 1 addition & 1 deletion Lib/json/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def default(self, o):
else:
return list(iterable)
# Let the base class default method raise the TypeError
return JSONEncoder.default(self, o)
return super().default(o)
"""
raise TypeError(f'Object of type {o.__class__.__name__} '
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,46 @@ def sleeping_retry(timeout, err_msg=None, /,
delay = min(delay * 2, max_delay)


class CPUStopwatch:
"""Context manager to roughly time a CPU-bound operation.
Disables GC. Uses CPU time if it can (i.e. excludes sleeps & time of
other processes).
N.B.:
- This *includes* time spent in other threads.
- Some systems only have a coarse resolution; check
stopwatch.clock_info.rseolution if.
Usage:
with ProcessStopwatch() as stopwatch:
...
elapsed = stopwatch.seconds
resolution = stopwatch.clock_info.resolution
"""
def __enter__(self):
get_time = time.process_time
clock_info = time.get_clock_info('process_time')
if get_time() <= 0: # some platforms like WASM lack process_time()
get_time = time.monotonic
clock_info = time.get_clock_info('monotonic')
self.context = disable_gc()
self.context.__enter__()
self.get_time = get_time
self.clock_info = clock_info
self.start_time = get_time()
return self

def __exit__(self, *exc):
try:
end_time = self.get_time()
finally:
result = self.context.__exit__(*exc)
self.seconds = end_time - self.start_time
return result


@contextlib.contextmanager
def adjust_int_max_str_digits(max_digits):
"""Temporarily change the integer string conversion length limit."""
Expand Down
Loading

0 comments on commit 5362360

Please sign in to comment.