Skip to content

Commit

Permalink
Convert everything to Python 3 using 2to3
Browse files Browse the repository at this point in the history
Note that this code is non-functional, but makes it easier to see the
changes done to get things Python 3 compliant.
  • Loading branch information
CendioOssman committed Sep 24, 2020
1 parent f944f92 commit 87cd72f
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 74 deletions.
79 changes: 36 additions & 43 deletions pyobfuscate
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*-mode: python; coding: utf-8 -*-
#
# pyobfuscate - Python source code obfuscator
Expand All @@ -19,7 +19,6 @@
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import sys
import types
import symbol
import token
import keyword
Expand All @@ -28,7 +27,7 @@ import compiler
import parser
import random
import symtable
import StringIO
import io
import getopt
import re

Expand All @@ -42,7 +41,7 @@ class NameTranslator:

def get_name(self, name):
"""Get a translation for a real name"""
if not self.realnames.has_key(name):
if name not in self.realnames:
self.realnames[name] = self.gen_unique_name()
return self.realnames[name]

Expand All @@ -60,9 +59,9 @@ class NameTranslator:
def gen_unique_name(self):
"""Generate a name that hasn't been used before;
not as a real name, not as a bogus name"""
existing_names = self.realnames.values() + self.bogusnames
existing_names = list(self.realnames.values()) + self.bogusnames
name = ""
while 1:
while True:
name += self.gen_name()
if name not in existing_names:
break
Expand Down Expand Up @@ -114,7 +113,7 @@ class LambdaSymTable:


def is_lambda_arg(self, id):
return self.mysymbs.has_key(id)
return id in self.mysymbs


class CSTWalker:
Expand Down Expand Up @@ -159,7 +158,7 @@ class CSTWalker:

def walk(self, elements, symtabs):
# We are not interested in terminal tokens
if type(elements) != types.TupleType:
if not isinstance(elements, tuple):
return
if token.ISTERMINAL(elements[0]):
return
Expand Down Expand Up @@ -203,7 +202,7 @@ class CSTWalker:
if not name.startswith("__"):
return name

for i in xrange(len(symtabs)):
for i in range(len(symtabs)):
tab = symtabs[-1 - i]
tabtype = tab.get_type()
if tabtype == "class":
Expand Down Expand Up @@ -236,11 +235,11 @@ class CSTWalker:
# Add the symbols you want to examine to this list
debug_symbols = []
if id in debug_symbols:
print >>sys.stderr, "%s:" % id
print >>sys.stderr, " Imported:", s.is_imported()
print >>sys.stderr, " Parameter:", s.is_parameter()
print >>sys.stderr, " Global:", s.is_global()
print >>sys.stderr, " Local:", s.is_local()
print("%s:" % id, file=sys.stderr)
print(" Imported:", s.is_imported(), file=sys.stderr)
print(" Parameter:", s.is_parameter(), file=sys.stderr)
print(" Global:", s.is_global(), file=sys.stderr)
print(" Local:", s.is_local(), file=sys.stderr)

# Explicit imports are a clear no
if s.is_imported():
Expand Down Expand Up @@ -287,10 +286,10 @@ class CSTWalker:

# XXX: See above:
if id in debug_symbols:
print >>sys.stderr, " Imported (G):", topsym.is_imported()
print >>sys.stderr, " Parameter (G):", topsym.is_parameter()
print >>sys.stderr, " Global (G):", topsym.is_global()
print >>sys.stderr, " Local (G):", topsym.is_local()
print(" Imported (G):", topsym.is_imported(), file=sys.stderr)
print(" Parameter (G):", topsym.is_parameter(), file=sys.stderr)
print(" Global (G):", topsym.is_global(), file=sys.stderr)
print(" Local (G):", topsym.is_local(), file=sys.stderr)

# Explicit imports are a clear no
if topsym.is_imported():
Expand Down Expand Up @@ -380,7 +379,7 @@ class CSTWalker:
tab = symtabs[-1]

for tok in elements:
if type(tok) != types.TupleType:
if not isinstance(tok, tuple):
continue

toktype = tok[0]
Expand Down Expand Up @@ -697,13 +696,14 @@ class CSTWalker:
for node in elements:
self.walk(node, symtabs)

@staticmethod
def get_varargs_names(elements):
"""Extract all argument names and lines from varargslist"""
result = []

next_is_name = False
for tok in elements:
if type(tok) != types.TupleType:
if not isinstance(tok, tuple):
continue

toktype = tok[0]
Expand All @@ -720,15 +720,13 @@ class CSTWalker:

return result

get_varargs_names = staticmethod(get_varargs_names)


@staticmethod
def get_fpdef_names(elements):
"""Extract all argument names from fpdef"""
result = []

# We are not interested in terminal tokens
if type(elements) != types.TupleType:
if not isinstance(elements, tuple):
return result
if token.ISTERMINAL(elements[0]):
return result
Expand All @@ -741,9 +739,6 @@ class CSTWalker:
for node in elements:
result.extend(CSTWalker.get_fpdef_names(node))
return result

get_fpdef_names = staticmethod(get_fpdef_names)



class PubApiExtractor:
Expand All @@ -756,14 +751,13 @@ class PubApiExtractor:
# Didn't find __all__.
if conf.allpublic:
symtab = symtable.symtable(source_no_encoding, "-", "exec")
self.pubapi = filter(lambda s: s[0] != "_",
symtab.get_identifiers())
self.pubapi = [s for s in symtab.get_identifiers() if s[0] != "_"]
else:
self.pubapi = []

if self.matches > 1:
print >>sys.stderr, "Warning: Found multiple __all__ definitions"
print >>sys.stderr, "Using last definition"
print("Warning: Found multiple __all__ definitions", file=sys.stderr)
print("Using last definition", file=sys.stderr)


def visitAssign(self, node):
Expand All @@ -787,7 +781,7 @@ class PubApiExtractor:
break

if not constant:
print >>sys.stderr, "Error: __all__ is not a list of constants."
print("Error: __all__ is not a list of constants.", file=sys.stderr)
sys.exit(1)


Expand All @@ -805,7 +799,7 @@ class ColumnExtractor:
# To detect line num changes; backslash constructs doesn't
# generate any token
self.this_lineno = 1
f = StringIO.StringIO(source)
f = io.StringIO(source)
self.parse(f)


Expand Down Expand Up @@ -889,7 +883,7 @@ class TokenPrinter:
self.commentstate = TokenPrinter.AFTERCOMMENT
else:
self.commentstate = TokenPrinter.BEFORECOMMENT
f = StringIO.StringIO(source)
f = io.StringIO(source)
self.play(f)


Expand Down Expand Up @@ -1015,8 +1009,7 @@ class TokenPrinter:
def line_append(self, s):
if self.pending:
indent = max(self.indent, self.pending_indent)
self.pending = map(lambda row: " "*indent + row,
self.pending)
self.pending = [" "*indent + row for row in self.pending]
if conf.blanks == conf.OBFUSCATE_BLANKS:
sys.stdout.write(''.join(self.pending))
self.pending = []
Expand Down Expand Up @@ -1047,7 +1040,7 @@ class TokenPrinter:


def strip_encoding(source):
f = StringIO.StringIO(source)
f = io.StringIO(source)
lines = [f.readline(), f.readline()]
buf = ""
for line in lines:
Expand All @@ -1057,7 +1050,7 @@ def strip_encoding(source):
buf += "\n"
else:
# Gosh, not a comment.
print >>sys.stderr, "ERROR: Python 2.3 with coding declaration in non-comment!"
print("ERROR: Python 2.3 with coding declaration in non-comment!", file=sys.stderr)
sys.exit(1)
else:
# Coding declaration not found on this line; add
Expand All @@ -1069,7 +1062,7 @@ def strip_encoding(source):


def usage():
print >>sys.stderr, """
print("""
Usage:
pyobfuscate [options] <file>
Expand All @@ -1086,7 +1079,7 @@ Options:
-a, --allpublic When __all__ is missing, assume everything is public.
The default is to assume nothing is public.
-v, --verbose Verbose mode.
"""
""", file=sys.stderr)


class Configuration:
Expand All @@ -1102,8 +1095,8 @@ class Configuration:
"verbose"])
if len(args) != 1:
raise getopt.GetoptError("A filename is required", "")
except getopt.GetoptError, e:
print >>sys.stderr, "Error:", e
except getopt.GetoptError as e:
print("Error:", e, file=sys.stderr)
usage()
sys.exit(2)

Expand Down Expand Up @@ -1182,7 +1175,7 @@ def main():

# Step 5: Output a marker that makes it possible to recognize
# obfuscated files
print "# dd678faae9ac167bc83abf78e5cb2f3f0688d3a3"
print("# dd678faae9ac167bc83abf78e5cb2f3f0688d3a3")

if __name__ == "__main__":
main()
Expand Down
10 changes: 5 additions & 5 deletions pyobfuscate-install
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# pyobfuscate - Python source code obfuscator
#
# Copyright 2004-2007 Peter Astrand <[email protected]> for Cendio AB
Expand Down Expand Up @@ -28,13 +28,13 @@ def get_origin_dir():


def usage():
print >>sys.stderr, """
print("""
Install and obfuscate a Python file in one step.
Usage:
pyobfuscate-install [-m <mode>] [pyobfuscate-args] <source> <dest>
"""
""", file=sys.stderr)
sys.exit(1)


Expand All @@ -43,7 +43,7 @@ def main():
usage()

origin = get_origin_dir()
mode = 0755
mode = 0o755
rest = []
next_is_mode = False
for arg in sys.argv[1:]:
Expand All @@ -63,7 +63,7 @@ def main():

cmd = os.path.join(origin, "pyobfuscate")
cmd += " " + source + " >" + dest
print >>sys.stderr, "Calling", cmd
print("Calling", cmd, file=sys.stderr)
retcode = os.system(cmd)
os.chmod(dest, mode)
if retcode > 254:
Expand Down
4 changes: 2 additions & 2 deletions pyobfuscate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Execute the file with the same name as myself minus the extension.
# Author: Peter Astrand <[email protected]>
#
import os, sys
root, ext = os.path.splitext(sys.argv[0])
execfile(root)
exec(compile(open(root, "rb").read(), root, 'exec'))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

DESCRIPTION = """\
Python source code obfuscator
Expand Down
2 changes: 1 addition & 1 deletion test/test_args.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest

Expand Down
2 changes: 1 addition & 1 deletion test/test_classes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest

Expand Down
2 changes: 1 addition & 1 deletion test/test_globals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest

Expand Down
2 changes: 1 addition & 1 deletion test/test_lambda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest

Expand Down
6 changes: 3 additions & 3 deletions test/test_literals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest

Expand All @@ -19,8 +19,8 @@

class LiteralTest(unittest.TestCase):
def test_dictionary(self):
self.assertTrue("key1" in dictionary.keys())
self.assertTrue("key2" in dictionary.keys())
self.assertTrue("key1" in list(dictionary.keys()))
self.assertTrue("key2" in list(dictionary.keys()))
self.assertEqual(dictionary["key1"][5], "foo")
self.assertEqual(dictionary["key2"][5], "foo")

Expand Down
2 changes: 1 addition & 1 deletion test/test_locals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest

Expand Down
Loading

0 comments on commit 87cd72f

Please sign in to comment.