Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork966
Expand file tree
/
Copy pathcompat.py
More file actions
115 lines (85 loc) · 2.85 KB
/
compat.py
File metadata and controls
115 lines (85 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
# config.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
"""utilities to help provide compatibility with python 3"""
# flake8: noqa
importlocale
importos
importsys
fromgitdb.utils.encodingimport (
force_bytes,# @UnusedImport
force_text# @UnusedImport
)
# typing --------------------------------------------------------------------
fromtypingimport (
Any,
AnyStr,
Dict,
IO,
Optional,
Tuple,
Type,
Union,
overload,
)
fromgit.typesimportTBD
# ---------------------------------------------------------------------------
is_win:bool= (os.name=='nt')
is_posix= (os.name=='posix')
is_darwin= (os.name=='darwin')
defenc=sys.getfilesystemencoding()
@overload
defsafe_decode(s:None)->None: ...
@overload
defsafe_decode(s:AnyStr)->str: ...
defsafe_decode(s:Union[AnyStr,None])->Optional[str]:
"""Safely decodes a binary string to unicode"""
ifisinstance(s,str):
returns
elifisinstance(s,bytes):
returns.decode(defenc,'surrogateescape')
elifsisNone:
returnNone
else:
raiseTypeError('Expected bytes or text, but got %r'% (s,))
@overload
defsafe_encode(s:None)->None: ...
@overload
defsafe_encode(s:AnyStr)->bytes: ...
defsafe_encode(s:Optional[AnyStr])->Optional[bytes]:
"""Safely encodes a binary string to unicode"""
ifisinstance(s,str):
returns.encode(defenc)
elifisinstance(s,bytes):
returns
elifsisNone:
returnNone
else:
raiseTypeError('Expected bytes or text, but got %r'% (s,))
@overload
defwin_encode(s:None)->None: ...
@overload
defwin_encode(s:AnyStr)->bytes: ...
defwin_encode(s:Optional[AnyStr])->Optional[bytes]:
"""Encode unicodes for process arguments on Windows."""
ifisinstance(s,str):
returns.encode(locale.getpreferredencoding(False))
elifisinstance(s,bytes):
returns
elifsisnotNone:
raiseTypeError('Expected bytes or text, but got %r'% (s,))
returnNone
# type: ignore ## mypy cannot understand dynamic class creation
defwith_metaclass(meta:Type[Any],*bases:Any)->TBD:
"""copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15"""
classmetaclass(meta):# type: ignore
__call__=type.__call__
__init__=type.__init__# type: ignore
def__new__(cls,name:str,nbases:Optional[Tuple[int, ...]],d:Dict[str,Any])->TBD:
ifnbasesisNone:
returntype.__new__(cls,name, (),d)
returnmeta(name,bases,d)
returnmetaclass(meta.__name__+'Helper',None, {})# type: ignore