commit 79c5845ff8e7fa4552cf263a1a688628f09f6eaf Author: Alexander Myltsev Date: Wed Aug 19 00:07:16 2009 +0400 Use mkstemp() instead of mucking with randint(). diff --git a/gajim/src/common/latex.py b/gajim/src/common/latex.py index d60922e..3237e4e 100644 --- a/gajim/src/common/latex.py +++ b/gajim/src/common/latex.py @@ -28,8 +28,7 @@ ## import os -import random -from tempfile import gettempdir +from tempfile import gettempdir, mkstemp from subprocess import Popen, PIPE import logging @@ -57,12 +56,7 @@ def check_blacklist(str_): return True return False -def get_tmpfile_name(): - random.seed() - int_ = random.randint(0, 100) - return os.path.join(gettempdir(), 'gajimtex_' + int_.__str__()) - -def write_latex(filename, str_): +def write_latex(file_, str_): texstr = '\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}' texstr += '\\usepackage{amsmath}\\usepackage{amssymb}' texstr += '\\pagestyle{empty}' @@ -70,10 +64,8 @@ def write_latex(filename, str_): texstr += str_ texstr += '\\end{gather*}\\end{large}\\end{document}' - file_ = open(filename, "w+") file_.write(texstr) file_.flush() - file_.close() # a wrapper for Popen so that no window gets opened on Windows # (i think this is the reason we're using Popen rather than just system()) @@ -126,10 +118,15 @@ def latex_to_image(str_): # we triggered the blacklist, immediately return None return None - tmpfile = get_tmpfile_name() + fd, tmpfile = mkstemp(suffix=".tex", prefix="gajimtex_", + dir=gettempdir()) + f = os.fdopen(fd, 'r+b') + tmpfile = tmpfile[:-4] # remove the .tex extension + # build latex string - write_latex(os.path.join(tmpfile + '.tex'), str_) + write_latex(f, str_) + f.close() # convert TeX to dvi exitcode = try_run(['latex', '--interaction=nonstopmode',