testing py files outside the tree

5 views
Skip to first unread message

John Cremona

unread,
Mar 31, 2009, 12:34:37 PM3/31/09
to SAGE devel
Suppose I have just written a whole lot of new functions in a .py file
outside the main source tree, complete with doctests. How can I test
these using sage -t, given that the file needs to be loaded or
attached for the functions to be visible? Do I have to actually put
the file somewhere in the source tree and add it to the appropriate
all.py before I can do this?

John

William Stein

unread,
Mar 31, 2009, 12:46:08 PM3/31/09
to sage-...@googlegroups.com

Remember that bug with absolute paths and "sage -t" that David
Loeffler mentioned recently? Well in sage-3.4.1.alpha0, at least, if
you doctest foo.py outside the tree, then "from foo import *" is done
first. E.g.,

wstein@sage:~/build/sage-3.4.1.alpha0$ more a.py
def f(n):
"""
sage: print f(3)
5
"""
return 4
wstein@sage:~/build/sage-3.4.1.alpha0$ ./sage -t a.py
sage -t "a.py"
**********************************************************************
File "/scratch/wstein/build/sage-3.4.1.alpha0/a.py", line 3:
sage: print f(3)
Expected:
5
Got:
4
**********************************************************************
1 items had failures:
1 of 3 in __main__.example_0
***Test Failed*** 1 failures.
For whitespace errors, see the file ./.doctest_a.py
[1.0 s]
exit code: 1024

----------------------------------------------------------------------
The following tests failed:


sage -t "a.py"
Total time for all tests: 1.0 seconds
wstein@sage:~/build/sage-3.4.1.alpha0$

Notice above that f *is* defined!

william

Florent Hivert

unread,
Mar 31, 2009, 1:22:28 PM3/31/09
to sage-...@googlegroups.com
Dear All,

> Remember that bug with absolute paths and "sage -t" that David
> Loeffler mentioned recently? Well in sage-3.4.1.alpha0, at least, if
> you doctest foo.py outside the tree, then "from foo import *" is done
> first. E.g.,

Please have a look at <http://wiki.sagemath.org/combinat/HelpOnTheDoc>

In particular, I tried to address this question in
4. 1.: How to use "sage -t" outside sage tree

It seems that you last comment shows that my page is outdated. If so please
correct the page or tell me how to do it.

By the way, since they are a lot of people asking question about the doc, this
page should definitely by more apparent. Here is an excerpt of the page:

It has been said on the mailing list that this page is of general interest
for the sage community and should not stay hidden inside combinat. I would
be happy to move it elsewhere, if someone suggest me a proper place and
from where to link it. If you move it, please don't forget to update the
link at the bottom of the sage-combinat home page -- Florent.

I would be happy to address this question if someone answer.

Or I can remain the only reader of this page :) Or maybe it's the page is only
wrong... If so please tell me.

Cheers,

Florent

John Cremona

unread,
Mar 31, 2009, 1:55:30 PM3/31/09
to sage-...@googlegroups.com
thanks! As you can tell, I had not actually tried it as I assumed it
would not work!

John

2009/3/31 William Stein <wst...@gmail.com>:

John Cremona

unread,
Mar 31, 2009, 1:57:11 PM3/31/09
to sage-...@googlegroups.com
That page looks very useful -- why not move it so that it is not under combinat?

John

2009/3/31 Florent Hivert <florent...@univ-rouen.fr>:

Florent Hivert

unread,
Mar 31, 2009, 2:11:00 PM3/31/09
to sage-...@googlegroups.com
Dear John,

> That page looks very useful -- why not move it so that it is not under combinat?

[...]

> > By the way, since they are a lot of people asking question about the doc, this
> > page should definitely by more apparent. Here is an excerpt of the page:
> >
> >    It has been said on the mailing list that this page is of general interest
> >    for the sage community and should not stay hidden inside combinat. I would
> >    be happy to move it elsewhere, if someone suggest me a proper place and
> >    from where to link it. If you move it, please don't forget to update the
> >    link at the bottom of the sage-combinat home page -- Florent.
> >
> > I would be happy to address this question if someone answer.

... :)

Cheers,

Florent

Georg S. Weber

unread,
Mar 31, 2009, 3:34:30 PM3/31/09
to sage-devel
Hi Florent,

personally, I would have searched the "Sage Developers Guide" for
information on how "sage -t" works. And if there is nothing to find,
I'd look directly at the code (.../local/bin/sage-test). Admittedly, I
rarely use the Wiki, because I do not feel comfortable with it --- nor
do I always have internet access when working with Sage.

But a local Sage install does include a lot of eye-candy documentation
I like to use.

Proposal:
Do move (better still: copy) the contents of your Wiki-entry to the
"Sage Developers Guide".


Cheers,
gsw

Florent Hivert

unread,
Mar 31, 2009, 4:22:36 PM3/31/09
to sage-...@googlegroups.com
Dear Georg,

> personally, I would have searched the "Sage Developers Guide" for
> information on how "sage -t" works. And if there is nothing to find,
> I'd look directly at the code (.../local/bin/sage-test).

So would I. The wiki is only temporary... See my comments below.

> Admittedly, I
> rarely use the Wiki, because I do not feel comfortable with it --- nor
> do I always have internet access when working with Sage.
>
> But a local Sage install does include a lot of eye-candy documentation
> I like to use.
>
> Proposal:
> Do move (better still: copy) the contents of your Wiki-entry to the
> "Sage Developers Guide".

That's the ultimate goal. However, I don't have a broad view of the doc
system. Right now the page is a only a bunch of tips and tricks gathered on
irc and this very mailing list. It is not at all in a stable state and I don't
feel expert enough to write such a text in the guide, not speaking about my
bad English. In particular, I'd like some expert of the build system and
ReST/sphinx to check that the tips I give are correct. Moreover, aside for the
tricks in my opinion the most important thing on the page are link to short
and extended description on the format that I don't want to (even can't ?)
copy in the Devel Guide.

I was hoping that starting the wiki page, I could get some help on expanding
it, or at least get some re-reader. I have chosen the wiki as a communication
mean to build such a doc because anyone can edit and improve it. But it seems
that as a consequence no-one does :) The trac / patch system seems not to be
interactive enough... Any suggestion ?

Cheers,

Florent

Georg S. Weber

unread,
Apr 1, 2009, 3:15:44 AM4/1/09
to sage-devel
Hi Florent,

On 31 Mrz., 22:22, Florent Hivert <florent.hiv...@univ-rouen.fr>
wrote:
From past experience, this is a kind of problem that has been solved
at some Sage Day, there even has been a "Sage Doc Day" as far as I
remember. So the strategy would be to wait for the next Sage day (No.
15?!), and to ping the participants at the right moment, so the their
attention will focus on this topic.

Cheers,
gsw
> Cheers,
>
> Florent

John Cremona

unread,
Apr 1, 2009, 7:13:52 AM4/1/09
to sage-...@googlegroups.com
Unfortunately this does not work for me:

masgaj@host-56-150%pwd
/home/masgaj/sage
masgaj@host-56-150%ls -l egros.py
-rw------- 1 masgaj ma 16242 2009-04-01 12:09 egros.py
masgaj@host-56-150%sage -t /home/masgaj/sage/egros.py
sage -t "/home/masgaj/sage/egros.py"
File "./egros.py", line 18
from /home/masgaj/sage/egros import *
^
SyntaxError: invalid syntax

[0.2 s]
exit code: 1024

----------------------------------------------------------------------
The following tests failed:


sage -t "/home/masgaj/sage/egros.py"
Total time for all tests: 0.2 seconds

If I give just the file name (as in : sage -t egros.py) then it is
even worse, starting with
NameError: global name 'QQ' is not defined

This is with 3.4.1.alpha0 after cloning and applying one patch.

John

2009/3/31 William Stein <wst...@gmail.com>:
>

simon...@uni-jena.de

unread,
Apr 1, 2009, 7:20:17 AM4/1/09
to sage-devel
Dear John,

when i had problems with doc testing outside the path, someone
(mabshoff?) pointed out that i need to do
export SAGEPATH=`pwd`
in the directory where the to-be-tested files are (in my case, it
was .pyx, not .py).

Your problem sounds related, so perhaps SAGEPATH helps.

Cheers,
Simon

John Cremona

unread,
Apr 1, 2009, 7:25:25 AM4/1/09
to sage-...@googlegroups.com
thanks for the suggestion, but unfortunately that makes no difference for me.

John

2009/4/1 <simon...@uni-jena.de>:

Burcin Erocal

unread,
Apr 1, 2009, 7:32:56 AM4/1/09
to sage-...@googlegroups.com
On Wed, 1 Apr 2009 12:25:25 +0100
John Cremona <john.c...@gmail.com> wrote:

>
> thanks for the suggestion, but unfortunately that makes no difference
> for me.

SAGE_PATH should work. I think this was also the tip on the wiki
page Florent was referring to.

Cheers,
Burcin

John Cremona

unread,
Apr 1, 2009, 7:44:17 AM4/1/09
to sage-...@googlegroups.com
2009/4/1 Burcin Erocal <bur...@erocal.org>:
>
> On Wed, 1 Apr 2009 12:25:25 +0100
> John Cremona <john.c...@gmail.com> wrote:
>
>>
>> thanks for the suggestion, but unfortunately that makes no difference
>> for me.
>
> SAGE_PATH should work. I think this was also the tip on the wiki
> page Florent was referring to.
>

I had already tried that variation. No luck!

masgaj@host-56-150%echo $SAGE_PATH
/home/masgaj/sage
masgaj@host-56-150%pwd
/home/masgaj/sage
masgaj@host-56-150%sage -t /home/masgaj/sage/egros.py
sage -t "/home/masgaj/sage/egros.py"
File "./egros.py", line 18
from /home/masgaj/sage/egros import *
^
SyntaxError: invalid syntax

[0.3 s]
exit code: 1024

----------------------------------------------------------------------
The following tests failed:


sage -t "/home/masgaj/sage/egros.py"
Total time for all tests: 0.3 seconds

masgaj@host-56-150%sage -v
| Sage Version 3.4.1.alpha0, Release Date: 2009-03-26 |


John

Joel B. Mohler

unread,
Apr 1, 2009, 8:30:21 AM4/1/09
to sage-...@googlegroups.com
On Wednesday 01 April 2009 07:44:17 am John Cremona wrote:
> I had already tried that variation.  No luck!
>
> masgaj@host-56-150%echo $SAGE_PATH
> /home/masgaj/sage
> masgaj@host-56-150%pwd
> /home/masgaj/sage
> masgaj@host-56-150%sage -t /home/masgaj/sage/egros.py
> sage -t  "/home/masgaj/sage/egros.py"
>   File "./egros.py", line 18
>     from /home/masgaj/sage/egros import *
>          ^
> SyntaxError: invalid syntax

What about?:

masgaj@host-56-150%cd /home/masgaj/sage
masgaj@host-56-150%sage -t egros.py

You can't specify paths in python imports even though it feels like a logical
thing to do. Perhaps 'sage -t' should handle a full path name by doing
a 'cd' and trimming the path off the file name.

--
Joel

John Cremona

unread,
Apr 1, 2009, 9:15:26 AM4/1/09
to sage-...@googlegroups.com
2009/4/1 Joel B. Mohler <jo...@kiwistrawberry.us>:
>
> On Wednesday 01 April 2009 07:44:17 am John Cremona wrote:
>> I had already tried that variation. No luck!
>>
>> masgaj@host-56-150%echo $SAGE_PATH
>> /home/masgaj/sage
>> masgaj@host-56-150%pwd
>> /home/masgaj/sage
>> masgaj@host-56-150%sage -t /home/masgaj/sage/egros.py
>> sage -t "/home/masgaj/sage/egros.py"
>> File "./egros.py", line 18
>> from /home/masgaj/sage/egros import *
>> ^
>> SyntaxError: invalid syntax
>
> What about?:
>
> masgaj@host-56-150%cd /home/masgaj/sage
> masgaj@host-56-150%sage -t egros.py
>

I had aready tried that too, as reported earlier: the result is much
worse since it looks as if none os sage is getting imported at all,
the first failure being NameError: global name 'QQ' is not defined

PROBLEM SOLVED!! I added the line

from sage.all import *

at the top of the .py file and now all is well. I'm sure that all the
helpful people who have been firing suggestion at me cannot believe
that I did not know that I had to do that.

Summary instructions to test a stand-alone .py file, say test.py:

1. No need to set any environment variables
2. Put the line
from sage.all import *
at the top of the file
3. cd to the directory the file is in
4. sage -t test.py

John

William Stein

unread,
Apr 1, 2009, 9:17:57 AM4/1/09
to sage-...@googlegroups.com

Yes. The "sage -t foo.py" thing only does "from foo import *" -- it
doesn't import the whole Sage library too. Maybe it should? That
would be easy to add, I think.

William

John Cremona

unread,
Apr 1, 2009, 9:26:36 AM4/1/09
to sage-...@googlegroups.com
2009/4/1 William Stein <wst...@gmail.com>:
>

>
> Yes. The "sage -t foo.py" thing only does "from foo import *" -- it
> doesn't import the whole Sage library too. Maybe it should? That
> would be easy to add, I think.

Perhaps not, as people might want to test python stuff without importing sage?

Incidentally I started out giving this file the suffix .sage instead
of .py. Like that, you do not need to add the exatr import line, but
you do get a warning:

/local/jec/sage-3.4.1.alpha0/local/bin/sage-preparse: File file_name is missing

which seems harmless. The (only) reason I switched to a .py suffix
was that I could not get sage-mode to work in emacs....but that is a
different thread!

John

>
> William

Nick Alexander

unread,
Apr 1, 2009, 10:44:49 AM4/1/09
to sage-...@googlegroups.com
> which seems harmless. The (only) reason I switched to a .py suffix
> was that I could not get sage-mode to work in emacs....but that is a
> different thread!

Please explain.

Nick

Jason Grout

unread,
Apr 1, 2009, 4:24:04 PM4/1/09
to sage-...@googlegroups.com


+1 on making sage -t my_file.py work with minimal hassle.

Jason

John Cremona

unread,
Apr 1, 2009, 5:27:19 PM4/1/09
to sage-...@googlegroups.com
2009/4/1 Jason Grout <jason...@creativetrax.com>:
I added this to the wiki tips page referred to earlier in this thread.

John

>
> Jason
>
>
> >
>

John Cremona

unread,
Apr 3, 2009, 4:36:33 AM4/3/09
to sage-...@googlegroups.com
2009/4/1 Nick Alexander <ncale...@gmail.com>:
I went through various attempts a few days ago. Currently I have
these relevant files in ~/.emacs:

(add-to-list 'load-path (expand-file-name "/home/masgaj/lisp"))
(require 'python)
(require 'pyrex "pyrex-mode")
(require 'sage "sage-mode")
(setq sage-command "/home/masgaj/bin/sage")

and in /home/masgaj/lisp I have

magma-mode.el
magma-mode.elc
pari2-51.el.in
pari.el
pari.elc
pariemacs2-51.txt
pyrex-mode.el
pyrex-mode.elc
python.el
python.el~
sage-build.el
sage-build.elc
sage.el
sage.elc
sage-flymake.el
sage-flymake.elc
sage-imenu.el
sage-imenu.elc
sage-load.el
sage-load.elc
sage-mode.el
sage-test.el
sage-view.el
sli-tools0-9.el
sli-tools0-9.elc

Now, when I start emacs I get an error message:


Wrong number of arguments: #[(regexp) "\301\302 DD\207" [regexp
rx-to-string quote] 3 ("/usr/share/emacs/21.3/lisp/emacs-lisp/rx.elc"
. 11482)], 3

To ensure normal operation, you should investigate the cause
of the error in your initialization file and remove it. Start
Emacs with the `--debug-init' option to view a complete error
backtrace

Editing .py files is fine (it says "using the CPYthon shell" and is in
python mode), but editing .sage files is just in text mode.

I think it does work to tell emacs to use python mode for all .sage
files, but that would not involve installing anything. I have no
interest in (for example) starting up sage from within emacs, or
runing sage in an emacs shell.

John


>
> Nick
>
> >
>

Nick Alexander

unread,
Apr 3, 2009, 11:43:23 AM4/3/09
to sage-...@googlegroups.com

On 3-Apr-09, at 1:36 AM, John Cremona wrote:

>
> 2009/4/1 Nick Alexander <ncale...@gmail.com>:
>>
>>> which seems harmless. The (only) reason I switched to a .py suffix
>>> was that I could not get sage-mode to work in emacs....but that is a
>>> different thread!
>>
>> Please explain.
>
> I went through various attempts a few days ago.

Could you copy me the output of `M-x version' and tell me what version
of sage-mode you have installed? (It looks like you have not used the
standard spkg install, which should be okay, but...) Also, could you
check the *Messages* buffer after you start up and verify that the
problem really is coming from a sage-*.el file.

Nick

John Cremona

unread,
Apr 3, 2009, 11:55:08 AM4/3/09
to sage-...@googlegroups.com
2009/4/3 Nick Alexander <ncale...@gmail.com>:
>
>
> On 3-Apr-09, at 1:36 AM, John Cremona wrote:
>
>>
>> 2009/4/1 Nick Alexander <ncale...@gmail.com>:
>>>
>>>> which seems harmless. The (only) reason I switched to a .py suffix
>>>> was that I could not get sage-mode to work in emacs....but that is a
>>>> different thread!
>>>
>>> Please explain.
>>
>> I went through various attempts a few days ago.
>
> Could you copy me the output of `M-x version' and tell me what version

GNU Emacs 21.3.1 (i586-suse-linux, X toolkit, Xaw3d scroll bars) of
2008-05-16 on stravinsky

> of sage-mode you have installed? (It looks like you have not used the

sage-mode-0.5.4.spkg (I think: there is a directory with that name in
my home directory).

I don't understand why installing some emacs lisp files needs to be
done with an spkg, but I'm sure there is a reason! I hope taht does
not mean that I would have to reinstall something with every new Sage
version.


> standard spkg install, which should be okay, but...) Also, could you
> check the *Messages* buffer after you start up and verify that the
> problem really is coming from a sage-*.el file.

Here is the complete buffer after stating up:

(emacs)
Loading tool-bar...done
Loading image...done
Loading tooltip...done
Loading /usr/lib/emacs/21.3/i586-suse-linux/fns-21.3.1.el (source)...done
Loading paren...done
Loading encoded-kb...done
Loading time...done
Loading server...done
Loading time-stamp...done
Loading /home/masgaj/lisp/magma-mode.elc...done
Loading rx...done


An error has occurred while loading `/home/masgaj/.emacs':

Wrong number of arguments: #[(regexp) "\301\302 DD\207" [regexp
rx-to-string quote] 3 ("/usr/share/emacs/21.3/lisp/emacs-lisp/rx.elc"
. 11482)], 3

To ensure normal operation, you should investigate the cause
of the error in your initialization file and remove it. Start
Emacs with the `--debug-init' option to view a complete error
backtrace
Loading mwheel...done

This happens if I have either
(require 'python)
or
(require 'sage "sage-mode")
in my .emacs.


Thanks,

John

>
> Nick
>
> >
>

Nick Alexander

unread,
Apr 3, 2009, 12:29:54 PM4/3/09
to sage-...@googlegroups.com
> Loading rx...done
>
>
> An error has occurred while loading `/home/masgaj/.emacs':
>
> Wrong number of arguments: #[(regexp) "\301\302 DD\207" [regexp
> rx-to-string quote] 3 ("/usr/share/emacs/21.3/lisp/emacs-lisp/rx.elc"
> . 11482)], 3
>
> To ensure normal operation, you should investigate the cause
> of the error in your initialization file and remove it. Start
> Emacs with the `--debug-init' option to view a complete error
> backtrace
> Loading mwheel...done
>
> This happens if I have either
> (require 'python)

sage-mode inherits from python-mode; if you can't use python-mode (due
to a broken rx module) I can't do much to help you. GNU emacs 21.3 is
relatively old; is there a possibility of upgrading to a 22.x build?
(22.x is considered stable now and offers numerous benefits.)

Nick

John Cremona

unread,
Apr 3, 2009, 12:34:29 PM4/3/09
to sage-...@googlegroups.com
2009/4/3 Nick Alexander <ncale...@gmail.com>:
python mode seems to work fine (no problem in editing .py files).

The arrangement we have with departmental desktop computers is that we
do not have root access and upgrading stuff is not an option.

It's ok, as I can use other machines in the department, and at home I
have no similar problems on my laptop which is more up-to-date.

thanks for your help,

John

>
> Nick
>
> >
>
Reply all
Reply to author
Forward
0 new messages