def test_reverse_ascii_upper_props_group(self):
"""Test reverse ASCII uppercase properties in a group."""
pattern = bre.compile_search(br'EX[\C]+LE')
m = pattern.match(br'EXampLE')
self.assertTrue(m is not None)
python类ASCII的实例源码
def test_reverse_ascii_props_mixed(self):
"""Test reverse ASCII properties."""
pattern = bre.compile_search(br'EX\C\L\CLE')
m = pattern.match(br'EXaMpLE')
self.assertTrue(m is not None)
def test_unicode_shorthand_ascii_only(self):
"""Ensure that when the Unicode flag is not used, only ASCII properties are used."""
flags = bre.ASCII if PY3 else 0
pattern = bre.compile_search(r'ex\lmple', flags)
m = pattern.match('exámple')
self.assertTrue(m is None)
m = pattern.match('example')
self.assertTrue(m is not None)
def test_unicode_string_flag(self):
"""Test finding Unicode/ASCII string flag."""
if PY3:
template = bre.SearchTemplate(r'Testing for (?ia) ASCII flag.', False, None)
template.apply()
self.assertFalse(template.unicode)
else:
template = bre.SearchTemplate(r'Testing for (?iu) Unicode flag.', False, None)
template.apply()
self.assertTrue(template.unicode)
def test_unicode_string_flag_in_group(self):
"""Test ignoring Unicode/ASCII string flag in group."""
if PY3:
template = bre.SearchTemplate(r'Testing for [(?ia)] ASCII flag.', False, None)
template.apply()
self.assertTrue(template.unicode)
else:
template = bre.SearchTemplate(r'Testing for [(?iu)] Unicode flag.', False, None)
template.apply()
self.assertFalse(template.unicode)
def test_unicode_string_flag_escaped(self):
"""Test ignoring Unicode/ASCII string flag in group."""
if PY3:
template = bre.SearchTemplate(r'Testing for \(?ia) ASCII flag.', False, None)
template.apply()
self.assertTrue(template.unicode)
else:
template = bre.SearchTemplate(r'Testing for \(?iu) Unicode flag.', False, None)
template.apply()
self.assertFalse(template.unicode)
def test_unicode_string_flag_escaped_deep(self):
"""Test deep escaped Unicode flag."""
if PY3:
template = bre.SearchTemplate(r'Testing for \\\(?ia) ASCII flag.', False, None)
template.apply()
self.assertTrue(template.unicode)
else:
template = bre.SearchTemplate(r'Testing for \\\(?iu) Unicode flag.', False, None)
template.apply()
self.assertFalse(template.unicode)
def test_replace_unicode_name_ascii_range(self):
"""Test replacing Unicode names in the ASCII range."""
pattern = re.compile(r"(some)(.*?)(pattern)(!)")
expand = bre.compile_replace(
pattern,
r'\1 \N{Latin small letter a}\l\N{Latin Capital Letter A} and '
r'\LSPAN \N{Latin Capital Letter A}\E and Escaped \\N{Latin Capital Letter A}\E \3'
)
results = expand(pattern.match('some test pattern!'))
self.assertEqual(
'some aa and span a and Escaped \\N{Latin Capital Letter A} pattern',
results
)
def _connect(self):
# Create unique tag for this session,
# and compile tagged response matcher.
self.tagpre = Int2AP(random.randint(4096, 65535))
self.tagre = re.compile(br'(?P<tag>'
+ self.tagpre
+ br'\d+) (?P<type>[A-Z]+) (?P<data>.*)', re.ASCII)
# Get server welcome message,
# request and store CAPABILITY response.
if __debug__:
self._cmd_log_len = 10
self._cmd_log_idx = 0
self._cmd_log = {} # Last `_cmd_log_len' interactions
if self.debug >= 1:
self._mesg('imaplib version %s' % __version__)
self._mesg('new IMAP4 connection, tag=%s' % self.tagpre)
self.welcome = self._get_response()
if 'PREAUTH' in self.untagged_responses:
self.state = 'AUTH'
elif 'OK' in self.untagged_responses:
self.state = 'NONAUTH'
else:
raise self.error(self.welcome)
self._get_capabilities()
if __debug__:
if self.debug >= 3:
self._mesg('CAPABILITIES: %r' % (self.capabilities,))
for version in AllowedVersions:
if not version in self.capabilities:
continue
self.PROTOCOL_VERSION = version
return
raise self.error('server not IMAP4 compliant')
def _get_capabilities(self):
typ, dat = self.capability()
if dat == [None]:
raise self.error('no CAPABILITY response from server')
dat = str(dat[-1], "ASCII")
dat = dat.upper()
self.capabilities = tuple(dat.split())
def _new_tag(self):
tag = self.tagpre + bytes(str(self.tagnum), 'ASCII')
self.tagnum = self.tagnum + 1
self.tagged_commands[tag] = None
return tag
def test_ascii_and_unicode_flag(self):
# String patterns
for flags in (0, re.UNICODE):
pat = re.compile('\xc0', flags | re.IGNORECASE)
self.assertNotEqual(pat.match('\xe0'), None)
pat = re.compile('\w', flags)
self.assertNotEqual(pat.match('\xe0'), None)
pat = re.compile('\xc0', re.ASCII | re.IGNORECASE)
self.assertEqual(pat.match('\xe0'), None)
pat = re.compile('(?a)\xc0', re.IGNORECASE)
self.assertEqual(pat.match('\xe0'), None)
pat = re.compile('\w', re.ASCII)
self.assertEqual(pat.match('\xe0'), None)
pat = re.compile('(?a)\w')
self.assertEqual(pat.match('\xe0'), None)
# Bytes patterns
for flags in (0, re.ASCII):
pat = re.compile(b'\xc0', re.IGNORECASE)
self.assertEqual(pat.match(b'\xe0'), None)
pat = re.compile(b'\w')
self.assertEqual(pat.match(b'\xe0'), None)
# Incompatibilities
self.assertRaises(ValueError, re.compile, b'\w', re.UNICODE)
self.assertRaises(ValueError, re.compile, b'(?u)\w')
self.assertRaises(ValueError, re.compile, '\w', re.UNICODE | re.ASCII)
self.assertRaises(ValueError, re.compile, '(?u)\w', re.ASCII)
self.assertRaises(ValueError, re.compile, '(?a)\w', re.UNICODE)
self.assertRaises(ValueError, re.compile, '(?au)\w')
def expanduser(path):
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
do nothing."""
if isinstance(path, bytes):
tilde = b'~'
else:
tilde = '~'
if not path.startswith(tilde):
return path
sep = _get_sep(path)
i = path.find(sep, 1)
if i < 0:
i = len(path)
if i == 1:
if 'HOME' not in os.environ:
import pwd
userhome = pwd.getpwuid(os.getuid()).pw_dir
else:
userhome = os.environ['HOME']
else:
import pwd
name = path[1:i]
if isinstance(name, bytes):
name = str(name, 'ASCII')
try:
pwent = pwd.getpwnam(name)
except KeyError:
return path
userhome = pwent.pw_dir
if isinstance(path, bytes):
userhome = os.fsencode(userhome)
root = b'/'
else:
root = '/'
userhome = userhome.rstrip(root) or userhome
return userhome + path[i:]
# Expand paths containing shell variable substitutions.
# This expands the forms $variable and ${variable} only.
# Non-existent variables are left unchanged.
def unquote(s):
"""Turn a string in the form =AB to the ASCII character with value 0xab"""
return chr(int(s[1:3], 16))
def _unquote_match(match):
"""Turn a match in the form =AB to the ASCII character with value 0xab"""
s = match.group(0)
return unquote(s)
# Header decoding is done a bit differently
def open_data(self, url, data=None):
"""Use "data" URL."""
if not isinstance(url, str):
raise URLError('data error', 'proxy support for data protocol currently not implemented')
# ignore POSTed data
#
# syntax of data URLs:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
# mediatype := [ type "/" subtype ] *( ";" parameter )
# data := *urlchar
# parameter := attribute "=" value
try:
[type, data] = url.split(',', 1)
except ValueError:
raise IOError('data error', 'bad data URL')
if not type:
type = 'text/plain;charset=US-ASCII'
semi = type.rfind(';')
if semi >= 0 and '=' not in type[semi:]:
encoding = type[semi+1:]
type = type[:semi]
else:
encoding = ''
msg = []
msg.append('Date: %s'%time.strftime('%a, %d %b %Y %H:%M:%S GMT',
time.gmtime(time.time())))
msg.append('Content-type: %s' % type)
if encoding == 'base64':
# XXX is this encoding/decoding ok?
data = base64.decodebytes(data.encode('ascii')).decode('latin1')
else:
data = unquote(data)
msg.append('Content-Length: %d' % len(data))
msg.append('')
msg.append(data)
msg = '\n'.join(msg)
headers = email.message_from_string(msg)
f = io.StringIO(msg)
#f.fileno = None # needed for addinfourl
return addinfourl(f, headers, url)
def expanduser(path):
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
do nothing."""
if isinstance(path, bytes):
tilde = b'~'
else:
tilde = '~'
if not path.startswith(tilde):
return path
sep = _get_sep(path)
i = path.find(sep, 1)
if i < 0:
i = len(path)
if i == 1:
if 'HOME' not in os.environ:
import pwd
userhome = pwd.getpwuid(os.getuid()).pw_dir
else:
userhome = os.environ['HOME']
else:
import pwd
name = path[1:i]
if isinstance(name, bytes):
name = str(name, 'ASCII')
try:
pwent = pwd.getpwnam(name)
except KeyError:
return path
userhome = pwent.pw_dir
if isinstance(path, bytes):
userhome = os.fsencode(userhome)
root = b'/'
else:
root = '/'
userhome = userhome.rstrip(root)
return (userhome + path[i:]) or root
# Expand paths containing shell variable substitutions.
# This expands the forms $variable and ${variable} only.
# Non-existent variables are left unchanged.
def compat(self, token, iterable):
startline = False
indents = []
toks_append = self.tokens.append
toknum, tokval = token
if toknum in (NAME, NUMBER):
tokval += ' '
if toknum in (NEWLINE, NL):
startline = True
for tok in iterable:
toknum, tokval = tok[:2]
if toknum in (NAME, NUMBER, ASYNC, AWAIT):
tokval += ' '
if toknum == INDENT:
indents.append(tokval)
continue
elif toknum == DEDENT:
indents.pop()
continue
elif toknum in (NEWLINE, NL):
startline = True
elif startline and indents:
toks_append(indents[-1])
startline = False
toks_append(tokval)
# Commented out because re.ASCII not in Python 2.
#cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
#blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
def ascii_regex(pattern):
flags = re.ASCII if six.PY3 else 0
return re.compile(pattern, flags)
def __init__(self, negate=False, flags=0):
self._categories = set()
self._whitelist_chars = set()
self._blacklist_chars = set()
self._negate = negate
self._ignorecase = flags & re.IGNORECASE
self._unicode = (not flags & re.ASCII) \
if six.PY3 else bool(flags & re.UNICODE)