~ubuntu-branches/debian/experimental/protobuf/experimental

« back to all changes in this revision

Viewing changes to python/google/protobuf/pyext/python-proto2.cc

  • Committer: Package Import Robot
  • Author(s): Robert S. Edmonds, Micah Anderson, Colin Watson, Steve Langasek, Robert S. Edmonds
  • Date: 2013-10-12 18:32:37 UTC
  • mfrom: (1.3.1) (10.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20131012183237-jz6tvmj9tn68atrf
Tags: 2.5.0-1
[ Micah Anderson ]
* New upstream version. (Closes: #704731.)
* Update debian/watch.
* Refresh patches.

[ Colin Watson ]
* Use the autotools-dev dh addon to update config.guess/config.sub for
  arm64. (Closes: #725976.)

[ Steve Langasek ]
* Don't recommend protobuf-compiler from the bindings, it's not used and
  this doesn't need to be pulled in at runtime. (Closes: #703628.)
* Mark protobuf-compiler Multi-Arch: foreign; the output of this command
  is architecture-independent source, we don't need the version of the
  compiler to match the target arch.
* Bump to debhelper compat 9, so that our libs get installed to the
  multiarch locations.
* Mark the library packages Multi-Arch: same.
* Fix debian/rules to support cross-building of the python bindings.
* Build-depend on libpython-dev, not python-dev, for cross-build
  compatibility.
* (Closes: #726083.)

[ Robert S. Edmonds ]
* Upload to experimental.
* Bump ABI version from 7 to 8.
* Bump Standards-Version to 3.9.4.
* Convert from python-support to dh-python.
* Drop support for python2.6.
* python-protobuf: switch back to the pure Python implementation, as
  upstream appears to no longer be maintaining the current C++ based Python
  binding. See the following upstream issues for details:
  - https://code.google.com/p/protobuf/issues/detail?id=434
  - https://code.google.com/p/protobuf/issues/detail?id=503

Show diffs side-by-side

added added

removed removed

Lines of Context:
207
207
          "Clears and sets the values of a repeated scalar field."),
208
208
  CMETHOD(ByteSize, METH_NOARGS,
209
209
          "Returns the size of the message in bytes."),
210
 
  CMETHOD(Clear, METH_NOARGS,
 
210
  CMETHOD(Clear, METH_O,
211
211
          "Clears a protocol message."),
212
 
  CMETHOD(ClearField, METH_O,
 
212
  CMETHOD(ClearField, METH_VARARGS,
213
213
          "Clears a protocol message field by name."),
214
214
  CMETHOD(ClearFieldByDescriptor, METH_O,
215
215
          "Clears a protocol message field by descriptor."),
274
274
PyTypeObject CMessage_Type = {
275
275
  PyObject_HEAD_INIT(&PyType_Type)
276
276
  0,
277
 
  C("google3.net.google.protobuf.python.internal."
 
277
  C("google.protobuf.internal."
278
278
    "_net_proto2___python."
279
279
    "CMessage"),                       // tp_name
280
280
  sizeof(CMessage),                    //  tp_basicsize
319
319
// ------ Helper Functions:
320
320
 
321
321
static void FormatTypeError(PyObject* arg, char* expected_types) {
322
 
  PyObject* s = PyObject_Str(PyObject_Type(arg));
323
 
  PyObject* repr = PyObject_Repr(PyObject_Type(arg));
 
322
  PyObject* repr = PyObject_Repr(arg);
324
323
  PyErr_Format(PyExc_TypeError,
325
324
               "%.100s has type %.100s, but expected one of: %s",
326
325
               PyString_AS_STRING(repr),
327
 
               PyString_AS_STRING(s),
 
326
               arg->ob_type->tp_name,
328
327
               expected_types);
329
 
  Py_DECREF(s);
330
328
  Py_DECREF(repr);
331
329
}
332
330
 
398
396
  return global_message_factory->GetPrototype(descriptor);
399
397
}
400
398
 
 
399
static void ReleaseSubMessage(google::protobuf::Message* message,
 
400
                           const google::protobuf::FieldDescriptor* field_descriptor,
 
401
                           CMessage* child_cmessage) {
 
402
  Message* released_message = message->GetReflection()->ReleaseMessage(
 
403
      message, field_descriptor, global_message_factory);
 
404
  GOOGLE_DCHECK(child_cmessage->message != NULL);
 
405
  // ReleaseMessage will return NULL which differs from
 
406
  // child_cmessage->message, if the field does not exist.  In this case,
 
407
  // the latter points to the default instance via a const_cast<>, so we
 
408
  // have to reset it to a new mutable object since we are taking ownership.
 
409
  if (released_message == NULL) {
 
410
    const Message* prototype = global_message_factory->GetPrototype(
 
411
        child_cmessage->message->GetDescriptor());
 
412
    GOOGLE_DCHECK(prototype != NULL);
 
413
    child_cmessage->message = prototype->New();
 
414
  }
 
415
  child_cmessage->parent = NULL;
 
416
  child_cmessage->parent_field = NULL;
 
417
  child_cmessage->free_message = true;
 
418
  child_cmessage->read_only = false;
 
419
}
 
420
 
401
421
static bool CheckAndSetString(
402
422
    PyObject* arg, google::protobuf::Message* message,
403
423
    const google::protobuf::FieldDescriptor* descriptor,
504
524
  self->message = reflection->MutableMessage(
505
525
      message, self->parent_field->descriptor, global_message_factory);
506
526
  self->read_only = false;
507
 
  self->parent = NULL;
508
 
  self->parent_field = NULL;
509
527
}
510
528
 
511
529
static PyObject* InternalGetScalar(
955
973
 
956
974
// ------ Methods:
957
975
 
958
 
static PyObject* CMessage_Clear(CMessage* self, PyObject* args) {
 
976
static PyObject* CMessage_Clear(CMessage* self, PyObject* arg) {
959
977
  AssureWritable(self);
960
 
  self->message->Clear();
 
978
  google::protobuf::Message* message = self->message;
 
979
 
 
980
  // This block of code is equivalent to the following:
 
981
  // for cfield_descriptor, child_cmessage in arg:
 
982
  //   ReleaseSubMessage(cfield_descriptor, child_cmessage)
 
983
  if (!PyList_Check(arg)) {
 
984
    PyErr_SetString(PyExc_TypeError, "Must be a list");
 
985
    return NULL;
 
986
  }
 
987
  PyObject* messages_to_clear = arg;
 
988
  Py_ssize_t num_messages_to_clear = PyList_GET_SIZE(messages_to_clear);
 
989
  for(int i = 0; i < num_messages_to_clear; ++i) {
 
990
    PyObject* message_tuple = PyList_GET_ITEM(messages_to_clear, i);
 
991
    if (!PyTuple_Check(message_tuple) || PyTuple_GET_SIZE(message_tuple) != 2) {
 
992
      PyErr_SetString(PyExc_TypeError, "Must be a tuple of size 2");
 
993
      return NULL;
 
994
    }
 
995
 
 
996
    PyObject* py_cfield_descriptor = PyTuple_GET_ITEM(message_tuple, 0);
 
997
    PyObject* py_child_cmessage = PyTuple_GET_ITEM(message_tuple, 1);
 
998
    if (!PyObject_TypeCheck(py_cfield_descriptor, &CFieldDescriptor_Type) ||
 
999
        !PyObject_TypeCheck(py_child_cmessage, &CMessage_Type)) {
 
1000
      PyErr_SetString(PyExc_ValueError, "Invalid Tuple");
 
1001
      return NULL;
 
1002
    }
 
1003
 
 
1004
    CFieldDescriptor* cfield_descriptor = reinterpret_cast<CFieldDescriptor *>(
 
1005
        py_cfield_descriptor);
 
1006
    CMessage* child_cmessage = reinterpret_cast<CMessage *>(py_child_cmessage);
 
1007
    ReleaseSubMessage(message, cfield_descriptor->descriptor, child_cmessage);
 
1008
  }
 
1009
 
 
1010
  message->Clear();
961
1011
  Py_RETURN_NONE;
962
1012
}
963
1013
 
1039
1089
  Py_RETURN_NONE;
1040
1090
}
1041
1091
 
1042
 
static PyObject* CMessage_ClearField(CMessage* self, PyObject* arg) {
 
1092
static PyObject* CMessage_ClearField(CMessage* self, PyObject* args) {
1043
1093
  char* field_name;
1044
 
  if (PyString_AsStringAndSize(arg, &field_name, NULL) < 0) {
 
1094
  CMessage* child_cmessage = NULL;
 
1095
  if (!PyArg_ParseTuple(args, C("s|O!:ClearField"), &field_name,
 
1096
                        &CMessage_Type, &child_cmessage)) {
1045
1097
    return NULL;
1046
1098
  }
1047
1099
 
1054
1106
    return NULL;
1055
1107
  }
1056
1108
 
1057
 
  message->GetReflection()->ClearField(message, field_descriptor);
 
1109
  if (child_cmessage != NULL && !FIELD_IS_REPEATED(field_descriptor)) {
 
1110
    ReleaseSubMessage(message, field_descriptor, child_cmessage);
 
1111
  } else {
 
1112
    message->GetReflection()->ClearField(message, field_descriptor);
 
1113
  }
1058
1114
  Py_RETURN_NONE;
1059
1115
}
1060
1116
 
1099
1155
  PyObject* next;
1100
1156
  while ((next = PyIter_Next(iter)) != NULL) {
1101
1157
    if (InternalAddRepeatedScalar(
1102
 
        message, cfield_descriptor->descriptor, next) == NULL) {
 
1158
            message, cfield_descriptor->descriptor, next) == NULL) {
 
1159
      Py_DECREF(next);
1103
1160
      Py_DECREF(iter);
1104
1161
      return NULL;
1105
1162
    }
 
1163
    Py_DECREF(next);
1106
1164
  }
1107
1165
  Py_DECREF(iter);
1108
1166
  Py_RETURN_NONE;
1311
1369
  AssureWritable(self);
1312
1370
  google::protobuf::io::CodedInputStream input(
1313
1371
      reinterpret_cast<const uint8*>(data), data_length);
 
1372
  input.SetExtensionRegistry(GetDescriptorPool(), global_message_factory);
1314
1373
  bool success = self->message->MergePartialFromCodedStream(&input);
1315
1374
  if (success) {
1316
1375
    return PyInt_FromLong(self->message->ByteSize());