Skip to content

Commit

Permalink
Fixed for Python 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dabeaz committed Feb 18, 2011
1 parent 124f030 commit 1e114fe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
65 changes: 45 additions & 20 deletions test/testlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@
import io as StringIO

import sys
import os
import imp
import warnings

sys.path.insert(0,"..")
sys.tracebacklimit = 0

import ply.lex

def make_pymodule_path(filename):
path = os.path.dirname(filename)
file = os.path.basename(filename)
mod, ext = os.path.splitext(file)

if sys.hexversion >= 0x3020000:
modname = mod+"."+imp.get_tag()+ext
fullpath = os.path.join(path,'__pycache__',modname)
else:
fullpath = filename
return fullpath

def pymodule_out_exists(filename):
return os.path.exists(make_pymodule_path(filename))

def pymodule_out_remove(filename):
os.remove(make_pymodule_path(filename))

def check_expected(result,expected):
if sys.version_info[0] >= 3:
if isinstance(result,str):
Expand Down Expand Up @@ -40,6 +62,9 @@ class LexErrorWarningTests(unittest.TestCase):
def setUp(self):
sys.stderr = StringIO.StringIO()
sys.stdout = StringIO.StringIO()
if sys.hexversion >= 0x3020000:
warnings.filterwarnings('ignore',category=ResourceWarning)

def tearDown(self):
sys.stderr = sys.__stderr__
sys.stdout = sys.__stdout__
Expand Down Expand Up @@ -325,27 +350,27 @@ def test_lex_optimize(self):
"(NUMBER,3,1,0)\n"
"(PLUS,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("lextab.pyo"))
self.assert_(pymodule_out_exists("lextab.pyo"))

os.remove("lextab.pyo")
pymodule_out_remove("lextab.pyo")
p = subprocess.Popen([sys.executable,'-OO','lex_optimize.py'],
stdout=subprocess.PIPE)
result = p.stdout.read()
self.assert_(check_expected(result,
"(NUMBER,3,1,0)\n"
"(PLUS,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("lextab.pyo"))
self.assert_(pymodule_out_exists("lextab.pyo"))
try:
os.remove("lextab.py")
except OSError:
pass
try:
os.remove("lextab.pyc")
pymodule_out_remove("lextab.pyc")
except OSError:
pass
try:
os.remove("lextab.pyo")
pymodule_out_remove("lextab.pyo")
except OSError:
pass

Expand Down Expand Up @@ -377,26 +402,26 @@ def test_lex_optimize2(self):
"(NUMBER,3,1,0)\n"
"(PLUS,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("opt2tab.pyo"))
os.remove("opt2tab.pyo")
self.assert_(pymodule_out_exists("opt2tab.pyo"))
pymodule_out_remove("opt2tab.pyo")
p = subprocess.Popen([sys.executable,'-OO','lex_optimize2.py'],
stdout=subprocess.PIPE)
result = p.stdout.read()
self.assert_(check_expected(result,
"(NUMBER,3,1,0)\n"
"(PLUS,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("opt2tab.pyo"))
self.assert_(pymodule_out_exists("opt2tab.pyo"))
try:
os.remove("opt2tab.py")
except OSError:
pass
try:
os.remove("opt2tab.pyc")
pymodule_out_remove("opt2tab.pyc")
except OSError:
pass
try:
os.remove("opt2tab.pyo")
pymodule_out_remove("opt2tab.pyo")
except OSError:
pass

Expand Down Expand Up @@ -425,16 +450,16 @@ def test_lex_optimize3(self):
"(NUMBER,3,1,0)\n"
"(PLUS,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("lexdir/sub/calctab.pyo"))
os.remove("lexdir/sub/calctab.pyo")
self.assert_(pymodule_out_exists("lexdir/sub/calctab.pyo"))
pymodule_out_remove("lexdir/sub/calctab.pyo")
p = subprocess.Popen([sys.executable,'-OO','lex_optimize3.py'],
stdout=subprocess.PIPE)
result = p.stdout.read()
self.assert_(check_expected(result,
"(NUMBER,3,1,0)\n"
"(PLUS,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("lexdir/sub/calctab.pyo"))
self.assert_(pymodule_out_exists("lexdir/sub/calctab.pyo"))
try:
shutil.rmtree("lexdir")
except OSError:
Expand Down Expand Up @@ -468,26 +493,26 @@ def test_lex_opt_alias(self):
"(NUMBER,3,1,0)\n"
"(+,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("aliastab.pyo"))
os.remove("aliastab.pyo")
self.assert_(pymodule_out_exists("aliastab.pyo"))
pymodule_out_remove("aliastab.pyo")
p = subprocess.Popen([sys.executable,'-OO','lex_opt_alias.py'],
stdout=subprocess.PIPE)
result = p.stdout.read()
self.assert_(check_expected(result,
"(NUMBER,3,1,0)\n"
"(+,'+',1,1)\n"
"(NUMBER,4,1,2)\n"))
self.assert_(os.path.exists("aliastab.pyo"))
self.assert_(pymodule_out_exists("aliastab.pyo"))
try:
os.remove("aliastab.py")
except OSError:
pass
try:
os.remove("aliastab.pyc")
pymodule_out_remove("aliastab.pyc")
except OSError:
pass
try:
os.remove("aliastab.pyo")
pymodule_out_remove("aliastab.pyo")
except OSError:
pass

Expand Down Expand Up @@ -531,8 +556,8 @@ def test_lex_many_tokens(self):
"(TOK999,'TOK999:',1,47)\n"
))

self.assert_(os.path.exists("manytab.pyo"))
os.remove("manytab.pyo")
self.assert_(pymodule_out_exists("manytab.pyo"))
pymodule_out_remove("manytab.pyo")
try:
os.remove("manytab.py")
except OSError:
Expand Down
27 changes: 25 additions & 2 deletions test/testyacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,32 @@

import sys
import os
import warnings

sys.path.insert(0,"..")
sys.tracebacklimit = 0

import ply.yacc
import imp

def make_pymodule_path(filename):
path = os.path.dirname(filename)
file = os.path.basename(filename)
mod, ext = os.path.splitext(file)

if sys.hexversion >= 0x3020000:
modname = mod+"."+imp.get_tag()+ext
fullpath = os.path.join(path,'__pycache__',modname)
else:
fullpath = filename
return fullpath

def pymodule_out_exists(filename):
return os.path.exists(make_pymodule_path(filename))

def pymodule_out_remove(filename):
os.remove(make_pymodule_path(filename))


def check_expected(result,expected):
resultlines = []
Expand Down Expand Up @@ -43,10 +64,13 @@ def setUp(self):
sys.stdout = StringIO.StringIO()
try:
os.remove("parsetab.py")
os.remove("parsetab.pyc")
pymodule_out_remove("parsetab.pyc")
except OSError:
pass

if sys.hexversion >= 0x3020000:
warnings.filterwarnings('ignore',category=ResourceWarning)

def tearDown(self):
sys.stderr = sys.__stderr__
sys.stdout = sys.__stdout__
Expand Down Expand Up @@ -297,7 +321,6 @@ def test_yacc_unused_rule(self):
def test_yacc_uprec(self):
self.assertRaises(ply.yacc.YaccError,run_import,"yacc_uprec")
result = sys.stderr.getvalue()
print repr(result)
self.assert_(check_expected(result,
"yacc_uprec.py:37: Nothing known about the precedence of 'UMINUS'\n"
))
Expand Down

0 comments on commit 1e114fe

Please sign in to comment.