Skip to content

Commit

Permalink
made readme file to be opened using with context
Browse files Browse the repository at this point in the history
The previous example doesn't explicitly close the opened file
object.  Although it's almost fine if the interpreter is CPython
but it could be broken if PyPy.

CPython does reference counting instead of garbage collections,
and file.__del__() closes the file itself, so files are automatically
closed when local scope ends unless these are "leaked" out of local
scope, in CPython.

PyPy does garbage collection so invocation time of __del__() methods
cannot be determined.  It means files can be unclosed even if local
scope ends and these are never leaked out.

If the `funniest` package in the example are being installed as
a dependency of another package by easy_install using PyPy,
it may cause "too many open files" error.

How to prevent this problem is to explicitly close() files or
to open files using with context (if Python 2.5+).

Many python packages in PyPI read their readme file to fill
long_description in setup.py, but this pattern without explicit
closing of file should be discouraged.
  • Loading branch information
dahlia committed Nov 19, 2012
1 parent 2038544 commit c53a3f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion funniest/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from setuptools import setup

def readme():
with open('README.rst') as f:
return f.read()

setup(name='funniest',
version='0.1',
description='The funniest joke in the world',
long_description=open('README.rst').read(),
long_description=readme(),
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: MIT License',
Expand Down
6 changes: 5 additions & 1 deletion metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ Now we can use it in setup.py like::

from setuptools import setup

def readme():
with open('README.rst') as f:
return f.read()

setup(name='funniest',
version='0.1',
description='The funniest joke in the world',
long_description=open('README.rst').read(),
long_description=readme(),
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: MIT License',
Expand Down

0 comments on commit c53a3f9

Please sign in to comment.