shared file access in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lev Elblert

    shared file access in python

    Does Python allow to open files in shared mode like fsopen or _open
    do? If not how to make it happen?
  • Peter Hansen

    #2
    Re: shared file access in python

    Lev Elblert wrote:
    [color=blue]
    > Does Python allow to open files in shared mode like fsopen or _open
    > do? If not how to make it happen?[/color]

    Probably the "os" module's version of open() will do what you
    need: http://docs.python.org/lib/os-fd-ops.html, assuming the
    standard builtin open() is not doing what you need.

    -Peter

    Comment

    • Jeff Epler

      #3
      Re: shared file access in python

      The file() constructor probably takes any second argument that your OS's
      fopen() does, though this may have changed with the creation of
      universal newlines.

      If there's something that os.open() can do that file() can't, you
      should be able to achieve it by using os.fdopen(): (untested)
      def magic_file(file name, flag=os.O_RDONL Y, mode='r', bufsize=None):
      fd = os.open(filenam e, flag)
      return os.fdopen(fd, mode, bufsize)

      I don't know what _open or fsopen are.

      Jeff

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.2.4 (GNU/Linux)

      iD8DBQFA02HkJd0 1MZaTXX0RAuEjAJ 9Sb67Ll/Jbwel3Uh0lnA2NO zUf3wCZASWv
      HwxkT3vLaZ1b+8Q mcKp+mKg=
      =aBvF
      -----END PGP SIGNATURE-----

      Comment

      • Andrew MacIntyre

        #4
        Re: shared file access in python

        On Sat, 18 Jun 2004, Lev Elblert wrote:
        [color=blue]
        > Does Python allow to open files in shared mode like fsopen or _open
        > do? If not how to make it happen?[/color]

        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.

        --
        Andrew I MacIntyre "These thoughts are mine alone..."
        E-mail: andymac@bullsey e.apana.org.au (pref) | Snail: PO Box 370
        andymac@pcug.or g.au (alt) | Belconnen ACT 2616
        Web: http://www.andymac.org/ | Australia

        Comment

        • Lev Elblert

          #5
          Re: shared file access in python

          Andrew MacIntyre <andymac@bullse ye.apana.org.au > wrote in message news:<mailman.6 8.1087793423.45 4.python-list@python.org >...[color=blue]
          > On Sat, 18 Jun 2004, Lev Elblert wrote:
          >[color=green]
          > > Does Python allow to open files in shared mode like fsopen or _open
          > > do? If not how to make it happen?[/color]
          >
          > 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.[/color]

          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(PyObje ct *self, PyObject *args)
          {
          char *FileName = NULL;
          char *Mode = NULL;
          char * Share = NULL;

          int ShareFlag = _SH_DENYNO;
          FILE *f;
          PyObject *FileObject;
          if (!PyArg_ParseTu ple(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(FileNam e, Mode, ShareFlag);
          if (!f)
          {
          PyErr_SetFromEr rno(PyExc_Excep tion);
          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.

          Comment

          • Peter Hansen

            #6
            Re: shared file access in python

            Lev Elblert wrote:
            [color=blue]
            > 2. msvcrt.lib does have a lot of functions, but not msvcrt module in
            > Python. (correct me if I'm wrong)[/color]

            http://docs.python.org/lib/module-msvcrt.html shows there are a variety
            of functions there (try typing "import msvcrt" as well), but I can't
            see that they have what you need.

            -Peter

            Comment

            • Dennis Lee Bieber

              #7
              Re: shared file access in python

              On Mon, 21 Jun 2004 10:28:24 -0400, Peter Hansen <peter@engcorp. com>
              declaimed the following in comp.lang.pytho n:
              [color=blue]
              > Lev Elblert wrote:
              >[color=green]
              > > 2. msvcrt.lib does have a lot of functions, but not msvcrt module in
              > > Python. (correct me if I'm wrong)[/color]
              >
              > http://docs.python.org/lib/module-msvcrt.html shows there are a variety
              > of functions there (try typing "import msvcrt" as well), but I can't
              > see that they have what you need.
              >[/color]

              Somehow, I suspect one would have to using the win32 modules for
              the desired functionality:

              From the ActivePython win32 documentation:
              """
              win32file.Creat eFile
              PyHANDLE = CreateFile(file Name, desiredAccess , shareMode , attributes ,
              creationDisposi tion , flagsAndAttribu tes , hTemplateFile )

              Creates or opens the a file or other object and returns a handle that
              can be used to access the object.
              """

              Of course, this then means one also has to use the win32file.*
              routines for all the I/O. Though maybe the msvcrt module
              open_osfhandle( ) can then be used -- pass it the handle from
              win32file.Creat eFile(), pass that result to os.fdopen(), etc... and use
              the rest of the msvcrt routines.

              Doesn't common Linux practice rely upon creating a lock-file
              first, to prevent shared access to a file? That would Linux/UNIX doesn't
              really have "exclusive access" control on files. So I wouldn't expect to
              find any support for such in anything using a Posix compatible I/O
              system.

              --[color=blue]
              > =============== =============== =============== =============== == <
              > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
              > wulfraed@dm.net | Bestiaria Support Staff <
              > =============== =============== =============== =============== == <
              > Home Page: <http://www.dm.net/~wulfraed/> <
              > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

              Comment

              • Lev Elblert

                #8
                Re: shared file access in python

                Peter!

                I do not know what kind of msvcrt module you have, but here is the
                output of relavant commands on my machine:
                [color=blue][color=green][color=darkred]
                >>> import msvcrt
                >>> str(msvcrt)[/color][/color][/color]
                "<module 'msvcrt' (built-in)>"[color=blue][color=green][color=darkred]
                >>> print msvcrt.__doc__[/color][/color][/color]
                None[color=blue][color=green][color=darkred]
                >>> dir (msvcrt)[/color][/color][/color]
                ['LK_LOCK', 'LK_NBLCK', 'LK_NBRLCK', 'LK_RLCK', 'LK_UNLCK', '__doc__',
                '__name__', 'get_osfhandle' , 'getch', 'getche', 'heapmin', 'kbhit',
                'locking', 'open_osfhandle ', 'putch', 'setmode', 'ungetch'][color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]


                out of all functions, probably kbhit and getch are used, to control
                multythereaded applications from console.



                Peter Hansen <peter@engcorp. com> wrote in message news:<ZcOdnWZyx boXbUvdRVn-jw@powergate.ca >...[color=blue]
                > Lev Elblert wrote:
                >[color=green]
                > > 2. msvcrt.lib does have a lot of functions, but not msvcrt module in
                > > Python. (correct me if I'm wrong)[/color]
                >
                > http://docs.python.org/lib/module-msvcrt.html shows there are a variety
                > of functions there (try typing "import msvcrt" as well), but I can't
                > see that they have what you need.
                >
                > -Peter[/color]

                Comment

                • Peter Hansen

                  #9
                  Re: shared file access in python

                  Lev Elblert wrote:
                  [color=blue]
                  > Peter!
                  >
                  > I do not know what kind of msvcrt module you have, but here is the
                  > output of relavant commands on my machine:[/color]
                  [snip]

                  You misread the message below, I believe. You seem to think I was
                  the one saying that the msvcrt module did not have lots of functions...

                  Also, please don't top-post. Thank you.

                  -Peter
                  [color=blue]
                  > Peter Hansen <peter@engcorp. com> wrote in message news:<ZcOdnWZyx boXbUvdRVn-jw@powergate.ca >...[color=green]
                  >>Lev Elblert wrote:[color=darkred]
                  >>>2. msvcrt.lib does have a lot of functions, but not msvcrt module in
                  >>>Python. (correct me if I'm wrong)[/color]
                  >>
                  >>http://docs.python.org/lib/module-msvcrt.html shows there are a variety
                  >>of functions there (try typing "import msvcrt" as well), but I can't
                  >>see that they have what you need.[/color][/color]

                  Comment

                  Working...