Update example files to work on Python 2 & 3

Almost all of the problems were using print statements instead of
equivalent Python 3 syntax. You'll notice that in Python 2, the AST
parses `print(...)` and `print ...` equivalently:

    $ python
    Python 2.7.9 (default, Dec 15 2014, 10:01:34)
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import ast
    >>> body = ast.parse('print("Foo")').body[0]
    >>> body
    <_ast.Print object at 0x1033452d0>
    >>> body.values
    [<_ast.Str object at 0x103345310>]
    >>> body2 = ast.parse('print "Foo"').body[0]
    >>> body2
    <_ast.Print object at 0x103345350>
    >>> body2.values
    [<_ast.Str object at 0x103345390>]

This leaves 2 files - exec.py, os-chmod.py - which are skipped due to
syntax errors on Python 3.4.

Change-Id: I2d97a249503317092372a874c018561cf875b066
This commit is contained in:
Ian Cordasco 2015-06-02 12:15:53 -05:00
parent ddf75663ce
commit 4af7ea6a20
13 changed files with 39 additions and 40 deletions

View File

@ -5,5 +5,4 @@ hashlib.md5(1).hexdigest()
abc = str.replace(hashlib.md5("1"), "###")
print hashlib.md5("1")
print(hashlib.md5("1"))

View File

@ -1,15 +1,15 @@
def someFunction(user, password="Admin"):
print "Hi " + user
print("Hi " + user)
def someFunction2(password):
if password == "root":
print "OK, logged in"
print("OK, logged in")
def noMatch(password):
if password == '':
print "No password!"
print("No password!")
def NoMatch2(password):
if password == "ajklawejrkl42348swfgkg":
print "Nice password!"
print("Nice password!")

View File

@ -16,4 +16,4 @@ if password:
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
print(tn.read_all())

View File

@ -3,10 +3,10 @@ import tempfile
serialized = marshal.dumps({'a': 1})
print marshal.loads(serialized)
print(marshal.loads(serialized))
file_obj = tempfile.TemporaryFile()
marshal.dump(range(5), file_obj)
file_obj.seek(0)
print marshal.load(file_obj)
print(marshal.load(file_obj))
file_obj.close()

View File

@ -5,25 +5,25 @@ import StringIO
# pickle
pick = pickle.dumps({'a': 'b', 'c': 'd'})
print pickle.loads(pick)
print(pickle.loads(pick))
file_obj = StringIO.StringIO()
pickle.dump([1, 2, '3'], file_obj)
file_obj.seek(0)
print pickle.load(file_obj)
print(pickle.load(file_obj))
file_obj.seek(0)
print pickle.Unpickler(file_obj).load()
print(pickle.Unpickler(file_obj).load())
# cPickle
serialized = cPickle.dumps({(): []})
print cPickle.loads(serialized)
print(cPickle.loads(serialized))
file_obj = StringIO.StringIO()
cPickle.dump((1,), file_obj)
file_obj.seek(0)
print cPickle.load(file_obj)
print(cPickle.load(file_obj))
file_obj.seek(0)
print cPickle.Unpickler(file_obj).load()
print(cPickle.Unpickler(file_obj).load())

View File

@ -2,14 +2,14 @@ import commands
import popen2
print commands.getstatusoutput('echo / | xargs ls')
print commands.getoutput('echo / | xargs ls')
print(commands.getstatusoutput('echo / | xargs ls'))
print(commands.getoutput('echo / | xargs ls'))
# This one is safe.
print commands.getstatus('echo / | xargs ls')
print(commands.getstatus('echo / | xargs ls'))
print popen2.popen2('echo / | xargs ls')[0].read()
print popen2.popen3('echo / | xargs ls')[0].read()
print popen2.popen4('echo / | xargs ls')[0].read()
print popen2.Popen3('echo / | xargs ls').fromchild.read()
print popen2.Popen4('echo / | xargs ls').fromchild.read()
print(popen2.popen2('echo / | xargs ls')[0].read())
print(popen2.popen3('echo / | xargs ls')[0].read())
print(popen2.popen4('echo / | xargs ls')[0].read())
print(popen2.Popen3('echo / | xargs ls').fromchild.read())
print(popen2.Popen4('echo / | xargs ls').fromchild.read())

View File

@ -5,14 +5,14 @@ xmlString = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</headin
# unsafe
tree = badET.fromstring(xmlString)
print tree
print(tree)
badET.parse('filethatdoesntexist.xml')
badET.iterparse('filethatdoesntexist.xml')
a = badET.XMLParser()
# safe
tree = goodET.fromstring(xmlString)
print tree
print(tree)
goodET.parse('filethatdoesntexist.xml')
goodET.iterparse('filethatdoesntexist.xml')
a = goodET.XMLParser()

View File

@ -5,14 +5,14 @@ xmlString = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</headin
# unsafe
tree = badET.fromstring(xmlString)
print tree
print(tree)
badET.parse('filethatdoesntexist.xml')
badET.iterparse('filethatdoesntexist.xml')
a = badET.XMLParser()
# safe
tree = goodET.fromstring(xmlString)
print tree
print(tree)
goodET.parse('filethatdoesntexist.xml')
goodET.iterparse('filethatdoesntexist.xml')
a = goodET.XMLParser()

View File

@ -1,14 +1,14 @@
from xml.dom.minidom import parseString as badParseString
from defusedxml.minidom import parseString as goodParseString
a = badParseString("<myxml>Some data some more data</myxml>")
print a
print(a)
b = goodParseString("<myxml>Some data some more data</myxml>")
print b
print(b)
from xml.dom.minidom import parse as badParse
from defusedxml.minidom import parse as goodParse
a = badParse("somfilethatdoesntexist.xml")
print a
print(a)
b = goodParse("somefilethatdoesntexist.xml")
print b
print(b)

View File

@ -1,14 +1,14 @@
from xml.dom.pulldom import parseString as badParseString
from defusedxml.pulldom import parseString as goodParseString
a = badParseString("<myxml>Some data some more data</myxml>")
print a
print(a)
b = goodParseString("<myxml>Some data some more data</myxml>")
print b
print(b)
from xml.dom.pulldom import parse as badParse
from defusedxml.pulldom import parse as goodParse
a = badParse("somfilethatdoesntexist.xml")
print a
print(a)
b = goodParse("somefilethatdoesntexist.xml")
print b
print(b)

View File

@ -7,13 +7,13 @@ class ExampleContentHandler(xml.sax.ContentHandler):
xml.sax.ContentHandler.__init__(self)
def startElement(self, name, attrs):
print 'start:', name
print('start:', name)
def endElement(self, name):
print 'end:', name
print('end:', name)
def characters(self, content):
print 'chars:', content
print('chars:', content)
def main():
xmlString = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>"
@ -29,7 +29,7 @@ def main():
# bad
xml.sax.make_parser()
sax.make_parser()
print 'nothing'
print('nothing')
# good
defusedxml.sax.make_parser()

View File

@ -5,6 +5,6 @@ def is_even(n):
return n%2 == 0
server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
print("Listening on port 8000...")
server.register_function(is_even, "is_even")
server.serve_forever()

View File

@ -90,7 +90,7 @@ class FunctionalTests(unittest.TestCase):
def test_crypto_md5(self):
'''Test the `hashlib.md5` example.'''
expect = {'SEVERITY': {'MEDIUM': 5}, 'CONFIDENCE': {'HIGH': 5}}
expect = {'SEVERITY': {'MEDIUM': 4}, 'CONFIDENCE': {'HIGH': 4}}
self.check_example('crypto-md5.py', expect)
def test_eval(self):