Commits

Anonymous committed 1603bbb

improve version mismatch checks; check label names when looking at max seen counter

  • Participants
  • Parent commits 9416145

Comments (0)

Files changed (3)

File py-and-sty.dtx

 %   eliminates all the irritating string munging below, and later we can
 %   remove this stuff and just use |sagetex.version|.
 %   \changes{v2.2.4}{2010/02/15}{Add version mismatch checking.}
+%   \changes{v2.3.3}{2012/01/13}{Improve version mismatch checking,
+%   include Mercurial revision in version string.}
 %    \begin{macrocode}
 \newcommand{\ST@wsf}[1]{\immediate\write\ST@sf{#1}}%
 \ST@wsf{%
 % \texttt{sagetex.dtx}) to extract what we want, then import what
 % we need:
 %    \begin{macrocode}
-pyversion = ' '.join(__version__.strip('[').split()[0:4])
+pyversion = ' '.join(__version__.strip('[').split()[0:2])
 from sage.misc.latex import latex
 from sage.misc.preparser import preparse
 import os
 import traceback
 import subprocess
 import shutil
+from collections import defaultdict
 %    \end{macrocode}
 % Define an exception class for version mismatches. I suppose I could
 % just use |ValueError|, but this is easy enough:
 class VersionError(Exception):
     pass
 %    \end{macrocode}
-% We define a class so that it's a bit easier to carry around internal
-% state. We used to just have some global variables and a bunch of
-% functions, but this seems a bit nicer and easier.
+% Sometimes our macros that write things to the |.sout| file get
+% evaluated twice, most commonly in the ``fancy'' AMS environments such
+% as |align| and |multline|. So we need to keep track of the counters
+% we've seen so we don't write labels to the |.sout| file more than
+% once. We have more than one kind of label, so a dictionary is the
+% natural way to store the counters we've seen for each kind of label.
+% For convenience let's make a dictionary subclass for which (1) values
+% default to $-1$, and (2) there's an |increment(key)| function that just
+% increments the value corresponding to the key.
+%    \begin{macrocode}
+class MyDict(defaultdict):
+    def __init__(self, *args, **kwargs):
+        defaultdict.__init__(self, *args, **kwargs)
+        self.default_factory = lambda: -1
+
+    def increment(self, key):
+        self[key] = self[key] + 1
+%    \end{macrocode}
+% We define a |SageTeXProcessor| class so that it's a bit easier to
+% carry around internal state. We used to just have some global
+% variables and a bunch of functions, but this seems a bit nicer and
+% easier.
 %    \begin{macrocode}
 class SageTeXProcessor():
 %    \end{macrocode}
 %
 % \changes{v2.3.1}{2011/02/02}{Handle filenames with
 % spaces in SageTeXProcessor and sagecommandline env.}
+% \changes{v2.3.2}{2012/01/13}{Improve version mismatch check. Fixes
+% trac ticket 8035.}
 %    \begin{macrocode}
   def __init__(self, jobname, version=None, version_check=True):
-    if version_check and version != pyversion:
-      raise VersionError, """versions of .sty and .py files do not match.
+    if version != pyversion:
+      errstr = """versions of .sty and .py files do not match.
 {0}.sagetex.sage was generated by sagetex.sty version "{1}", but
 is being processed by sagetex.py version "{2}".
 Please make sure that TeX is using the sagetex.sty 
 from your current version of Sage; see
 http://www.sagemath.org/doc/installation/sagetex.html.""".format(jobname,
   version, pyversion)
+      if version_check:
+        raise VersionError, errstr
+      else:
+        print '**** WARNING! Skipping version check for .sty and .py files, and'
+        print errstr
     if ' ' in jobname:
       jobname = jobname.strip('"')
     self.progress('Processing Sage code for {0}.tex...'.format(jobname))
 % largest label we've seen so that we don't write two labels with the same
 % name.
 %    \begin{macrocode}
-    self.max_inline_counter_seen = -1
+    self.max_counter_seen = MyDict()
 %    \end{macrocode}
 % Open a |.sout.tmp| file and write all our output to that. Then, when
 % we're done, we move that to |.sout|. The ``autogenerated'' line is
 % and the labels from |sagecommandline| contain a |lstlistings|
 % environment---pulling such an environment into math mode produces
 % strange, unrecoverable errors, and if you can't typeset your file, you
-% can't product an updated |.sagetex.sage| file to run Sage on to
+% can't produce an updated |.sagetex.sage| file to run Sage on to
 % produce a reasonable |.sagetext.sout| file that will fix the label
 % problem. So it works much better to use distinct labels for such
 % things.
+% \changes{v2.3.3}{2012/01/16}{check label name when comparing against
+% maximum counter seen; trac ticket 12267}
 %
 % We print out the line number so if something goes wrong, the user can
 % more easily track down the offending |\sage| command in the source
 % file.
 %
-% That's a lot of explanation for a very short function:
+% That's a lot of explanation for a short function:
 %    \begin{macrocode}
-  def inline(self, counter, s, labelname=None):
-      if counter <= self.max_inline_counter_seen:
+  def inline(self, counter, s, labelname='sageinline'):
+      if counter <= self.max_counter_seen[labelname]:
           return
       else:
-          self.max_inline_counter_seen += 1
-      if labelname is None:
-          labelname = 'sageinline'
+          self.max_counter_seen.increment(labelname)
+      if labelname == 'sageinline':
           self.progress('Inline formula {0}'.format(counter))
       elif labelname == 'sagecmdline':
           pass # output message already printed
       else:
-          raise ValueError, 'inline() got a bad labelname'
+          raise ValueError, 'inline() got a bad labelname "{0}"'.format(labelname)
       self.souttmp.write(r'\newlabel{@' + labelname + str(counter) + 
                          '}{{%\n' + s.rstrip() + '}{}{}{}{}}\n')
 %    \end{macrocode}
 % just like that for doctests in the Sage library.
 %    \begin{macrocode}
   def doctest(self, counter, str, globals, locals, include_text_output):
-      print 'in doctest'
       current_statement = None
       current_lines = None
       latex_string = ""
 % \iffalse meta-comment
 %
-% Copyright (C) 2008--2011 by Dan Drake <ddrake (at) member (dot) ams (dot) org>
+% Copyright (C) 2008--2012 by Dan Drake <ddrake (at) member (dot) ams (dot) org>
 % -------------------------------------------------------
 %
 % See the "Copying and licenses" section in this file for the terms
 %<latex>\ProvidesPackage{sagetex}
 %<python>__version__ = """
 %<*latex|python>
-  [2012/01/13 v2.3.2 rev 6cc763c2b649 embedding Sage into LaTeX documents]
+  [2012/01/16 v2.3.3-94161450606b embedding Sage into LaTeX documents]
 %</latex|python>
 %<python>""".strip()
-%<latex>\newcommand{\ST@ver}{2012/01/13 v2.3.2 rev 6cc763c2b649}
+%<latex>\newcommand{\ST@ver}{2012/01/16 v2.3.3-94161450606b}
 %<*driver>
 \documentclass[a4paper]{ltxdoc}
 \usepackage{sagetex}
 %</driver>
 % \fi
 %
-% \CheckSum{566}
+% \CheckSum{565}
 %
 % \CharacterTable
 %  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
 % \begin{quote}
 % |Here is a lovely graph of the sine curve:|
 %
-% |\sageplot{plot(sin(x), x, 0, 2*pi)}|
+% |\sageplot[width=.75\textwidth]{plot(sin(x), x, 0, 2*pi)}|
 % \end{quote}
 % in your \LTX file, it produces
 % \begin{quote}
 % Here is a lovely graph of the sine curve:
 %
-% \sageplot{plot(sin(x), x, 0, 2*pi)}
+% \sageplot[width=.75\textwidth]{plot(sin(x), x, 0, 2*pi)}
 % \end{quote}
 % Again, you need only worry about the logical/mathematical structure of
 % your document (``I need a plot of the sine curve over the interval
 %
 % Here are some other notes on using \ST.
 %
-% \subsection{Using the \texttt{sage} macro inside align environments}
+% \subsection{Using the \texttt{sage} macro inside align (and similar) environments}
 % \label{sec:sage-inside-align}
 %
-% The |align| and |align*| environments in the |amsmath| package do some
-% special processing---in particular, they evaluate everything inside
-% twice. This means that if you use |\sage| or |\sagestr| inside such an
-% environment, it will be evaluated twice, and its argument will be put
-% into the generated |.sage| file twice---and if that argument has side
-% effects, those side effects will be executed twice! Doing something
-% such as popping an element from a list will actually pop \emph{two}
-% elements and typeset the second. The solution is to do any processing
-% that has side effects before the |align| environment (in a
-% |sagesilent| environment, say) and to give |\sage| or |\sagestr| an
-% argument with no side effects.
+% The |align|, |align*|, and some other ``fancy'' math environments in
+% the |amsmath| package do some special processing---in particular, they
+% evaluate everything inside twice. This means that if you use |\sage|
+% or |\sagestr| inside such an environment, it will be evaluated twice,
+% and its argument will be put into the generated |.sage| file
+% twice---and if that argument has side effects, those side effects will
+% be executed twice! Doing something such as popping an element from a
+% list will actually pop \emph{two} elements and typeset the second. The
+% solution is to do any processing that has side effects before the
+% |align| environment (in a |sagesilent| environment, say) and to give
+% |\sage| or |\sagestr| an argument with no side effects.
 %
 % Thanks to Bruno Le Floch for reporting this.
 %
 %% This is `sagetex.ins', part of the sagetex package.
 %%
-%% Copyright (C) 2008--2011 by Dan Drake <ddrake@member.ams.org>
+%% Copyright (C) 2008--2012 by Dan Drake <ddrake@member.ams.org>
 %%
 %% This program is free software: you can redistribute it and/or modify
 %% it under the terms of the GNU General Public License as published by
 
 This is a generated file. It is part of the SageTeX package.
 
-Copyright (C) 2008--2011 by Dan Drake <ddrake@member.ams.org>
+Copyright (C) 2008--2012 by Dan Drake <ddrake@member.ams.org>
 
 This program is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
 
 This is a generated file. It is part of the SageTeX package.
 
-Copyright (C) 2008--2011 by Dan Drake <ddrake@member.ams.org>
+Copyright (C) 2008--2012 by Dan Drake <ddrake@member.ams.org>
 
 This program is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the