deprecate py.magic.autopath() and finally remove py/magic directory.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-08-27 18:46:42 +02:00
parent 13932b7f4b
commit 681d344eac
18 changed files with 150 additions and 131 deletions

View File

@ -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

View File

@ -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

View File

@ -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__)

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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", "..")

View File

@ -1 +0,0 @@
""" some nice, slightly magic APIs """

View File

@ -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

View File

@ -1 +0,0 @@
#

View File

@ -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

View File

@ -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
''')

View File

@ -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')

View File

@ -0,0 +1,3 @@
import py
import sys

View File

@ -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

View File

@ -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):

View File

@ -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)

View File

@ -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