mercurial/pure/osutil.py
author Cédric Krier <ced@b2ck.com>
Sat, 17 Jul 2021 12:47:07 +0200
changeset 47657 1bad89a67745
parent 46819 d4ba4d51f85f
child 48875 6000f5b25c9b
permissions -rw-r--r--
testing: do not stop waiting if timeout is 0 (issue6541) Differential Revision: https://phab.mercurial-scm.org/D11102
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8232
823f25b25dea pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7704
diff changeset
     1
# osutil.py - pure Python version of osutil.c
823f25b25dea pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7704
diff changeset
     2
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 46786
diff changeset
     3
#  Copyright 2009 Olivia Mackall <olivia@selenic.com> and others
8232
823f25b25dea pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7704
diff changeset
     4
#
823f25b25dea pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7704
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9031
diff changeset
     6
# GNU General Public License version 2 or any later version.
8232
823f25b25dea pure/osutil: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 7704
diff changeset
     7
42524
f3fa10a5877d py3: use integer division for the value passed to xrange
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 40940
diff changeset
     8
from __future__ import absolute_import, division
27338
810337ae1b76 osutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25645
diff changeset
     9
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    10
import ctypes
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    11
import ctypes.util
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    12
import os
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    13
import socket
10651
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    14
import stat as statmod
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    15
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    16
from ..pycompat import getattr
32367
a9c71d578a1c osutil: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents: 31644
diff changeset
    17
from .. import (
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
    18
    encoding,
30304
ba2c04059317 py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29821
diff changeset
    19
    pycompat,
ba2c04059317 py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29821
diff changeset
    20
)
ba2c04059317 py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29821
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
    22
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    23
def _mode_to_kind(mode):
10651
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    24
    if statmod.S_ISREG(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    25
        return statmod.S_IFREG
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    26
    if statmod.S_ISDIR(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    27
        return statmod.S_IFDIR
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    28
    if statmod.S_ISLNK(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    29
        return statmod.S_IFLNK
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    30
    if statmod.S_ISBLK(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    31
        return statmod.S_IFBLK
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    32
    if statmod.S_ISCHR(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    33
        return statmod.S_IFCHR
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    34
    if statmod.S_ISFIFO(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    35
        return statmod.S_IFIFO
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    36
    if statmod.S_ISSOCK(mode):
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    37
        return statmod.S_IFSOCK
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    38
    return mode
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    39
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
    40
32512
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
    41
def listdir(path, stat=False, skip=None):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45941
diff changeset
    42
    """listdir(path, stat=False) -> list_of_tuples
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    43
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    44
    Return a sorted list containing information about the entries
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    45
    in the directory.
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    46
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    47
    If stat is True, each element is a 3-tuple:
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    48
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    49
      (name, type, stat object)
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    50
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    51
    Otherwise, each element is a 2-tuple:
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    52
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    53
      (name, type)
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45941
diff changeset
    54
    """
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    55
    result = []
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    56
    prefix = path
30304
ba2c04059317 py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29821
diff changeset
    57
    if not prefix.endswith(pycompat.ossep):
ba2c04059317 py3: use pycompat.ossep at certain places
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29821
diff changeset
    58
        prefix += pycompat.ossep
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    59
    names = os.listdir(path)
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    60
    names.sort()
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    61
    for fn in names:
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    62
        st = os.lstat(prefix + fn)
10651
5f091fc1bab7 style: use consistent variable names (*mod) with imports which would shadow
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    63
        if fn == skip and statmod.S_ISDIR(st.st_mode):
7704
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    64
            return []
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    65
        if stat:
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    66
            result.append((fn, _mode_to_kind(st.st_mode), st))
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    67
        else:
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    68
            result.append((fn, _mode_to_kind(st.st_mode)))
30d1d313370b move mercurial.osutil to mercurial.pure.osutil
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    69
    return result
8421
b6d0fa8c7685 posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents: 8232
diff changeset
    70
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
    71
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 32512
diff changeset
    72
if not pycompat.iswindows:
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
    73
    posixfile = open
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    74
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    75
    _SCM_RIGHTS = 0x01
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    76
    _socklen_t = ctypes.c_uint
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    77
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
    if pycompat.sysplatform.startswith(b'linux'):
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    79
        # socket.h says "the type should be socklen_t but the definition of
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    80
        # the kernel is incompatible with this."
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    81
        _cmsg_len_t = ctypes.c_size_t
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    82
        _msg_controllen_t = ctypes.c_size_t
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    83
        _msg_iovlen_t = ctypes.c_size_t
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    84
    else:
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    85
        _cmsg_len_t = _socklen_t
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    86
        _msg_controllen_t = _socklen_t
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    87
        _msg_iovlen_t = ctypes.c_int
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    88
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    89
    class _iovec(ctypes.Structure):
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    90
        _fields_ = [
29698
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
    91
            (u'iov_base', ctypes.c_void_p),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
    92
            (u'iov_len', ctypes.c_size_t),
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    93
        ]
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    94
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    95
    class _msghdr(ctypes.Structure):
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
    96
        _fields_ = [
29698
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
    97
            (u'msg_name', ctypes.c_void_p),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
    98
            (u'msg_namelen', _socklen_t),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
    99
            (u'msg_iov', ctypes.POINTER(_iovec)),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   100
            (u'msg_iovlen', _msg_iovlen_t),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   101
            (u'msg_control', ctypes.c_void_p),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   102
            (u'msg_controllen', _msg_controllen_t),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   103
            (u'msg_flags', ctypes.c_int),
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   104
        ]
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   105
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   106
    class _cmsghdr(ctypes.Structure):
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   107
        _fields_ = [
29698
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   108
            (u'cmsg_len', _cmsg_len_t),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   109
            (u'cmsg_level', ctypes.c_int),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   110
            (u'cmsg_type', ctypes.c_int),
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   111
            (u'cmsg_data', ctypes.c_ubyte * 0),
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   112
        ]
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   113
29698
f15f31505f12 py3: use unicode literals in pure/osutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29600
diff changeset
   114
    _libc = ctypes.CDLL(ctypes.util.find_library(u'c'), use_errno=True)
27971
f7d0c28d34b3 osutil: do not abort loading pure module just because libc has no recvmsg()
Yuya Nishihara <yuya@tcha.org>
parents: 27704
diff changeset
   115
    _recvmsg = getattr(_libc, 'recvmsg', None)
f7d0c28d34b3 osutil: do not abort loading pure module just because libc has no recvmsg()
Yuya Nishihara <yuya@tcha.org>
parents: 27704
diff changeset
   116
    if _recvmsg:
f7d0c28d34b3 osutil: do not abort loading pure module just because libc has no recvmsg()
Yuya Nishihara <yuya@tcha.org>
parents: 27704
diff changeset
   117
        _recvmsg.restype = getattr(ctypes, 'c_ssize_t', ctypes.c_long)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   118
        _recvmsg.argtypes = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   119
            ctypes.c_int,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   120
            ctypes.POINTER(_msghdr),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   121
            ctypes.c_int,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   122
        )
27971
f7d0c28d34b3 osutil: do not abort loading pure module just because libc has no recvmsg()
Yuya Nishihara <yuya@tcha.org>
parents: 27704
diff changeset
   123
    else:
f7d0c28d34b3 osutil: do not abort loading pure module just because libc has no recvmsg()
Yuya Nishihara <yuya@tcha.org>
parents: 27704
diff changeset
   124
        # recvmsg isn't always provided by libc; such systems are unsupported
f7d0c28d34b3 osutil: do not abort loading pure module just because libc has no recvmsg()
Yuya Nishihara <yuya@tcha.org>
parents: 27704
diff changeset
   125
        def _recvmsg(sockfd, msg, flags):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
            raise NotImplementedError(b'unsupported platform')
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   127
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   128
    def _CMSG_FIRSTHDR(msgh):
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   129
        if msgh.msg_controllen < ctypes.sizeof(_cmsghdr):
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   130
            return
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   131
        cmsgptr = ctypes.cast(msgh.msg_control, ctypes.POINTER(_cmsghdr))
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   132
        return cmsgptr.contents
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   133
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   134
    # The pure version is less portable than the native version because the
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   135
    # handling of socket ancillary data heavily depends on C preprocessor.
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   136
    # Also, some length fields are wrongly typed in Linux kernel.
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   137
    def recvfds(sockfd):
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   138
        """receive list of file descriptors via socket"""
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   139
        dummy = (ctypes.c_ubyte * 1)()
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   140
        iov = _iovec(ctypes.cast(dummy, ctypes.c_void_p), ctypes.sizeof(dummy))
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   141
        cbuf = ctypes.create_string_buffer(256)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   142
        msgh = _msghdr(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   143
            None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   144
            0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   145
            ctypes.pointer(iov),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   146
            1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   147
            ctypes.cast(cbuf, ctypes.c_void_p),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   148
            ctypes.sizeof(cbuf),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   149
            0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   150
        )
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   151
        r = _recvmsg(sockfd, ctypes.byref(msgh), 0)
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   152
        if r < 0:
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   153
            e = ctypes.get_errno()
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   154
            raise OSError(e, os.strerror(e))
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   155
        # assumes that the first cmsg has fds because it isn't easy to write
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   156
        # portable CMSG_NXTHDR() with ctypes.
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   157
        cmsg = _CMSG_FIRSTHDR(msgh)
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   158
        if not cmsg:
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   159
            return []
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   160
        if (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   161
            cmsg.cmsg_level != socket.SOL_SOCKET
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   162
            or cmsg.cmsg_type != _SCM_RIGHTS
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   163
        ):
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   164
            return []
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   165
        rfds = ctypes.cast(cmsg.cmsg_data, ctypes.POINTER(ctypes.c_int))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   166
        rfdscount = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   167
            cmsg.cmsg_len - _cmsghdr.cmsg_data.offset
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   168
        ) // ctypes.sizeof(ctypes.c_int)
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34645
diff changeset
   169
        return [rfds[i] for i in pycompat.xrange(rfdscount)]
27474
e517a89c24e1 osutil: implement pure version of recvfds() for PyPy
Yuya Nishihara <yuya@tcha.org>
parents: 27338
diff changeset
   170
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   171
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   172
else:
27338
810337ae1b76 osutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25645
diff changeset
   173
    import msvcrt
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   174
46786
52528570312e typing: disable module attribute warnings for properly conditionalized code
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   175
    _kernel32 = ctypes.windll.kernel32  # pytype: disable=module-attr
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   176
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   177
    _DWORD = ctypes.c_ulong
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   178
    _LPCSTR = _LPSTR = ctypes.c_char_p
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   179
    _HANDLE = ctypes.c_void_p
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   180
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   181
    _INVALID_HANDLE_VALUE = _HANDLE(-1).value
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   182
18959
2f6418d8a4c9 check-code: catch trailing space in comments
Mads Kiilerich <madski@unity3d.com>
parents: 17429
diff changeset
   183
    # CreateFile
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   184
    _FILE_SHARE_READ = 0x00000001
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   185
    _FILE_SHARE_WRITE = 0x00000002
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   186
    _FILE_SHARE_DELETE = 0x00000004
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   187
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   188
    _CREATE_ALWAYS = 2
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   189
    _OPEN_EXISTING = 3
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   190
    _OPEN_ALWAYS = 4
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   191
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   192
    _GENERIC_READ = 0x80000000
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   193
    _GENERIC_WRITE = 0x40000000
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   194
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   195
    _FILE_ATTRIBUTE_NORMAL = 0x80
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   196
17429
72fa4ef2245f declare local constants instead of using magic values and comments
Mads Kiilerich <mads@kiilerich.com>
parents: 16686
diff changeset
   197
    # open_osfhandle flags
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   198
    _O_RDONLY = 0x0000
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   199
    _O_RDWR = 0x0002
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   200
    _O_APPEND = 0x0008
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   201
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   202
    _O_TEXT = 0x4000
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   203
    _O_BINARY = 0x8000
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   204
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   205
    # types of parameters of C functions used (required by pypy)
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   206
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   207
    _kernel32.CreateFileA.argtypes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   208
        _LPCSTR,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   209
        _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   210
        _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   211
        ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   212
        _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   213
        _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   214
        _HANDLE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   215
    ]
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   216
    _kernel32.CreateFileA.restype = _HANDLE
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   217
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   218
    def _raiseioerror(name):
46786
52528570312e typing: disable module attribute warnings for properly conditionalized code
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   219
        err = ctypes.WinError()  # pytype: disable=module-attr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   220
        raise IOError(
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   221
            err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   222
        )
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   223
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   224
    class posixfile(object):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45941
diff changeset
   225
        """a file object aiming for POSIX-like semantics
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   226
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   227
        CPython's open() returns a file that was opened *without* setting the
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   228
        _FILE_SHARE_DELETE flag, which causes rename and unlink to abort.
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   229
        This even happens if any hardlinked copy of the file is in open state.
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   230
        We set _FILE_SHARE_DELETE here, so files opened with posixfile can be
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   231
        renamed and deleted while they are held open.
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   232
        Note that if a file opened with posixfile is unlinked, the file
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   233
        remains but cannot be opened again or be recreated under the same name,
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45941
diff changeset
   234
        until all reading processes have closed the file."""
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   235
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   236
        def __init__(self, name, mode=b'r', bufsize=-1):
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   237
            if b'b' in mode:
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   238
                flags = _O_BINARY
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   239
            else:
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   240
                flags = _O_TEXT
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   241
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   242
            m0 = mode[0:1]
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   243
            if m0 == b'r' and b'+' not in mode:
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   244
                flags |= _O_RDONLY
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   245
                access = _GENERIC_READ
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   246
            else:
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   247
                # work around http://support.microsoft.com/kb/899149 and
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   248
                # set _O_RDWR for 'w' and 'a', even if mode has no '+'
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   249
                flags |= _O_RDWR
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   250
                access = _GENERIC_READ | _GENERIC_WRITE
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   251
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   252
            if m0 == b'r':
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   253
                creation = _OPEN_EXISTING
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   254
            elif m0 == b'w':
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   255
                creation = _CREATE_ALWAYS
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38783
diff changeset
   256
            elif m0 == b'a':
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   257
                creation = _OPEN_ALWAYS
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   258
                flags |= _O_APPEND
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   259
            else:
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43089
diff changeset
   260
                raise ValueError("invalid mode: %s" % pycompat.sysstr(mode))
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   261
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   262
            fh = _kernel32.CreateFileA(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   263
                name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   264
                access,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   265
                _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   266
                None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   267
                creation,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   268
                _FILE_ATTRIBUTE_NORMAL,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   269
                None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42524
diff changeset
   270
            )
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   271
            if fh == _INVALID_HANDLE_VALUE:
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   272
                _raiseioerror(name)
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   273
46786
52528570312e typing: disable module attribute warnings for properly conditionalized code
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   274
            fd = msvcrt.open_osfhandle(fh, flags)  # pytype: disable=module-attr
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   275
            if fd == -1:
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   276
                _kernel32.CloseHandle(fh)
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   277
                _raiseioerror(name)
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   278
30925
82f1ef8b4477 py3: convert the mode argument of os.fdopen to unicodes (2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30642
diff changeset
   279
            f = os.fdopen(fd, pycompat.sysstr(mode), bufsize)
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   280
            # unfortunately, f.name is '<fdopen>' at this point -- so we store
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   281
            # the name on this wrapper. We cannot just assign to f.name,
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   282
            # because that attribute is read-only.
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   283
            object.__setattr__(self, 'name', name)
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   284
            object.__setattr__(self, '_file', f)
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   285
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   286
        def __iter__(self):
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   287
            return self._file
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   288
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   289
        def __getattr__(self, name):
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   290
            return getattr(self._file, name)
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   291
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   292
        def __setattr__(self, name, value):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45941
diff changeset
   293
            """mimics the read-only attributes of Python file objects
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   294
            by raising 'TypeError: readonly attribute' if someone tries:
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   295
              f = posixfile('foo.txt')
45941
346af7687c6f osutil: reformat triple-quoted string so black doesn't confuse itself
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   296
              f.name = 'bla'
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45941
diff changeset
   297
            """
14413
5ef18e28df19 pure: provide more correct implementation of posixfile for Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 10651
diff changeset
   298
            return self._file.__setattr__(name, value)
27704
051b0dcec98b osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27512
diff changeset
   299
051b0dcec98b osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27512
diff changeset
   300
        def __enter__(self):
40940
120ecb17242b windows: ensure pure posixfile fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents: 39644
diff changeset
   301
            self._file.__enter__()
120ecb17242b windows: ensure pure posixfile fd doesn't escape by entering context manager
Matt Harbison <matt_harbison@yahoo.com>
parents: 39644
diff changeset
   302
            return self
27704
051b0dcec98b osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27512
diff changeset
   303
051b0dcec98b osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27512
diff changeset
   304
        def __exit__(self, exc_type, exc_value, exc_tb):
051b0dcec98b osutil: implement __enter__ and __exit__ on posixfile
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27512
diff changeset
   305
            return self._file.__exit__(exc_type, exc_value, exc_tb)