Debian Codegraph @

rpython/rlib/_os_support.py

raw symbols SCIP
1 import sys
2
3 from rpython.annotator.model import s_Str0, s_Unicode0
4 from rpython.rlib import rstring
5 from rpython.rlib.rutf8 import codepoints_in_utf8
6 from rpython.rlib.objectmodel import specialize
7 from rpython.rtyper.lltypesystem import rffi
8
9
10 _CYGWIN = sys.platform == 'cygwin'
11 _WIN32 = sys.platform.startswith('win')
12 _LINUX = sys.platform.startswith('linux')
13 UNDERSCORE_ON_WIN32 = '_' if _WIN32 else ''
14 POSIX_SIZE_T = rffi.UINT if _WIN32 else rffi.SIZE_T
15 POSIX_SSIZE_T = rffi.INT if _WIN32 else rffi.SSIZE_T
16 _MACRO_ON_POSIX = True if not _WIN32 else None
17
18
19 class StringTraits(object):
20 nickname = "bytes"
21 str0 = s_Str0
22 CHAR = rffi.CHAR
23 CCHARP = rffi.CCHARP
24 charp2str = staticmethod(rffi.charp2str)
25 charpsize2str = staticmethod(rffi.charpsize2str)
26 scoped_str2charp = staticmethod(rffi.scoped_str2charp)
27 str2charp = staticmethod(rffi.str2charp)
28 free_charp = staticmethod(rffi.free_charp)
29 scoped_alloc_buffer = staticmethod(rffi.scoped_alloc_buffer)
30
31 @staticmethod
32 @specialize.argtype(0)
33 def as_str(path):
34 assert path is not None
35 if isinstance(path, str):
36 return path
37 elif isinstance(path, unicode):
38 # This never happens in PyPy's Python interpreter!
39 # Only in raw RPython code that uses unicode strings.
40 # We implement python2 behavior: silently convert to ascii.
41 return path.encode('ascii')
42 else:
43 return path.as_bytes()
44
45 @staticmethod
46 @specialize.argtype(0)
47 def as_str0(path):
48 res = StringTraits.as_str(path)
49 rstring.check_str0(res)
50 return res
51
52
53 class Utf8Traits(object):
54 nickname = "utf8"
55 str = str
56 str0 = s_Str0
57 CHAR = rffi.WCHAR_T
58 CCHARP = rffi.CWCHARP
59 charpsize2str = staticmethod(rffi.wcharpsize2utf8)
60 free_charp = staticmethod(rffi.free_wcharp)
61 scoped_alloc_buffer = staticmethod(rffi.scoped_alloc_unicodebuffer)
62
63 @staticmethod
64 def scoped_str2charp(value):
65 return rffi.scoped_utf82wcharp(value, codepoints_in_utf8(value))
66
67 @staticmethod
68 def str2charp(value):
69 return rffi.utf82wcharp(value, codepoints_in_utf8(value))
70
71 @staticmethod
72 def charp2str(p):
73 s, lgt = rffi.wcharp2utf8(p)
74 return rstring.assert_str0(s)
75
76 @staticmethod
77 @specialize.argtype(0)
78 def as_str(path):
79 assert path is not None
80 if isinstance(path, str):
81 return path
82 elif isinstance(path, unicode):
83 # This never happens in PyPy's Python interpreter!
84 # Only in raw RPython code that uses unicode strings.
85 # We implement python3 behavior: silently convert to utf8.
86 return path.encode('utf8')
87 else:
88 return path.as_bytes()
89
90 @staticmethod
91 @specialize.argtype(0)
92 def as_str0(path):
93 res = Utf8Traits.as_str(path)
94 rstring.check_str0(res)
95 return res
96
97 @staticmethod
98 @specialize.argtype(0)
99 def as_utf80(path):
100 # XXX get rid of this assert
101 assert path is not None
102 if isinstance(path, str):
103 res = path
104 elif isinstance(path, unicode):
105 res = path.encode('utf-8')
106 else:
107 res = path.as_bytes()
108 assert res is not None
109 rstring.check_str0(res)
110 return res
111
112
113 string_traits = StringTraits()
114 utf8_traits = Utf8Traits()
115
116 # Always use utf8_traits