Example #1
0
static PyObject* OperationOptions_SetTimeout(OperationOptions* self, PyObject* timeout)
{
	PyDateTime_IMPORT;

	if (!PyDelta_Check(timeout))
	{
		PyErr_SetString(PyExc_TypeError, "parameter timeout must be of type datetime.timedelta");
		return NULL;
	}

	try
	{
		MI_Interval miTimeout;
		MIIntervalFromPyDelta(timeout, miTimeout);
        AllowThreads(&self->cs, [&]() {
            self->operationOptions->SetTimeout(miTimeout);
        });
		Py_RETURN_NONE;
	}
	catch (std::exception& ex)
	{
		SetPyException(ex);
		return NULL;
	}
}
Example #2
0
static int fudgepyc_convertUtcOffset ( int * target, PyObject * obj )
{
    int days, seconds, microseconds;

    if ( ! PyDelta_Check ( obj ) )
    {
        exception_raise_any ( PyExc_TypeError,
                              "datetime.time.utcoffset() did not return "
                              "a datetime.timedelta instance as expected" );
        return -1;
    }

    if ( fudgepyc_convertAttrToInt ( &days, obj, "days" ) ||
         fudgepyc_convertAttrToInt ( &seconds, obj, "seconds" ) ||
         fudgepyc_convertAttrToInt ( &microseconds, obj, "microseconds" ) )
        return -1;

    seconds += days * 86400;

    if ( seconds % 60 || microseconds )
    {
        exception_raise_any ( PyExc_ValueError,
                              "The maximum resolution for datetime.tzinfo "
                              "instances is 15 minutes; UTC offsets not "
                              "exactly divisible by this are not supported" );
        return -1;
    }

    *target = seconds / 900;    /* 15 minutes in seconds */
    return 0;
}
Example #3
0
static void * pydelta_to_timespec(PyObject *td, struct timespec *ts)
{

    if (!PyDelta_Check(td)) {
        return NULL;
    }

    /* ts->tv_nsec = (PyDateTime_DELTA_GET_MICROSECONDS(td) % MICROSEC) * 1000; */
    ts->tv_nsec = MICRO2NANO(PyDateTime_DELTA_GET_MICROSECONDS(td));
    ts->tv_sec = PyDateTime_DELTA_GET_SECONDS(td);
    if (PyErr_Occurred() != NULL) {
        return NULL;
    }

    ts->tv_sec += PyDateTime_DELTA_GET_DAYS(td) * 24 * 60 * 60;
    if (PyErr_Occurred() != NULL) {
        return NULL;
    }

    printf("pydelta_to_timespec sec: %ld, nsec: %ld\n", ts->tv_sec, ts->tv_nsec);

    return td;
}//pydelta_to_timespec()
/**
  Convert a Python datetime.timedelta to MySQL TIME.

  Convert the PyObject obj, which must be a datetime.timedelta,
  to MySQL TIME value.

  Raises TypeError when obj is not a PyDelta_Type.

  @param    obj     the PyObject to be converted

  @return   Converted timedelta object.
    @retval PyBytes     Python v3
    @retval PyString    Python v2
    @retval NULL        Exception
*/
PyObject*
pytomy_timedelta(PyObject *obj)
{
    int days= 0, secs= 0 , micro_secs= 0, total_secs= 0;
    int hours= 0, mins= 0, remainder= 0;
    char fmt[32]= "";
    char result[17]= "";
    char minus[1]= "";

    PyDateTime_IMPORT;

    if (!obj || !PyDelta_Check(obj))
    {
        PyErr_SetString(PyExc_ValueError,
                        "Object must be a datetime.timedelta");
        return NULL;
    }

    // Cannot use PyDateTime_DELTA_* (new in Python v3.3)
    days= ((PyDateTime_Delta*)obj)->days;
    secs= ((PyDateTime_Delta*)obj)->seconds;
    micro_secs= ((PyDateTime_Delta*)obj)->microseconds;

    total_secs= abs(days * 86400 + secs);

#pragma warning(push)
// result of strncpy does not accept direct user input
#pragma warning(disable: 4996)
    if (micro_secs)
    {
        strncpy(fmt, "%s%02d:%02d:%02d.%06d", 21);
        if (days < 0)
        {
            micro_secs= 1000000 - micro_secs;
            total_secs-= 1;
        }
    }
    else
    {
        strncpy(fmt, "%s%02d:%02d:%02d", 16);
    }
#pragma warning(pop)

    if (days < 0)
    {
        minus[0]= '-';
    }

    hours= total_secs / 3600;
    remainder= total_secs % 3600;
    mins= remainder / 60;
    secs= remainder % 60;

    if (micro_secs)
    {
        PyOS_snprintf(result, 17, fmt, minus, hours, mins, secs, micro_secs);
    }
    else
    {
        PyOS_snprintf(result, 17, fmt, minus, hours, mins, secs);
    }

    return PyBytesFromString(result);
}
Example #5
0
static PyObject *m_timerfd_settime(PyObject *self, PyObject *args, PyObject *kwargs)
{
    /* printf("m_timerfd_settime\n"); */
    /* XXX We don't accept flags at this time and we only take relatvie times.
     */

    /* char *kw[] = {"fd", "deadline", "interval", "flags", NULL}; */
    char *kw[] = {"fd", "deadline", "interval", NULL};
    long fd = 0;
    /* long  flags = 0; */
    long msec = 0; // microseconds
    int t_res;
    PyObject *deadline = NULL;
    PyObject *interval = NULL;

    struct itimerspec new_val;
    struct itimerspec old_val;
    memset(&new_val, 0, sizeof(struct itimerspec));
    memset(&old_val, 0, sizeof(struct itimerspec));

    if (!PyArg_ParseTupleAndKeywords(
                args, kwargs, "l|OO:timerfd_settime", kw,
                &fd, &deadline, &interval)) {
        return NULL;
    }
    printf("fd %ld, deadline %p interval %p\n", fd, deadline, interval);

    if (deadline != NULL) {
        /* printf("deadline is set\n"); */
        if (PyDelta_Check(deadline)) {
            /* printf("deadline is Delta\n"); */
            if(pydelta_to_timespec(deadline, &new_val.it_value) == NULL) {
                return NULL;
            }
        } else if (PyLong_Check(deadline)) {
            /* printf("deadline is long\n"); */
            msec = PyLong_AsLong(deadline);
            new_val.it_value.tv_sec = msec / MICROSEC;
            /* new_val.it_value.tv_nsec = (msec % MICROSEC) * 1000; */
            new_val.it_value.tv_nsec = MICRO2NANO(msec);
        } else {
            /* printf("deadline is UK\n"); */
            PyErr_SetString(
                PyExc_TypeError,
                "deadline is not a valid type. Must be a timedelta object or integer");
            return NULL;
        }
    }
    /* printf("deadline sec %ld nsec %ld\n", new_val.it_value.tv_sec, new_val.it_value.tv_nsec); */

    if (interval != NULL) {
        /* printf("interval is set\n"); */
        /* Py_INCREF(interval); */
        if (PyDelta_Check(interval)) {
            /* printf("interval is Delta\n"); */
            if(pydelta_to_timespec(interval, &new_val.it_interval) == NULL) {
                return NULL;
            }
        } else if (PyLong_Check(interval)) {
            /* printf("interval is long\n"); */
            msec = PyLong_AsLong(interval);
            new_val.it_interval.tv_sec = msec / MICROSEC;
            /* new_val.it_interval.tv_nsec = (msec % MICROSEC) * 1000; */
            new_val.it_interval.tv_nsec = MICRO2NANO(msec);
        } else {
            PyErr_SetString(
                PyExc_TypeError,
                "interval is not a valid type. Must be a timedelta object or integer");
            return NULL;
        }
    }
    /* printf("interval is %ld\n", msec); */
    /*     [> printf("interval sec %ld nsec %ld\n", <] */
    /*             [> new_val.it_interval.tv_sec, <] */
    /*             [> new_val.it_interval.tv_nsec); <] */

    /* printf("settime fd %ld,  new_val %p old_val %p\n", fd, &new_val, &old_val); */
    t_res = timerfd_settime(fd, 0, &new_val, &old_val);
    /* printf("timerfd_settime response %d\n", t_res); */

    if (t_res != 0) {
        /* printf("error out\n"); */
        return PyErr_SetFromErrno(PyExc_IOError);
    }

    /* printf("create tuple\n"); */
    PyObject *resp = PyTuple_New(2);
    if (resp == NULL) {
        /* printf("bad tuple\n"); */
        return NULL;
    }

    printf("settime fd %ld,  old_sec %ld old_nsec %ld\n",
           fd, old_val.it_value.tv_sec, old_val.it_value.tv_nsec);
    PyTuple_SetItem(
        resp,
        0,
        PyDelta_FromDSU(0, old_val.it_value.tv_sec,
                        NANO2MICRO(old_val.it_value.tv_nsec)));
    if (PyErr_Occurred() != NULL) {
        Py_XDECREF(resp);
        return NULL;
    }

    printf("settime fd %ld,  old_int_sec %ld old_int_nsec %ld\n",
           fd, old_val.it_interval.tv_sec, old_val.it_interval.tv_nsec);
    PyTuple_SetItem(
        resp,
        1,
        PyDelta_FromDSU(0, old_val.it_interval.tv_sec,
                        NANO2MICRO(old_val.it_interval.tv_nsec)));
    if (PyErr_Occurred() != NULL) {
        Py_XDECREF(resp);
        return NULL;
    }

    /* printf("return %p\n", resp); */
    return (PyObject *)resp;
}//m_timerfd_settime()