Skip to content

Commit

Permalink
Merge branch 'trunk' into 9074-rodrigc-txchecker-py3
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph authored Mar 21, 2017
2 parents 036278b + 79ae400 commit 9825b73
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 204 deletions.
2 changes: 1 addition & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ Bugfixes

Deprecations and Removals
-------------------------
- twisted.web.http.Request's headers and request_headers attributes,
- twisted.web.http.Request's headers and received_headers attributes,
deprecated since Twisted 13.2, have been removed. (#8136)
- twisted.web.static.addSlash is deprecated. (#8169)

Expand Down
5 changes: 2 additions & 3 deletions src/twisted/application/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,9 @@ def loadApplication(filename, kind, passphrase=None):
@type passphrase: C{str}
"""
if kind == 'python':
application = sob.loadValueFromFile(filename, 'application',
passphrase)
application = sob.loadValueFromFile(filename, 'application')
else:
application = sob.load(filename, kind, passphrase)
application = sob.load(filename, kind)
return application


Expand Down
2 changes: 2 additions & 0 deletions src/twisted/conch/scripts/ckeygen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import sys, os, getpass, socket
from functools import wraps
from imp import reload

if getpass.getpass == getpass.unix_getpass:
try:
import termios # hack around broken termios
Expand Down
70 changes: 12 additions & 58 deletions src/twisted/persisted/sob.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,15 @@

import os
import sys
import warnings

try:
import cPickle as pickle
except ImportError:
import pickle
from io import BytesIO
from hashlib import md5
from twisted.python import log, runtime
from twisted.persisted import styles
from zope.interface import implementer, Interface

# Note:
# These encrypt/decrypt functions only work for data formats
# which are immune to having spaces tucked at the end.
# All data formats which persist saves hold that condition.
def _encrypt(passphrase, data):
from Crypto.Cipher import AES as cipher

warnings.warn(
'Saving encrypted persisted data is deprecated since Twisted 15.5.0',
DeprecationWarning, stacklevel=2)

leftover = len(data) % cipher.block_size
if leftover:
data += b' ' * (cipher.block_size - leftover)
return cipher.new(md5(passphrase).digest()[:16]).encrypt(data)



def _decrypt(passphrase, data):
from Crypto.Cipher import AES

warnings.warn(
'Loading encrypted persisted data is deprecated since Twisted 15.5.0',
DeprecationWarning, stacklevel=2)

return AES.new(md5(passphrase).digest()[:16]).decrypt(data)



class IPersistable(Interface):
Expand Down Expand Up @@ -101,14 +71,9 @@ def _getFilename(self, filename, ext, tag):
finalname = "%s.%s" % (self.name, ext)
return finalname, filename

def _saveTemp(self, filename, passphrase, dumpFunc):
def _saveTemp(self, filename, dumpFunc):
with open(filename, 'wb') as f:
if passphrase is None:
dumpFunc(self.original, f)
else:
s = BytesIO()
dumpFunc(self.original, s)
f.write(_encrypt(passphrase, s.getvalue()))
dumpFunc(self.original, f)

def _getStyle(self):
if self.style == "source":
Expand All @@ -128,11 +93,12 @@ def save(self, tag=None, filename=None, passphrase=None):
@type passphrase: string
"""
ext, dumpFunc = self._getStyle()
if passphrase:
if passphrase is not None:
raise TypeError("passphrase must be None")
ext = 'e' + ext
finalname, filename = self._getFilename(filename, ext, tag)
log.msg("Saving "+self.name+" application to "+finalname+"...")
self._saveTemp(filename, passphrase, dumpFunc)
self._saveTemp(filename, dumpFunc)
if runtime.platformType == "win32" and os.path.isfile(finalname):
os.remove(finalname)
os.rename(filename, finalname)
Expand Down Expand Up @@ -162,25 +128,21 @@ def __getattr__(self, key):
return styles.Ephemeral()


def load(filename, style, passphrase=None):
def load(filename, style):
"""Load an object from a file.
Deserialize an object from a file. The file can be encrypted.
@param filename: string
@param style: string (one of 'pickle' or 'source')
@param passphrase: string
"""
mode = 'r'
if style=='source':
from twisted.persisted.aot import unjellyFromSource as _load
else:
_load, mode = pickle.load, 'rb'
if passphrase:
with open(filename, 'rb') as loadedFile:
fp = BytesIO(_decrypt(passphrase, loadedFile.read()))
else:
fp = open(filename, mode)

fp = open(filename, mode)
ee = _EverythingEphemeral(sys.modules['__main__'])
sys.modules['__main__'] = ee
ee.initRun = 1
Expand All @@ -199,26 +161,18 @@ def load(filename, style, passphrase=None):
return value


def loadValueFromFile(filename, variable, passphrase=None):
def loadValueFromFile(filename, variable):
"""Load the value of a variable in a Python file.
Run the contents of the file, after decrypting if C{passphrase} is
given, in a namespace and return the result of the variable
named C{variable}.
Run the contents of the file in a namespace and return the result of the
variable named C{variable}.
@param filename: string
@param variable: string
@param passphrase: string
"""
if passphrase:
mode = 'rb'
else:
mode = 'r'
with open(filename, mode) as fileObj:
with open(filename, 'r') as fileObj:
data = fileObj.read()
d = {'__file__': filename}
if passphrase:
data = _decrypt(passphrase, data)
codeObj = compile(data, filename, "exec")
eval(codeObj, d, d)
value = d[variable]
Expand Down
5 changes: 3 additions & 2 deletions src/twisted/protocols/test/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def test_disorderlyShutdown(self):

# And when the connection completely dies, check the reason.
def cbDisconnected(clientProtocol):
clientProtocol.lostConnectionReason.trap(Error)
clientProtocol.lostConnectionReason.trap(Error, ConnectionLost)
clientConnectionLost.addCallback(cbDisconnected)
return clientConnectionLost

Expand Down Expand Up @@ -1379,7 +1379,8 @@ def registerProducerAfterConnectionLost(self, streaming):
producer is not used, and its stopProducing method is called.
"""
clientProtocol, tlsProtocol = buildTLSProtocol()
clientProtocol.connectionLost = lambda reason: reason.trap(Error)
clientProtocol.connectionLost = lambda reason: reason.trap(
Error, ConnectionLost)

class Producer(object):
stopped = False
Expand Down
7 changes: 3 additions & 4 deletions src/twisted/python/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@
tls=[
'pyopenssl >= 16.0.0',
'service_identity',
# idna 2.3 introduced some changes that break a few things. Avoid it
# for the time being. I expect we'll work around those changes and
# lift the < 2.3 restriction here shortly.
'idna >= 0.6, < 2.3',
# idna 2.3 introduced some changes that break a few things. Avoid it.
# The problems were fixed in 2.4.
'idna >= 0.6, != 2.3',
],
conch=[
'pyasn1',
Expand Down
2 changes: 2 additions & 0 deletions src/twisted/python/rebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import time
import linecache

from imp import reload

# Sibling Imports
from twisted.python import log, reflect
from twisted.python._oldstyle import _oldStyle
Expand Down
8 changes: 4 additions & 4 deletions src/twisted/python/test/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_extrasRequiresTlsDeps(self):
deps = _EXTRAS_REQUIRE['tls']
self.assertIn('pyopenssl >= 16.0.0', deps)
self.assertIn('service_identity', deps)
self.assertIn('idna >= 0.6, < 2.3', deps)
self.assertIn('idna >= 0.6, != 2.3', deps)


def test_extrasRequiresConchDeps(self):
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_extrasRequiresAllNonPlatformDeps(self):
deps = _EXTRAS_REQUIRE['all_non_platform']
self.assertIn('pyopenssl >= 16.0.0', deps)
self.assertIn('service_identity', deps)
self.assertIn('idna >= 0.6, < 2.3', deps)
self.assertIn('idna >= 0.6, != 2.3', deps)
self.assertIn('pyasn1', deps)
self.assertIn('cryptography >= 0.9.1', deps)
self.assertIn('soappy', deps)
Expand All @@ -203,7 +203,7 @@ def test_extrasRequiresOsxPlatformDeps(self):
deps = _EXTRAS_REQUIRE['osx_platform']
self.assertIn('pyopenssl >= 16.0.0', deps)
self.assertIn('service_identity', deps)
self.assertIn('idna >= 0.6, < 2.3', deps)
self.assertIn('idna >= 0.6, != 2.3', deps)
self.assertIn('pyasn1', deps)
self.assertIn('cryptography >= 0.9.1', deps)
self.assertIn('soappy', deps)
Expand All @@ -222,7 +222,7 @@ def test_extrasRequiresWindowsPlatformDeps(self):
deps = _EXTRAS_REQUIRE['windows_platform']
self.assertIn('pyopenssl >= 16.0.0', deps)
self.assertIn('service_identity', deps)
self.assertIn('idna >= 0.6, < 2.3', deps)
self.assertIn('idna >= 0.6, != 2.3', deps)
self.assertIn('pyasn1', deps)
self.assertIn('cryptography >= 0.9.1', deps)
self.assertIn('soappy', deps)
Expand Down
1 change: 1 addition & 0 deletions src/twisted/test/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import warnings
import calendar
from io import IOBase
from imp import reload

from twisted.trial import unittest

Expand Down
Loading

0 comments on commit 9825b73

Please sign in to comment.