comparison mercurial/pure/osutil.py @ 39644:3b421154d2ca

py3: fix str vs bytes in enough places to run `hg version` on Windows I don't have Visual Studio 2015 at home, but this now works with a handful of extensions (blackbox, extdiff, patchbomb, phabricator and rebase, but not evolve): $ HGMODULEPOLICY=py py -3 ../hg version Enabling the evolve extension causes the usual "failed to import ..." line, but then print this before the usual version output: ('commit', '[b'debugancestor', b'debugapplystreamclonebundle', ..., b'verify', b'version']') ... where the elided part seems to be every command and alias known.
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 13 Sep 2018 22:07:00 -0400
parents e7aa113b14f7
children 120ecb17242b
comparison
equal deleted inserted replaced
39643:47ac5d93d708 39644:3b421154d2ca
12 import os 12 import os
13 import socket 13 import socket
14 import stat as statmod 14 import stat as statmod
15 15
16 from .. import ( 16 from .. import (
17 encoding,
17 pycompat, 18 pycompat,
18 ) 19 )
19 20
20 def _mode_to_kind(mode): 21 def _mode_to_kind(mode):
21 if statmod.S_ISREG(mode): 22 if statmod.S_ISREG(mode):
191 _DWORD, _DWORD, _HANDLE] 192 _DWORD, _DWORD, _HANDLE]
192 _kernel32.CreateFileA.restype = _HANDLE 193 _kernel32.CreateFileA.restype = _HANDLE
193 194
194 def _raiseioerror(name): 195 def _raiseioerror(name):
195 err = ctypes.WinError() 196 err = ctypes.WinError()
196 raise IOError(err.errno, '%s: %s' % (name, err.strerror)) 197 raise IOError(err.errno, r'%s: %s' % (encoding.strfromlocal(name),
198 err.strerror))
197 199
198 class posixfile(object): 200 class posixfile(object):
199 '''a file object aiming for POSIX-like semantics 201 '''a file object aiming for POSIX-like semantics
200 202
201 CPython's open() returns a file that was opened *without* setting the 203 CPython's open() returns a file that was opened *without* setting the
205 renamed and deleted while they are held open. 207 renamed and deleted while they are held open.
206 Note that if a file opened with posixfile is unlinked, the file 208 Note that if a file opened with posixfile is unlinked, the file
207 remains but cannot be opened again or be recreated under the same name, 209 remains but cannot be opened again or be recreated under the same name,
208 until all reading processes have closed the file.''' 210 until all reading processes have closed the file.'''
209 211
210 def __init__(self, name, mode='r', bufsize=-1): 212 def __init__(self, name, mode=b'r', bufsize=-1):
211 if 'b' in mode: 213 if b'b' in mode:
212 flags = _O_BINARY 214 flags = _O_BINARY
213 else: 215 else:
214 flags = _O_TEXT 216 flags = _O_TEXT
215 217
216 m0 = mode[0] 218 m0 = mode[0:1]
217 if m0 == 'r' and '+' not in mode: 219 if m0 == b'r' and b'+' not in mode:
218 flags |= _O_RDONLY 220 flags |= _O_RDONLY
219 access = _GENERIC_READ 221 access = _GENERIC_READ
220 else: 222 else:
221 # work around http://support.microsoft.com/kb/899149 and 223 # work around http://support.microsoft.com/kb/899149 and
222 # set _O_RDWR for 'w' and 'a', even if mode has no '+' 224 # set _O_RDWR for 'w' and 'a', even if mode has no '+'
223 flags |= _O_RDWR 225 flags |= _O_RDWR
224 access = _GENERIC_READ | _GENERIC_WRITE 226 access = _GENERIC_READ | _GENERIC_WRITE
225 227
226 if m0 == 'r': 228 if m0 == b'r':
227 creation = _OPEN_EXISTING 229 creation = _OPEN_EXISTING
228 elif m0 == 'w': 230 elif m0 == b'w':
229 creation = _CREATE_ALWAYS 231 creation = _CREATE_ALWAYS
230 elif m0 == 'a': 232 elif m0 == b'a':
231 creation = _OPEN_ALWAYS 233 creation = _OPEN_ALWAYS
232 flags |= _O_APPEND 234 flags |= _O_APPEND
233 else: 235 else:
234 raise ValueError("invalid mode: %s" % mode) 236 raise ValueError(r"invalid mode: %s" % pycompat.sysstr(mode))
235 237
236 fh = _kernel32.CreateFileA(name, access, 238 fh = _kernel32.CreateFileA(name, access,
237 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE, 239 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
238 None, creation, _FILE_ATTRIBUTE_NORMAL, None) 240 None, creation, _FILE_ATTRIBUTE_NORMAL, None)
239 if fh == _INVALID_HANDLE_VALUE: 241 if fh == _INVALID_HANDLE_VALUE: