WindowsError when renaming temporary file

Bug #731803 reported by jorgen jorgensen
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Chamelon Core
Fix Committed
Undecided
Unassigned

Bug Description

Following the tutorial pyramid-1.0.pdf, paragraph 5.6 on win XP I get the following at localhost:6543
I am not sure whether the bug belongs here or over at pyramid, so bear with me.

I worked around the following problem by inserting the line:
            os.close(fd)
just after the line:
            fd, fn = tempfile.mkstemp(prefix=base, suffix='.tmp', dir=self.path)
In the file builder.py inside ModuleLoader.build

Module pyramid.chameleon_zpt:52 in __call__ view
<< except (TypeError, ValueError):
                   raise ValueError('renderer was passed non-dictionary as value')
               result = self.template(**system)
               return result
>> result = self.template(**system)
Module chameleon.template:121 in __call__ view
<< def __call__(self, **kwargs):
               return self.render(**kwargs)

           def __repr__(self):
>> return self.render(**kwargs)
Module chameleon.zpt.template:96 in render view
<< setdefault("nothing", None)

               return super(PageTemplate, self).render(**kwargs)

           def include(self, stream, econtext, rcontext):
>> return super(PageTemplate, self).render(**kwargs)
Module chameleon.template:153 in render view
<< stream = self.output_stream_factory()
               econtext = rcontext.pop('econtext', False) or Scope(rcontext)
               self.cook_check()
               try:
                   self._render(stream, econtext, rcontext)
>> self.cook_check()
Module chameleon.template:260 in cook_check view
<< if self._cooked is False:
                   body = self.read()
                   self.cook(body)

           def detect_encoding(self, body):
>> self.cook(body)
Module chameleon.template:134 in cook view
<< def cook(self, body):
               digest = self._digest(body)
               program = self._cook(body, digest)

               for name, function in program.items():
>> program = self._cook(body, digest)
Module chameleon.template:333 in _cook view
<< source = "# filename: %s\n#\n%s" % (self.filename, source)

                       cooked = self.loader.build(source, name)
                   except TemplateError:
                       exc = sys.exc_info()[1]
>> cooked = self.loader.build(source, name)
Module chameleon.loader:112 in build view
<< raise

                   os.rename(fn, name)
                   log.debug("compiling %s into byte-code..." % filename)
                   py_compile.compile(name)
>> os.rename(fn, name)

and furthermore (in the console)

Traceback (most recent call last):
  File "C:\Python27\Lib\atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "D:\Users\Rendon\servage\pd\lib\site-packages\chameleon-2.0_rc5-py2.7.egg
\chameleon\template.py", line 84, in cleanup
    shutil.rmtree(path)
  File "C:\Python27\Lib\shutil.py", line 249, in rmtree
    onerror(os.remove, fullname, sys.exc_info())
  File "C:\Python27\Lib\shutil.py", line 247, in rmtree
    os.remove(fullname)
WindowsError: [Error 32] The process cannot access the file because it is being
used by another process: 'c:\\docume~1\\user\\locals~1\\temp\\tmpwxeqh6\\mytempl
ate_8612ea41c3c22dac09c97155d2364cf65886b7007bajdn.tmp'

Tags: windows
Revision history for this message
Pascal Bach (pascal-bach) wrote :

Hi

I can confirm this problem. The problem is that tempfile.mkstemp() returns a file handler to an open file which prevents os.rename from working on this file.
As a solution I close the file directly after creating the temporary file using os.close

I attached a patch for this.

Revision history for this message
Malthe Borch (mborch) wrote :

I have reworked the loader somewhat, to an effect which should be the same as your patch. Can you try it out?

Thanks!

Revision history for this message
jorgen jorgensen (gardsted-gmail) wrote : Re: [Bug 731803] Re: WindowsError when renaming temporary file
Download full text (4.8 KiB)

I will, though I am convinced it works if just os.close(the handle) is
there and it works on other platforms.

I must first find out how to get the correct source

Is there a way to easy_install the trunk into a virtualenv directly or
should I get it first via svn?git or what is holding it?

Never mind that question, I'll find out.
Just in the process of drinking a beer (red horse, 1 ltr) - so it may
take some time.
On vacation in the phillipines

I am very excited about pyramid+chameleon.
I have been using cherrypy+simpletal up until now, so I am pleased
that chameleon (and tal) is built in, including the ability to take
the curly braces shortcut where applicable.

On Mon, Mar 14, 2011 at 4:33 PM, Malthe Borch
<email address hidden> wrote:
> I have reworked the loader somewhat, to an effect which should be the
> same as your patch. Can you try it out?
>
> Thanks!
>
> --
> You received this bug notification because you are a direct subscriber
> of the bug.
> https://bugs.launchpad.net/bugs/731803
>
> Title:
>  WindowsError when renaming temporary file
>
> Status in Attribute language template compiler:
>  New
>
> Bug description:
>  Following the tutorial pyramid-1.0.pdf, paragraph 5.6 on win XP I get the following at localhost:6543
>  I am not sure whether the bug belongs here or over at pyramid, so bear with me.
>
>  I worked around the following problem by inserting the line:
>              os.close(fd)
>  just after the line:
>              fd, fn = tempfile.mkstemp(prefix=base, suffix='.tmp', dir=self.path)
>  In the file builder.py inside ModuleLoader.build
>
>
>  Module pyramid.chameleon_zpt:52 in __call__         view
>  <<          except (TypeError, ValueError):
>                     raise ValueError('renderer was passed non-dictionary as value')
>                 result = self.template(**system)
>                 return result
>  >>  result = self.template(**system)
>  Module chameleon.template:121 in __call__         view
>  <<      def __call__(self, **kwargs):
>                 return self.render(**kwargs)
>
>             def __repr__(self):
>  >>  return self.render(**kwargs)
>  Module chameleon.zpt.template:96 in render         view
>  <<          setdefault("nothing", None)
>
>                 return super(PageTemplate, self).render(**kwargs)
>
>             def include(self, stream, econtext, rcontext):
>  >>  return super(PageTemplate, self).render(**kwargs)
>  Module chameleon.template:153 in render         view
>  <<          stream = self.output_stream_factory()
>                 econtext = rcontext.pop('econtext', False) or Scope(rcontext)
>                 self.cook_check()
>                 try:
>                     self._render(stream, econtext, rcontext)
>  >>  self.cook_check()
>  Module chameleon.template:260 in cook_check         view
>  <<          if self._cooked is False:
>                     body = self.read()
>                     self.cook(body)
>
>             def detect_encoding(self, body):
>  >>  self.cook(body)
>  Module chameleon.template:134 in cook         view
>  <<      def cook(self, body):
>                 digest = self._digest(body)
>                 program = self._cook(body, digest)...

Read more...

Revision history for this message
jorgen jorgensen (gardsted-gmail) wrote :

I tried the source link on chameleons page and got
(http://repoze.org/viewcvs/?root=cvs) :

An Exception Has Occurred

cvs not found! The wrong path for this repository was configured, or
the server on which the CVS tree lives may be down. Please try again
in a few minutes.

regards jj

Revision history for this message
jorgen jorgensen (gardsted-gmail) wrote :

It's me again - found it!
https://code.launchpad.net/chameleon.core says:

Attribute language template compiler hosts its code at
http://svn.repoze.org/chameleon/trunk/.

and going there it says it has moved to: https://github.com/malthe/chameleon

so now i installed git

and cloned it from the https://github.com/malthe/chameleon

puhhh - no pyramid install - nose 1.0.0 couldn't install (venusian requirement)

so i had to install the new chameleon on top of my old virtualenv

here goes

D:\Users\Rendon\servage\pd>Scripts\paster create -t pyramid_starter
Selected and implied templates:
  pyramid#pyramid_starter pyramid starter project

Enter project name: chtest1
...
D:\Users\Rendon\servage\pd\chtest1>..\Scripts\python.exe setup.py develop
...
Searching for chameleon==2.0-rc5
Best match: chameleon 2.0-rc5
Processing chameleon-2.0_rc5-py2.7.egg
chameleon 2.0-rc5 is already the active version in easy-install.pth

Using d:\users\rendon\servage\pd\lib\site-packages\chameleon-2.0_rc5-py2.7.egg
======================
loader has this, so it is the right package:
            fd, fn = tempfile.mkstemp(prefix=base, suffix='.tmp', dir=self.path)
            temp = os.fdopen(fd, 'w')
======================
...

D:\Users\Rendon\servage\pd\chtest1>..\Scripts\paster serve development.ini

Welcome to chtest1, an application generated by
the Pyramid web application development framework.

Revision history for this message
jorgen jorgensen (gardsted-gmail) wrote :

Bug is fixed, I think

Revision history for this message
Pascal Bach (pascal-bach) wrote :

I think this bug is fixed in the newest version on github. I close this bug as fixed.

Changed in chameleon.core:
status: New → Fix Committed
Revision history for this message
jorgen jorgensen (jgj-barskdata) wrote :

it is - i wrote on the bug

On 29-04-2011 20:52, Pascal Bach wrote:
> I think this bug is fixed in the newest version on github. I close this
> bug as fixed.
>
> ** Changed in: chameleon.core
> Status: New => Fix Committed
>

--
Med venlig hilsen / With kind regards

Jørgen Gårdsted Jørgensen

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.