作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序,可以逐个创建大量 LaTeX 文件。创建这些 LaTeX 文件时,检查它们是否能够正确编译为 .pdf 且不会出现错误,这一点非常重要。
为此,它使用 subprocess.call(['pdflatex', '-halt-on-error', tex_file_name])
.
返回 0
从 .tex
成功编译到 .pdf
,否则为 1。
我遇到的问题是,这行代码没有执行我认为应该执行的操作的唯一情况是 py.test
运行它。如果我从解释器运行此代码,或从命令行运行脚本,它就会起作用。但 py.test 没有。
何时 py.test
错误,它会留下由 pdflatex
创建的日志文件,其中有此错误:
{c:/texlive/2012/texmf-var/fonts/map/pdftex/updmap/pdftex.map
!pdfTeX error: pdflatex.exe (file c:/texlive/2012/texmf-var/fonts/map/pdftex/up
dmap/pdftex.map): fflush() failed (Bad file descriptor)
==> Fatal error occurred, no output PDF file produced!
我在这里冒险猜测 py.test
正在使用 .tex
做一些事情 pdflatex
之前的文件能够编译它。但我不知道是什么。
临时文件和目录在 py.test docs 中讨论。 。我不知道它们是否与我的问题相关,但我只是简单地尝试了一下。
如果您想查看代码,测试用例如下所示:
from a import Foo
from b import Tree
from latex_tester import latex_tester
def test_Foo():
q1 = foo.Foo()
latex_tester(Tree(1, q1))
latex_tester 看起来像这样:
import uuid
import os
import subprocess
def latex_tester(tree):
""" Test whether latex is compilable to a PDF.
"""
full_path = r'some_path'
uid = str(uuid.uuid1())
file_name = os.path.join(full_path, 'test' + uid + '.tex')
with open(file_name, 'w') as f:
_write_tree(f, tree)
retcode = subprocess.call(['pdflatex', '-halt-on-error', file_name])
if retcode != 0:
raise RuntimeError("This latex could not be compiled.")
最佳答案
奇怪的是,使用“xelatex”而不是“pdflatex”使事情正常工作。
对于任何 future 的读者 - 我安装了 TeXworks,它可能安装了这两个工具。我不知道xelatex是否会影响最终生成的pdf。它似乎正在生成一个很好的 .pdf
无论如何,我对自己的问题做出了这个回答,因为似乎没有其他任何事情发生,而且它确实解决了我的问题。
关于python - py.test 无法处理创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18615798/
我是一名优秀的程序员,十分优秀!