From 681d344eac4450301ffb01b178ddc23e4df9213c Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 27 Aug 2009 18:46:42 +0200 Subject: [PATCH] deprecate py.magic.autopath() and finally remove py/magic directory. --HG-- branch : trunk --- doc/changelog.txt | 6 ++ py/__init__.py | 4 +- .../test_oldmagic.py} | 89 ++++++++++++++++++- py/code/testing/test_assertion.py | 10 --- py/code/testing/test_oldmagic.py | 64 ------------- py/execnet/testing/test_gateway.py | 2 - py/log/testing/test_warning.py | 2 +- py/magic/__init__.py | 1 - py/magic/autopath.py | 35 -------- py/magic/testing/__init__.py | 1 - py/path/local.py | 35 ++++++++ py/path/testing/fscommon.py | 4 +- py/path/testing/svntestbase.py | 3 +- py/path/testing/test_sourcepath.py | 3 + py/path/testing/test_svnwc.py | 16 ++-- py/rest/latex.py | 2 +- py/rest/testing/setup.py | 2 +- py/test/conftesthandle.py | 2 +- 18 files changed, 150 insertions(+), 131 deletions(-) rename py/{magic/testing/test_autopath.py => _testing/test_oldmagic.py} (55%) delete mode 100644 py/code/testing/test_oldmagic.py delete mode 100644 py/magic/__init__.py delete mode 100644 py/magic/autopath.py delete mode 100644 py/magic/testing/__init__.py create mode 100644 py/path/testing/test_sourcepath.py diff --git a/doc/changelog.txt b/doc/changelog.txt index 52d25b1e0..037cd49fd 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -1,6 +1,12 @@ Changes between 1.0.x and 'trunk' ===================================== +* deprecate py.magic.autopath, remove py/magic directory + +* move pytest assertion handling to py/code and a pytest_assertion + plugin, add "--no-assert" option, deprecate py.magic namespaces + in favour of (less) py.code ones. + * consolidate and cleanup py/code classes and files * cleanup py/misc, move tests to bin-for-dist diff --git a/py/__init__.py b/py/__init__.py index ffecca12c..5f2103e67 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -113,12 +113,12 @@ initpkg(__name__, 'path.SvnAuth' : ('./path/svnwc.py', 'SvnAuth'), # some nice slightly magic APIs - 'magic.__doc__' : ('./magic/__init__.py', '__doc__'), + #'magic.__doc__' : ('./magic/__init__.py', '__doc__'), 'magic.invoke' : ('./code/oldmagic.py', 'invoke'), 'magic.revoke' : ('./code/oldmagic.py', 'revoke'), 'magic.patch' : ('./code/oldmagic.py', 'patch'), 'magic.revert' : ('./code/oldmagic.py', 'revert'), - 'magic.autopath' : ('./magic/autopath.py', 'autopath'), + 'magic.autopath' : ('./path/local.py', 'autopath'), 'magic.AssertionError' : ('./code/oldmagic2.py', 'AssertionError'), # python inspection/code-generation API diff --git a/py/magic/testing/test_autopath.py b/py/_testing/test_oldmagic.py similarity index 55% rename from py/magic/testing/test_autopath.py rename to py/_testing/test_oldmagic.py index 170fa844e..6c3386fb7 100644 --- a/py/magic/testing/test_autopath.py +++ b/py/_testing/test_oldmagic.py @@ -1,7 +1,92 @@ import py -import sys +import sys, os -class TestAutoPath: +def check_assertion(): + excinfo = py.test.raises(AssertionError, "assert 1 == 2") + s = excinfo.exconly(tryshort=True) + if not s == "assert 1 == 2": + raise ValueError("assertion not enabled: got %s" % s) + +def test_invoke_assertion(recwarn, monkeypatch): + monkeypatch.setattr(py.std.__builtin__, 'AssertionError', None) + py.magic.invoke(assertion=True) + try: + check_assertion() + finally: + py.magic.revoke(assertion=True) + recwarn.pop(DeprecationWarning) + +def test_invoke_compile(recwarn, monkeypatch): + monkeypatch.setattr(py.std.__builtin__, 'compile', None) + py.magic.invoke(compile=True) + try: + co = compile("""if 1: + def f(): + return 1 + \n""", '', 'exec') + d = {} + exec co in d + assert py.code.Source(d['f']) + finally: + py.magic.revoke(compile=True) + recwarn.pop(DeprecationWarning) + +def test_patch_revert(recwarn): + class a: + pass + py.test.raises(AttributeError, "py.magic.patch(a, 'i', 42)") + + a.i = 42 + py.magic.patch(a, 'i', 23) + assert a.i == 23 + recwarn.pop(DeprecationWarning) + py.magic.revert(a, 'i') + assert a.i == 42 + recwarn.pop(DeprecationWarning) + +def test_double_patch(recwarn): + class a: + i = 42 + assert py.magic.patch(a, 'i', 2) == 42 + recwarn.pop(DeprecationWarning) + assert py.magic.patch(a, 'i', 3) == 2 + assert a.i == 3 + assert py.magic.revert(a, 'i') == 3 + recwarn.pop(DeprecationWarning) + assert a.i == 2 + assert py.magic.revert(a, 'i') == 2 + assert a.i == 42 + +def test_valueerror(recwarn): + class a: + i = 2 + pass + py.test.raises(ValueError, "py.magic.revert(a, 'i')") + recwarn.pop(DeprecationWarning) + +def test_AssertionError(testdir): + testdir.makepyfile(""" + import py + def test_hello(recwarn): + err = py.magic.AssertionError + recwarn.pop(DeprecationWarning) + assert err is py.code._AssertionError + """) + result = testdir.runpytest() + assert "1 passed" in result.stdout.str() + +def test_autopath_deprecation(testdir): + testdir.makepyfile(""" + import py + def test_hello(recwarn): + p = py.magic.autopath() + recwarn.pop(DeprecationWarning) + assert py.path.local(__file__).dirpath() == p.dirpath() + """) + result = testdir.runpytest() + assert "1 passed" in result.stdout.str() + +class Testautopath: getauto = "from py.magic import autopath ; autopath = autopath()" def setup_class(cls): cls.root = py.test.ensuretemp(cls.__name__) diff --git a/py/code/testing/test_assertion.py b/py/code/testing/test_assertion.py index dc8dfeac3..9ecce2601 100644 --- a/py/code/testing/test_assertion.py +++ b/py/code/testing/test_assertion.py @@ -161,13 +161,3 @@ class TestView: assert codelines == ["4 + 5", "getitem('', 'join')", "setattr('x', 'y', 3)", "12 - 1"] -def test_AssertionError(testdir): - testdir.makepyfile(""" - import py - def test_hello(recwarn): - err = py.magic.AssertionError - recwarn.pop(DeprecationWarning) - assert err is py.code._AssertionError - """) - result = testdir.runpytest() - assert "1 passed" in result.stdout.str() diff --git a/py/code/testing/test_oldmagic.py b/py/code/testing/test_oldmagic.py deleted file mode 100644 index 2d20031c0..000000000 --- a/py/code/testing/test_oldmagic.py +++ /dev/null @@ -1,64 +0,0 @@ -import py - -def check_assertion(): - excinfo = py.test.raises(AssertionError, "assert 1 == 2") - s = excinfo.exconly(tryshort=True) - if not s == "assert 1 == 2": - raise ValueError("assertion not enabled: got %s" % s) - -def test_invoke_assertion(recwarn, monkeypatch): - monkeypatch.setattr(py.std.__builtin__, 'AssertionError', None) - py.magic.invoke(assertion=True) - try: - check_assertion() - finally: - py.magic.revoke(assertion=True) - recwarn.pop(DeprecationWarning) - -def test_invoke_compile(recwarn, monkeypatch): - monkeypatch.setattr(py.std.__builtin__, 'compile', None) - py.magic.invoke(compile=True) - try: - co = compile("""if 1: - def f(): - return 1 - \n""", '', 'exec') - d = {} - exec co in d - assert py.code.Source(d['f']) - finally: - py.magic.revoke(compile=True) - recwarn.pop(DeprecationWarning) - -def test_patch_revert(recwarn): - class a: - pass - py.test.raises(AttributeError, "py.magic.patch(a, 'i', 42)") - - a.i = 42 - py.magic.patch(a, 'i', 23) - assert a.i == 23 - recwarn.pop(DeprecationWarning) - py.magic.revert(a, 'i') - assert a.i == 42 - recwarn.pop(DeprecationWarning) - -def test_double_patch(recwarn): - class a: - i = 42 - assert py.magic.patch(a, 'i', 2) == 42 - recwarn.pop(DeprecationWarning) - assert py.magic.patch(a, 'i', 3) == 2 - assert a.i == 3 - assert py.magic.revert(a, 'i') == 3 - recwarn.pop(DeprecationWarning) - assert a.i == 2 - assert py.magic.revert(a, 'i') == 2 - assert a.i == 42 - -def test_valueerror(recwarn): - class a: - i = 2 - pass - py.test.raises(ValueError, "py.magic.revert(a, 'i')") - recwarn.pop(DeprecationWarning) diff --git a/py/execnet/testing/test_gateway.py b/py/execnet/testing/test_gateway.py index 137718996..d52f7ec31 100644 --- a/py/execnet/testing/test_gateway.py +++ b/py/execnet/testing/test_gateway.py @@ -2,8 +2,6 @@ from __future__ import generators import os, sys, time, signal import py from py.__.execnet import gateway -mypath = py.magic.autopath() - from py.__.execnet.register import startup_modules, getsource TESTTIMEOUT = 10.0 # seconds diff --git a/py/log/testing/test_warning.py b/py/log/testing/test_warning.py index 83610250e..638052875 100644 --- a/py/log/testing/test_warning.py +++ b/py/log/testing/test_warning.py @@ -1,5 +1,5 @@ import py -mypath = py.magic.autopath() +mypath = py.path.local(__file__).new(ext=".py") def test_forwarding_to_warnings_module(): py.test.deprecated_call(py.log._apiwarn, "1.3", "..") diff --git a/py/magic/__init__.py b/py/magic/__init__.py deleted file mode 100644 index 5f73d550b..000000000 --- a/py/magic/__init__.py +++ /dev/null @@ -1 +0,0 @@ -""" some nice, slightly magic APIs """ diff --git a/py/magic/autopath.py b/py/magic/autopath.py deleted file mode 100644 index 78bbafebe..000000000 --- a/py/magic/autopath.py +++ /dev/null @@ -1,35 +0,0 @@ -import os, sys -from py.path import local - -def autopath(globs=None): - """ return the (local) path of the "current" file pointed to by globals - or - if it is none - alternatively the callers frame globals. - - the path will always point to a .py file or to None. - the path will have the following payload: - pkgdir is the last parent directory path containing __init__.py - """ - if globs is None: - globs = sys._getframe(1).f_globals - try: - __file__ = globs['__file__'] - except KeyError: - if not sys.argv[0]: - raise ValueError, "cannot compute autopath in interactive mode" - __file__ = os.path.abspath(sys.argv[0]) - - ret = local(__file__) - if ret.ext in ('.pyc', '.pyo'): - ret = ret.new(ext='.py') - current = pkgdir = ret.dirpath() - while 1: - if current.join('__init__.py').check(): - pkgdir = current - current = current.dirpath() - if pkgdir != current: - continue - elif str(current) not in sys.path: - sys.path.insert(0, str(current)) - break - ret.pkgdir = pkgdir - return ret diff --git a/py/magic/testing/__init__.py b/py/magic/testing/__init__.py deleted file mode 100644 index 792d60054..000000000 --- a/py/magic/testing/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/py/path/local.py b/py/path/local.py index 544574734..45b445c28 100644 --- a/py/path/local.py +++ b/py/path/local.py @@ -744,3 +744,38 @@ def copychunked(src, dest): fdest.close() finally: fsrc.close() + +def autopath(globs=None): + """ (deprecated) return the (local) path of the "current" file pointed to by globals or - if it is none - alternatively the callers frame globals. + + the path will always point to a .py file or to None. + the path will have the following payload: + pkgdir is the last parent directory path containing __init__.py + """ + py.log._apiwarn("1.1", "py.magic.autopath deprecated, " + "use py.path.local(__file__) and maybe pypkgpath/pyimport().") + if globs is None: + globs = sys._getframe(1).f_globals + try: + __file__ = globs['__file__'] + except KeyError: + if not sys.argv[0]: + raise ValueError, "cannot compute autopath in interactive mode" + __file__ = os.path.abspath(sys.argv[0]) + + ret = py.path.local(__file__) + if ret.ext in ('.pyc', '.pyo'): + ret = ret.new(ext='.py') + current = pkgdir = ret.dirpath() + while 1: + if current.join('__init__.py').check(): + pkgdir = current + current = current.dirpath() + if pkgdir != current: + continue + elif str(current) not in sys.path: + sys.path.insert(0, str(current)) + break + ret.pkgdir = pkgdir + return ret + diff --git a/py/path/testing/fscommon.py b/py/path/testing/fscommon.py index 36d0d7909..3cd70e2c4 100644 --- a/py/path/testing/fscommon.py +++ b/py/path/testing/fscommon.py @@ -28,12 +28,12 @@ def setuptestfs(path): module_b = otherdir.ensure('b.py') module_b.write('stuff="got it"\n') module_c = otherdir.ensure('c.py') - module_c.write('''import py; py.magic.autopath() + module_c.write('''import py; import otherdir.a value = otherdir.a.result ''') module_d = otherdir.ensure('d.py') - module_d.write('''import py; py.magic.autopath() + module_d.write('''import py; from otherdir import a value2 = a.result ''') diff --git a/py/path/testing/svntestbase.py b/py/path/testing/svntestbase.py index 7b3cbaf5f..d8fc7b1b5 100644 --- a/py/path/testing/svntestbase.py +++ b/py/path/testing/svntestbase.py @@ -4,8 +4,7 @@ from py import path, test, process from py.__.path.testing.fscommon import CommonFSTests, setuptestfs from py.__.path import svnwc as svncommon -mypath = py.magic.autopath() -repodump = mypath.dirpath('repotest.dump') +repodump = py.path.local(__file__).dirpath('repotest.dump') def getsvnbin(): svnbin = py.path.local.sysfind('svn') diff --git a/py/path/testing/test_sourcepath.py b/py/path/testing/test_sourcepath.py new file mode 100644 index 000000000..9ca4a626a --- /dev/null +++ b/py/path/testing/test_sourcepath.py @@ -0,0 +1,3 @@ +import py +import sys + diff --git a/py/path/testing/test_svnwc.py b/py/path/testing/test_svnwc.py index 1e57b04c7..028e9dd77 100644 --- a/py/path/testing/test_svnwc.py +++ b/py/path/testing/test_svnwc.py @@ -461,9 +461,11 @@ class TestInfoSvnWCCommand: Properties Last Updated: 2006-05-23 11:54:59 +0200 (Tue, 23 May 2006) Checksum: 357e44880e5d80157cc5fbc3ce9822e3 """ - path = py.magic.autopath().dirpath().chdir() - info = InfoSvnWCCommand(output) - path.chdir() + path = py.path.local(__file__).dirpath().chdir() + try: + info = InfoSvnWCCommand(output) + finally: + path.chdir() assert info.last_author == 'jan' assert info.kind == 'file' assert info.mtime == 1149021926.0 @@ -489,9 +491,11 @@ class TestInfoSvnWCCommand: Properties Last Updated: 2006-06-02 23:45:28 +0200 (Fri, 02 Jun 2006) Checksum: 357e44880e5d80157cc5fbc3ce9822e3 """ - path = py.magic.autopath().dirpath().chdir() - info = InfoSvnWCCommand(output) - path.chdir() + path = py.path.local(__file__).dirpath().chdir() + try: + info = InfoSvnWCCommand(output) + finally: + path.chdir() assert info.last_author == 'jan' assert info.kind == 'file' assert info.mtime == 1149021926.0 diff --git a/py/rest/latex.py b/py/rest/latex.py index 863b9f422..1451d3485 100644 --- a/py/rest/latex.py +++ b/py/rest/latex.py @@ -71,7 +71,7 @@ def create_stylesheet(options, path): fill_in["heading"] = options.get("heading", "") template_file = path.join("rest.sty.template") if not template_file.check(): - template_file = py.magic.autopath().dirpath().join("rest.sty.template") + template_file = py.path.local(__file__).dirpath("rest.sty.template") return template_file.read() % fill_in def process_configfile(configfile, debug=False): diff --git a/py/rest/testing/setup.py b/py/rest/testing/setup.py index dc1cbaa98..e0e039f93 100644 --- a/py/rest/testing/setup.py +++ b/py/rest/testing/setup.py @@ -1,7 +1,7 @@ import py pydir = py.path.local(py.__file__).dirpath() -mydatadir = py.magic.autopath().dirpath().join("data") +mydatadir = py.path.local(__file__).dirpath('data') def getdata(): rel = mydatadir.relto(pydir) diff --git a/py/test/conftesthandle.py b/py/test/conftesthandle.py index 7c172b5de..4326c3789 100644 --- a/py/test/conftesthandle.py +++ b/py/test/conftesthandle.py @@ -1,5 +1,5 @@ import py -defaultconftestpath = py.magic.autopath().dirpath('defaultconftest.py') +defaultconftestpath = py.path.local(__file__).dirpath("defaultconftest.py") class Conftest(object): """ the single place for accessing values and interacting