shared file access in python

Lev Elblert elbertlev at hotmail.com
Mon Jun 21 10:18:24 EDT 2004


Andrew MacIntyre <andymac at bullseye.apana.org.au> wrote in message news:<mailman.68.1087793423.454.python-list at python.org>...
> On Sat, 18 Jun 2004, Lev Elblert wrote:
> 
> > Does Python allow to open files in shared mode like fsopen or _open
> > do? If not how to make it happen?
> 
> Assuming you're on a Win32 platform, I think the msvcrt module has what
> you're looking for.  The standard file object supports only the standard C
> library file stream API.

Thanks all!

1. os.open flags parameter tells creation disposition (read-only,
executable(unix)) etc, not shared.

2. msvcrt.lib does have a lot of functions, but not msvcrt module in
Python. (correct me if I'm wrong)

3. I created and extension:


/*=====================*/

#include "Python.h"
#include <share.h>

static PyObject *
Dofsopen(PyObject *self, PyObject *args)
{
char *FileName = NULL;
char *Mode = NULL;
char * Share = NULL;

int ShareFlag = _SH_DENYNO;
FILE *f;
PyObject *FileObject;
	if (!PyArg_ParseTuple(args, "sss", &FileName, &Mode, &Share))
        return NULL;
	if (strcmp(Share, "") == 0) // allow all 
		ShareFlag = _SH_DENYNO;
	else if (strcmp(Share, "r") == 0) // deny read
		ShareFlag = _SH_DENYRD;
	else if (strcmp(Share, "w") == 0) // deny write
		ShareFlag = _SH_DENYWR;
	else if (strcmp(Share, "rw") == 0) // deny read/write
		ShareFlag = _SH_DENYRW;
	f = _fsopen(FileName, Mode, ShareFlag);
	if (!f)
	{
		PyErr_SetFromErrno(PyExc_Exception);
		return NULL;
	}
	FileObject = PyFile_FromFile(f, FileName, Mode, fclose);
	return FileObject;
}

static PyMethodDef fsopen_methods[] = {
	{"fsopen", Dofsopen, METH_VARARGS, "fsopen() doc string"},
	{NULL, NULL}
};

void
initfsopen(void)
{
	Py_InitModule("fsopen", fsopen_methods);
}

/*=====================*/

This extension returnns a Python file object or rases an exception.
Also MS docs say, that fsopen is ANSI C function. I do not have access
to unix machine, so can't check it.

Thanks again.



More information about the Python-list mailing list