Merge pull request #463 from haberman/python-repeated

Added repeated fields for Python
pull/13171/head
Joshua Haberman 3 years ago committed by GitHub
commit b58824152e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      python/BUILD
  2. 6
      python/convert.c
  3. 35
      python/message.c
  4. 16
      python/minimal_test.py
  5. 4
      python/pb_unit_tests/descriptor_test_wrapper.py
  6. 3
      python/pb_unit_tests/json_format_test_wrapper.py
  7. 100
      python/pb_unit_tests/message_test_wrapper.py
  8. 47
      python/pb_unit_tests/reflection_test_wrapper.py
  9. 104
      python/pb_unit_tests/text_format_test_wrapper.py
  10. 33
      python/pb_unit_tests/unknown_fields_test_wrapper.py
  11. 8
      python/pb_unit_tests/well_known_types_test_wrapper.py
  12. 3
      python/protobuf.c
  13. 4
      python/protobuf.h
  14. 738
      python/repeated.c
  15. 66
      python/repeated.h
  16. 31
      upb/reflection.c
  17. 17
      upb/reflection.h

@ -44,6 +44,8 @@ cc_binary(
"protobuf.c",
"protobuf.h",
"python.h",
"repeated.c",
"repeated.h",
],
copts = UPB_DEFAULT_COPTS + [
# The Python API requires patterns that are ISO C incompatible, like

@ -27,6 +27,7 @@
#include "python/convert.h"
#include "python/message.h"
#include "python/protobuf.h"
#include "upb/reflection.h"
#include "upb/util/compare.h"
@ -53,9 +54,8 @@ PyObject* PyUpb_UpbToPy(upb_msgval val, const upb_fielddef *f, PyObject *arena)
case UPB_TYPE_STRING:
return PyUnicode_DecodeUTF8(val.str_val.data, val.str_val.size, NULL);
case UPB_TYPE_MESSAGE:
PyErr_Format(PyExc_NotImplementedError,
"Conversion of message types not yet implemented");
return NULL;
return PyUpb_CMessage_Get((upb_msg*)val.msg_val,
upb_fielddef_msgsubdef(f), arena);
default:
PyErr_Format(PyExc_SystemError,
"Getting a value from a field of unknown type %d",

@ -29,6 +29,7 @@
#include "python/convert.h"
#include "python/descriptor.h"
#include "python/repeated.h"
#include "upb/def.h"
#include "upb/reflection.h"
#include "upb/text_encode.h"
@ -335,14 +336,14 @@ static bool PyUpb_CMessage_InitMapAttribute(PyObject* _self, PyObject* name,
return false;
}
static bool PyUpb_CMessage_InitRepeatedAttribute(void) {
// TODO(haberman): disabled until repeated container is in.
// PyObject* repeated = PyUpb_CMessage_GetAttr(_self, name);
// PyObject* tmp = PyUpb_RepeatedContainer_Extend(repeated, value);
// if (!tmp) return -1;
// Py_DECREF(tmp);
PyErr_SetString(PyExc_NotImplementedError, "repeated init");
return false;
static bool PyUpb_CMessage_InitRepeatedAttribute(PyObject* _self,
PyObject* name,
PyObject* value) {
PyObject* repeated = PyUpb_CMessage_GetAttr(_self, name);
PyObject* tmp = PyUpb_RepeatedContainer_Extend(repeated, value);
if (!tmp) return false;
Py_DECREF(tmp);
return true;
}
static bool PyUpb_CMessage_InitMessageAttribute(PyObject* _self, PyObject* name,
@ -413,7 +414,7 @@ int PyUpb_CMessage_InitAttributes(PyObject* _self, PyObject* args,
if (upb_fielddef_ismap(f)) {
if (!PyUpb_CMessage_InitMapAttribute(_self, name, f, value)) return -1;
} else if (upb_fielddef_isseq(f)) {
if (!PyUpb_CMessage_InitRepeatedAttribute()) return -1;
if (!PyUpb_CMessage_InitRepeatedAttribute(_self, name, value)) return -1;
} else if (upb_fielddef_issubmsg(f)) {
if (!PyUpb_CMessage_InitMessageAttribute(_self, name, value)) return -1;
} else {
@ -601,8 +602,7 @@ static void PyUpb_CMessage_SyncSubobjs(PyUpb_CMessage* self) {
// PyUpb_MapContainer_SwitchToSet(obj, (upb_map*)msgval.map_val);
} else if (upb_fielddef_isseq(f)) {
if (!msgval.array_val) continue;
// TODO(haberman): re-enable when repeated fields are checked in.
// PyUpb_RepeatedContainer_SwitchToSet(obj, (upb_array*)msgval.array_val);
PyUpb_RepeatedContainer_Reify(obj, (upb_array*)msgval.array_val);
} else {
PyUpb_CMessage* sub = (void*)obj;
assert(self == sub->ptr.parent);
@ -718,6 +718,7 @@ PyObject* PyUpb_CMessage_Get(upb_msg* u_msg, const upb_msgdef* m,
PyObject* PyUpb_CMessage_GetUnsetWrapper(PyUpb_CMessage* self,
const upb_fielddef* field) {
PyObject* _self = (void*)self;
// Non-present messages return magical "empty" messages that point to their
// parent, but will materialize into real messages if any fields are
// assigned.
@ -734,10 +735,7 @@ PyObject* PyUpb_CMessage_GetUnsetWrapper(PyUpb_CMessage* self,
PyErr_SetString(PyExc_NotImplementedError, "unset map");
return NULL;
} else if (upb_fielddef_isseq(field)) {
// TODO(haberman): re-enable when repeated fields are checked in.
// subobj = PyUpb_RepeatedContainer_NewUnset(_self, field, self->arena);
PyErr_SetString(PyExc_NotImplementedError, "unset repeated");
return NULL;
subobj = PyUpb_RepeatedContainer_NewUnset(_self, field, self->arena);
} else {
subobj = PyUpb_CMessage_NewUnset(&self->ob_base, field, self->arena);
}
@ -760,11 +758,8 @@ PyObject* PyUpb_CMessage_GetPresentWrapper(PyUpb_CMessage* self,
PyErr_SetString(PyExc_NotImplementedError, "access map");
return NULL;
} else {
// TODO(haberman): re-enable when repeated fields are checked in.
// return PyUpb_RepeatedContainer_GetOrCreateWrapper(mutval.array, _self,
// field, self->arena);
PyErr_SetString(PyExc_NotImplementedError, "access repeated");
return NULL;
return PyUpb_RepeatedContainer_GetOrCreateWrapper(mutval.array, field,
self->arena);
}
}

@ -30,6 +30,7 @@
import unittest
from google.protobuf.pyext import _message
from google.protobuf.internal import api_implementation
from google.protobuf import unittest_pb2
class TestMessageExtension(unittest.TestCase):
@ -53,6 +54,21 @@ class TestMessageExtension(unittest.TestCase):
self.assertTrue(_message._IS_UPB)
self.assertEqual(api_implementation.Type(), "cpp")
def test_repeated_field_slice_delete(self):
def test_slice(start, end, step):
vals = list(range(20))
message = unittest_pb2.TestAllTypes(repeated_int32=vals)
del vals[start:end:step]
del message.repeated_int32[start:end:step]
self.assertEqual(vals, list(message.repeated_int32))
test_slice(3, 11, 1)
test_slice(3, 11, 2)
test_slice(3, 11, 3)
test_slice(11, 3, -1)
test_slice(11, 3, -2)
test_slice(11, 3, -3)
test_slice(10, 25, 4)
#TestMessageExtension.test_descriptor_pool.__unittest_expecting_failure__ = True

@ -47,9 +47,9 @@ descriptor_test.MakeDescriptorTest.testMakeDescriptorWithUnsignedIntField.__unit
# We must skip these tests entirely (rather than running them with
# __unittest_expecting_failure__) because they error out in setUp():
#
# NotImplementedError: unset repeated
# AttributeError: 'google.protobuf.pyext._message.DescriptorPool' object has no attribute 'Add'
#
# TODO: change to __unittest_expecting_failure__ when repeated fields are checked in.
# TODO: change to __unittest_expecting_failure__ when DescriptorPool.Add() is implemented
descriptor_test.DescriptorTest.testAggregateOptions.__unittest_skip__ = True
descriptor_test.DescriptorTest.testComplexExtensionOptions.__unittest_skip__ = True
descriptor_test.DescriptorTest.testContainingServiceFixups.__unittest_skip__ = True

@ -40,11 +40,9 @@ json_format_test.JsonFormatTest.testExtensionToDictAndBackWithScalar.__unittest_
json_format_test.JsonFormatTest.testExtensionToJsonAndBack.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testFieldMaskMessage.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testFloatPrecision.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testFormatEnumsAsInts.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testIgnoreUnknownField.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testInvalidAny.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testInvalidMap.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testInvalidTimestamp.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testJsonEscapeString.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testJsonName.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testJsonParseDictToAnyDoesNotAlterInput.__unittest_expecting_failure__ = True
@ -53,7 +51,6 @@ json_format_test.JsonFormatTest.testMapFields.__unittest_expecting_failure__ = T
json_format_test.JsonFormatTest.testNullValue.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testOneofFields.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testParseDictAnyDescriptorPoolMissingType.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testParseDoubleToFloat.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testParseNull.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testPartialMessageToJson.__unittest_expecting_failure__ = True
json_format_test.JsonFormatTest.testStructMessage.__unittest_expecting_failure__ = True

@ -26,36 +26,14 @@
from google.protobuf.internal import message_test
import unittest
message_test.MessageTest.testAddWrongRepeatedNestedField_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testAddWrongRepeatedNestedField_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testAppendRepeatedCompositeField_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testAppendRepeatedCompositeField_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testBadUtf8String_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testBadUtf8String_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testDeterminismParameters_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testDeterminismParameters_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendFloatWithIterable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendFloatWithIterable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendFloatWithNothing_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendFloatWithNothing_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendFloatWithPythonList_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendFloatWithPythonList_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendInt32WithIterable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendInt32WithIterable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendInt32WithNothing_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendInt32WithNothing_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendInt32WithPythonList_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendInt32WithPythonList_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendShouldNotSwallowExceptions_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendShouldNotSwallowExceptions_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithIterable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithIterable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithNothing_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithNothing_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithPythonList_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithPythonList_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithString_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testExtendStringWithString_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testFloatPrinting_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testFloatPrinting_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testGoldenMessage_proto2.__unittest_expecting_failure__ = True
@ -66,85 +44,15 @@ message_test.MessageTest.testHighPrecisionDoublePrinting_proto2.__unittest_expec
message_test.MessageTest.testHighPrecisionDoublePrinting_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testInsertRepeatedCompositeField_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testInsertRepeatedCompositeField_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testLongValuedSlice_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testLongValuedSlice_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFromRepeatedField_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFromRepeatedField_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFromStringUsingMemoryView_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFromStringUsingMemoryView_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFromString_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFromString_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFrom_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testMergeFrom_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testNegativeInfinityPacked_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testNegativeInfinityPacked_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testNegativeInfinity_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testNegativeInfinity_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testNotANumberPacked_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testNotANumberPacked_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testNotANumber_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testNotANumber_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testOneofMessageMergeFrom_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testOneofMessageMergeFrom_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testOneofNestedMergeFrom_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testOneofNestedMergeFrom_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testPickleNestedMessage_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testPickleNestedMessage_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testPickleNestedNestedMessage_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testPickleNestedNestedMessage_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testPickleSupport_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testPickleSupport_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testPositiveInfinityPacked_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testPositiveInfinityPacked_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testPositiveInfinity_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testPositiveInfinity_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testReleasedNestedMessages_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testReleasedNestedMessages_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedCompareWithSelf_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedCompareWithSelf_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedCompositeFieldPop_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedCompositeFieldPop_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedCompositeFieldSortArguments_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedCompositeFieldSortArguments_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedContains_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedContains_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldInsideNestedMessage_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldInsideNestedMessage_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldsAreSequences_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldsAreSequences_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldsComparable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldsComparable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldsNotHashable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedFieldsNotHashable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedNestedFieldIteration_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedNestedFieldIteration_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedScalarFieldPop_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedScalarFieldPop_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedScalarFieldSortArguments_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedScalarFieldSortArguments_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedScalarIterable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testRepeatedScalarIterable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testReturningType_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testReturningType_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testSetRepeatedComposite_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testSetRepeatedComposite_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortEmptyRepeatedCompositeContainer_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortEmptyRepeatedCompositeContainer_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedCompositeFieldsCustomComparator_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedCompositeFieldsCustomComparator_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedCompositeFieldsStable_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedCompositeFieldsStable_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedScalarFieldsCustomComparator_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedScalarFieldsCustomComparator_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedScalarFieldsDefaultComparator_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testSortingRepeatedScalarFieldsDefaultComparator_proto3.__unittest_expecting_failure__ = True
message_test.MessageTest.testUnknownFieldPrinting_proto2.__unittest_expecting_failure__ = True
message_test.MessageTest.testUnknownFieldPrinting_proto3.__unittest_expecting_failure__ = True
message_test.PackedFieldTest.testPackedFields.__unittest_expecting_failure__ = True
message_test.PackedFieldTest.testUnpackedFields.__unittest_expecting_failure__ = True
message_test.Proto2Test.testAssignInvalidEnum.__unittest_expecting_failure__ = True
message_test.Proto2Test.testExtensionsErrors.__unittest_expecting_failure__ = True
message_test.Proto2Test.testFieldPresence.__unittest_expecting_failure__ = True
message_test.Proto2Test.testGoldenExtensions.__unittest_expecting_failure__ = True
message_test.Proto2Test.testGoldenPackedExtensions.__unittest_expecting_failure__ = True
message_test.Proto2Test.testMergeFromExtensions.__unittest_expecting_failure__ = True
@ -153,9 +61,7 @@ message_test.Proto2Test.testPickleIncompleteProto.__unittest_expecting_failure__
message_test.Proto2Test.testPythonicInit.__unittest_expecting_failure__ = True
message_test.Proto2Test.testUnknownEnumMap.__unittest_expecting_failure__ = True
message_test.Proto2Test.test_documentation.__unittest_expecting_failure__ = True
message_test.Proto3Test.testAssignUnknownEnum.__unittest_expecting_failure__ = True
message_test.Proto3Test.testCopyFromBadType.__unittest_expecting_failure__ = True
message_test.Proto3Test.testFieldPresence.__unittest_expecting_failure__ = True
message_test.Proto3Test.testIntegerMapWithLongs.__unittest_expecting_failure__ = True
message_test.Proto3Test.testMapAssignmentCausesPresence.__unittest_expecting_failure__ = True
message_test.Proto3Test.testMapAssignmentCausesPresenceForSubmessages.__unittest_expecting_failure__ = True
@ -184,19 +90,17 @@ message_test.Proto3Test.testMessageMapItemValidAfterTopMessageCleared.__unittest
message_test.Proto3Test.testMessageMapValidAfterFieldCleared.__unittest_expecting_failure__ = True
message_test.Proto3Test.testModifyMapWhileIterating.__unittest_expecting_failure__ = True
message_test.Proto3Test.testNestedMessageMapItemDelete.__unittest_expecting_failure__ = True
message_test.Proto3Test.testProto3Optional.__unittest_expecting_failure__ = True
message_test.Proto3Test.testScalarMap.__unittest_expecting_failure__ = True
message_test.Proto3Test.testScalarMapDefaults.__unittest_expecting_failure__ = True
message_test.Proto3Test.testStringUnicodeConversionInMap.__unittest_expecting_failure__ = True
message_test.Proto3Test.testSubmessageMap.__unittest_expecting_failure__ = True
message_test.ValidTypeNamesTest.testTypeNamesCanBeImported.__unittest_expecting_failure__ = True
# We must skip these tests entirely (rather than running them with
# __unittest_expecting_failure__) because they error out in setUpClass():
#
# NotImplementedError: unset repeated
# AttributeError: 'google.protobuf.pyext._message.DescriptorPool' object has no attribute 'Add'
#
# TODO: change to __unittest_expecting_failure__ when repeated fields are available
# TODO: change to __unittest_expecting_failure__ when DescriptorPoo.Add is implemented
message_test.OversizeProtosTest.__unittest_skip__ = True
if __name__ == '__main__':

@ -32,12 +32,7 @@ reflection_test.ByteSizeTest.testCacheInvalidationForRepeatedMessage.__unittest_
reflection_test.ByteSizeTest.testCacheInvalidationForRepeatedScalar.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testExtensions.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testPackedExtensions.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testPackedRepeatedScalars.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testRepeatedComposites.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testRepeatedCompositesDelete.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testRepeatedGroups.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testRepeatedScalars.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testRepeatedScalarsExtend.__unittest_expecting_failure__ = True
reflection_test.ByteSizeTest.testRepeatedScalarsRemove.__unittest_expecting_failure__ = True
reflection_test.ClassAPITest.testMakeClassWithNestedDescriptor.__unittest_expecting_failure__ = True
reflection_test.ClassAPITest.testParsingFlatClass.__unittest_expecting_failure__ = True
@ -47,23 +42,17 @@ reflection_test.MutualRecursionEqualityTest.testEqualityWithMutualRecursion.__un
reflection_test.OptionsTest.testMessageOptions.__unittest_expecting_failure__ = True
reflection_test.OptionsTest.testPackedOptions.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testClear.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testClearRemovesChildren.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testDisconnectingInOneof.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testDisconnectionAfterClearingEmptyMessage.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testExtensionContainsError.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testExtensionDelete.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testExtensionFailureModes.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testExtensionIter.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testFileDescriptorErrors.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testHasBitsForAncestorsOfExtendedMessage.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testHasBitsForManyLevelsOfNesting.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testHasBitsWhenModifyingRepeatedFields.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testIsInitialized.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testListFieldsAndExtensions.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testMergeFromExtensionsNestedMessage.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testMergeFromExtensionsRepeated.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testMergeFromExtensionsSingular.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testMergeFromOptionalGroup.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testNestedExtensions.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testRegisteredExtensions.__unittest_expecting_failure__ = True
reflection_test.Proto2ReflectionTest.testRepeatedCompositeConstructor.__unittest_expecting_failure__ = True
@ -84,20 +73,12 @@ reflection_test.Proto2ReflectionTest.testTopLevelExtensionsForRepeatedMessage.__
reflection_test.Proto2ReflectionTest.testTopLevelExtensionsForRepeatedScalar.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testClearFieldWithUnknownFieldName_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testClearFieldWithUnknownFieldName_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testConstructorInvalidatesCachedByteSize_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testConstructorInvalidatesCachedByteSize_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testConstructorTypeError_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testConstructorTypeError_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testCopyFromAllFields_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testCopyFromAllFields_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testCopyFromRepeatedField_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testCopyFromRepeatedField_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testCopyFromSelf_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testCopyFromSelf_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testDeepCopy_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testDeepCopy_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testDisallowedAssignments_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testDisallowedAssignments_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testDisconnectingBeforeClear_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testDisconnectingBeforeClear_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testEnum_KeysAndValues_proto2.__unittest_expecting_failure__ = True
@ -110,24 +91,12 @@ reflection_test.ReflectionTest.testIllegalValuesForIntegers_proto2.__unittest_ex
reflection_test.ReflectionTest.testIllegalValuesForIntegers_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMergeFromAllFields_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMergeFromAllFields_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMergeFromRepeatedField_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMergeFromRepeatedField_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMergeFromRepeatedNestedMessage_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMergeFromRepeatedNestedMessage_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMixedConstructor_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testMixedConstructor_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testOneOf_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testOneOf_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testRepeatedListFields_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testRepeatedListFields_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testRepeatedScalarConstructor_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testRepeatedScalarConstructor_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testRepeatedScalarTypeSafety_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testRepeatedScalarTypeSafety_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testSingleScalarTypeSafety_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testSingleScalarTypeSafety_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testSingularListFields_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testSingularListFields_proto3.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testStaticParseFrom_proto2.__unittest_expecting_failure__ = True
reflection_test.ReflectionTest.testStaticParseFrom_proto3.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testCanonicalSerializationOrder.__unittest_expecting_failure__ = True
@ -141,11 +110,8 @@ reflection_test.SerializationTest.testInitKwargs.__unittest_expecting_failure__
reflection_test.SerializationTest.testInitRepeatedKwargs.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testInitRequiredForeignKwargs.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testInitRequiredKwargs.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testMergeFromStringWhenFieldsAlreadySet.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testMergePackedFromStringWhenSomeFieldsAlreadySet.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testMessageSetWireFormat.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testMessageSetWireFormatUnknownExtension.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testPackedFieldsWireFormat.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testParsePackedFromUnpacked.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testParseUnpackedFromPacked.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testSerializeAllExtensions.__unittest_expecting_failure__ = True
@ -157,16 +123,11 @@ reflection_test.SerializationTest.testSerializeNegativeValues.__unittest_expecti
reflection_test.SerializationTest.testSerializeUninitialized.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testSerializeUninitializedSubMessage.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testSerializeWithOptionalGroup.__unittest_expecting_failure__ = True
reflection_test.SerializationTest.testUnknownFields.__unittest_expecting_failure__ = True
reflection_test.TestAllTypesEqualityTest.testEmptyProtosEqual.__unittest_expecting_failure__ = True
# We must skip these tests entirely (rather than running them with
# __unittest_expecting_failure__) because they error out in setUp():
#
# NotImplementedError: access repeated
#
# TODO: change to __unittest_expecting_failure__ when repeated fields are available.
reflection_test.FullProtosEqualityTest.__unittest_skip__ = True
reflection_test.FullProtosEqualityTest.testAllFieldsFilledEquality.__unittest_expecting_failure__ = True
reflection_test.FullProtosEqualityTest.testNonRepeatedComposite.__unittest_expecting_failure__ = True
reflection_test.FullProtosEqualityTest.testNonRepeatedCompositeHasBits.__unittest_expecting_failure__ = True
reflection_test.FullProtosEqualityTest.testRepeatedComposite.__unittest_expecting_failure__ = True
if __name__ == '__main__':
unittest.main(module=reflection_test, verbosity=2)

@ -41,17 +41,6 @@ text_format_test.OnlyWorksWithProto2RightNowTests.testPrintMap.__unittest_expect
text_format_test.OnlyWorksWithProto2RightNowTests.testPrintMapUsingCppImplementation.__unittest_expecting_failure__ = True
text_format_test.OnlyWorksWithProto2RightNowTests.testPrintUnknownFields.__unittest_expecting_failure__ = True
text_format_test.OptionalColonMessageToStringTest.testForcePrintOptionalColon.__unittest_expecting_failure__ = True
text_format_test.OptionalColonMessageToStringTest.testPrintShortFormatRepeatedFields.__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintMultiLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintMultiLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintMultiLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintMultiLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintMultipleParts" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintMultipleParts" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintNoMatch" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintNoMatch" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.PrettyPrinterTest, "testPrettyPrintOneLine" + sep + "1").__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testExtensionInsideAnyMessage.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testMergeDuplicateExtensionScalars.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseAllExtensions.__unittest_expecting_failure__ = True
@ -60,7 +49,6 @@ text_format_test.Proto2Tests.testParseBadExtension.__unittest_expecting_failure_
text_format_test.Proto2Tests.testParseDuplicateExtensionMessages.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseDuplicateExtensionScalars.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseGoldenExtensions.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseGroupNotClosed.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseMap.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseMessageByFieldNumber.__unittest_expecting_failure__ = True
text_format_test.Proto2Tests.testParseMessageSet.__unittest_expecting_failure__ = True
@ -75,7 +63,6 @@ text_format_test.Proto3Tests.testMergeExpandedAnyDescriptorPoolMissingType.__uni
text_format_test.Proto3Tests.testMergeExpandedAnyPointyBrackets.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testMergeExpandedAnyRepeated.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testMergeMissingAnyEndToken.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testMergeUnexpandedAny.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testPrintAndParseMessageInvalidAny.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testPrintMessageExpandAny.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testPrintMessageExpandAnyAsOneLine.__unittest_expecting_failure__ = True
@ -83,52 +70,15 @@ text_format_test.Proto3Tests.testPrintMessageExpandAnyAsOneLinePointyBrackets.__
text_format_test.Proto3Tests.testPrintMessageExpandAnyDescriptorPoolMissingType.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testPrintMessageExpandAnyPointyBrackets.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testPrintMessageExpandAnyRepeated.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testProto3Optional.__unittest_expecting_failure__ = True
text_format_test.Proto3Tests.testTopAnyMessage.__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testMergeDuplicateNestedMessageScalars" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testMergeDuplicateNestedMessageScalars" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testMergeDuplicateNestedMessageScalars" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testMergeDuplicateNestedMessageScalars" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testReplaceMessageInMessage" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testReplaceMessageInMessage" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testReplaceMessageInMessage" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMergeTests, "testReplaceMessageInMessage" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testCustomOptions" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testCustomOptions" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testCustomOptions" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testCustomOptions" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticAsOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticAsOneLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticAsOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticAsOneLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticUnicodeSubclass" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticUnicodeSubclass" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticUnicodeSubclass" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExoticUnicodeSubclass" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExotic" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExotic" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExotic" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintExotic" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatFormat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatFormat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatFormat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatFormat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatPrecision" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatPrecision" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatPrecision" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintFloatPrecision" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintNestedMessageAsOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintNestedMessageAsOneLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintNestedMessageAsOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintNestedMessageAsOneLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRawUtf8String" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRawUtf8String" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRawUtf8String" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRawUtf8String" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRepeatedFieldsAsOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRepeatedFieldsAsOneLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRepeatedFieldsAsOneLine" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintRepeatedFieldsAsOneLine" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintShortFormatRepeatedFields" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintShortFormatRepeatedFields" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToStringTests, "testPrintShortFormatRepeatedFields" + sep + "0").__unittest_expecting_failure__ = True
@ -149,22 +99,6 @@ getattr(text_format_test.TextFormatMessageToTextBytesTests, "testRawUtf8RoundTri
getattr(text_format_test.TextFormatMessageToTextBytesTests, "testRawUtf8RoundTrip" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToTextBytesTests, "testRawUtf8RoundTrip" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatMessageToTextBytesTests, "testRawUtf8RoundTrip" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesFile" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesFile" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesFile" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesFile" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesLines" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesLines" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesLines" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromBytesLines" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeFile" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeFile" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeFile" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeFile" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeLines" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeLines" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeLines" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testFromUnicodeLines" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseAllFields" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseAllFields" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseAllFields" + sep + "0").__unittest_expecting_failure__ = True
@ -173,46 +107,10 @@ getattr(text_format_test.TextFormatParserTests, "testParseAndMergeUtf8" + sep +
getattr(text_format_test.TextFormatParserTests, "testParseAndMergeUtf8" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseAndMergeUtf8" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseAndMergeUtf8" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseBytes" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseBytes" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseBytes" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseBytes" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseDoubleToFloat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseDoubleToFloat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseDoubleToFloat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseDoubleToFloat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseEmptyText" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseEmptyText" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseEmptyText" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseEmptyText" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseExotic" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseExotic" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseExotic" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseExotic" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedMessageShortFormat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedMessageShortFormat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedMessageShortFormat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedMessageShortFormat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedScalarShortFormat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedScalarShortFormat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedScalarShortFormat" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseRepeatedScalarShortFormat" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseStringFieldUnescape" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseStringFieldUnescape" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseStringFieldUnescape" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseStringFieldUnescape" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseTrailingCommas" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseTrailingCommas" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseTrailingCommas" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseTrailingCommas" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnicode" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnicode" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnicode" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnicode" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnknownField" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnknownField" + sep + "1").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnknownField" + sep + "0").__unittest_expecting_failure__ = True
getattr(text_format_test.TextFormatParserTests, "testParseUnknownField" + sep + "1").__unittest_expecting_failure__ = True
# We must skip these tests entirely (rather than running them with
# __unittest_expecting_failure__) because they error out in setUp():
@ -220,7 +118,7 @@ getattr(text_format_test.TextFormatParserTests, "testParseUnknownField" + sep +
# NotImplementedError: Conversion of message types not yet implemented
#
# TODO: change to __unittest_expecting_failure__ when message types can be converted
text_format_test.WhitespaceTest.__unittest_skip__ = True
#text_format_test.WhitespaceTest.__unittest_skip__ = True
if __name__ == '__main__':
unittest.main(module=text_format_test, verbosity=2)

@ -32,27 +32,18 @@ import unittest
# NotImplementedError: access repeated
#
# TODO: change to __unittest_expecting_failure__ when repeated fields are available
unknown_fields_test.UnknownEnumValuesTest.testCheckUnknownFieldValueForEnum.__unittest_skip__ = True
unknown_fields_test.UnknownEnumValuesTest.testRoundTrip.__unittest_skip__ = True
unknown_fields_test.UnknownEnumValuesTest.testUnknownEnumValue.__unittest_skip__ = True
unknown_fields_test.UnknownEnumValuesTest.testUnknownPackedEnumValue.__unittest_skip__ = True
unknown_fields_test.UnknownEnumValuesTest.testUnknownParseMismatchEnumValue.__unittest_skip__ = True
unknown_fields_test.UnknownEnumValuesTest.testUnknownRepeatedEnumValue.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testCheckUnknownFieldValue.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testClear.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testCopyFrom.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testMergeFrom.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testSubUnknownFields.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testUnknownExtensions.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testUnknownField.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testUnknownFieldsNoMemoryLeak.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testByteSize.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testDiscardUnknownFields.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testEquals.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testListFields.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testSerialize.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testSerializeMessageSetWireFormatUnknownExtension.__unittest_skip__ = True
unknown_fields_test.UnknownFieldsTest.testSerializeProto3.__unittest_skip__ = True
unknown_fields_test.UnknownEnumValuesTest.testCheckUnknownFieldValueForEnum.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownEnumValuesTest.testRoundTrip.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testCheckUnknownFieldValue.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testClear.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testMergeFrom.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testSubUnknownFields.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testUnknownExtensions.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testUnknownField.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsAccessorsTest.testUnknownFieldsNoMemoryLeak.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsTest.testDiscardUnknownFields.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsTest.testEquals.__unittest_expecting_failure__ = True
unknown_fields_test.UnknownFieldsTest.testSerializeMessageSetWireFormatUnknownExtension.__unittest_expecting_failure__ = True
if __name__ == '__main__':
unittest.main(module=unknown_fields_test, verbosity=2)

@ -29,16 +29,8 @@ import unittest
well_known_types_test.AnyTest.testAnyMessage.__unittest_expecting_failure__ = True
well_known_types_test.AnyTest.testPackDeterministic.__unittest_expecting_failure__ = True
well_known_types_test.AnyTest.testPackWithCustomTypeUrl.__unittest_expecting_failure__ = True
well_known_types_test.AnyTest.testUnpackWithNoSlashInTypeUrl.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testCanonicalFrom.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testDescriptorToFieldMask.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testIntersect.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testIsValidForDescriptor.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testMergeErrors.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testMergeMessageWithMapField.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testMergeMessageWithoutMapFields.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testStringFormat.__unittest_expecting_failure__ = True
well_known_types_test.FieldMaskTest.testUnion.__unittest_expecting_failure__ = True
well_known_types_test.StructTest.testMergeFrom.__unittest_expecting_failure__ = True
well_known_types_test.StructTest.testStruct.__unittest_expecting_failure__ = True
well_known_types_test.StructTest.testStructAssignment.__unittest_expecting_failure__ = True

@ -31,6 +31,7 @@
#include "python/descriptor_containers.h"
#include "python/descriptor_pool.h"
#include "python/message.h"
#include "python/repeated.h"
static void PyUpb_ModuleDealloc(void *module) {
PyUpb_ModuleState *s = PyModule_GetState(module);
@ -290,7 +291,7 @@ PyMODINIT_FUNC PyInit__message(void) {
if (!PyUpb_InitDescriptorContainers(m) || !PyUpb_InitDescriptorPool(m) ||
!PyUpb_InitDescriptor(m) || !PyUpb_InitArena(m) ||
!PyUpb_InitMessage(m)) {
!PyUpb_InitMessage(m) || !PyUpb_Repeated_Init(m)) {
Py_DECREF(m);
return NULL;
}

@ -77,6 +77,10 @@ typedef struct {
PyObject *wkt_bases;
PyTypeObject *arena_type;
PyUpb_WeakMap *obj_cache;
// From repeated.c
PyTypeObject* repeated_composite_container_type;
PyTypeObject* repeated_scalar_container_type;
} PyUpb_ModuleState;
// Returns the global state object from the current interpreter. The current

@ -0,0 +1,738 @@
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google LLC nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "python/repeated.h"
#include "python/convert.h"
#include "python/message.h"
#include "python/protobuf.h"
static PyObject* PyUpb_RepeatedCompositeContainer_Append(PyObject* _self,
PyObject* value);
static PyObject* PyUpb_RepeatedScalarContainer_Append(PyObject* _self,
PyObject* value);
// For an expression like:
// foo[index]
//
// Converts `index` to an effective i/count/step, for a repeated field
// field of size `size`.
static bool IndexToRange(PyObject* index, Py_ssize_t size, Py_ssize_t* i,
Py_ssize_t* count, Py_ssize_t* step) {
assert(i && count && step);
if (PySlice_Check(index)) {
Py_ssize_t start, stop;
if (PySlice_Unpack(index, &start, &stop, step) < 0) return false;
*count = PySlice_AdjustIndices(size, &start, &stop, *step);
*i = start;
} else {
*i = PyNumber_AsSsize_t(index, PyExc_IndexError);
if (*i == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "list indices must be integers");
return false;
}
if (*i < 0) *i += size;
*step = 0;
*count = 1;
if (*i < 0 || size <= *i) {
PyErr_Format(PyExc_IndexError, "list index out of range");
return false;
}
}
return true;
}
// Wrapper for a repeated field.
typedef struct {
PyObject_HEAD
PyObject* arena;
// The field descriptor (PyObject*).
// The low bit indicates whether the container is reified (see ptr below).
// - low bit set: repeated field is a stub (no underlying data).
// - low bit clear: repeated field is reified (points to upb_array).
uintptr_t field;
union {
PyObject* parent; // stub: owning pointer to parent message.
upb_array* arr; // reified: the data for this array.
} ptr;
} PyUpb_RepeatedContainer;
static bool PyUpb_RepeatedContainer_IsStub(PyUpb_RepeatedContainer* self) {
return self->field & 1;
}
static PyObject* PyUpb_RepeatedContainer_GetFieldDescriptor(
PyUpb_RepeatedContainer* self) {
return (PyObject*)(self->field & ~(uintptr_t)1);
}
static const upb_fielddef* PyUpb_RepeatedContainer_GetField(
PyUpb_RepeatedContainer* self) {
return PyUpb_FieldDescriptor_GetDef(
PyUpb_RepeatedContainer_GetFieldDescriptor(self));
}
static upb_array* PyUpb_RepeatedContainer_GetIfWritable(
PyUpb_RepeatedContainer* self) {
return PyUpb_RepeatedContainer_IsStub(self) ? NULL : self->ptr.arr;
}
void PyUpb_RepeatedContainer_Reify(PyObject* _self, upb_array* arr) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
assert(PyUpb_RepeatedContainer_IsStub(self));
PyUpb_ObjCache_Add(arr, &self->ob_base);
Py_DECREF(self->ptr.parent);
self->ptr.arr = arr; // Overwrites self->ptr.parent.
self->field &= ~(uintptr_t)1;
assert(!PyUpb_RepeatedContainer_IsStub(self));
}
static upb_array* PyUpb_RepeatedContainer_AssureWritable(
PyUpb_RepeatedContainer* self) {
upb_array* arr = PyUpb_RepeatedContainer_GetIfWritable(self);
if (arr) return arr; // Already writable.
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_arena* arena = PyUpb_Arena_Get(self->arena);
arr = upb_array_new(arena, upb_fielddef_type(f));
PyUpb_CMessage_SetConcreteSubobj(self->ptr.parent, f,
(upb_msgval){.array_val = arr});
PyUpb_RepeatedContainer_Reify((PyObject*)self, arr);
return arr;
}
static void PyUpb_RepeatedContainer_Dealloc(PyObject* _self) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
Py_DECREF(self->arena);
if (PyUpb_RepeatedContainer_IsStub(self)) {
PyUpb_CMessage_CacheDelete(self->ptr.parent,
PyUpb_RepeatedContainer_GetField(self));
Py_DECREF(self->ptr.parent);
} else {
PyUpb_ObjCache_Delete(self->ptr.arr);
}
Py_DECREF(PyUpb_RepeatedContainer_GetFieldDescriptor(self));
PyUpb_Dealloc(self);
}
static PyTypeObject* PyUpb_RepeatedContainer_GetClass(const upb_fielddef* f) {
assert(upb_fielddef_isseq(f) && !upb_fielddef_ismap(f));
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
return upb_fielddef_issubmsg(f) ? state->repeated_composite_container_type
: state->repeated_scalar_container_type;
}
static Py_ssize_t PyUpb_RepeatedContainer_Length(PyObject* self) {
upb_array* arr =
PyUpb_RepeatedContainer_GetIfWritable((PyUpb_RepeatedContainer*)self);
return arr ? upb_array_size(arr) : 0;
}
PyObject* PyUpb_RepeatedContainer_NewUnset(PyObject* parent,
const upb_fielddef* f,
PyObject* arena) {
PyTypeObject* cls = PyUpb_RepeatedContainer_GetClass(f);
PyUpb_RepeatedContainer* repeated = (void*)PyType_GenericAlloc(cls, 0);
repeated->arena = arena;
repeated->field = (uintptr_t)PyUpb_FieldDescriptor_Get(f) | 1;
repeated->ptr.parent = parent;
Py_INCREF(arena);
Py_INCREF(parent);
return &repeated->ob_base;
}
PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* arr,
const upb_fielddef* f,
PyObject* arena) {
PyObject* ret = PyUpb_ObjCache_Get(arr);
if (ret) return ret;
PyTypeObject* cls = PyUpb_RepeatedContainer_GetClass(f);
PyUpb_RepeatedContainer* repeated = (void*)PyType_GenericAlloc(cls, 0);
repeated->arena = arena;
repeated->field = (uintptr_t)PyUpb_FieldDescriptor_Get(f);
repeated->ptr.arr = arr;
ret = &repeated->ob_base;
Py_INCREF(arena);
PyUpb_ObjCache_Add(arr, ret);
return ret;
}
PyObject* PyUpb_RepeatedContainer_Extend(PyObject* _self, PyObject* value) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_AssureWritable(self);
size_t start_size = upb_array_size(arr);
PyObject* it = PyObject_GetIter(value);
if (!it) {
PyErr_SetString(PyExc_TypeError, "Value must be iterable");
return NULL;
}
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
bool submsg = upb_fielddef_issubmsg(f);
PyObject* e;
while ((e = PyIter_Next(it))) {
PyObject* ret;
if (submsg) {
ret = PyUpb_RepeatedCompositeContainer_Append(_self, e);
} else {
ret = PyUpb_RepeatedScalarContainer_Append(_self, e);
}
Py_XDECREF(ret);
Py_DECREF(e);
}
Py_DECREF(it);
if (PyErr_Occurred()) {
upb_array_resize(arr, start_size, NULL);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject* PyUpb_RepeatedContainer_Item(PyObject* _self,
Py_ssize_t index) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_GetIfWritable(self);
Py_ssize_t size = arr ? upb_array_size(arr) : 0;
if (index < 0 || index >= size) {
PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index);
return NULL;
}
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
return PyUpb_UpbToPy(upb_array_get(arr, index), f, self->arena);
}
PyObject* PyUpb_RepeatedContainer_ToList(PyObject* _self) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_GetIfWritable(self);
if (!arr) return PyList_New(0);
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
size_t n = upb_array_size(arr);
PyObject* list = PyList_New(n);
for (size_t i = 0; i < n; i++) {
PyObject* val = PyUpb_UpbToPy(upb_array_get(arr, i), f, self->arena);
if (!val) {
Py_DECREF(list);
return NULL;
}
PyList_SetItem(list, i, val);
}
return list;
}
static PyObject* PyUpb_RepeatedContainer_Repr(PyObject* _self) {
PyObject* list = PyUpb_RepeatedContainer_ToList(_self);
if (!list) return NULL;
assert(!PyErr_Occurred());
PyObject* repr = PyObject_Repr(list);
Py_DECREF(list);
return repr;
}
static PyObject* PyUpb_RepeatedContainer_RichCompare(PyObject* _self,
PyObject* _other,
int opid) {
if (opid != Py_EQ && opid != Py_NE) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyObject* list1 = PyUpb_RepeatedContainer_ToList(_self);
PyObject* list2 = _other;
PyObject* del = NULL;
if (PyObject_TypeCheck(_other, _self->ob_type)) {
del = list2 = PyUpb_RepeatedContainer_ToList(_other);
}
PyObject* ret = PyObject_RichCompare(list1, list2, opid);
Py_DECREF(list1);
Py_XDECREF(del);
return ret;
}
static PyObject* PyUpb_RepeatedContainer_Subscript(PyObject* _self,
PyObject* key) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_GetIfWritable(self);
Py_ssize_t size = arr ? upb_array_size(arr) : 0;
Py_ssize_t idx, count, step;
if (!IndexToRange(key, size, &idx, &count, &step)) return NULL;
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
if (step == 0) {
return PyUpb_UpbToPy(upb_array_get(arr, idx), f, self->arena);
} else {
PyObject* list = PyList_New(count);
for (Py_ssize_t i = 0; i < count; i++, idx += step) {
upb_msgval msgval = upb_array_get(self->ptr.arr, idx);
PyObject* item = PyUpb_UpbToPy(msgval, f, self->arena);
if (!item) {
Py_DECREF(list);
return NULL;
}
PyList_SetItem(list, i, item);
}
return list;
}
}
static int PyUpb_RepeatedContainer_SetSubscript(
PyUpb_RepeatedContainer* self, upb_array* arr, const upb_fielddef* f,
Py_ssize_t idx, Py_ssize_t count, Py_ssize_t step, PyObject* value) {
upb_arena* arena = PyUpb_Arena_Get(self->arena);
if (upb_fielddef_issubmsg(f)) {
PyErr_SetString(PyExc_TypeError, "does not support assignment");
return -1;
}
if (step == 0) {
// Set single value.
upb_msgval msgval;
if (!PyUpb_PyToUpb(value, f, &msgval, arena)) return -1;
upb_array_set(arr, idx, msgval);
return 0;
}
// Set range.
PyObject* seq =
PySequence_Fast(value, "must assign iterable to extended slice");
PyObject* item = NULL;
int ret = -1;
if (!seq) goto err;
if (PySequence_Size(seq) != count) {
PyErr_Format(PyExc_ValueError,
"attempt to assign sequence of size %zd to extended slice "
"of size %zd",
PySequence_Size(seq), count);
goto err;
}
for (Py_ssize_t i = 0; i < count; i++, idx += step) {
upb_msgval msgval;
item = PySequence_GetItem(seq, i);
if (!item) goto err;
// XXX: if this fails we can leave the list partially mutated.
if (!PyUpb_PyToUpb(item, f, &msgval, arena)) goto err;
Py_DECREF(item);
item = NULL;
upb_array_set(arr, idx, msgval);
}
ret = 0;
err:
Py_XDECREF(seq);
Py_XDECREF(item);
return ret;
}
static int PyUpb_RepeatedContainer_DeleteSubscript(upb_array* arr,
Py_ssize_t idx,
Py_ssize_t count,
Py_ssize_t step) {
// Normalize direction: deletion is order-independent.
Py_ssize_t start = idx;
if (step < 0) {
Py_ssize_t end = start + step * (count - 1);
start = end;
step = -step;
}
size_t dst = start;
size_t src;
if (step > 1) {
// Move elements between steps:
//
// src
// |
// |------X---X---X---X------------------------------|
// |
// dst <-------- tail -------------->
src = start + 1;
for (Py_ssize_t i = 1; i < count; i++, dst += step - 1, src += step) {
upb_array_move(arr, dst, src, step);
}
} else {
src = start + count;
}
// Move tail.
size_t tail = upb_array_size(arr) - src;
size_t new_size = dst + tail;
assert(new_size == upb_array_size(arr) - count);
upb_array_move(arr, dst, src, tail);
upb_array_resize(arr, new_size, NULL);
return 0;
}
static int PyUpb_RepeatedContainer_AssignSubscript(PyObject* _self,
PyObject* key,
PyObject* value) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_array* arr = PyUpb_RepeatedContainer_GetIfWritable(self);
Py_ssize_t size = arr ? upb_array_size(arr) : 0;
Py_ssize_t idx, count, step;
if (!IndexToRange(key, size, &idx, &count, &step)) return -1;
if (value) {
return PyUpb_RepeatedContainer_SetSubscript(self, arr, f, idx, count, step,
value);
} else {
return PyUpb_RepeatedContainer_DeleteSubscript(arr, idx, count, step);
}
}
static PyObject* PyUpb_RepeatedContainer_Pop(PyObject* _self, PyObject* args) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
Py_ssize_t index = -1;
if (!PyArg_ParseTuple(args, "|n", &index)) return NULL;
upb_array* arr = PyUpb_RepeatedContainer_AssureWritable(self);
size_t size = upb_array_size(arr);
if (index < 0) index += size;
if (index >= size) index = size - 1;
PyObject* ret = PyUpb_RepeatedContainer_Item(_self, index);
if (!ret) return NULL;
upb_array_delete(self->ptr.arr, index, 1);
return ret;
}
// A helper function used only for Sort().
static bool PyUpb_RepeatedContainer_Assign(PyObject* _self, PyObject* list) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_array* arr = PyUpb_RepeatedContainer_AssureWritable(self);
Py_ssize_t size = PyList_Size(list);
bool submsg = upb_fielddef_issubmsg(f);
upb_arena* arena = PyUpb_Arena_Get(self->arena);
for (Py_ssize_t i = 0; i < size; ++i) {
PyObject* obj = PyList_GetItem(list, i);
upb_msgval msgval;
if (submsg) {
msgval.msg_val = PyUpb_CMessage_GetIfWritable(obj);
assert(msgval.msg_val);
} else {
if (!PyUpb_PyToUpb(obj, f, &msgval, arena)) return false;
}
upb_array_set(arr, i, msgval);
}
return true;
}
static PyObject* PyUpb_RepeatedContainer_Sort(PyObject* pself, PyObject* args,
PyObject* kwds) {
// Support the old sort_function argument for backwards
// compatibility.
if (kwds != NULL) {
PyObject* sort_func = PyDict_GetItemString(kwds, "sort_function");
if (sort_func != NULL) {
// Must set before deleting as sort_func is a borrowed reference
// and kwds might be the only thing keeping it alive.
if (PyDict_SetItemString(kwds, "cmp", sort_func) == -1) return NULL;
if (PyDict_DelItemString(kwds, "sort_function") == -1) return NULL;
}
}
PyObject* ret = NULL;
PyObject* full_slice = NULL;
PyObject* list = NULL;
PyObject* m = NULL;
PyObject* res = NULL;
if ((full_slice = PySlice_New(NULL, NULL, NULL)) &&
(list = PyUpb_RepeatedContainer_Subscript(pself, full_slice)) &&
(m = PyObject_GetAttrString(list, "sort")) &&
(res = PyObject_Call(m, args, kwds)) &&
PyUpb_RepeatedContainer_Assign(pself, list)) {
Py_INCREF(Py_None);
ret = Py_None;
}
Py_XDECREF(full_slice);
Py_XDECREF(list);
Py_XDECREF(m);
Py_XDECREF(res);
return ret;
}
static PyObject* PyUpb_RepeatedContainer_MergeFrom(PyObject* _self,
PyObject* args) {
return PyUpb_RepeatedContainer_Extend(_self, args);
}
// -----------------------------------------------------------------------------
// RepeatedCompositeContainer
// -----------------------------------------------------------------------------
static PyObject* PyUpb_RepeatedCompositeContainer_AppendNew(PyObject* _self) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_AssureWritable(self);
if (!arr) return NULL;
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_arena* arena = PyUpb_Arena_Get(self->arena);
const upb_msgdef* m = upb_fielddef_msgsubdef(f);
upb_msg* msg = upb_msg_new(m, arena);
upb_msgval msgval = {.msg_val = msg};
upb_array_append(arr, msgval, arena);
return PyUpb_CMessage_Get(msg, m, self->arena);
}
PyObject* PyUpb_RepeatedCompositeContainer_Add(PyObject* _self, PyObject* args,
PyObject* kwargs) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
PyObject* py_msg = PyUpb_RepeatedCompositeContainer_AppendNew(_self);
if (!py_msg) return NULL;
if (PyUpb_CMessage_InitAttributes(py_msg, args, kwargs) < 0) {
Py_DECREF(py_msg);
upb_array_delete(self->ptr.arr, upb_array_size(self->ptr.arr) - 1, 1);
return NULL;
}
return py_msg;
}
static PyObject* PyUpb_RepeatedCompositeContainer_Append(PyObject* _self,
PyObject* value) {
if (!PyUpb_CMessage_Check(value)) return NULL;
PyObject* py_msg = PyUpb_RepeatedCompositeContainer_AppendNew(_self);
if (!py_msg) return NULL;
PyObject* none = PyUpb_CMessage_MergeFrom(py_msg, value);
if (!none) {
Py_DECREF(py_msg);
return NULL;
}
Py_DECREF(none);
return py_msg;
}
static PyObject* PyUpb_RepeatedContainer_Insert(PyObject* _self,
PyObject* args) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
Py_ssize_t index;
PyObject* value;
if (!PyArg_ParseTuple(args, "nO", &index, &value)) return NULL;
upb_array* arr = PyUpb_RepeatedContainer_AssureWritable(self);
if (!arr) return NULL;
// Normalize index.
Py_ssize_t size = upb_array_size(arr);
if (index < 0) index += size;
if (index < 0) index = 0;
if (index > size) index = size;
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_msgval msgval;
upb_arena* arena = PyUpb_Arena_Get(self->arena);
if (upb_fielddef_issubmsg(f)) {
// Create message.
const upb_msgdef* m = upb_fielddef_msgsubdef(f);
upb_msg* msg = upb_msg_new(m, arena);
PyObject* py_msg = PyUpb_CMessage_Get(msg, m, self->arena);
PyObject* ret = PyUpb_CMessage_MergeFrom(py_msg, value);
Py_DECREF(py_msg);
if (!ret) return NULL;
Py_DECREF(ret);
msgval.msg_val = msg;
} else {
if (!PyUpb_PyToUpb(value, f, &msgval, arena)) return NULL;
}
upb_array_insert(arr, index, 1, arena);
upb_array_set(arr, index, msgval);
Py_RETURN_NONE;
}
static PyMethodDef PyUpb_RepeatedCompositeContainer_Methods[] = {
// TODO(https://github.com/protocolbuffers/upb/issues/459)
// {"__deepcopy__", DeepCopy, METH_VARARGS, "Makes a deep copy of the class."},
{"add", (PyCFunction)PyUpb_RepeatedCompositeContainer_Add,
METH_VARARGS | METH_KEYWORDS, "Adds an object to the repeated container."},
{"append", PyUpb_RepeatedCompositeContainer_Append, METH_O,
"Appends a message to the end of the repeated container."},
{"insert", PyUpb_RepeatedContainer_Insert, METH_VARARGS,
"Inserts a message before the specified index."},
{"extend", PyUpb_RepeatedContainer_Extend, METH_O,
"Adds objects to the repeated container."},
{"pop", PyUpb_RepeatedContainer_Pop, METH_VARARGS,
"Removes an object from the repeated container and returns it."},
// TODO(https://github.com/protocolbuffers/upb/issues/459)
//{"remove", Remove, METH_O,
// "Removes an object from the repeated container."},
{"sort", (PyCFunction)PyUpb_RepeatedContainer_Sort,
METH_VARARGS | METH_KEYWORDS, "Sorts the repeated container."},
// TODO(https://github.com/protocolbuffers/upb/issues/459)
//{"reverse", reinterpret_cast<PyCFunction>(Reverse), METH_NOARGS,
// "Reverses elements order of the repeated container."},
{"MergeFrom", PyUpb_RepeatedContainer_MergeFrom, METH_O,
"Adds objects to the repeated container."},
{NULL, NULL}};
static PyType_Slot PyUpb_RepeatedCompositeContainer_Slots[] = {
{Py_tp_dealloc, PyUpb_RepeatedContainer_Dealloc},
{Py_tp_methods, PyUpb_RepeatedCompositeContainer_Methods},
{Py_sq_length, PyUpb_RepeatedContainer_Length},
{Py_sq_item, PyUpb_RepeatedContainer_Item},
{Py_mp_length, PyUpb_RepeatedContainer_Length},
{Py_tp_repr, PyUpb_RepeatedContainer_Repr},
{Py_mp_subscript, PyUpb_RepeatedContainer_Subscript},
{Py_mp_ass_subscript, PyUpb_RepeatedContainer_AssignSubscript},
{Py_tp_new, PyUpb_Forbidden_New},
{Py_tp_hash, PyObject_HashNotImplemented},
{0, NULL}};
static PyType_Spec PyUpb_RepeatedCompositeContainer_Spec = {
PYUPB_MODULE_NAME ".RepeatedCompositeContainer",
sizeof(PyUpb_RepeatedContainer),
0, // tp_itemsize
Py_TPFLAGS_DEFAULT,
PyUpb_RepeatedCompositeContainer_Slots,
};
// -----------------------------------------------------------------------------
// RepeatedScalarContainer
// -----------------------------------------------------------------------------
static PyObject* PyUpb_RepeatedScalarContainer_Append(PyObject* _self,
PyObject* value) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_AssureWritable(self);
upb_arena* arena = PyUpb_Arena_Get(self->arena);
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_msgval msgval;
if (!PyUpb_PyToUpb(value, f, &msgval, arena)) {
return NULL;
}
upb_array_append(arr, msgval, arena);
Py_RETURN_NONE;
}
static int PyUpb_RepeatedScalarContainer_AssignItem(PyObject* _self,
Py_ssize_t index,
PyObject* item) {
PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
upb_array* arr = PyUpb_RepeatedContainer_GetIfWritable(self);
Py_ssize_t size = arr ? upb_array_size(arr) : 0;
if (index < 0 || index >= size) {
PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index);
return -1;
}
const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self);
upb_msgval msgval;
upb_arena* arena = PyUpb_Arena_Get(self->arena);
if (!PyUpb_PyToUpb(item, f, &msgval, arena)) {
return -1;
}
upb_array_set(self->ptr.arr, index, msgval);
return 0;
}
static PyMethodDef PyUpb_RepeatedScalarContainer_Methods[] = {
// TODO(https://github.com/protocolbuffers/upb/issues/459)
// {"__deepcopy__", DeepCopy, METH_VARARGS, "Makes a deep copy of the
// class."},
// {"__reduce__", Reduce, METH_NOARGS,
// "Outputs picklable representation of the repeated field."},
{"append", PyUpb_RepeatedScalarContainer_Append, METH_O,
"Appends an object to the repeated container."},
{"extend", PyUpb_RepeatedContainer_Extend, METH_O,
"Appends objects to the repeated container."},
{"insert", PyUpb_RepeatedContainer_Insert, METH_VARARGS,
"Inserts an object at the specified position in the container."},
{"pop", PyUpb_RepeatedContainer_Pop, METH_VARARGS,
"Removes an object from the repeated container and returns it."},
// TODO(https://github.com/protocolbuffers/upb/issues/459)
// {"remove", Remove, METH_O,
// "Removes an object from the repeated container."},
{"sort", (PyCFunction)PyUpb_RepeatedContainer_Sort,
METH_VARARGS | METH_KEYWORDS, "Sorts the repeated container."},
// TODO(https://github.com/protocolbuffers/upb/issues/459)
// {"reverse", reinterpret_cast<PyCFunction>(Reverse), METH_NOARGS,
// "Reverses elements order of the repeated container."},
{"MergeFrom", PyUpb_RepeatedContainer_MergeFrom, METH_O,
"Merges a repeated container into the current container."},
{NULL, NULL}};
static PyType_Slot PyUpb_RepeatedScalarContainer_Slots[] = {
{Py_tp_dealloc, PyUpb_RepeatedContainer_Dealloc},
{Py_tp_methods, PyUpb_RepeatedScalarContainer_Methods},
{Py_tp_new, PyUpb_Forbidden_New},
{Py_tp_repr, PyUpb_RepeatedContainer_Repr},
{Py_sq_length, PyUpb_RepeatedContainer_Length},
{Py_sq_item, PyUpb_RepeatedContainer_Item},
{Py_sq_ass_item, PyUpb_RepeatedScalarContainer_AssignItem},
{Py_mp_length, PyUpb_RepeatedContainer_Length},
{Py_mp_subscript, PyUpb_RepeatedContainer_Subscript},
{Py_mp_ass_subscript, PyUpb_RepeatedContainer_AssignSubscript},
{Py_tp_richcompare, PyUpb_RepeatedContainer_RichCompare},
{Py_tp_hash, PyObject_HashNotImplemented},
{0, NULL}};
static PyType_Spec PyUpb_RepeatedScalarContainer_Spec = {
PYUPB_MODULE_NAME ".RepeatedScalarContainer",
sizeof(PyUpb_RepeatedContainer),
0, // tp_itemsize
Py_TPFLAGS_DEFAULT,
PyUpb_RepeatedScalarContainer_Slots,
};
// -----------------------------------------------------------------------------
// Top Level
// -----------------------------------------------------------------------------
static bool PyUpb_Repeated_RegisterAsSequence(PyUpb_ModuleState* state) {
PyObject* collections = NULL;
PyObject* seq = NULL;
PyObject* ret1 = NULL;
PyObject* ret2 = NULL;
PyTypeObject* type1 = state->repeated_scalar_container_type;
PyTypeObject* type2 = state->repeated_composite_container_type;
bool ok = (collections = PyImport_ImportModule("collections.abc")) &&
(seq = PyObject_GetAttrString(collections, "MutableSequence")) &&
(ret1 = PyObject_CallMethod(seq, "register", "O", type1)) &&
(ret2 = PyObject_CallMethod(seq, "register", "O", type2));
Py_XDECREF(collections);
Py_XDECREF(seq);
Py_XDECREF(ret1);
Py_XDECREF(ret2);
return ok;
}
bool PyUpb_Repeated_Init(PyObject* m) {
PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
state->repeated_composite_container_type =
PyUpb_AddClass(m, &PyUpb_RepeatedCompositeContainer_Spec);
state->repeated_scalar_container_type =
PyUpb_AddClass(m, &PyUpb_RepeatedScalarContainer_Spec);
return state->repeated_composite_container_type &&
state->repeated_scalar_container_type &&
PyUpb_Repeated_RegisterAsSequence(state);
}

@ -0,0 +1,66 @@
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google LLC nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PYUPB_REPEATED_H__
#define PYUPB_REPEATED_H__
#include <stdbool.h>
#include "python/python.h"
#include "upb/def.h"
// Creates a new repeated field in the unset state for field `f` of message
// object `parent`.
PyObject* PyUpb_RepeatedContainer_NewUnset(PyObject* parent,
const upb_fielddef* f,
PyObject* arena);
// Returns a repeated field object wrapping `arr`, of field type `f`, which
// must be on `arena`. If an existing wrapper object exists, it will be
// returned, otherwise a new object will be created. The caller always owns a
// ref on the returned value.
PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* arr,
const upb_fielddef* f,
PyObject* arena);
// Switches a repeated field in the unset state to be set, with `arr` as the
// data being pointed to.
void PyUpb_RepeatedContainer_Reify(PyObject* self, upb_array* arr);
// Implements repeated_field.extend(iterable). `_self` must be a repeated
// field (either repeated composite or repeated scalar).
PyObject* PyUpb_RepeatedContainer_Extend(PyObject* _self, PyObject* value);
// Implements repeated_field.add(initial_values). `_self` must be a repeated
// composite field.
PyObject* PyUpb_RepeatedCompositeContainer_Add(PyObject* _self, PyObject* args,
PyObject* kwargs);
// Module-level init.
bool PyUpb_Repeated_Init(PyObject* m);
#endif // PYUPB_REPEATED_H__

@ -373,6 +373,37 @@ bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) {
return true;
}
void upb_array_move(upb_array* arr, size_t dst_idx, size_t src_idx,
size_t count) {
char* data = _upb_array_ptr(arr);
int lg2 = arr->data & 7;
memmove(&data[dst_idx << lg2], &data[src_idx << lg2], count << lg2);
}
bool upb_array_insert(upb_array *arr, size_t i, size_t count,
upb_arena *arena) {
UPB_ASSERT(i <= arr->len);
UPB_ASSERT(count + arr->len >= count);
size_t oldsize = arr->len;
if (!upb_array_resize(arr, arr->len + count, arena)) {
return false;
}
upb_array_move(arr, i + count, i, oldsize - i);
return true;
}
/*
* i end arr->len
* |------------|XXXXXXXX|--------|
*/
void upb_array_delete(upb_array *arr, size_t i, size_t count) {
size_t end = i + count;
UPB_ASSERT(i <= end);
UPB_ASSERT(end <= arr->len);
upb_array_move(arr, i, end, arr->len - end);
arr->len -= count;
}
bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) {
return _upb_array_resize(arr, size, arena);
}

@ -134,6 +134,23 @@ void upb_array_set(upb_array *arr, size_t i, upb_msgval val);
/* Appends an element to the array. Returns false on allocation failure. */
bool upb_array_append(upb_array *array, upb_msgval val, upb_arena *arena);
/* Moves elements within the array using memmove(). Like memmove(), the source
* and destination elements may be overlapping. */
void upb_array_move(upb_array* array, size_t dst_idx, size_t src_idx,
size_t count);
/* Inserts one or more empty elements into the array. Existing elements are
* shifted right. The new elements have undefined state and must be set with
* `upb_array_set()`.
* REQUIRES: `i <= upb_array_size(arr)` */
bool upb_array_insert(upb_array *array, size_t i, size_t count,
upb_arena *arena);
/* Deletes one or more elements from the array. Existing elements are shifted
* left.
* REQUIRES: `i + count <= upb_array_size(arr)` */
void upb_array_delete(upb_array *array, size_t i, size_t count);
/* Changes the size of a vector. New elements are initialized to empty/0.
* Returns false on allocation failure. */
bool upb_array_resize(upb_array *array, size_t size, upb_arena *arena);

Loading…
Cancel
Save