dep-protobuf/python
2024-01-24 07:08:16 -08:00
..
dist upb: delete upb:upb, upb:collections, upb/upb.hpp, upb/collections/ 2024-01-12 12:44:04 -08:00
docs Copybara import of the project: 2022-11-11 10:31:20 -08:00
google Fixed a bug in cl/600990001 -- we need to propagate the as_utf8 parameter that was passed. 2024-01-24 07:08:16 -08:00
pb_unit_tests Add "extend repeated with nothing" tests back for upb python 2024-01-05 09:46:52 -08:00
protobuf_distutils Add support and partial CI coverage for Python 3.12 2023-11-08 08:53:27 -08:00
.repo-metadata.json Sync from Piper @457757259 2022-06-28 17:01:18 +00:00
BUILD.bazel upb: delete the aliases for upb:hash, upb:lex, upb:reflection_internal 2024-01-01 18:21:58 -08:00
build_targets.bzl Breaking Change: Use Editions features in Java full runtimes. 2024-01-22 09:57:26 -08:00
convert.c Make the utf8_range implementation just in C 2023-12-14 09:25:06 -08:00
convert.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
descriptor.c Remove edition getter from python descriptor APIs 2024-01-05 09:01:37 -08:00
descriptor.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
descriptor_containers.c Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
descriptor_containers.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
descriptor_pool.c upb: add a non-void typedef for upb_Message 2023-12-21 08:12:16 -08:00
descriptor_pool.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
extension_dict.c Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
extension_dict.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
internal.bzl Fix bug in _internal_copy_files where the rule would fail in downstream repositories. 2023-05-02 17:24:22 +00:00
MANIFEST.in Add LICENSE to released python packages (#8913) 2021-08-27 15:41:58 -07:00
map.c upb: add a non-void typedef for upb_Message 2023-12-21 08:12:16 -08:00
map.h upb: add 'static' to several Python map functions 2023-12-19 17:52:06 -08:00
message.c Fixed a SEGV when deep copying a non-reified sub-message. 2024-01-23 17:13:47 -08:00
message.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
minimal_test.py Reorganize upb file structure 2023-09-26 14:38:35 -07:00
protobuf.c Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
protobuf.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
py_extension.bzl Expose editions in Python/upb. 2023-11-10 15:20:41 -08:00
python_api.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
python_version_test.py Prepare to reorganize upb file structure 2023-09-20 17:02:51 -07:00
README.md Reorganize upb file structure 2023-09-26 14:38:35 -07:00
repeated.c Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
repeated.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
requirements.txt Add support and partial CI coverage for Python 3.12 2023-11-08 08:53:27 -08:00
setup.cfg add setup.cfg for building wheels 2016-07-29 14:02:07 -07:00
setup.py Add support and partial CI coverage for Python 3.12 2023-11-08 08:53:27 -08:00
unknown_fields.c Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
unknown_fields.h Update remainder of upb to new short license style. 2023-11-20 13:43:32 -08:00
version_script.lds Reorganize upb file structure 2023-09-26 14:38:35 -07:00

Protocol Buffers Python

This directory contains the Protobuf library for Python.

In most cases you should install the library using pip or another package manager:

$ pip install protobuf

The packages released on https://pypi.org/project/protobuf/#files include both a source distribution and binary wheels.

For user documentation about how to use Protobuf Python, see https://protobuf.dev/getting-started/pythontutorial/

Building packages from this repo

If for some reason you wish to build the packages directly from this repo, you can use the following Bazel commands:

$ bazel build //python/dist:source_wheel
$ bazel build //python/dist:binary_wheel

The binary wheel will build against whatever version of Python is installed on your system. The source package is always the same and does not depend on a local version of Python.

Implementation backends

There are three separate implementations of Python Protobuf. All of them offer the same API and are thus functionally the same, though they have very different performance characteristics.

The runtime library contains a switching layer that can choose between these backends at runtime. Normally it will choose between them automatically, using priority-ordered list, and skipping any backends that are not available. However you can request a specific backend by setting the PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION environment variable to one of the following values:

  1. upb: Built on the upb C library, this is a new extension module released in 4.21.0. It offers better performance than any of the previous backends, and it is now the default. It is distributed in our PyPI packages, and requires no special installation. The code for this module lives in this directory.
  2. cpp: This extension module wraps the C++ protobuf library. It is deprecated and is no longer released in our PyPI packages, however it is still used in some legacy cases where apps want to perform zero-copy message sharing between Python and C++. It must be installed separately before it can be used. The code for this module lives in google/protobuf/pyext.
  3. python: The pure-Python backend, this does not require any extension module to be present on the system. The code for the pure-Python backend lives in google/protobuf/internal

The order given above is the general priority order, with upb being preferred the most and the python backend preferred the least. However this ordering can be overridden by the presence of a google.protobuf.internal._api_implementation module. See the logic in api_implementation.py for details.

You can check which backend you are using with the following snippet:

$ python
Python 3.10.9 (main, Dec  7 2022, 13:47:07) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.protobuf.internal import api_implementation
>>> print(api_implementation.Type())
upb

This is not an officially-supported or stable API, but it is useful for ad hoc diagnostics.

More information about sharing messages between Python and C++ is available here: https://protobuf.dev/reference/python/python-generated/#sharing-messages

Code generator

The code for the Protobuf Python code generator lives in //src/google/protobuf/compiler/python. The code generator can output two different files for each proto foo.proto:

  • foo_pb2.py: The module you import to actually use the protos.
  • foo_pb2.pyi: A stub file that describes the interface of the protos.

The foo_pb2.pyi file is useful for IDEs or for users who want to read the output file. The foo_pb2.py file is optimized for fast loading and is not readable at all.

Note that the pyi file is only generated if you pass the pyi_out option to protoc:

$ protoc --python_out=pyi_out:output_dir