Skip to content

Commit 15a7e12

Browse files
authored
Merge pull request #168 from Juanlu001/require-psutil
Require psutil
2 parents e2a97b9 + 4fb941e commit 15a7e12

4 files changed

Lines changed: 37 additions & 35 deletions

File tree

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ python:
33
- "2.7"
44
- "3.3"
55
install:
6-
- sudo apt-get update
76
# We do this conditionally because it saves us some downloading if the
87
# version is the same.
98
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
@@ -24,4 +23,6 @@ install:
2423
- source activate test-environment
2524
- python setup.py install
2625
script:
27-
- make test
26+
- make test
27+
28+
sudo: false

README.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
This is a python module for monitoring memory consumption of a process
99
as well as line-by-line analysis of memory consumption for python
10-
programs. It is a pure python module and has the `psutil
11-
<http://pypi.python.org/pypi/psutil>`_ module as optional (but highly
12-
recommended) dependencies.
10+
programs. It is a pure python module which depends on the `psutil
11+
<http://pypi.python.org/pypi/psutil>`_ module.
1312

1413

1514
==============
@@ -385,7 +384,7 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
385384
between runs.
386385

387386
* Q: Does it work under windows ?
388-
* A: Yes, but you will need the
387+
* A: Yes, thanks to the
389388
`psutil <http://pypi.python.org/pypi/psutil>`_ module.
390389

391390

memory_profiler.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# .. we'll use this to pass it to the child script ..
44
_CLEAN_GLOBALS = globals().copy()
55

6-
__version__ = '0.47'
6+
__version__ = '0.48.dev0'
77

88
_CMD_USAGE = "python -m memory_profiler script_file.py"
99

@@ -19,6 +19,8 @@
1919
import traceback
2020
from signal import SIGKILL
2121

22+
import psutil
23+
2224

2325
# TODO: provide alternative when multiprocessing is not available
2426
try:
@@ -48,13 +50,6 @@ def unicode(x, *args):
4850
return str(x)
4951

5052
# .. get available packages ..
51-
try:
52-
import psutil
53-
54-
has_psutil = True
55-
except ImportError:
56-
has_psutil = False
57-
5853
try:
5954
import tracemalloc
6055

@@ -92,12 +87,6 @@ def _get_child_memory(process, meminfo_attr=None):
9287
"""
9388
Returns a generator that yields memory for all child processes.
9489
"""
95-
if not has_psutil:
96-
raise NotImplementedError((
97-
"The psutil module is required to monitor the "
98-
"memory usage of child processes."
99-
))
100-
10190
# Convert a pid to a process
10291
if isinstance(process, int):
10392
if process == -1: process = os.getpid()
@@ -344,10 +333,9 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
344333
if retval:
345334
ret = ret, returned
346335
except Exception:
347-
if has_psutil:
348-
parent = psutil.Process(os.getpid())
349-
for child in parent.children(recursive=True):
350-
os.kill(child.pid, SIGKILL)
336+
parent = psutil.Process(os.getpid())
337+
for child in parent.children(recursive=True):
338+
os.kill(child.pid, SIGKILL)
351339
p.join(0)
352340
raise
353341

@@ -1086,10 +1074,9 @@ def choose_backend(new_backend=None):
10861074

10871075
_backend = 'no_backend'
10881076
all_backends = [
1089-
('psutil', has_psutil),
1077+
('psutil', True),
10901078
('posix', os.name == 'posix'),
10911079
('tracemalloc', has_tracemalloc),
1092-
('no_backend', True)
10931080
]
10941081
backends_indices = dict((b[0], i) for i, b in enumerate(all_backends))
10951082

@@ -1100,10 +1087,6 @@ def choose_backend(new_backend=None):
11001087
if is_available:
11011088
_backend = n_backend
11021089
break
1103-
if _backend == 'no_backend':
1104-
raise NotImplementedError(
1105-
'Tracemalloc or psutil module is required for non-unix '
1106-
'platforms')
11071090
if _backend != new_backend and new_backend is not None:
11081091
warnings.warn('{0} can not be used, {1} used instead'.format(
11091092
new_backend, _backend))

setup.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
import memory_profiler
2-
from distutils.core import setup
3-
import setuptools
1+
import os
2+
import io
3+
import re
4+
from setuptools import setup
5+
6+
7+
# https://packaging.python.org/guides/single-sourcing-package-version/
8+
def read(*names, **kwargs):
9+
with io.open(
10+
os.path.join(os.path.dirname(__file__), *names),
11+
encoding=kwargs.get("encoding", "utf8")
12+
) as fp:
13+
return fp.read()
14+
15+
16+
def find_version(*file_paths):
17+
version_file = read(*file_paths)
18+
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
19+
version_file, re.M)
20+
if version_match:
21+
return version_match.group(1)
22+
423

524
CLASSIFIERS = """\
625
Development Status :: 5 - Production/Stable
@@ -24,13 +43,13 @@
2443
name='memory_profiler',
2544
description='A module for monitoring memory usage of a python program',
2645
long_description=open('README.rst').read(),
27-
version=memory_profiler.__version__,
46+
version=find_version("memory_profiler.py"),
2847
author='Fabian Pedregosa',
2948
author_email='f@bianp.net',
3049
url='http://pypi.python.org/pypi/memory_profiler',
3150
py_modules=['memory_profiler'],
3251
scripts=['mprof'],
52+
install_requires=['psutil'],
3353
classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f],
3454
license='BSD'
35-
3655
)

0 commit comments

Comments
 (0)