Commit graph

19 commits

Author SHA1 Message Date
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)