Skip to content

Commit

Permalink
port just enough of twisted.mail to support running the test setUps
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Oct 10, 2020
1 parent 4327591 commit 291b1f0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/twisted/mail/mail.py
Expand Up @@ -431,7 +431,7 @@ def lineReceived(self, line):
@type line: L{bytes}
@param line: A received line.
"""
self.fp.write(line + "\n")
self.fp.write(line + b"\n")

def eomReceived(self):
"""
Expand Down
25 changes: 14 additions & 11 deletions src/twisted/mail/maildir.py
Expand Up @@ -7,18 +7,14 @@
Maildir-style mailbox support.
"""

import io
import os
import stat
import socket
from hashlib import md5

from zope.interface import implementer

try:
import cStringIO as StringIO
except ImportError:
import StringIO

from twisted.mail import pop3
from twisted.mail import smtp
from twisted.protocols import basic
Expand All @@ -28,6 +24,7 @@
from twisted.internet import interfaces, defer, reactor
from twisted.cred import portal, credentials, checkers
from twisted.cred.error import UnauthorizedLogin
from typing import IO

INTERNAL_ERROR = """\
From: Twisted.mail Internals
Expand Down Expand Up @@ -94,6 +91,7 @@ def initializeMaildir(dir):
@type dir: L{bytes}
@param dir: The path name for a user directory.
"""
dir = os.fsdecode(dir)
if not os.path.isdir(dir):
os.mkdir(dir, 0o700)
for subdir in ["new", "cur", "tmp", ".Trash"]:
Expand Down Expand Up @@ -358,7 +356,7 @@ def __init__(self, mbox, msg):
self.defer = defer.Deferred()
self.openCall = None
if not hasattr(msg, "read"):
msg = StringIO.StringIO(msg)
msg = io.BytesIO(msg)
self.msg = msg

def startUp(self):
Expand Down Expand Up @@ -687,20 +685,20 @@ def listMessages(self, i=None):
return 0
return len(self.msgs[i])

def getMessage(self, i):
def getMessage(self, i: int) -> IO[bytes]:
"""
Return an in-memory file-like object with the contents of a message.
@type i: L{int}
@param i: The 0-based index of a message.
@rtype: L{StringIO <cStringIO.StringIO>}
@rtype: L{IO[bytes]}
@return: An in-memory file-like object containing the message.
@raise IndexError: When the index does not correspond to a message in
the mailbox.
"""
return StringIO.StringIO(self.msgs[i])
return io.BytesIO(self.msgs[i])

def getUidl(self, i):
"""
Expand Down Expand Up @@ -778,8 +776,9 @@ def __init__(self, service, root, postmaster=0):
should be forwarded to the postmaster (C{True}) or
bounced (C{False}).
"""
root = os.fsencode(root)
AbstractMaildirDomain.__init__(self, service, root)
dbm = os.path.join(root, "passwd")
dbm = os.path.join(root, b"passwd")
if not os.path.exists(dbm):
os.makedirs(dbm)
self.dbm = dirdbm.open(dbm)
Expand All @@ -801,7 +800,11 @@ def userDirectory(self, name):
if not self.postmaster:
return None
name = "postmaster"
dir = os.path.join(self.root, name)
try:
dir = os.path.join(self.root, name)
except Exception:
breakpoint()
raise
if not os.path.exists(dir):
initializeMaildir(dir)
return dir
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/mail/relay.py
Expand Up @@ -83,7 +83,7 @@ def loadMessages(self, messagePaths):
self.messages = []
self.names = []
for message in messagePaths:
with open(message + "-H") as fp:
with open(message + "-H", "rb") as fp:
messageContents = pickle.load(fp)
fp = open(message + "-D")
messageContents.append(fp)
Expand Down

0 comments on commit 291b1f0

Please sign in to comment.