TheChymera / RepSeP

Reproducible Self-Publishing - Demo Publications in the Most Common Formats
14 stars 6 forks source link

Referencing variables in list comprehension. #3

Closed TheChymera closed 6 years ago

TheChymera commented 7 years ago

e8bbb85ba122d2b1dd7d423d217b8f88a842c086 demonstrates a very weird bug I have come across. PythonTeX will report that all variables inside a list comprehension - except for the iterator - cannot be found. This behaviour does not occur when I execute the script with the Python interpreter, or when I run it via exec (which is how the RepSeP function runs it).

Here's what the error looks like (reproducible via the cloned RepSeP):

chymera@quiethost ~/docsrc/RepSeP $ pdflatex pres.tex && pythontex.py pres.tex && pdflatex pres.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./pres.tex
LaTeX2e <2017/01/01> patch level 3
Babel <3.10> and hyphenation patterns for 8 language(s) loaded.
(/usr/share/texmf-site/tex/latex/beamer/base/beamer.cls
Document Class: beamer 2017/01/23 v3.41 A class for typesetting presentations
(/usr/share/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasemodes.sty
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasedecode.sty))
(/usr/share/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseoptions.sty
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty))
(/usr/share/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texmf-dist/tex/generic/ifxetex/ifxetex.sty))
(/usr/share/texmf-dist/tex/latex/base/size11.clo)
(/usr/share/texmf-site/tex/latex/pgf/basiclayer/pgfcore.sty
(/usr/share/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/share/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/share/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/share/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/usr/share/texmf-dist/tex/latex/graphics-def/pdftex.def
(/usr/share/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/share/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))))
(/usr/share/texmf-site/tex/latex/pgf/systemlayer/pgfsys.sty
(/usr/share/texmf-site/tex/latex/pgf/utilities/pgfrcs.sty
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfutil-common.tex
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfutil-common-lists.tex))
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfutil-latex.def
(/usr/share/texmf-dist/tex/latex/ms/everyshi.sty))
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfrcs.code.tex))
(/usr/share/texmf-site/tex/generic/pgf/systemlayer/pgfsys.code.tex
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfkeys.code.tex
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfkeysfiltered.code.tex))
(/usr/share/texmf-site/tex/generic/pgf/systemlayer/pgf.cfg)
(/usr/share/texmf-site/tex/generic/pgf/systemlayer/pgfsys-pdftex.def
(/usr/share/texmf-site/tex/generic/pgf/systemlayer/pgfsys-common-pdf.def)))
(/usr/share/texmf-site/tex/generic/pgf/systemlayer/pgfsyssoftpath.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/systemlayer/pgfsysprotocol.code.tex))
(/usr/share/texmf-site/tex/latex/xcolor/xcolor.sty
(/usr/share/texmf-dist/tex/latex/graphics-cfg/color.cfg)
(/usr/share/texmf-dist/tex/latex/colortbl/colortbl.sty
(/usr/share/texmf-dist/tex/latex/tools/array.sty))
(/usr/share/texmf-dist/tex/latex/graphics/dvipsnam.def))
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcore.code.tex
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmath.code.tex
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathcalc.code.tex
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathutil.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathparser.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.code.tex
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.basic.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.trigonometric.code
.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.random.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.comparison.code.te
x) (/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.base.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.round.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.misc.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfunctions.integerarithmetics
.code.tex))) (/usr/share/texmf-site/tex/generic/pgf/math/pgfmathfloat.code.tex)
) (/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorepoints.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorepathconstruct.code.tex
) (/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorepathusage.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorescopes.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoregraphicstate.code.tex)

(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoretransformations.code.t
ex) (/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorequick.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoreobjects.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorepathprocessing.code.te
x) (/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorearrows.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoreshade.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoreimage.code.tex
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoreexternal.code.tex))
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorelayers.code.tex)
(/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcoretransparency.code.tex)
 (/usr/share/texmf-site/tex/generic/pgf/basiclayer/pgfcorepatterns.code.tex)))
(/usr/share/texmf-site/tex/latex/pgf/utilities/xxcolor.sty)
(/usr/share/texmf-dist/tex/generic/oberdiek/atbegshi.sty)
(/usr/share/texmf-dist/tex/latex/hyperref/hyperref.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty))
(/usr/share/texmf-dist/tex/latex/oberdiek/auxhook.sty)
(/usr/share/texmf-dist/tex/latex/oberdiek/kvoptions.sty)
(/usr/share/texmf-dist/tex/latex/hyperref/pd1enc.def)
(/usr/share/texmf-dist/tex/latex/latexconfig/hyperref.cfg)
(/usr/share/texmf-dist/tex/latex/url/url.sty)

Package hyperref Message: Stopped early.

)

Package hyperref Message: Driver (autodetected): hpdftex.

(/usr/share/texmf-dist/tex/latex/hyperref/hpdftex.def
(/usr/share/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaserequires.sty
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasecompatibility.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasefont.sty
(/usr/share/texmf-dist/tex/latex/amsfonts/amssymb.sty
(/usr/share/texmf-dist/tex/latex/amsfonts/amsfonts.sty))
(/usr/share/texmf-dist/tex/latex/sansmathaccent/sansmathaccent.sty
(/usr/share/texmf-dist/tex/latex/filehook/filehook.sty)))
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasetranslator.sty
(/usr/share/texmf-site/tex/latex/beamer/base/translator/translator.sty
(/usr/share/texmf-site/tex/latex/beamer/base/translator/translator-language-map
pings.tex))) (/usr/share/texmf-site/tex/latex/beamer/base/beamerbasemisc.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasetwoscreens.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseoverlay.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasetitle.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasesection.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseframe.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseverbatim.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseframesize.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseframecomponents.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasecolor.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasenotes.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasetoc.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasetemplates.sty
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseauxtemplates.sty
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaseboxes.sty)))
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbaselocalstructure.sty
(/usr/share/texmf-dist/tex/latex/tools/enumerate.sty))
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasenavigation.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasetheorems.sty
(/usr/share/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/usr/share/texmf-dist/tex/latex/amscls/amsthm.sty))
(/usr/share/texmf-site/tex/latex/beamer/base/beamerbasethemes.sty))
(/usr/share/texmf-site/tex/latex/beamer/base/themes/theme/beamerthemedefault.st
y
(/usr/share/texmf-site/tex/latex/beamer/base/themes/font/beamerfontthemedefault
.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/themes/color/beamercolorthemedefau
lt.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/themes/inner/beamerinnerthemedefau
lt.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/themes/outer/beamerouterthemedefau
lt.sty))) (/usr/share/texmf-dist/tex/latex/tools/bm.sty)
(/usr/share/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texmf-dist/tex/latex/base/utf8.def
(/usr/share/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/share/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/share/texmf-dist/tex/latex/base/omsenc.dfu)))
(/usr/share/texmf-dist/tex/latex/booktabs/booktabs.sty)
(/usr/share/texmf-dist/tex/latex/tools/multicol.sty)
(/usr/share/texmf-dist/tex/latex/siunitx/siunitx.sty
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/share/texmf-dist/tex/latex/l3kernel/l3pdfmode.def))
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
(/usr/share/texmf-dist/tex/latex/l3packages/l3keys2e/l3keys2e.sty))
(/usr/share/texmf-dist/tex/latex/multirow/multirow.sty)
(/usr/share/texmf-dist/tex/latex/easy/easytable.sty
Document Style `easytable' v1.0 <2001/06/13>.
(/usr/share/texmf-dist/tex/latex/easy/easy.sty
Document Style `easy' v1.1 <2001/05/31>.
)) (/usr/share/texmf-dist/tex/latex/oberdiek/grffile.sty)
(/usr/share/texmf-dist/tex/latex/soul/soul.sty)
(/usr/share/texmf-site/tex/latex/pythontex/pythontex.sty
(/usr/share/texmf-dist/tex/latex/fvextra/fvextra.sty
(/usr/share/texmf-dist/tex/latex/base/ifthen.sty)
(/usr/share/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix 
<2008/02/07> (tvz)) (/usr/share/texmf-dist/tex/latex/upquote/upquote.sty)
(/usr/share/texmf-dist/tex/latex/lineno/lineno.sty))
(/usr/share/texmf-dist/tex/generic/xstring/xstring.sty
(/usr/share/texmf-dist/tex/generic/xstring/xstring.tex))
(/usr/share/texmf-dist/tex/latex/pgfopts/pgfopts.sty
(/usr/share/texmf-site/tex/latex/pgf/utilities/pgfkeys.sty
(/usr/share/texmf-site/tex/generic/pgf/utilities/pgfkeys.code.tex)))
(/usr/share/texmf-dist/tex/latex/caption/newfloat.sty)
(/usr/share/texmf-dist/tex/latex/currfile/currfile.sty))
(./pythontex/functions.tex)
(/usr/share/texmf-site/tex/latex/beamer/base/themes/color/beamercolorthemestruc
ture.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/themes/theme/beamerthemeDresden.st
y
(/usr/share/texmf-site/tex/latex/beamer/base/themes/outer/beamerouterthememinif
rames.sty)
(/usr/share/texmf-site/tex/latex/beamer/base/themes/color/beamercolorthemewhale
.sty)) (./pres.aux)
*geometry* driver: auto-detecting
*geometry* detected driver: pdftex
(/usr/share/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/share/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/share/texmf-dist/tex/latex/oberdiek/grfext.sty)
(/usr/share/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
ABD: EveryShipout initializing macros
(/usr/share/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/gettitlestring.sty)) (./pres.out)
(./pres.out)
(/usr/share/texmf-site/tex/latex/beamer/base/translator/dicts/translator-basic-
dictionary/translator-basic-dictionary-English.dict)
(/usr/share/texmf-site/tex/latex/beamer/base/translator/dicts/translator-biblio
graphy-dictionary/translator-bibliography-dictionary-English.dict)
(/usr/share/texmf-site/tex/latex/beamer/base/translator/dicts/translator-enviro
nment-dictionary/translator-environment-dictionary-English.dict)
(/usr/share/texmf-site/tex/latex/beamer/base/translator/dicts/translator-months
-dictionary/translator-months-dictionary-English.dict)
(/usr/share/texmf-site/tex/latex/beamer/base/translator/dicts/translator-number
s-dictionary/translator-numbers-dictionary-English.dict)
(/usr/share/texmf-site/tex/latex/beamer/base/translator/dicts/translator-theore
m-dictionary/translator-theorem-dictionary-English.dict)
(/usr/share/texmf-dist/tex/latex/base/ts1enc.dfu)
(/usr/share/texmf-dist/tex/latex/siunitx/siunitx-abbreviations.cfg)
(./pythontex-files-pres/pres.pytxmcr) (./pythontex-files-pres/pres.pytxpyg)
(./pres.nav) (/usr/share/texmf-dist/tex/latex/amsfonts/umsa.fd)
(/usr/share/texmf-dist/tex/latex/amsfonts/umsb.fd)
(/usr/share/texmf-dist/tex/latex/sansmathaccent/ot1mathkerncmss.fd)

LaTeX Font Warning: Font shape `OT1/cmss/m/n' in size <4> not available
(Font)              size <5> substituted on input line 60.

[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
(./auto_fig_py_default_default-0.pgf

LaTeX Font Warning: Font shape `OT1/cmss/m/n' in size <4.053> not available
(Font)              size <5> substituted on input line 122.

)
Overfull \vbox (33.16605pt too high) detected at line 65
[2] (./auto_fig_py_default_default-1.pgf)
Overfull \vbox (28.11655pt too high) detected at line 68
[3] (./auto_fig_py_default_default-2.pgf)
Overfull \hbox (128.90445pt too wide) in paragraph at lines 1388--5
 [] 

Overfull \vbox (147.10413pt too high) detected at line 76
[4] (./pres.vrb

Package PythonTeX Warning: Non-existent Pygments content on input line 11.

) [5] (./pres.vrb

Package PythonTeX Warning: Non-existent Pygments content on input line 13.

) [6]

Package PythonTeX Warning: Missing autoprint content on input line 118.

[7] [8] (./pres.aux)

Package rerunfilecheck Warning: File `pres.out' has changed.
(rerunfilecheck)                Rerun to get outlines right
(rerunfilecheck)                or use package `bookmark'.

LaTeX Font Warning: Size substitutions with differences
(Font)              up to 1.0pt have occurred.

 )
(see the transcript file for additional information)</usr/share/texmf-dist/font
s/type1/public/amsfonts/cm/cmss10.pfb></usr/share/texmf-dist/fonts/type1/public
/amsfonts/cm/cmss12.pfb></usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/c
mss8.pfb></usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmssbx10.pfb></u
sr/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy5.pfb></usr/share/texmf-
dist/fonts/type1/public/amsfonts/cm/cmsy7.pfb></usr/share/texmf-dist/fonts/type
1/public/amsfonts/symbols/msam10.pfb></usr/share/texmf-dist/fonts/type1/public/
amsfonts/symbols/msam7.pfb>
Output written on pres.pdf (8 pages, 250945 bytes).
Transcript written on pres.log.
This is PythonTeX 0.16dev

----  Messages for py:default:default  ----
* PythonTeX notice
    1 message(s) could not be classified
    Interpreted as errors, based on the return code(s)
  Traceback (most recent call last):
* PythonTeX stderr - unknown on line 76:
    File "<outputdir>/py_default_default.py", line 295, in <module>
      print(pytex.formatter(pytex_fig('scripts/radar.py', label='radar', caption='A radar plot.', ) ))
* PythonTeX stderr - error on line -81:
    File "<outputdir>/py_default_default.py", line 138, in pytex_fig
      exec(open(script).read())
    File "<string>", line 21, in <module>
    File "<string>", line 21, in <listcomp>
  NameError: name 'r' is not defined

--------------------------------------------------
PythonTeX:  pres - 2 error(s), 0 warning(s)

I wasn't able to reproduce this bug in any other way outside of PythonTeX. @gpoore could you help me figure this out? This issue occurs on Python3.4 and not on Python2.7 (haven't tested other versions) - but again, only when the code is executed via PythonTeX. apparently >Python3.2 has some quirky namespace management for list comprehensions - but this only manifests inside classes. Is my code by any chance executed via class Pytxcode(object)? Could this be the culprit? Is there any way to avoid this? ^^

gpoore commented 7 years ago

The figure creation code is run via exec(), and it seems that that is introducing scoping issues. See https://stackoverflow.com/a/45132815/2002379.

TheChymera commented 6 years ago

The only feasible fix I have found is to not use list comprehensions and go for loops instead. Please comment if you find a better suggestion.