[py-svn] r57047 - in py/branch/event/py/code: . testing
hpk at codespeak.net
hpk at codespeak.net
Thu Aug 7 08:29:05 CEST 2008
Author: hpk
Date: Thu Aug 7 08:29:03 2008
New Revision: 57047
Modified:
py/branch/event/py/code/code.py
py/branch/event/py/code/source.py
py/branch/event/py/code/testing/test_code.py
py/branch/event/py/code/testing/test_source.py
Log:
streamline representation of paths from generated code objects (finally)
Modified: py/branch/event/py/code/code.py
==============================================================================
--- py/branch/event/py/code/code.py (original)
+++ py/branch/event/py/code/code.py Thu Aug 7 08:29:03 2008
@@ -59,12 +59,13 @@
def path(self):
""" return a py.path.local object wrapping the source of the code """
+ fn = self.raw.co_filename
try:
- return self.raw.co_filename.__path__
+ return fn.__path__
except AttributeError:
p = py.path.local(self.raw.co_filename)
- if not p.check() and self.raw.co_filename == "<string>":
- p = "<string>"
+ if not p.check():
+ p = self.raw.co_filename
return p
path = property(path, None, None, "path of this code object")
Modified: py/branch/event/py/code/source.py
==============================================================================
--- py/branch/event/py/code/source.py (original)
+++ py/branch/event/py/code/source.py Thu Aug 7 08:29:03 2008
@@ -166,16 +166,20 @@
def __str__(self):
return "\n".join(self.lines)
- def compile(self, filename=None, mode='exec',
- flag=generators.compiler_flag, dont_inherit=0):
+ def compile(self, filename=None, mode='exec', flag=generators.compiler_flag,
+ dont_inherit=0, _genframe=None):
""" return compiled code object. if filename is None
invent an artificial filename which displays
the source/line position of the caller frame.
"""
if not filename or py.path.local(filename).check(file=0):
- frame = sys._getframe(1) # the caller
- filename = '%s<%s:%d>' % (filename, frame.f_code.co_filename,
- frame.f_lineno)
+ if _genframe is None:
+ _genframe = sys._getframe(1) # the caller
+ fn,lineno = _genframe.f_code.co_filename, _genframe.f_lineno
+ if not filename:
+ filename = '<codegen %s:%d>' % (fn, lineno)
+ else:
+ filename = '<codegen %r %s:%d>' % (filename, fn, lineno)
source = "\n".join(self.lines) + '\n'
try:
co = cpy_compile(source, filename, mode, flag)
@@ -209,13 +213,14 @@
also have this special subclass-of-string
filename.
"""
+ _genframe = sys._getframe(1) # the caller
s = Source(source)
- co = s.compile(filename, mode, flags)
+ co = s.compile(filename, mode, flags, _genframe=_genframe)
return co
#
-# various helper functions
+# helper functions
#
class MyStr(str):
""" custom string which allows to add attributes. """
Modified: py/branch/event/py/code/testing/test_code.py
==============================================================================
--- py/branch/event/py/code/testing/test_code.py (original)
+++ py/branch/event/py/code/testing/test_code.py Thu Aug 7 08:29:03 2008
@@ -70,3 +70,10 @@
co.co_name, co.co_firstlineno, co.co_lnotab,
co.co_freevars, co.co_cellvars)
assert c2.co_filename is filename
+
+def test_code_gives_back_name_for_not_existing_file():
+ name = 'abc-123'
+ co_code = compile("pass\n", name, 'exec')
+ assert co_code.co_filename == name
+ code = py.code.Code(co_code)
+ assert str(code.path) == name
Modified: py/branch/event/py/code/testing/test_source.py
==============================================================================
--- py/branch/event/py/code/testing/test_source.py (original)
+++ py/branch/event/py/code/testing/test_source.py Thu Aug 7 08:29:03 2008
@@ -199,6 +199,24 @@
#print "block", str(block)
assert str(stmt).strip().startswith('assert')
+ def test_compilefuncs_and_path_sanity(self):
+ def check(comp, name):
+ co = comp(self.source, name)
+ if not name:
+ expected = "<codegen %s:%d>" %(mypath, mylineno+2+1)
+ else:
+ expected = "<codegen %r %s:%d>" % (name, mypath, mylineno+2+1)
+ fn = co.co_filename
+ assert fn == expected
+
+ mycode = py.code.Code(self.test_compilefuncs_and_path_sanity)
+ mylineno = mycode.firstlineno
+ mypath = mycode.path
+
+ for comp in py.code.compile, py.code.Source.compile:
+ for name in '', None, 'my':
+ yield check, comp, name
+
def test_offsetless_synerr(self):
py.test.raises(SyntaxError, py.code.compile, "lambda a,a: 0", mode='eval')
More information about the pytest-commit
mailing list