简体   繁体   English

py.test无法处理创建文件

[英]py.test is failing to deal with creating files

I have a program where I am creating a multitude of LaTeX files one by one. 我有一个程序,在其中我一个一个地创建多个LaTeX文件。 It is important when creating these LaTeX files to check that they can actually compile to a .pdf without error. 创建这些LaTeX文件时,检查它们是否可以真正编译为.pdf且没有错误非常重要。

To do so it uses subprocess.call(['pdflatex', '-halt-on-error', tex_file_name]) . 为此,它使用subprocess.call(['pdflatex', '-halt-on-error', tex_file_name])

Which returns 0 on a successful compile from a .tex to a .pdf , and a 1 otherwise. 成功从.tex编译为.pdf返回0 ,否则返回1。

The problem I am having is that the only circumstance under which this line of code does not do what I think it should do, is when py.test runs it. 我遇到的问题是,这行代码没有执行我认为应该做的唯一情况是py.test运行它。 If I run this code from an interpreter, or running a script from the command line, it works. 如果我从解释器运行此代码,或者从命令行运行脚本,则它可以工作。 But py.test doesn't. 但是py.test没有。

When py.test errors, it leaves behind a log file created by pdflatex , which has this error in it: 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!

I am hazarding a guess here that py.test is doing some thing with the .tex file prior to pdflatex being able to compile it. 我在这里hazarding猜测, py.test一些事情.tex文件之前pdflatex能够编译。 But I don't know what. 但是我不知道。

Temporary files and directories are talked about in the py.test docs . 临时文件和目录在py.test docs中讨论 I don't know if they are relevant to my problem, but I have only played around with them briefly. 我不知道它们是否与我的问题有关,但我只是短暂地与他们一起玩。

In case you want to look at the code, a test case looks like this: 如果您想查看代码,则测试用例如下所示:

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))

and latex_tester looks like this: 和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.")

Oddly enough, using 'xelatex' instead of 'pdflatex' makes things work as normal. 奇怪的是,使用“ xelatex”而不是“ pdflatex”可使事情正常进行。

For any future readers - I have TeXworks installed which presumably installed both these tools. 对于以后的读者-我已经安装了TeXworks,大概安装了这两个工具。 I don't know if xelatex influences the final pdf produced. 我不知道xelatex是否会影响最终生成的pdf。 It seems to be producing a good .pdf 它似乎产生了一个好的.pdf

Anyway, I made this answer to my own question since there doesn't seem to be anything else coming and it certainly solved my problem. 无论如何,我回答了我自己的问题,因为似乎没有其他问题了,它肯定解决了我的问题。

I had exactly the same problem. 我有完全一样的问题。

I'm using C# as programming language for creating the .tex documents and it crashed during pdflatex when I included a image into the pdf. 我使用C#作为创建.tex文档的编程语言,当我将图像包含到pdf中时,它在pdflatex中崩溃了。

And it worked if i started it manually... 如果我手动启动它就可以了...

Error: 错误:

pdfTeX warning: pdflatex
!pdfTeX error: pdflatex (file <linktoFile>/file.pdf): fflush() failed (Bad file descriptor)

Unfortunately xelatex didn't work either so I searched and eventually stumbled upon it. 不幸的是,xelatex也不起作用,所以我进行搜索并最终偶然发现了它。

Basically the error happened for me at this line: 基本上,错误发生在这一行:

string tex = tex.Replace("\uFFFD\uFFFDMEMNAME\uFFFD\uFFFD", user.Surname)

Here user.Surname was null. 这里的user.Surname为null。

When the tex string is saved into the file it mysteriously remembers the null and pdflatex crashes somewhere completely different. 将tex字符串保存到文件中后,它神秘地记住了null,而pdflatex在完全不同的位置崩溃。 If you on the other hand start pdflatex on the same file again manually the null is gone and it works. 另一方面,如果您再次手动在同一文件上启动pdflatex,则null消失了,它可以工作了。

The whole mess went away when I entered a Surname and it works now from the program. 当我输入一个姓氏时,整个混乱就消失了,现在可以从该程序运行了。

Maybe this will help someone with the same problem. 也许这会帮助遇到相同问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM