VirtualBox

Changeset 70518 in vbox


Ignore:
Timestamp:
Jan 10, 2018 2:14:56 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
120146
Message:

common/utils.py: Python 3 adjustments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/common/utils.py

    r69578 r70518  
    283283    """
    284284
    285     try:
    286         from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=F0401
    287     except:
    288         # On windows, use the 'N' flag introduces in Visual C++ 7.0 or 7.1.
    289         if getHostOs() == 'win':
    290             offComma = sMode.find(',');
    291             if offComma < 0:
    292                 return open(sFile, sMode + 'N');
    293             return open(sFile, sMode[:offComma] + 'N' + sMode[offComma:]);
    294 
    295         # Just in case.
    296         return open(sFile, sMode);
    297 
    298     oFile = open(sFile, sMode)
    299     #try:
    300     fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC);
    301     #except:
    302     #    pass;
     285    # Python 3.4 and later automatically creates non-inherit handles. See PEP-0446.
     286    uPythonVer = (sys.version_info[0] << 16) | (sys.version_info[1] & 0xffff);
     287    if uPythonVer >= ((3 << 16) | 4):
     288        oFile = open(sFile, sMode);
     289    else:
     290        try:
     291            from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=F0401
     292        except:
     293            # On windows, we can use the 'N' flag introduced in Visual C++ 7.0 or 7.1 with python 2.x.
     294            if getHostOs() == 'win':
     295                if uPythonVer < (3 << 16):
     296                    offComma = sMode.find(',');
     297                    if offComma < 0:
     298                        return open(sFile, sMode + 'N');
     299                    return open(sFile, sMode[:offComma] + 'N' + sMode[offComma:]);
     300
     301            # Just in case.
     302            return open(sFile, sMode);
     303
     304        oFile = open(sFile, sMode);
     305        #try:
     306        fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC);
     307        #except:
     308        #    pass;
    303309    return oFile;
    304310
     
    313319    """
    314320
    315     try:
    316         from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=F0401
    317     except:
    318         if getHostOs() == 'win':
    319             # Need to use CreateFile directly to open the file so we can feed it FILE_SHARE_DELETE.
    320             fAccess = 0;
    321             fDisposition = win32file.OPEN_EXISTING;                                                 # pylint: disable=no-member
    322             if 'r' in sMode or '+' in sMode:
    323                 fAccess |= win32file.GENERIC_READ;                                                  # pylint: disable=no-member
    324             if 'a' in sMode:
    325                 fAccess |= win32file.GENERIC_WRITE;                                                 # pylint: disable=no-member
    326                 fDisposition = win32file.OPEN_ALWAYS;                                               # pylint: disable=no-member
    327             elif 'w' in sMode:
    328                 fAccess = win32file.GENERIC_WRITE;                                                  # pylint: disable=no-member
    329                 if '+' in sMode:
    330                     fDisposition = win32file.OPEN_ALWAYS;                                           # pylint: disable=no-member
    331                     fAccess |= win32file.GENERIC_READ;                                              # pylint: disable=no-member
    332                 else:
    333                     fDisposition = win32file.CREATE_ALWAYS;                                         # pylint: disable=no-member
    334             if not fAccess:
    335                 fAccess |= win32file.GENERIC_READ;                                                  # pylint: disable=no-member
    336             fSharing = (win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE                      # pylint: disable=no-member
    337                         | win32file.FILE_SHARE_DELETE);                                             # pylint: disable=no-member
    338             hFile = win32file.CreateFile(sFile, fAccess, fSharing, None, fDisposition, 0, None);    # pylint: disable=no-member
    339             if 'a' in sMode:
    340                 win32file.SetFilePointer(hFile, 0, win32file.FILE_END);                             # pylint: disable=no-member
    341 
    342             # Turn the NT handle into a CRT file descriptor.
    343             hDetachedFile = hFile.Detach();
    344             if fAccess == win32file.GENERIC_READ:                                                   # pylint: disable=no-member
    345                 fOpen = os.O_RDONLY;
    346             elif fAccess == win32file.GENERIC_WRITE:                                                # pylint: disable=no-member
    347                 fOpen = os.O_WRONLY;
     321    if getHostOs() == 'win':
     322        # Need to use CreateFile directly to open the file so we can feed it FILE_SHARE_DELETE.
     323        fAccess = 0;
     324        fDisposition = win32file.OPEN_EXISTING;                                                 # pylint: disable=no-member
     325        if 'r' in sMode or '+' in sMode:
     326            fAccess |= win32file.GENERIC_READ;                                                  # pylint: disable=no-member
     327        if 'a' in sMode:
     328            fAccess |= win32file.GENERIC_WRITE;                                                 # pylint: disable=no-member
     329            fDisposition = win32file.OPEN_ALWAYS;                                               # pylint: disable=no-member
     330        elif 'w' in sMode:
     331            fAccess = win32file.GENERIC_WRITE;                                                  # pylint: disable=no-member
     332            if '+' in sMode:
     333                fDisposition = win32file.OPEN_ALWAYS;                                           # pylint: disable=no-member
     334                fAccess |= win32file.GENERIC_READ;                                              # pylint: disable=no-member
    348335            else:
    349                 fOpen = os.O_RDWR;
    350             if 'a' in sMode:
    351                 fOpen |= os.O_APPEND;
    352             if 'b' in sMode or 't' in sMode:
    353                 fOpen |= os.O_TEXT;                                                                 # pylint: disable=no-member
    354             fdFile = msvcrt.open_osfhandle(hDetachedFile, fOpen);
    355 
    356             # Tell python to use this handle.
    357             return os.fdopen(fdFile, sMode);
    358 
    359         # Just in case.
    360         return open(sFile, sMode);
    361 
    362     oFile = open(sFile, sMode)
    363     #try:
    364     fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC);
    365     #except:
    366     #    pass;
     336                fDisposition = win32file.CREATE_ALWAYS;                                         # pylint: disable=no-member
     337        if not fAccess:
     338            fAccess |= win32file.GENERIC_READ;                                                  # pylint: disable=no-member
     339        fSharing = (win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE                      # pylint: disable=no-member
     340                    | win32file.FILE_SHARE_DELETE);                                             # pylint: disable=no-member
     341        hFile = win32file.CreateFile(sFile, fAccess, fSharing, None, fDisposition, 0, None);    # pylint: disable=no-member
     342        if 'a' in sMode:
     343            win32file.SetFilePointer(hFile, 0, win32file.FILE_END);                             # pylint: disable=no-member
     344
     345        # Turn the NT handle into a CRT file descriptor.
     346        hDetachedFile = hFile.Detach();
     347        if fAccess == win32file.GENERIC_READ:                                                   # pylint: disable=no-member
     348            fOpen = os.O_RDONLY;
     349        elif fAccess == win32file.GENERIC_WRITE:                                                # pylint: disable=no-member
     350            fOpen = os.O_WRONLY;
     351        else:
     352            fOpen = os.O_RDWR;
     353        if 'a' in sMode:
     354            fOpen |= os.O_APPEND;
     355        if 'b' in sMode or 't' in sMode:
     356            fOpen |= os.O_TEXT;                                                                 # pylint: disable=no-member
     357        fdFile = msvcrt.open_osfhandle(hDetachedFile, fOpen);
     358
     359        # Tell python to use this handle.
     360        oFile = os.fdopen(fdFile, sMode);
     361    else:
     362        oFile = open(sFile, sMode);
     363
     364        # Python 3.4 and later automatically creates non-inherit handles. See PEP-0446.
     365        uPythonVer = (sys.version_info[0] << 16) | (sys.version_info[1] & 0xffff);
     366        if uPythonVer < ((3 << 16) | 4):
     367            try:
     368                from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=F0401
     369            except:
     370                pass;
     371            else:
     372                fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC);
    367373    return oFile;
    368374
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette