Fixed exception thrown when in FreeBSD when /proc exists but it's not mounted. Closes #235

This commit is contained in:
Pablo Hoffman 2010-09-10 15:17:36 -03:00
parent b16eff227b
commit e7a958a035
3 changed files with 11 additions and 7 deletions

View File

@ -4,7 +4,6 @@ MemoryUsage extension
See documentation in docs/topics/extensions.rst
"""
import os
import socket
from pprint import pformat
@ -18,7 +17,7 @@ from scrapy.exceptions import NotConfigured
from scrapy.mail import MailSender
from scrapy.conf import settings
from scrapy.stats import stats
from scrapy.utils.memory import get_vmvalue_from_procfs
from scrapy.utils.memory import get_vmvalue_from_procfs, procfs_supported
from scrapy.utils.engine import get_engine_status
class MemoryUsage(object):
@ -26,7 +25,7 @@ class MemoryUsage(object):
def __init__(self):
if not settings.getbool('MEMUSAGE_ENABLED'):
raise NotConfigured
if not os.path.exists('/proc'):
if not procfs_supported():
raise NotConfigured
self.warned = False

View File

@ -1,13 +1,11 @@
import os
from twisted.trial import unittest
from scrapy.utils.memory import get_vmvalue_from_procfs
from scrapy.utils.memory import get_vmvalue_from_procfs, procfs_supported
class UtilsMemoryTestCase(unittest.TestCase):
def test_get_vmvalue_from_procfs(self):
if not os.path.exists('/proc'):
if not procfs_supported():
raise unittest.SkipTest('/proc filesystem not supported')
vmsize = get_vmvalue_from_procfs('VmSize')
vmrss = get_vmvalue_from_procfs('VmRSS')

View File

@ -23,3 +23,10 @@ def get_vmvalue_from_procfs(vmkey='VmSize', pid=None):
# convert Vm value to bytes
return int(v[1]) * _vmvalue_scale[v[2]]
def procfs_supported():
try:
open('/proc/%d/status' % os.getpid())
except IOError:
return False
else:
return True