<commit_before>/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2009 <NAME>.  See LICENSE for details.
 *
 * This file implements an interface to Python that is compatible
 * (as much as possible) with proto1 (the first implementation of
 * protocol buffers, which is only released internally to Google).
 *
 * The key interface we must support is ProtocolMessage.  Each message
 * type has its own Python class that supports the ProtocolMessage
 * interface (obj.Clear(), obj.IsInitialized(), etc) as well as
 * message-specific accessors (obj.foo(), obj.set_foo(),
 * obj.clear_foo(), etc).
 *
 * accessors.  We represent these message types as instances as
 * upb.pb.MessageType objects.  In other words, these instances
 * are both instances of upb.pb.MessageType *and* classes of
 * type MyProtoType.
 */

#include <Python.h>
#include <stddef.h>
#include "upb_mm.h"
#include "definition.h"

/* Opcodes that describe all of the operations you can perform on a field of a
 * protobuf from Python.  For example, foo.has_bar() uses opcode OP_HAS. */
typedef enum {
  /* For non-repeated fields. */
  OP_HAS,
  /* For non-repeated fields that are not submessages. */
  OP_SET,
  /* For non-repeated message fields. */
  OP_MUTABLE,

  /* For repeated fields. */
  OP_SIZE, OP_LIST, OP_ADD,

  /* For all types of fields. */
  OP_GET, OP_CLEAR
} PyUpb_PbBoundFieldOpCode;

const char *opcode_names[] = {
  "OP_HAS", "OP_SET", "OP_MUTABLE", "OP_SIZE", "OP_LIST", "OP_ADD", "OP_GET", "OP_CLEAR"
};

/* Structures for the Python objects we define. */
typedef struct {
  PyObject_HEAD;
  PyUpb_MsgDef *def;
} PyUpb_PbMsgCreator;

typedef struct {
  PyObject_HEAD;
  struct upb_mm_ref ref;
  PyUpb_MsgDef *def;
} PyUpb_PbMsg;

typedef struct {
  PyObject_HEAD;
  PyUpb_PbMsg *msg;
  struct upb_msg_fielddef *f;
  PyUpb_PbBoundFieldOpCode code;
} PyUpb_PbBoundFieldOp;

static PyTypeObject PyUpb_PbMsgCreatorType;
static PyTypeObject PyUpb_PbMsgType;
static PyTypeObject PyUpb_PbBoundFieldOpType;

#define Check_MsgCreator(obj) \
  (void*)obj; do { \
    if(!PyObject_TypeCheck(obj, &PyUpb_PbMsgCreatorType)) { \
      PyErr_SetString(PyExc_TypeError, "must be a MessageCreator"); \
      return NULL; \
    } \
  } while(0)<commit_msg>Updated/corrected comments in pb.c.<commit_after>/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2009 <NAME>.  See LICENSE for details.
 *
 * This file implements an interface to Python that is compatible
 * (as much as possible) with proto1 (the first implementation of
 * protocol buffers, which is only released internally to Google).
 */

#include <Python.h>
#include <stddef.h>
#include "upb_mm.h"
#include "definition.h"

/* Opcodes that describe all of the operations you can perform on a field of a
 * protobuf from Python.  For example, foo.has_bar() uses opcode OP_HAS. */
typedef enum {
  /* For non-repeated fields. */
  OP_HAS,
  /* For non-repeated fields that are not submessages. */
  OP_SET,
  /* For non-repeated message fields. */
  OP_MUTABLE,

  /* For repeated fields. */
  OP_SIZE, OP_LIST, OP_ADD,

  /* For all types of fields. */
  OP_GET, OP_CLEAR
} PyUpb_PbBoundFieldOpCode;

const char *opcode_names[] = {
  "OP_HAS", "OP_SET", "OP_MUTABLE", "OP_SIZE", "OP_LIST", "OP_ADD", "OP_GET", "OP_CLEAR"
};

/* Structures for the Python objects we define. */

/* Callable that will create a new message object of a specific type.  In this
 * sense it sort of "pretends" to be a type, but it is not actually a type. */
typedef struct {
  PyObject_HEAD;
  PyUpb_MsgDef *def;
} PyUpb_PbMsgCreator;

/* Message object.  All messages use this structure and have the same Python
 * type (even if their .proto types are different).  The type dictionary for
 * this type does not include field accessors -- those are dynamically looked
 * up in msg_getattro. */
typedef struct {
  PyObject_HEAD;
  struct upb_mm_ref ref;
  PyUpb_MsgDef *def;
} PyUpb_PbMsg;

/* Represents a "bound" operation like obj.has_foo, that will perform the
 * operation when called.  This is necessary because proto1 has all of its
 * operations modeled as methods, so one calls obj.has_foo(), not obj.has_foo
 * alone. */
typedef struct {
  PyObject_HEAD;
  PyUpb_PbMsg *msg;
  struct upb_msg_fielddef *f;
  PyUpb_PbBoundFieldOpCode code;
} PyUpb_PbBoundFieldOp;

static PyTypeObject PyUpb_PbMsgCreatorType;
static PyTypeObject PyUpb_PbMsgType;
static PyTypeObject PyUpb_PbBoundFieldOpType;

#define Check_MsgCreator(obj) \
  (void*)obj; do { \
    if(!PyObject_TypeCheck(obj, &PyUpb_PbMsgCreatorType)) { \
      PyErr_SetString(PyExc_TypeError, "must be a MessageCreator"); \
      return NULL; \
    } \
  } while(0)
{'fraction_non_alphanumeric': 0.09936170212765957, 'fraction_numerical': 0.0027659574468085106, 'mean_word_length': 3.948421052631579, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7618615', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1561, 'n_words': 516}