comparison mercurial/pycompat.py @ 30302:3874ddba1ab4

py3: add a bytes version of os.name os.name returns unicodes on py3. Most of our checks are like os.name == 'nt' Because of the transformer, on the right hand side we have b'nt'. The condition will never satisfy even if os.name returns 'nt' as that will be an unicode. We either need to encode every occurence of os.name or have a new variable which is much cleaner. Now we have pycompat.osname. There are around 53 occurences of os.name in the codebase which needs to be replaced by pycompat.osname to support Python 3.
author Pulkit Goyal <7895pulkit@gmail.com>
date Sun, 06 Nov 2016 03:33:22 +0530
parents 42af0590f4b9
children ad40d307a9f0
comparison
equal deleted inserted replaced
30301:8321b083a83d 30302:3874ddba1ab4
8 This contains aliases to hide python version-specific details from the core. 8 This contains aliases to hide python version-specific details from the core.
9 """ 9 """
10 10
11 from __future__ import absolute_import 11 from __future__ import absolute_import
12 12
13 import os
13 import sys 14 import sys
14 15
15 ispy3 = (sys.version_info[0] >= 3) 16 ispy3 = (sys.version_info[0] >= 3)
16 17
17 if not ispy3: 18 if not ispy3:
32 import xmlrpc.client as xmlrpclib 33 import xmlrpc.client as xmlrpclib
33 34
34 if ispy3: 35 if ispy3:
35 import builtins 36 import builtins
36 import functools 37 import functools
37 import os
38 fsencode = os.fsencode 38 fsencode = os.fsencode
39 fsdecode = os.fsdecode 39 fsdecode = os.fsdecode
40 # A bytes version of os.name.
41 osname = os.name.encode('ascii')
40 42
41 def sysstr(s): 43 def sysstr(s):
42 """Return a keyword str to be passed to Python functions such as 44 """Return a keyword str to be passed to Python functions such as
43 getattr() and str.encode() 45 getattr() and str.encode()
44 46
79 81
80 # In Python 2, fsdecode() has a very chance to receive bytes. So it's 82 # In Python 2, fsdecode() has a very chance to receive bytes. So it's
81 # better not to touch Python 2 part as it's already working fine. 83 # better not to touch Python 2 part as it's already working fine.
82 def fsdecode(filename): 84 def fsdecode(filename):
83 return filename 85 return filename
86
87 osname = os.name
84 88
85 stringio = io.StringIO 89 stringio = io.StringIO
86 empty = _queue.Empty 90 empty = _queue.Empty
87 queue = _queue.Queue 91 queue = _queue.Queue
88 92