Commit graph

17 commits

Author SHA1 Message Date
Protobuf Team Bot
ad6a7e8b64 Check return values and handle alloc failures in python
PiperOrigin-RevId: 955022274
2026-07-27 22:11:16 -07:00
Runze Wang
ae4f98fe70 Fix segfault via strcmp(NULL) in numpy type detection
When PyObject_GetAttrString returns NULL (e.g., because __name__ or __module__ attribute access raises an exception on metaclass), PyUpb_GetStrData returns NULL. Passing NULL to strcmp previously caused a segmentation fault.

Check for NULL before calling PyUpb_GetStrData and strcmp, and call PyErr_Clear() when attribute lookup fails.

PiperOrigin-RevId: 938039167
2026-06-25 09:52:49 -07:00
Charlie Beattie
2906b1e81b Internal change.
PiperOrigin-RevId: 927241088
2026-06-05 05:35:17 -07:00
Protobuf Team Bot
5be0a43cd6 Avoid NULL pointer dereference in PyUpb_PyToUpbEnum if run with a malformed UTF-8 str (e.g. "\ud800")
Add test coverage of 'assign string to enum-typed field' in general (including this case). This appears to be accepted only Py-upb and not otherwise.

Fixes https://github.com/protocolbuffers/protobuf/issues/27106

PiperOrigin-RevId: 908148411
2026-04-30 07:01:58 -07:00
Jie Luo
5b116fe2f1 Breaking change: Raise errors in OSS when assign bool to int/enum field in Python Proto.
https://protobuf.dev/news/2025-09-19/#python-reject-bool-enum-int

PiperOrigin-RevId: 843305319
2025-12-11 11:28:56 -08:00
Jie Luo
4ee55d7e31 Raise warnings when assign bool to int/enum field in Python Proto. This will turn into error in 34.0 release.
PiperOrigin-RevId: 789619218
2025-07-31 22:48:32 -07:00
Peter Hawkins
64f5694cb2 [numpy] Fix breakage in users of Python protobufs under NumPy 2.3rc1.
As of NumPy 2.3.0rc1, numpy.bool scalars can no longer be interpreted as index values (https://github.com/numpy/numpy/releases/tag/v2.3.0rc1). This causes protobuf no longer to accept a np.bool scalar as a legal value for a boolean field.

We have two options:
a) either we can change protobuf so that it continues to accept NumPy boolean scalars (this change), or
b) decide that protobuf should reject NumPy boolean scalars and that users must update their code to cast to a Python bool explicitly.

I have no strong opinion as to which, but option (a) seems less disruptive.

No test updates are needed: the existing tests fail under NumPy 2.3.

PiperOrigin-RevId: 766629310
2025-06-03 06:10:18 -07:00
Protobuf Team Bot
9e44913d43 Performance optimization: check for python float before type-checking numpy.
Checking whether the object is a numpy array checking is pretty expensive, so we only want to do it when the object is not trivially a float.

This makes appending to a repeated field of float ~3x faster in a common case:

```
_bench_append, 1000000, 284.1
_bench_extend, 1000000, 209.0
_bench_assign, 1000000, 175.8
_bench_pybind11, 1000000, 3.7
```

```
_bench_append, 1000000, 128.4
_bench_extend, 1000000, 67.1
_bench_assign, 1000000, 57.2
_bench_pybind11, 1000000, 3.5
```

PiperOrigin-RevId: 707811151
2024-12-19 00:28:49 -08:00
Mike Kruskal
72b3eda2ec Breaking change: fix closed enum validation under editions
See https://protobuf.dev/news/2024-10-02/#python-setter-validation

PiperOrigin-RevId: 707612946
2024-12-18 11:25:33 -08:00
Mike Kruskal
ce6dbe478a Setup fix for python/upb for the enforcement of closed enums in editions.
This will be a breaking change in the 30.0 release.

PiperOrigin-RevId: 676683430
2024-09-19 21:29:58 -07:00
Eric Salo
e1a19ba8bb upb: add 'options' arg to upb_Message_IsEqual()
PiperOrigin-RevId: 614707625
2024-03-11 10:16:13 -07:00
Eric Salo
c69ed472cd upb: implement minitable-based field iterators
PiperOrigin-RevId: 605718863
2024-02-09 13:39:18 -08:00
Protobuf Team Bot
9c7d2b9d83 Make the utf8_range implementation just in C
PiperOrigin-RevId: 590961088
2023-12-14 09:25:06 -08:00
Protobuf Team Bot
0fab773c1d Update remainder of upb to new short license style.
PiperOrigin-RevId: 584116886
2023-11-20 13:43:32 -08:00
Joshua Haberman
b0eeb356b8 Fixed Python memory leak in map lookup.
Previously we were allocating memory on the message's arena every time we performed a `map[key]` or `map.get(key)` operation.  This is unnecessary, as the key's data is only needed ephemerally, for the duration of the lookup, and we can therefore alias the Python object's string data instead of copying it.

This required fixing a bug in the convert.c operation.  Previously in the `arena==NULL` case, if the user passes a bytes object instead of a unicode string, the code would return a pointer to a temporary Python object that had already been freed, leading to use-after-free.  I fixed this by referencing the bytes object's data directly, and using utf8_range to verify the UTF-8.

Fixes: https://github.com/protocolbuffers/protobuf/issues/14571
PiperOrigin-RevId: 578563555
2023-11-01 10:34:44 -07:00
Eric Salo
07fba1da07 upb: merge ":collections" into ":message"
PiperOrigin-RevId: 569594353
2023-09-29 14:53:28 -07:00
Adam Cozzette
501ececd39 Reorganize upb file structure
This change moves almost everything in the `upb/` directory up one level, so
that for example `upb/upb/generated_code_support.h` becomes just
`upb/generated_code_support.h`. The only exceptions I made to this were that I
left `upb/cmake` and `upb/BUILD` where they are, mostly because that avoids
conflict with other files and the current locations seem reasonable for now.

The `python/` directory is a little bit of a challenge because we had to merge
the existing directory there with `upb/python/`. I made `upb/python/BUILD` into
the BUILD file for the merged directory, and it effectively loads the contents
of the other BUILD file via `python/build_targets.bzl`, but I plan to clean
this up soon.

PiperOrigin-RevId: 568651768
2023-09-26 14:38:35 -07:00
Renamed from upb/python/convert.c (Browse further)