Re: compiler module bug?

2007-10-21 Thread Brian Blais

On Oct 21, 2007, at Oct 21:2:05 PM, Gabriel Genellina wrote:


The parseFile function does exactly that, along with this comment:


thanks!



bb
--
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compiler module bug?

2007-10-21 Thread Gabriel Genellina
En Sun, 21 Oct 2007 14:33:00 -0300, Brian Blais <[EMAIL PROTECTED]>  
escribió:

> On Oct 21, 2007, at Oct 21:1:15 PM, Gabriel Genellina wrote:

>> The comment itself is not a problem; but the last line in the
>> source must
>> end in a newline.
>
> then, even if it is known problem, the docs are wrong.  the two
> (parseFile(path) and parse(open(path).read())) are *not* the same:
> one can handle a file which doesn't end in a newline, another one
> can't handle the same file.

Feel free to submit a bug report to http://bugs.python.org

> Can one hack it like:
>
>  filestr=open(filename).read()
>  filestr+="\n"

The parseFile function does exactly that, along with this comment:

 # XXX The parser API tolerates files without a trailing newline,
 # but not strings without a trailing newline.  Always add an extra
 # newline to the file contents, since we're going through the string
 # version of the API.

The compile function in py_compile.py that I've menctioned earlier does  
the same but only when needed:

 f = open(file, 'U')
 codestring = f.read()
 f.close()
 if codestring and codestring[-1] != '\n':
 codestring = codestring + '\n'

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: compiler module bug?

2007-10-21 Thread Brian Blais

On Oct 21, 2007, at Oct 21:1:15 PM, Gabriel Genellina wrote:


En Sun, 21 Oct 2007 13:36:46 -0300, Brian Blais <[EMAIL PROTECTED]>
escribi�:


I am experiencing a problem with the compiler module.  Is this a bug,
or am I doing something wrong?


I think it's a well-known fact...


it seems like a comment at the end breaks the parse command, but not
parseFile.  Is this reproducible by others?


The comment itself is not a problem; but the last line in the  
source must

end in a newline.
See the py_compile/compileall modules as an example.



I didn't see anything in that module which mentions this issue  
(perhaps I missed it).  however, if the documentation states:


parseFile(
path)
Return an abstract syntax tree for the Python source code in the file  
specified by path. It is equivalent to parse(open(path).read()).


then, even if it is known problem, the docs are wrong.  the two  
(parseFile(path) and parse(open(path).read())) are *not* the same:  
one can handle a file which doesn't end in a newline, another one  
can't handle the same file.


Can one hack it like:

filestr=open(filename).read()
filestr+="\n"

before going to parse, or will that give problems?

thanks,

bb


--
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compiler module bug?

2007-10-21 Thread Gabriel Genellina
En Sun, 21 Oct 2007 13:36:46 -0300, Brian Blais <[EMAIL PROTECTED]>  
escribi�:

> I am experiencing a problem with the compiler module.  Is this a bug,
> or am I doing something wrong?

I think it's a well-known fact...

> it seems like a comment at the end breaks the parse command, but not
> parseFile.  Is this reproducible by others?

The comment itself is not a problem; but the last line in the source must  
end in a newline.
See the py_compile/compileall modules as an example.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

compiler module bug?

2007-10-21 Thread Brian Blais

Hello,

I am experiencing a problem with the compiler module.  Is this a bug,  
or am I doing something wrong?


so I have the following code:

from compiler import parse, parseFile

filename='blah.py'

if False:  # turn this to false, to get it to work
ast = parse(open(filename).read())   # this line gives a syntax  
error

else:
ast = parseFile(filename)  # this line works


print ast


the documentation, here http://docs.python.org/lib/module- 
compiler.html  says that the two lines with parse and parseFile are  
equivalent.


the file I am trying to parse is simply:

def main():

a=10

# comment at the end of the file




it seems like a comment at the end breaks the parse command, but not  
parseFile.  Is this reproducible by others?


am I doing something wrong?

thanks,

Brian Blais



--
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais



-- 
http://mail.python.org/mailman/listinfo/python-list