Пример #1
0
static void
gen_del(PyObject *self)
{
    PyObject *res;
    PyObject *error_type, *error_value, *error_traceback;
    PyGenObject *gen = (PyGenObject *)self;

    if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
        /* Generator isn't paused, so no need to close */
        return;

    /* Temporarily resurrect the object. */
    assert(self->ob_refcnt == 0);
    self->ob_refcnt = 1;

    /* Save the current exception, if any. */
    PyErr_Fetch(&error_type, &error_value, &error_traceback);

    res = gen_close(gen, NULL);

    if (res == NULL)
        PyErr_WriteUnraisable(self);
    else
        Py_DECREF(res);

    /* Restore the saved exception. */
    PyErr_Restore(error_type, error_value, error_traceback);

    /* Undo the temporary resurrection; can't use DECREF here, it would
     * cause a recursive call.
     */
    assert(self->ob_refcnt > 0);
    if (--self->ob_refcnt == 0)
        return; /* this is the normal path out */

    /* close() resurrected it!  Make it look like the original Py_DECREF
     * never happened.
     */
    {
        Py_ssize_t refcnt = self->ob_refcnt;
        _Py_NewReference(self);
        self->ob_refcnt = refcnt;
    }
    assert(PyType_IS_GC(self->ob_type) &&
           _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);

    /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
     * we need to undo that. */
    _Py_DEC_REFTOTAL;
    /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
     * chain, so no more to do there.
     * If COUNT_ALLOCS, the original decref bumped tp_frees, and
     * _Py_NewReference bumped tp_allocs:  both of those need to be
     * undone.
     */
#ifdef COUNT_ALLOCS
    --self->ob_type->tp_frees;
    --self->ob_type->tp_allocs;
#endif
}
Пример #2
0
static int
gen_close_iter(PyObject *yf)
{
    PyObject *retval = NULL;
    _Py_IDENTIFIER(close);

    if (PyGen_CheckExact(yf)) {
        retval = gen_close((PyGenObject *)yf, NULL);
        if (retval == NULL)
            return -1;
    } else {
        PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
        if (meth == NULL) {
            if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                PyErr_WriteUnraisable(yf);
            PyErr_Clear();
        } else {
            retval = PyObject_CallFunction(meth, "");
            Py_DECREF(meth);
            if (retval == NULL)
                return -1;
        }
    }
    Py_XDECREF(retval);
    return 0;
}
Пример #3
0
/*
 * Re-Open the device if it is open.
 */
int
wmcd_reopen( struct wm_drive *d )
{
  int status;
  
  do {
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen\n");
    status = gen_close( d );
    wm_susleep( 1000 );
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calling wmcd_open()\n");
    status = wmcd_open( d ); /* open it as usual */
    wm_susleep( 1000 );
  } while ( status != 0 );
  return status;
} /* wmcd_reopen() */
Пример #4
0
int main()
{
	srand(time(NULL));

	T = MAXT;
	printf("%d\n", T);

	while (T) {
		int r = rand() % 3;

		if (r == 0) gen_close();
		else if (r == 1) gen_rand();
		else gen();
	}

	return 0;
}
Пример #5
0
/*
 * Eject the current CD, if there is one.
 */
int
gen_eject( struct wm_drive *d )
{
    struct stat stbuf;
    struct ustat ust;

    if (fstat(d->fd, &stbuf) != 0)
        return -2;

    /* Is this a mounted filesystem? */
    if (ustat(stbuf.st_rdev, &ust) == 0)
        return -3;

    if (CD_AutoEject(d->fd))
        return -1);

        /* Close the device if it needs to vanish. */
        if (intermittent_dev)
        gen_close(d);

        return 0;
    } /* gen_eject() */
Пример #6
0
void
_PyGen_Finalize(PyObject *self)
{
    PyGenObject *gen = (PyGenObject *)self;
    PyObject *res;
    PyObject *error_type, *error_value, *error_traceback;

    /* If `gen` is a coroutine, and if it was never awaited on,
       issue a RuntimeWarning. */
    if (gen->gi_code != NULL
            && ((PyCodeObject *)gen->gi_code)->co_flags & (CO_COROUTINE
                                                       | CO_ITERABLE_COROUTINE)
            && gen->gi_frame != NULL
            && gen->gi_frame->f_lasti == -1
            && !PyErr_Occurred()
            && PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
                                "coroutine '%.50S' was never awaited",
                                gen->gi_qualname))
        return;

    if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
        /* Generator isn't paused, so no need to close */
        return;

    /* Save the current exception, if any. */
    PyErr_Fetch(&error_type, &error_value, &error_traceback);

    res = gen_close(gen, NULL);

    if (res == NULL)
        PyErr_WriteUnraisable(self);
    else
        Py_DECREF(res);

    /* Restore the saved exception. */
    PyErr_Restore(error_type, error_value, error_traceback);
}
Пример #7
0
void
_PyGen_Finalize(PyObject *self)
{
    PyGenObject *gen = (PyGenObject *)self;
    PyObject *res;
    PyObject *error_type, *error_value, *error_traceback;

    if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
        /* Generator isn't paused, so no need to close */
        return;

    /* Save the current exception, if any. */
    PyErr_Fetch(&error_type, &error_value, &error_traceback);

    res = gen_close(gen, NULL);

    if (res == NULL)
        PyErr_WriteUnraisable(self);
    else
        Py_DECREF(res);

    /* Restore the saved exception. */
    PyErr_Restore(error_type, error_value, error_traceback);
}
Пример #8
0
static PyObject *
coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
{
    return gen_close((PyGenObject *)cw->cw_coroutine, args);
}
Пример #9
0
/*
 * Get the current status of the drive: the current play mode, the absolute
 * position from start of disc (in frames), and the current track and index
 * numbers if the CD is playing or paused.
 */
int
gen_get_drive_status( struct wm_drive *d, int oldmode,
                      int *mode, int *pos, int *track, int *index)
{
    struct CD_Status		sc;

    /* If we can't get status, the CD is ejected, so default to that. */
    *mode = WM_CDM_EJECTED;

    /* Is the device open? */
    if (d->fd < 0) {
        switch (d->proto.open(d)) {
        case -1:	/* error */
            return -1;

        case 1:		/* retry */
            return 0;
        }
    }

    /* Disc is ejected.  Close the device. */
    if (CD_GetStatus(d->fd, &sc)) {
        gen_close(d);
        return 0;
    }

    switch (sc.status) {
    case CDSTAT_PLAY:
        *mode = WM_CDM_PLAYING;
        break;

    case CDSTAT_PAUSE:
        if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
            *mode = WM_CDM_PAUSED;
        else
            *mode = WM_CDM_STOPPED;
        break;

    case CDSTAT_STOP:
        if (oldmode == WM_CDM_PLAYING) {
            *mode = WM_CDM_TRACK_DONE;	/* waiting for next track. */
            break;
        }
    /* fall through */

    default:
        *mode = WM_CDM_STOPPED;
        break;
    }

    switch(*mode) {
    case WM_CDM_PLAYING:
    case WM_CDM_PAUSED:
        *track = sc.tno;
        *index = sc.index;
        *pos = sc.baddr;
        break;
    }

    return 0;
} /* gen_get_drive_status() */
Пример #10
0
int repo_new(repo* rep, const char* newname) {
	int err = 0;
	// New branches track empty directories. The branch has no old roots.
	// It is staging and the current root being staged points to the only
	// object already staged: an empty directory.

	// Create a string buffer for making paths
	char* path = gen_malloc(MAX_PATH_LEN);
	if (!path) {
		err = -ENOMEM;
		goto exit;
	}
	gen_sprintf(path, "%s/br/%s", rep->repo, newname);

	// mkdir <rep->repo>/br/<newname>
	err = gen_mkdir(path, (mode_t)0700);
	if (err)
		goto exit;

	// mkdir <rep->repo>/br/<newname>/oldroots
	strcat(path, "/oldroots");
	err = gen_mkdir(path, (mode_t)0700);
	if (err)
		goto exit;

	// mkdir <rep->repo>/br/<newname>/stage
	gen_sprintf(path, "%s/br/%s/stage", rep->repo, newname);
	err = gen_mkdir(path, (mode_t)0700);
	if (err)
		goto exit;

	// mkdir <rep->repo>/br/<newname>/stage/objs
	strcat(path, "/objs");
	err = gen_mkdir(path, (mode_t)0700);
	if (err)
		goto exit;

	// mkdir <rep->repo>/br/<newname>/stage/objs/<nextid>
	uint64_t nextid = repo_newid(rep);
	gen_sprintf(path, "%s/br/%s/%lu", rep->repo, newname, nextid);
	err = gen_mkdir(path, (mode_t)0700);
	if (err)
		goto exit;

	// echo `repo_newid` > <rep->repo>/br/<newname>/stage/root
	gen_sprintf(path, "%s/br/%s/root", rep->repo, newname);
	int fd = gen_open(path, O_CREAT|O_RDWR, (mode_t)0700);
	if (fd < 0) {
		err = fd;
		goto exit;
	}
	gen_sprintf(path, "%lu\n", nextid);
	int nbytes;
	err = gen_write(fd, path, strlen(path), &nbytes);
	if (err)
		goto exit;
	gen_close(fd);
	sync();
exit:
	gen_free(path);
	return err;
}