diff --git a/Lib/imaplib.py b/Lib/imaplib.py --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -120,6 +120,7 @@ _Literal = br'.*{(?P\d+)}$' _Untagged_status = br'\* (?P\d+) (?P[A-Z-]+)( (?P.*))?' +_Atom_Specials = re.compile(r'[\(\)\{ \%\*\"\\\]]') class IMAP4: @@ -941,7 +942,10 @@ for arg in args: if arg is None: continue if isinstance(arg, str): + if _Atom_Specials.search(arg): + arg = '"' + arg + '"' arg = bytes(arg, self._encoding) + data = data + b' ' + arg literal = self.literal diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -10,6 +10,7 @@ import socketserver import time import calendar +import re from test.support import (reap_threads, verbose, transient_internet, run_with_tz, run_with_locale) @@ -142,7 +143,7 @@ except StopIteration: self.continuation = None continue - splitline = line.decode('ASCII').split() + splitline = re.findall(r'[^"\s]\S*|".+?"', line.decode('ASCII')) tag = splitline[0] cmd = splitline[1] args = splitline[2:] @@ -265,6 +266,23 @@ self.assertRaises(imaplib.IMAP4.abort, self.imap_class, *server.server_address) + # Issue 13940: space in mailbox names + @reap_threads + def test_create_with_space(self): + class CreateHandler(SimpleIMAPHandler): + def cmd_CREATE(self, tag, args): + if args[0] == '"name with space"': + self._send_tagged(tag, 'OK', 'CREATE completed') + return self._send_tagged(tag, 'BAD', args[0]) + + with self.reaped_server(CreateHandler) as server: + client = self.imap_class(*server.server_address) + client.state = 'AUTH' + typ, data = client.create('name with space') + self.assertEqual(typ, 'OK') + self.assertEqual(data[0], b'CREATE completed') + client.shutdown() + class UTF8Server(SimpleIMAPHandler): capabilities = 'AUTH ENABLE UTF8=ACCEPT'

AltStyle によって変換されたページ (->オリジナル) /