Commit graph

43 commits

Author SHA1 Message Date
Protobuf Team Bot
2a36520396 fix missed oom handling in unset required
PiperOrigin-RevId: 956610684
2026-07-30 10:42:17 -07:00
Protobuf Team Bot
01033aca05 Add more oom test coverage and fix errors on copy
PiperOrigin-RevId: 956087593
2026-07-29 14:24:43 -07:00
Protobuf Team Bot
ad6a7e8b64 Check return values and handle alloc failures in python
PiperOrigin-RevId: 955022274
2026-07-27 22:11:16 -07:00
Joshua Haberman
414e4142cd Sync sub-objects in PyUpb_Message_MergeFromString even when decode fails.
In 'PyUpb_Message_MergeFromString', 'upb_Decode' may partially mutate or populate sub-messages on the parent message before failing with a decode error status. Skipping 'PyUpb_Message_SyncSubobjs' when 'status != kUpb_DecodeStatus_Ok' leaves stub sub-object wrappers unsynced in 'unset_subobj_map', causing duplicate keys in 'ObjCache' on subsequent access and leading to heap-use-after-free.

Move 'PyUpb_Message_SyncSubobjs(self)' before the decode status check in 'PyUpb_Message_MergeFromString' so any sub-message wrappers modified during decoding are synced properly.

Add 'testMergeFromStringDecodeErrorSync' to 'third_party/py/google/protobuf/internal/message_test.py' to verify that stub sub-objects remain synced and intact after a failed 'MergeFromString'.

PiperOrigin-RevId: 953615725
2026-07-24 16:53:12 -07:00
Runze Wang
52e82c810c Emit future warning when mutating GetOptions() in OSS
PiperOrigin-RevId: 946657065
2026-07-12 13:41:24 -07:00
Runze Wang
9ac0b22526 Propagate non-AttributeError exceptions in PyUpb_MessageMeta_GetAttr
When cpython_bits.type_getattro(self, name) returns NULL due to an exception raised in a descriptor (such as KeyboardInterrupt, MemoryError, or SystemExit), PyUpb_MessageMeta_GetAttr previously cleared the error and raised AttributeError.

Check PyErr_ExceptionMatches(PyExc_AttributeError) before clearing the error to ensure non-AttributeError exceptions are properly propagated.

PiperOrigin-RevId: 938246969
2026-06-25 16:13:45 -07:00
Charlie Beattie
a9c1c48f23 Internal change.
PiperOrigin-RevId: 937266383
2026-06-24 05:13:39 -07:00
Runze Wang
be53faf37d internal change
PiperOrigin-RevId: 930072020
2026-06-10 14:17:37 -07:00
Runze Wang
403d429eab [py/upb] Make GetOptions() return immutable options for scalar type. UPB will raise a TypeError when options returned GetOptions() by is mutated.
PiperOrigin-RevId: 918559519
2026-05-20 11:33:26 -07:00
Protobuf Team Bot
9b38d6d9a1 Add decoding error details for UPB proto parsing failures.
PiperOrigin-RevId: 883263178
2026-03-13 11:42:08 -07:00
Protobuf Team Bot
1c2af13202 Add missing upb_XXXDef_FindXXXByNameWithSize functions and index methods by name
PiperOrigin-RevId: 878471014
2026-03-04 07:06:24 -08:00
Mikita Belahlazau
4f076d951f Add metadata annotations for generated Python protobuf symbols.
The pyi generator now includes Kythe annotations for:
*   Extension field constants (e.g., `EXTENSION_FIELD`).
*   Field number constants (e.g., `STRING_FIELD_FIELD_NUMBER`).
*   `Create` methods in generated Stubby client classes.

These annotations allow the Python indexer to link these generated symbols back to their definitions in the `.proto` files.

PiperOrigin-RevId: 852971617
2026-01-06 16:05:56 -08:00
Protobuf Team Bot
ae67a4c195 Internal version update.
PiperOrigin-RevId: 852733884
2026-01-06 05:27:44 -08:00
Protobuf Team Bot
60f0834f1c Fixed case of referencing invalid pointer.
When running __new__ MessageMeta, the cache access can crash because it's missing a validation on the object it is given.

PiperOrigin-RevId: 844820889
2025-12-15 10:14:13 -08:00
Jie Luo
5f6c0139a7 Add construction support for repeated Timestamp/Duration/Struct/ListValue.
-Users can now assign list datetime/temedelta to repeated Timestamp/Duration in construction.
-Can assign list dictionary to repeated Struct.
-Can assign list of list to repeated ListValue.

https://github.com/protocolbuffers/protobuf/issues/21541

PiperOrigin-RevId: 804523563
2025-09-08 11:47:20 -07:00
Jie Luo
e5f9ab4d07 Add reference leak check to Python well_known_types_test. Fix two refleak bugs.
-Fix ref leak when assign Struct field with creation
-Fix ref leak on "in" operator for ListValue

PiperOrigin-RevId: 759744611
2025-05-16 14:00:15 -07:00
Joshua Haberman
a014dbbd3d Removed old Python bug workaround now that we have dropped Python <3.8.
PiperOrigin-RevId: 758825465
2025-05-14 14:06:12 -07:00
Jie Luo
e004c8bb3f Add reference leak check to Python timestamp_test and duration_test. Fix a refleak bug when assign datetime/duration to Timestamp/Duration
PiperOrigin-RevId: 758812845
2025-05-14 13:40:46 -07:00
Jie Luo
42749093dc Fix python upb crashes on map/repeated reference stub destructor
The original code removes map/repeated stub from the weak map without reifying in some situations. Changed _Reify() functions to decide if need to delete from WeakMap or SetConcreteSubobj.

PiperOrigin-RevId: 752524491
2025-04-28 18:35:14 -07:00
Mark Jan van Kampen
92d6cbf042 fix: contains check segfaults on empty map (#20446)
Fixes #19624

Closes #20446

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/20446 from mjvankampen:fix/segfault-empty-map ea872030c8
PiperOrigin-RevId: 736519479
2025-03-13 08:50:17 -07:00
Protobuf Team Bot
6235687246 Python protobuf: kwargs initialization now allows dict() for proto map values.
It is now possible to initialize a field of type `map<string, Message>` with nested dict objects:
```
storage_pb2.Directory(
  name='/home/user',
  owner='user',
  content={
    '.bashrc': dict(    # <=== dict() allowed here
      size=1234,
      permissions='PRIVATE',
    ),
  },
)
```

PiperOrigin-RevId: 719437601
2025-01-24 14:42:07 -08:00
Jie Luo
98aab04c0b Fix bug that DiscardUnknownFields did not discard unknown fields of extensions in Py-upb and Php-upb and ruby-upb.
PiperOrigin-RevId: 713147042
2025-01-07 21:33:32 -08:00
Geoffry Song
5752b2d07b [python] Remove semicolon after PyObject_HEAD (#16123)
`PyObject_HEAD` already expands to a field with a semicolon; the extra semicolon is unnecessary, and makes some compilers unhappy.

Closes #16123

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16123 from goffrie:pyobject_head 947438678f
PiperOrigin-RevId: 700735003
2024-11-27 10:08:21 -08:00
Protobuf Team Bot
ae488a016a Use noncontiguous unknown fields API in upb python
PiperOrigin-RevId: 696258062
2024-11-13 13:54:47 -08:00
Hood Chatham
b915e9f44e Fix signature of PyUpb_MessageMeta_Clear (#17959)
A `tp_clear` function should have signature `int f(PyObject*)`. The presence of erroneous extra parameters leads to undefined behavior as indicated in the C specification 6.3.2.3.8. In WebAssembly builds, this causes crashes. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf#page=60

Closes #17959

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17959 from hoodmane:fix-message-meta-clear-sig b618f806a9
PiperOrigin-RevId: 669639153
2024-08-31 05:26:35 -07:00
Jie Luo
e17821cac1 Nextgen Proto Pythonic API: Struct/ListValue assignment and creation
Python dict is now able to be assigned (by create and copy, not reference) and compared with the Protobuf Struct field.
Python list is now able to be assigned (by create and copy, not reference) and compared with the Protobuf ListValue field.

example usage:
  dictionary = {'key1': 5.0, 'key2': {'subkey': 11.0, 'k': False},}
  list_value = [6, 'seven', True, False, None, dictionary]
  msg = more_messages_pb2.WKTMessage(
      optional_struct=dictionary, optional_list_value=list_value
  )
  self.assertEqual(msg.optional_struct, dictionary)
  self.assertEqual(msg.optional_list_value, list_value)

PiperOrigin-RevId: 646099987
2024-06-24 08:41:33 -07:00
Jie Luo
d879311cac Add type name info for upb python parse error message in MergeFromStrig(). To keep same with cpp extension.
PiperOrigin-RevId: 643138099
2024-06-13 15:33:17 -07:00
Joshua Haberman
1f984453e8 Fix forward for Python bytearray.
We need to use the stable functions instead of the unsafe macros, as the latter are not part of the stable ABI.

PiperOrigin-RevId: 642679733
2024-06-12 11:32:16 -07:00
jensbjorgensen
eb67a91cac make bytearray work (again) (#16691)
fix is pretty simple, just check if the type is bytearray and get the bytes if it is

addresses issue: https://github.com/protocolbuffers/protobuf/issues/15911

Closes #16691

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16691 from jensbjorgensen:main 6249e629d7
PiperOrigin-RevId: 642623917
2024-06-12 08:33:37 -07:00
Joshua Haberman
bffd01c649 Fixed a couple of compiler warnings.
PiperOrigin-RevId: 642339381
2024-06-11 11:43:35 -07:00
Jie Luo
b690e729eb Nextgen Proto Pythonic API: Timestamp/Duration assignment, creation and calculation
Timestamp and Duration are now have more support with datetime and timedelta:
- Allows assign python datetime to protobuf DateTime field in addition to current FromDatetime/ToDatetime (Note: will throw exceptions for the differences in supported ranges)
- Allows assign python timedelta to protobuf Duration field in addition to current FromTimedelta/ToTimedelta
- Calculation between Timestamp, Duration, datetime and timedelta will also be supported.

example usage:

from datetime import datetime, timedelta
from event_pb2 import Event
e = Event(start_time=datetime(year=2112, month=2, day=3),
          duration=timedelta(hours=10))
duration = timedelta(hours=10))
end_time = e.start_time + timedelta(hours=4)
e.duration = end_time - e.start_time
PiperOrigin-RevId: 640639168
2024-06-05 13:55:39 -07:00
Hong Shin
0730adb1c1 rename upb_Message_WhichOneof to upb_Message_WhichOneofByDef
and introduce upb_Message_WhichOneof that returns a minitable field without needing reflection

PiperOrigin-RevId: 637932147
2024-05-28 10:04:13 -07:00
Jie Luo
24f27c3b88 Nextgen Proto Pythonic API: Add 'in' operator
(Second attempt. The first attempt missed ListValue)

The “in” operator will be consistent with HasField but a little different with Proto Plus.

The detail behavior of “in” operator in Nextgen

* For WKT Struct (to be consist with old Struct behavior):
    -Raise TypeError if not pass a string
    -Check if the key is in the struct.fields

* For WKT ListValue (to be consist with old behavior):
    -Check if the key is in the list_value.values

* For other messages:
    -Raise ValueError if not pass a string
    -Raise ValueError if the string is not a field
    -For Oneof: Check any field under the oneof is set
    -For has-presence field: check if set
    -For non-has-presence field (include repeated fields): raise ValueError

PiperOrigin-RevId: 631143378
2024-05-06 12:13:09 -07:00
Eric Salo
3d21bc26c9 upb: define kUpb_CompareOption_IncludeUnknownFields flag for upb_Message_IsEqual()
PiperOrigin-RevId: 622335300
2024-04-05 18:44:54 -07:00
Jie Luo
baf211bf63 Automated rollback of commit de8e550e90.
PiperOrigin-RevId: 621326794
2024-04-02 16:28:26 -07:00
Jie Luo
de8e550e90 Nextgen Proto Pythonic API: Add 'in' operator
The “in” operator will be consistent with HasField but a little different with Proto Plus.

The detail behavior of “in” operator in Nextgen for Struct (to be consist with old Struct behavior):
-Raise TypeError if not pass a string
-Check if the key is in the struct.fields

The detail behavior of “in” operator in Nextgen(for other message):
-Raise ValueError if not pass a string
-Raise ValueError if the string is not a field
-For Oneof: Check any field under the oneof is set
-For has-presence field: check if set
-For non-has-presence field (include repeated fields): raise ValueError

PiperOrigin-RevId: 621240977
2024-04-02 11:32:18 -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
Joshua Haberman
b9e4894462 Fixed a SEGV when deep copying a non-reified sub-message.
PiperOrigin-RevId: 600951523
2024-01-23 17:13:47 -08:00
Joshua Haberman
de52944f38 [Python/upb] Fixed SEGV when attempting to delete a message attribute
Deleting an attribute is not allowed in any Proto Python implementation, but upb was not checking for this case.

PiperOrigin-RevId: 589995449
2023-12-11 16:57:49 -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
Protobuf Team Bot
e32d0948e7 Properly untrack Python GC objects during deallocation.
Add PyObject_GC_UnTrack() in deallocation functions for Python types that
have PyTPFLAGS_HAVE_GC set, either explicitly or by inheriting from a type
with GC set. Not untracking before clearing instance data introduces
potential race conditions (if GC happens to run between the partial clearing
and the actual deallocation) and produces a warning under Python 3.11.

(The warning then triggered an assertion failure, which only showed up when
building in Py_DEBUG mode; this therefor also fixes that assertion failure.)

PiperOrigin-RevId: 579827001
2023-11-06 06:44:56 -08: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/message.c (Browse further)