| 1 |
.. comment: this document may get out of synch with the code, but to generate |
| 2 |
it automatically we would need to use pypy to run sphinx-build |
| 3 |
|
| 4 |
The ``__pypy__`` module |
| 5 |
======================= |
| 6 |
|
| 7 |
The ``__pypy__`` module is the main entry point to special features provided |
| 8 |
by PyPy's standard interpreter. Its content depends on :doc:`configuration |
| 9 |
options <config/index>` which may add new functionality and functions whose |
| 10 |
existence or non-existence indicates the presence of such features. These are |
| 11 |
generally used for compatibility when writing pure python modules that in |
| 12 |
CPython are written in C. Not available in CPython, and so must be used inside a |
| 13 |
``if platform.python_implementation == 'PyPy'`` block or otherwise hidden from |
| 14 |
the CPython interpreter. |
| 15 |
|
| 16 |
Generally available functionality |
| 17 |
--------------------------------- |
| 18 |
|
| 19 |
- ``internal_repr(obj)``: return the interpreter-level representation of an |
| 20 |
object. |
| 21 |
- ``bytebuffer(length)``: return a new read-write buffer of the given length. |
| 22 |
It works like a simplified array of characters (actually, depending on the |
| 23 |
configuration the ``array`` module internally uses this). |
| 24 |
|
| 25 |
- ``attach_gdb()``: start a GDB at the interpreter-level (or a PDB before |
| 26 |
translation). |
| 27 |
|
| 28 |
- ``newmemoryview(buffer, itemsize, format, shape=None, strides=None)``: |
| 29 |
create a `memoryview` instance with the data from ``buffer`` and the |
| 30 |
specified itemsize, format, and optional shape and strides. |
| 31 |
|
| 32 |
- ``bufferable``: a base class that provides a ``__buffer__(self, flags)`` |
| 33 |
method for subclasses to override. This method should return a memoryview |
| 34 |
instance of the class instance. It is called by the C-API's ``tp_as_buffer. |
| 35 |
bf_getbuffer``. |
| 36 |
|
| 37 |
- ``builtinify(func)``: To implement at app-level modules that are, in CPython, |
| 38 |
implemented in C: this decorator protects a function from being ever bound |
| 39 |
like a method. Useful because some tests do things like put a "built-in" |
| 40 |
function on a class and access it via the instance. |
| 41 |
|
| 42 |
- ``hidden_applevel(func)``: Decorator that hides a function's frame from |
| 43 |
app-level |
| 44 |
|
| 45 |
- ``get_hidden_tb()``: Return the traceback of the current exception being |
| 46 |
handled by a frame hidden from applevel. |
| 47 |
|
| 48 |
- ``lookup_special(obj, meth)``: Lookup up a special method on an object. |
| 49 |
- ``do_what_I_mean`` |
| 50 |
|
| 51 |
- ``resizelist_hint(sizehint)`` Reallocate the underlying storage of the argument |
| 52 |
list to sizehint |
| 53 |
|
| 54 |
- ``newlist_hint(...)``: Create a new empty list that has an underlying |
| 55 |
storage of length sizehint |
| 56 |
|
| 57 |
- ``add_memory_pressure(bytes)``: Add memory pressure of estimate bytes. |
| 58 |
Useful when calling a C function that internally allocates a big chunk of |
| 59 |
memory. This instructs the GC to garbage collect sooner than it would |
| 60 |
otherwise. |
| 61 |
|
| 62 |
- ``newdict(type)``: Create a normal dict with a special implementation |
| 63 |
strategy. ``type`` is a string and can be: |
| 64 |
|
| 65 |
* ``"module"`` - equivalent to ``some_module.__dict__`` |
| 66 |
|
| 67 |
* ``"instance"`` - equivalent to an instance dict with a not-changing-much |
| 68 |
set of keys |
| 69 |
|
| 70 |
* ``"kwargs"`` - keyword args dict equivalent of what you get from |
| 71 |
``**kwargs`` in a function, optimized for passing around |
| 72 |
|
| 73 |
* ``"strdict"`` - string-key only dict. This one should be chosen |
| 74 |
automatically |
| 75 |
|
| 76 |
- ``reversed_dict``: Enumerate the keys in a dictionary object in reversed |
| 77 |
order. This is a ``__pypy__`` function instead of being simply done by |
| 78 |
calling reversed(), for CPython compatibility: dictionaries are ordered in |
| 79 |
PyPY but not in Cpython2.7. You should use the collections.OrderedDict |
| 80 |
class for cases where ordering is important. That class implements |
| 81 |
``__reversed__`` by calling __pypy__.reversed_dict() |
| 82 |
|
| 83 |
- ``dict_popitem_first``: Interp-level implementation of |
| 84 |
``OrderedDict.popitem(last=False)``. |
| 85 |
|
| 86 |
- ``delitem_if_value_is`` Atomic equivalent to: ``if dict.get(key) is value: |
| 87 |
del dict[key]``. |
| 88 |
|
| 89 |
SPECIAL USE CASES ONLY! Avoid using on dicts which are specialized, |
| 90 |
e.g. to ``int`` or ``str`` keys, because it switches to the object |
| 91 |
strategy. Also, the ``is`` operation is really pointer equality, so avoid |
| 92 |
using it if ``value`` is an immutable object like ``int`` or ``str``. |
| 93 |
|
| 94 |
- ``move_to_end``: Move the key in a dictionary object into the first or last |
| 95 |
position. This is used in Python 3.x to implement ``OrderedDict.move_to_end()``. |
| 96 |
|
| 97 |
- ``strategy(dict or list or set)``: Return the underlying strategy currently |
| 98 |
used by the object |
| 99 |
|
| 100 |
- ``list_get_physical_size(obj)``: Return the physical (ie overallocated |
| 101 |
size) of the underlying list |
| 102 |
|
| 103 |
- ``specialized_zip_2_lists`` |
| 104 |
- ``locals_to_fast`` |
| 105 |
- ``set_code_callback`` |
| 106 |
- ``save_module_content_for_future_reload`` |
| 107 |
- ``decode_long`` |
| 108 |
- ``side_effects_ok``: For use with the reverse-debugger: this function |
| 109 |
normally returns True, but will return False if we are evaluating a |
| 110 |
debugging command like a watchpoint. You are responsible for not doing any |
| 111 |
side effect at all (including no caching) when evaluating watchpoints. This |
| 112 |
function is meant to help a bit---you can write:: |
| 113 |
|
| 114 |
if not __pypy__.side_effects_ok(): |
| 115 |
skip the caching logic |
| 116 |
|
| 117 |
inside getter methods or properties, to make them usable from |
| 118 |
watchpoints. Note that you need to re-run ``REVDB=.. pypy`` |
| 119 |
after changing the Python code. |
| 120 |
|
| 121 |
- ``stack_almost_full``: Return True if the stack is more than 15/16th full. |
| 122 |
- ``pyos_inputhook``: Call PyOS_InputHook() from the CPython C API |
| 123 |
- ``get_console_cp()``: (Windows-only) Return the console and console output |
| 124 |
code pages. Equivalent to calling ``GetConsoleCP`` and |
| 125 |
``GetConsoleOuputCP``. |
| 126 |
- ``utf8content(u)``: Given a unicode string u, return it's internal byte |
| 127 |
representation. Useful for debugging only. |
| 128 |
- ``os.real_getenv(...)`` gets OS environment variables skipping python code |
| 129 |
- ``os._get_multiarch(...)`` gets the platform-specific multiarch label put into |
| 130 |
``sys.implementation._multiarch``. |
| 131 |
- ``_pypydatetime`` provides base classes with correct C API interactions for |
| 132 |
the pure-python ``datetime`` stdlib module |
| 133 |
|
| 134 |
Fast String Concatenation |
| 135 |
------------------------- |
| 136 |
Rather than in-place concatenation ``+=``, use these to enable fast, minimal |
| 137 |
copy, string building. |
| 138 |
|
| 139 |
- ``builders.StringBuilder`` |
| 140 |
- ``builders.UnicodeBuilder`` |
| 141 |
|
| 142 |
Interacting with the PyPy debug log |
| 143 |
------------------------------------ |
| 144 |
|
| 145 |
The following functions can be used to write your own content to the |
| 146 |
:ref:`PYPYLOG <pypylog>`. |
| 147 |
|
| 148 |
- ``debug_start(category, timestamp=False)``: open a new section; if |
| 149 |
``timestamp`` is ``True``, also return the timestamp which was written to |
| 150 |
the log. |
| 151 |
|
| 152 |
- ``debug_stop(category, timestamp=False)``: close a section opened by |
| 153 |
``debug_start``. |
| 154 |
|
| 155 |
- ``debug_print(...)``: print arbitrary text to the log. |
| 156 |
|
| 157 |
- ``debug_print_once(category, ...)``: equivalent to ``debug_start`` + |
| 158 |
``debug_print`` + ``debug_stop``. |
| 159 |
|
| 160 |
- ``debug_flush``: flush the log. |
| 161 |
|
| 162 |
- ``debug_read_timestamp()``: read the timestamp from the same timer used by |
| 163 |
the log. |
| 164 |
|
| 165 |
- ``debug_get_timestamp_unit()``: get the unit of the value returned by |
| 166 |
``debug_read_timestamp()``. |
| 167 |
|
| 168 |
|
| 169 |
Depending on the architecture and operating system, PyPy uses different ways |
| 170 |
to read timestamps, so the timestamps used in the log file are expressed in |
| 171 |
varying units. It is possible to know which by calling |
| 172 |
``debug_get_timestamp_unit()``, which can be one of the following values: |
| 173 |
|
| 174 |
``tsc`` |
| 175 |
The default on ``x86`` machines: timestamps are expressed in CPU ticks, as |
| 176 |
read by the `Time Stamp Counter`_. |
| 177 |
|
| 178 |
``ns`` |
| 179 |
Timestamps are expressed in nanoseconds. |
| 180 |
|
| 181 |
``QueryPerformanceCounter`` |
| 182 |
On Windows, in case for some reason ``tsc`` is not available: timestamps |
| 183 |
are read using the win API ``QueryPerformanceCounter()``. |
| 184 |
|
| 185 |
|
| 186 |
Unfortunately, there does not seem to be a reliable standard way for |
| 187 |
converting ``tsc`` ticks into nanoseconds, although in practice on modern CPUs |
| 188 |
it is enough to divide the ticks by the maximum nominal frequency of the CPU. |
| 189 |
For this reason, PyPy gives the raw value, and leaves the job of doing the |
| 190 |
conversion to external libraries. |
| 191 |
|
| 192 |
.. _`Time Stamp Counter`: https://en.wikipedia.org/wiki/Time_Stamp_Counter |
| 193 |
|
| 194 |
|
| 195 |
Transparent Proxy Functionality |
| 196 |
------------------------------- |
| 197 |
|
| 198 |
If :ref:`transparent proxies <tproxy>` are enabled (with :config:`objspace.std.withtproxy`) |
| 199 |
the following functions are put into ``__pypy__``: |
| 200 |
|
| 201 |
- ``tproxy(typ, controller)``: Return something that looks like it is of type |
| 202 |
typ. Its behaviour is completely controlled by the controller. See the docs |
| 203 |
about :ref:`transparent proxies <tproxy>` for detail. |
| 204 |
- ``get_tproxy_controller(obj)``: If obj is really a transparent proxy, return |
| 205 |
its controller. Otherwise return None. |
| 206 |
|
| 207 |
|
| 208 |
Additional Clocks for Timing |
| 209 |
---------------------------- |
| 210 |
The ``time`` submodule exposes the platform-dependent clock types such as |
| 211 |
``CLOCK_BOOTTIME``, ``CLOCK_MONOTONIC``, ``CLOCK_MONOTONIC_COARSE``, |
| 212 |
``CLOCK_MONOTONIC_RAW`` and two functions: |
| 213 |
|
| 214 |
- ``clock_gettime(m)`` which returns the clock type time in seconds and |
| 215 |
- ``clock_getres(m)`` which returns the clock resolution in seconds. |
| 216 |
|
| 217 |
These are in the builtin ``time`` module on Python3.3+ and so have been |
| 218 |
removed from ``__pypy__`` on PyPy3.11+ |
| 219 |
|
| 220 |
Extended Signal Handling |
| 221 |
------------------------ |
| 222 |
``thread.signals_enabled`` is a context manager to use in non-main threads. |
| 223 |
enables receiving signals in a "with" statement. More precisely, if a |
| 224 |
signal is received by the process, then the signal handler might be |
| 225 |
called either in the main thread (as usual) or within another thread |
| 226 |
that is within a "with signals_enabled:". This other thread should be |
| 227 |
ready to handle unexpected exceptions that the signal handler might |
| 228 |
raise --- notably KeyboardInterrupt. |
| 229 |
|
| 230 |
Integer Operations with Overflow |
| 231 |
-------------------------------- |
| 232 |
- ``intop`` provides a module with integer operations that have |
| 233 |
two-complement overflow behaviour instead of overflowing to longs |
| 234 |
|
| 235 |
Functionality available on py.py (not after translation) |
| 236 |
-------------------------------------------------------- |
| 237 |
|
| 238 |
- ``isfake(obj)``: returns True if ``obj`` is faked. |