Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Use print as a function for Python 3. [PEP 3105]
Browse files Browse the repository at this point in the history
For Python 2 compatibility, import print_function from the __future__ module.
This works as far back as Python 2.6.0a2:
    https://docs.python.org/2/library/__future__.html
  • Loading branch information
rodrigc authored and mcepl committed Nov 16, 2015
1 parent 227aeb7 commit 4964fb1
Show file tree
Hide file tree
Showing 77 changed files with 521 additions and 368 deletions.
6 changes: 4 additions & 2 deletions M2Crypto/DSA.py
@@ -1,3 +1,5 @@
from __future__ import print_function

"""
M2Crypto wrapper for OpenSSL DSA API.
Expand Down Expand Up @@ -34,9 +36,9 @@ class DSA:
r, s = dsa.sign(digest)
good = dsa.verify(digest, r, s)
if good:
print ' ** success **'
print(' ** success **')
else:
print ' ** verification failed **'
print(' ** verification failed **')
"""

m2_dsa_free = m2.dsa_free
Expand Down
6 changes: 4 additions & 2 deletions M2Crypto/SSL/SSLServer.py
@@ -1,3 +1,5 @@
from __future__ import print_function

"""SSLServer
Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved."""
Expand Down Expand Up @@ -37,10 +39,10 @@ def handle_request(self):
self.handle_error(request, client_address)

def handle_error(self, request, client_address):
print '-'*40
print('-'*40)
import traceback
traceback.print_exc()
print '-'*40
print('-'*40)


class ForkingSSLServer(SocketServer.ForkingMixIn, SSLServer):
Expand Down
4 changes: 3 additions & 1 deletion M2Crypto/m2urllib.py
@@ -1,3 +1,5 @@
from __future__ import print_function

"""M2Crypto enhancement to Python's urllib for handling
'https' url's.
Expand Down Expand Up @@ -36,7 +38,7 @@ def open_https(self, url, data=None, ssl_context=None):
user_passwd, realhost = splituser(realhost)
if user_passwd:
selector = "%s://%s%s" % (urltype, realhost, rest)
#print "proxy via http:", host, selector
#print("proxy via http:", host, selector)
if not host: raise IOError('http error', 'no host given')
if user_passwd:
import base64
Expand Down
13 changes: 7 additions & 6 deletions contrib/SimpleX509create.py
Expand Up @@ -2,6 +2,7 @@
#
#vim: ts=4 sw=4 nowrap
#
from __future__ import print_function

"""PKI demo by Peter Teniz <peter.teniz@inverisa.net>"""

Expand Down Expand Up @@ -71,7 +72,7 @@ def CreateX509Request ( self ):

self.X509Request.set_pubkey ( pkey=self.PKey )
self.X509Request.sign ( pkey=self.PKey, md='sha1' )
#print X509Request.as_text ()
#print(X509Request.as_text ())



Expand Down Expand Up @@ -119,8 +120,8 @@ def CreateX509Certificate ( self ):

X509Name = self.X509Request.get_subject ()

#print X509Name.entry_count ()
#print X509Name.as_text ()
#print(X509Name.entry_count ())
#print(X509Name.as_text ())

self.X509Certificate.set_subject_name( X509Name )

Expand All @@ -139,8 +140,8 @@ def CreateX509Certificate ( self ):
X509Name.add_entry_by_txt ( field='Email', type=MBSTRING_ASC, entry='admin@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address
X509Name.add_entry_by_txt ( field='emailAddress', type=MBSTRING_ASC, entry='admin@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address

#print X509Name.entry_count ()
#print X509Name.as_text ()
#print(X509Name.entry_count ())
#print(X509Name.as_text ())

self.X509Certificate.set_issuer_name( X509Name )

Expand All @@ -149,7 +150,7 @@ def CreateX509Certificate ( self ):
#

self.X509Certificate.sign( pkey=self.PKey, md='sha1' )
print self.X509Certificate.as_text ()
print(self.X509Certificate.as_text ())



Expand Down
11 changes: 7 additions & 4 deletions contrib/dispatcher.py
@@ -1,4 +1,7 @@
#!/usr/local/bin/python -O

from __future__ import print_function

"""
Implements a [hopefully] non-blocking SSL dispatcher on top of
M2Crypto package.
Expand Down Expand Up @@ -149,18 +152,18 @@ def handle_ssl_connect(self):
"""Invoked on SSL connection establishment (whilst
in client mode)
"""
print 'Unhandled handle_ssl_connect()'
print('Unhandled handle_ssl_connect()')

def handle_ssl_accept(self):
"""Invoked on SSL connection establishment (whilst
in server mode)
"""
print 'Unhandled handle_ssl_accept()'
print('Unhandled handle_ssl_accept()')

def handle_ssl_read(self, data):
"""Invoked on new data arrival to SSL connection
"""
print 'Unhandled handle_ssl_read event'
print('Unhandled handle_ssl_read event')

def handle_ssl_close(self):
"""Invoked on SSL connection termination
Expand All @@ -182,7 +185,7 @@ def __init__ (self, cert, key):
dispatcher.__init__(self, cert, key)

def handle_ssl_read(self, data):
print data
print(data)
self.ssl_write('test write')

ssl = client('test.cert', 'test.key')
Expand Down
6 changes: 4 additions & 2 deletions contrib/isaac.httpslib.py
@@ -1,3 +1,5 @@
from __future__ import print_function

"""M2Crypto support for Python 1.5.2 and Python 2.x's httplib.
Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved."""
Expand Down Expand Up @@ -102,7 +104,7 @@ def connect(self, host, port=None):
raise socket.error("nonnumeric port")
if not port: port = HTTPS_PORT
self.sock = SSL.Connection(self.ssl_ctx)
if self.debuglevel > 0: print 'connect:', (host, port)
if self.debuglevel > 0: print('connect:', (host, port))
self.sock.connect((host, port))

# ISS Added.
Expand Down Expand Up @@ -257,7 +259,7 @@ def connect(self):
# Fake the socket
ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
self.sock = FakeSocket(self.sock, ssl)
if self.debuglevel > 0: print 'socket type:', self.sock
if self.debuglevel > 0: print('socket type:', self.sock)

def putrequest(self, method, url):
"""Send a request to the server.
Expand Down
14 changes: 8 additions & 6 deletions demo/Zope/z2s.py
Expand Up @@ -10,6 +10,9 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################

from __future__ import print_function

"""Zope 2 ZServer start-up file
Usage: %(program)s [options] [environment settings]
Expand Down Expand Up @@ -257,7 +260,6 @@
Note: you *must* use Python 2.1 or later!
"""


# This is required path hackery for the win32 binary distribution
# that ensures that the bundled python libraries are used. In a
# win32 binary distribution, the installer will have replaced the
Expand Down Expand Up @@ -511,7 +513,7 @@ def server_info(old, v, offset=0):
if v=='-': v=''
PCGI_FILE=v
elif o=='-h':
print __doc__ % vars()
print(__doc__ % vars())
sys.exit(0)
elif o=='-2': MODULE='Main'
elif o=='-l': LOG_FILE=v
Expand All @@ -525,10 +527,10 @@ def server_info(old, v, offset=0):

except SystemExit: sys.exit(0)
except:
print __doc__ % vars()
print
print 'Error:'
print "%s: %s" % (sys.exc_type, sys.exc_value)
print(__doc__ % vars())
print()
print('Error:')
print("%s: %s" % (sys.exc_type, sys.exc_value))
sys.exit(1)

#
Expand Down
4 changes: 2 additions & 2 deletions demo/Zope27/INSTALL.txt
Expand Up @@ -149,7 +149,7 @@ header: Connection: close
>>> while 1:
... data = u.read()
... if not data: break
... print data
... print(data)
...

::
Expand Down Expand Up @@ -188,7 +188,7 @@ XMLRPC-over-HTTPS

>>> from M2Crypto.m2xmlrpclib import Server, SSL_Transport
>>> zs = Server('https://127.0.0.1:8443/', SSL_Transport())
>>> print zs.propertyMap()
>>> print(zs.propertyMap())
[{'type': 'string', 'id': 'title', 'mode': 'w'}]
>>>

Expand Down
Expand Up @@ -6,6 +6,9 @@
# XXX license TBD; should be Zope 3's ZPL, I just haven't read thru that.
#
##############################################################################

from __future__ import print_function

"""HTTPS Server
This is a HTTPS version of HTTPServer.
Expand Down Expand Up @@ -100,5 +103,5 @@ def handle_accept(self):
asyncore.poll(5)

except KeyboardInterrupt:
print 'shutting down...'
print('shutting down...')
td.shutdown()
Expand Up @@ -6,6 +6,9 @@
# XXX license TBD; should be Zope 3's ZPL, I just haven't read thru that.
#
##############################################################################

from __future__ import print_function

"""HTTPS Server that uses the Zope Publisher for executing a task.
$Id$
Expand Down Expand Up @@ -76,8 +79,8 @@ def executeRequest(self, task):
publish(request, handle_errors=False)
except:
import sys, pdb
print "%s:" % sys.exc_info()[0]
print sys.exc_info()[1]
print("%s:" % sys.exc_info()[0])
print(sys.exc_info()[1])
pdb.post_mortem(sys.exc_info()[2])
raise

6 changes: 4 additions & 2 deletions demo/bio_mem_rw.py
@@ -1,5 +1,7 @@
#!/usr/bin/env python2.0

from __future__ import print_function

"""Demonstrates the use of m2.bio_set_mem_eof_return().
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""

Expand All @@ -23,7 +25,7 @@
if use_mem:
m2.bio_set_mem_eof_return(bio, 0)
xxx = m2.bio_read(bio, 100)
print `xxx`, len(xxx)
print(`xxx`, len(xxx))
m2.bio_free(bio)

if use_mem:
Expand All @@ -37,7 +39,7 @@
m2.bio_set_cipher(filt, ciph, 'key', 'iv', 0)
m2.bio_push(filt, bio)
yyy = m2.bio_read(filt, 100)
print `yyy`
print(`yyy`)
m2.bio_pop(filt)
m2.bio_free(filt)
m2.bio_free(bio)
Expand Down
20 changes: 11 additions & 9 deletions demo/dhtest.py
@@ -1,25 +1,27 @@
#!/usr/bin/env python

from __future__ import print_function

"""DH demonstration.
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""

from M2Crypto import DH, Rand

def test():
print 'generating dh params:'
print('generating dh params:')
a = DH.gen_params(128, 2)
b = DH.set_params(a.p, a.g)
a.gen_key()
b.gen_key()
print 'p = ', `a.p`
print 'g = ', `a.g`
print 'a.pub =', `a.pub`
print 'a.priv =', `a.priv`
print 'b.pub =', `b.pub`
print 'b.priv =', `b.priv`
print 'a.key = ', `a.compute_key(b.pub)`
print 'b.key = ', `b.compute_key(a.pub)`
print('p = ', `a.p`)
print('g = ', `a.g`)
print('a.pub =', `a.pub`)
print('a.priv =', `a.priv`)
print('b.pub =', `b.pub`)
print('b.priv =', `b.priv`)
print('a.key = ', `a.compute_key(b.pub)`)
print('b.key = ', `b.compute_key(a.pub)`)

if __name__=='__main__':
Rand.load_file('randpool.dat', -1)
Expand Down

0 comments on commit 4964fb1

Please sign in to comment.