<commit_before>// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <assert.h>

#include "rcl/error_handling.h"

#include "rclpy_common/common.h"

/**
 * Mirrors the struct rmw_qos_profile_t from rmw/types.h
 */
typedef struct rclpy_qos_profile
{
  PyObject * depth;
  PyObject * history;
  PyObject * reliability;
  PyObject * durability;
  PyObject * lifespan;
  PyObject * deadline;
  PyObject * liveliness;
  PyObject * liveliness_lease_duration;
  PyObject * avoid_ros_namespace_conventions;
} rclpy_qos_profile_t;

void
init_rclpy_qos_profile(rclpy_qos_profile_t * rclpy_profile)
{
  assert(rclpy_profile);
  memset(rclpy_profile, 0, sizeof(*rclpy_profile));
}

void
cleanup_rclpy_qos_profile(rclpy_qos_profile_t * profile)
{
  Py_XDECREF(profile->depth);
  Py_XDECREF(profile->history);
  Py_XDECREF(profile->reliability);
  Py_XDECREF(profile->durability);
  Py_XDECREF(profile->lifespan);
  Py_XDECREF(profile->deadline);
  Py_XDECREF(profile->liveliness);
  Py_XDECREF(profile->liveliness_lease_duration);
  Py_XDECREF(profile->avoid_ros_namespace_conventions);
}

bool
rclpy_names_and_types_fini(rcl_names_and_types_t * names_and_types)
{
  if (!names_and_types) {
    return true;
  }
  rcl_ret_t ret = rcl_names_and_types_fini(names_and_types);
  if (ret != RCL_RET_OK) {
    PyErr_Format(
      PyExc_RuntimeError,
      "Failed to destroy rcl_names_and_types_t: %s", rcl_get_error_string().str);
    rcl_reset_error();
    return false;
  }
  return true;
}

void *
rclpy_common_get_type_support(PyObject * pymsg_type)
{
  PyObject * pymetaclass = PyObject_GetAttrString(pymsg_type, "__class__");
  if (!pymetaclass) {
    return NULL;
  }

  PyObject * pyts = PyObject_GetAttrString(pymetaclass, "_TYPE_SUPPORT");
  Py_DECREF(pymetaclass);
  if (!pyts) {
    return NULL;
  }

  void * ts = PyCapsule_GetPointer(pyts, NULL);
  Py_DECREF(pyts);
  return ts;
}

PyObject *
rclpy_convert_to_py_names_and_types(rcl_names_and_types_t * names_and_types)
{
  if (!names_and_types) {
    return NULL;
  }

  PyObject * pynames_and_types = PyList_New(names_and_types->names.size);
  if (!pynames_and_types) {
    return NULL;
  }

  size_t i;
  for (i = 0; i < names_and_types->names.size; ++i) {
    PyObject * pytuple = PyTuple_New(2);
    if (!pytuple) {
      Py_DECREF(pynames_and_types);
      return NULL;
    }
    PyObject * pyname = PyUnicode_FromString(names_and_types->names.data[i]);
    if (!pyname) {
      Py_DECREF(pynames_and_types);
      Py_DECREF(pytuple);
      return NULL;
    }
    PyTuple_SET_ITEM(pytuple, 0, pyname);
    PyObject * pytypes_list = PyList_New(names_and_types->types[i].size);
    if (!pytypes_list) {
      Py_DECREF(pynames_and_types);
      Py_DECREF(pytuple);
      return NULL;
    }
    size_t j;
    for (j = 0; j < names_and_types->types[i].size; ++j) {
      PyObject * pytype = PyUnicode_FromString(names_and_types->types[i].data[j]);
      if (!pytype) {
        Py_DECREF(pynames_and_types);
        Py_DECREF(pytuple);
        Py_DECREF(pytypes_list);
        return NULL;
      }
      PyList_SET_ITEM(pytypes_list, j, pytype);
    }
    PyTuple_SET_ITEM(pytuple, 1, pytypes_list);
    PyList_SET_ITEM(pynames_and_types, i, pytuple);
  }
  return pynames_and_types;
}

static
PyObject *
_convert_rmw_time_to_py_duration(const rmw_time_t * duration)
{
  uint64_t total_nanoseconds = RCUTILS_S_TO_NS(duration->sec) + duration->nsec;
  PyObject * pyduration_module = NULL;
  PyObject * pyduration_class = NULL;
  PyObject * args = NULL;
  PyObject * kwargs = NULL;
  PyObject * pyduration_object = NULL;

  pyduration_module = PyImport_ImportModule("rclpy.duration");
  if (!pyduration_module) {
    goto cleanup;
  }
  pyduration_class = PyObject_GetAttrString(pyduration_module, "Duration");
  if (!pyduration_class) {
    goto cleanup;
  }
  args = PyTuple_New(0);
  if (!args) {
    goto cleanup;
  }
  kwargs = Py_BuildValue("{sK}", "nanoseconds", total_nanoseconds);
  if (!kwargs) {
    goto cleanup;
  }
  pyduration_object = PyObject_Call(pyduration_class, args, kwargs);
  if (!pyduration_object) {
    goto cleanup;
  }

cleanup:<commit_msg>Fix bad rmw_time_t to nanoseconds conversion. (#555)<commit_after>// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <assert.h>

#include "rcl/error_handling.h"

#include "rclpy_common/common.h"

/**
 * Mirrors the struct rmw_qos_profile_t from rmw/types.h
 */
typedef struct rclpy_qos_profile
{
  PyObject * depth;
  PyObject * history;
  PyObject * reliability;
  PyObject * durability;
  PyObject * lifespan;
  PyObject * deadline;
  PyObject * liveliness;
  PyObject * liveliness_lease_duration;
  PyObject * avoid_ros_namespace_conventions;
} rclpy_qos_profile_t;

void
init_rclpy_qos_profile(rclpy_qos_profile_t * rclpy_profile)
{
  assert(rclpy_profile);
  memset(rclpy_profile, 0, sizeof(*rclpy_profile));
}

void
cleanup_rclpy_qos_profile(rclpy_qos_profile_t * profile)
{
  Py_XDECREF(profile->depth);
  Py_XDECREF(profile->history);
  Py_XDECREF(profile->reliability);
  Py_XDECREF(profile->durability);
  Py_XDECREF(profile->lifespan);
  Py_XDECREF(profile->deadline);
  Py_XDECREF(profile->liveliness);
  Py_XDECREF(profile->liveliness_lease_duration);
  Py_XDECREF(profile->avoid_ros_namespace_conventions);
}

bool
rclpy_names_and_types_fini(rcl_names_and_types_t * names_and_types)
{
  if (!names_and_types) {
    return true;
  }
  rcl_ret_t ret = rcl_names_and_types_fini(names_and_types);
  if (ret != RCL_RET_OK) {
    PyErr_Format(
      PyExc_RuntimeError,
      "Failed to destroy rcl_names_and_types_t: %s", rcl_get_error_string().str);
    rcl_reset_error();
    return false;
  }
  return true;
}

void *
rclpy_common_get_type_support(PyObject * pymsg_type)
{
  PyObject * pymetaclass = PyObject_GetAttrString(pymsg_type, "__class__");
  if (!pymetaclass) {
    return NULL;
  }

  PyObject * pyts = PyObject_GetAttrString(pymetaclass, "_TYPE_SUPPORT");
  Py_DECREF(pymetaclass);
  if (!pyts) {
    return NULL;
  }

  void * ts = PyCapsule_GetPointer(pyts, NULL);
  Py_DECREF(pyts);
  return ts;
}

PyObject *
rclpy_convert_to_py_names_and_types(rcl_names_and_types_t * names_and_types)
{
  if (!names_and_types) {
    return NULL;
  }

  PyObject * pynames_and_types = PyList_New(names_and_types->names.size);
  if (!pynames_and_types) {
    return NULL;
  }

  size_t i;
  for (i = 0; i < names_and_types->names.size; ++i) {
    PyObject * pytuple = PyTuple_New(2);
    if (!pytuple) {
      Py_DECREF(pynames_and_types);
      return NULL;
    }
    PyObject * pyname = PyUnicode_FromString(names_and_types->names.data[i]);
    if (!pyname) {
      Py_DECREF(pynames_and_types);
      Py_DECREF(pytuple);
      return NULL;
    }
    PyTuple_SET_ITEM(pytuple, 0, pyname);
    PyObject * pytypes_list = PyList_New(names_and_types->types[i].size);
    if (!pytypes_list) {
      Py_DECREF(pynames_and_types);
      Py_DECREF(pytuple);
      return NULL;
    }
    size_t j;
    for (j = 0; j < names_and_types->types[i].size; ++j) {
      PyObject * pytype = PyUnicode_FromString(names_and_types->types[i].data[j]);
      if (!pytype) {
        Py_DECREF(pynames_and_types);
        Py_DECREF(pytuple);
        Py_DECREF(pytypes_list);
        return NULL;
      }
      PyList_SET_ITEM(pytypes_list, j, pytype);
    }
    PyTuple_SET_ITEM(pytuple, 1, pytypes_list);
    PyList_SET_ITEM(pynames_and_types, i, pytuple);
  }
  return pynames_and_types;
}

static
PyObject *
_convert_rmw_time_to_py_duration(const rmw_time_t * duration)
{
  PyObject * pyduration_module = NULL;
  PyObject * pyduration_class = NULL;
  PyObject * args = NULL;
  PyObject * kwargs = NULL;
  PyObject * pyduration_object = NULL;

  pyduration_module = PyImport_ImportModule("rclpy.duration");
  if (!pyduration_module) {
    goto cleanup;
  }
  pyduration_class = PyObject_GetAttrString(pyduration_module, "Duration");
  if (!pyduration_class) {
    goto cleanup;
  }
  args = PyTuple_New(0);
  if (!args) {
    goto cleanup;
  }
  kwargs = Py_BuildValue("{sKsK}", "seconds", duration->sec, "nanoseconds", duration->nsec);
  if (!kwargs) {
    goto cleanup;
  }
  pyduration_object = PyObject_Call(pyduration_class, args, kwargs);
  if (!pyduration_object) {
    goto cleanup;
  }

cleanup:
{'fraction_non_alphanumeric': 0.10877005347593582, 'fraction_numerical': 0.0037433155080213902, 'mean_word_length': 4.160596026490066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1348006', 'n_tokens_mistral': 3447, 'n_tokens_neox': 3491, 'n_words': 667}