Skip to content

Commit c4ba43b

Browse files
committed
remove some of Python 2.5 compatibility hacks
1 parent 1837ad1 commit c4ba43b

File tree

3 files changed

+28
-68
lines changed

3 files changed

+28
-68
lines changed

_line_profiler.pyx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,9 @@ cdef class LineProfiler:
127127
try:
128128
code = func.__code__
129129
except AttributeError:
130-
try:
131-
# Python 2.x
132-
code = func.func_code
133-
except AttributeError:
134-
import warnings
135-
warnings.warn("Could not extract a code object for the object %r" % (func,))
136-
return
130+
import warnings
131+
warnings.warn("Could not extract a code object for the object %r" % (func,))
132+
return
137133
if code not in self.code_map:
138134
self.code_map[code] = {}
139135
self.functions.append(func)

line_profiler.py

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: UTF-8 -*-
3-
from __future__ import with_statement
43
try:
54
import cPickle as pickle
65
except ImportError:
@@ -23,12 +22,6 @@
2322
# ===========================================================
2423
PY3 = sys.version_info[0] == 3
2524

26-
# next:
27-
try:
28-
next_ = next
29-
except NameError:
30-
next_ = lambda obj: obj.next()
31-
3225
# exec (from https://bitbucket.org/gutworth/six/):
3326
if PY3:
3427
import builtins
@@ -53,36 +46,10 @@ def exec_(_code_, _globs_=None, _locs_=None):
5346
def is_generator(f):
5447
""" Return True if a function is a generator.
5548
"""
56-
func_code = f.__code__ if PY3 else f.func_code
49+
func_code = f.__code__
5750
isgen = (func_code.co_flags & CO_GENERATOR) != 0
5851
return isgen
5952

60-
# Code to exec inside of LineProfiler.__call__ to support PEP-342-style
61-
# generators in Python 2.5+.
62-
pep342_gen_wrapper = '''
63-
def wrap_generator(self, func):
64-
""" Wrap a generator to profile it.
65-
"""
66-
def f(*args, **kwds):
67-
g = func(*args, **kwds)
68-
# The first iterate will not be a .send()
69-
self.enable_by_count()
70-
try:
71-
item = next_(g)
72-
finally:
73-
self.disable_by_count()
74-
input = (yield item)
75-
# But any following one might be.
76-
while True:
77-
self.enable_by_count()
78-
try:
79-
item = g.send(input)
80-
finally:
81-
self.disable_by_count()
82-
input = (yield item)
83-
return f
84-
'''
85-
8653
class LineProfiler(CLineProfiler):
8754
""" A profiler that records the execution times of individual lines.
8855
"""
@@ -102,24 +69,27 @@ def __call__(self, func):
10269
f.__dict__.update(getattr(func, '__dict__', {}))
10370
return f
10471

105-
if sys.version_info[:2] >= (2,5):
106-
# Delay compilation because the syntax is not compatible with older
107-
# Python versions.
108-
exec_(pep342_gen_wrapper)
109-
else:
110-
def wrap_generator(self, func):
111-
""" Wrap a generator to profile it.
112-
"""
113-
def f(*args, **kwds):
114-
g = func(*args, **kwds)
115-
while True:
116-
self.enable_by_count()
117-
try:
118-
item = next_(g)
119-
finally:
120-
self.disable_by_count()
121-
yield item
122-
return f
72+
def wrap_generator(self, func):
73+
""" Wrap a generator to profile it.
74+
"""
75+
def f(*args, **kwds):
76+
g = func(*args, **kwds)
77+
# The first iterate will not be a .send()
78+
self.enable_by_count()
79+
try:
80+
item = next(g)
81+
finally:
82+
self.disable_by_count()
83+
input = (yield item)
84+
# But any following one might be.
85+
while True:
86+
self.enable_by_count()
87+
try:
88+
item = g.send(input)
89+
finally:
90+
self.disable_by_count()
91+
input = (yield item)
92+
return f
12393

12494
def wrap_function(self, func):
12595
""" Wrap a function to profile it.
@@ -138,11 +108,8 @@ def dump_stats(self, filename):
138108
object from `get_stats()`.
139109
"""
140110
lstats = self.get_stats()
141-
f = open(filename, 'wb')
142-
try:
111+
with open(filename, 'wb') as f:
143112
pickle.dump(lstats, f, pickle.HIGHEST_PROTOCOL)
144-
finally:
145-
f.close()
146113

147114
def print_stats(self, stream=None):
148115
""" Show the gathered statistics.
@@ -298,10 +265,7 @@ def magic_lprun(self, parameter_s=''):
298265
for name in opts.f:
299266
try:
300267
funcs.append(eval(name, global_ns, local_ns))
301-
except Exception:
302-
# "except Exception as e" is not supported in Python 2.5
303-
# so we're using a hack to get the exception
304-
e = sys.exc_info()[1]
268+
except Exception as e:
305269
raise UsageError('Could not find function %r.\n%s: %s' % (name,
306270
e.__class__.__name__, e))
307271

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
import os.path
23

34
import distutils.errors
@@ -52,7 +53,6 @@
5253
"Programming Language :: C",
5354
"Programming Language :: Python",
5455
'Programming Language :: Python :: 2',
55-
'Programming Language :: Python :: 2.5',
5656
'Programming Language :: Python :: 2.6',
5757
'Programming Language :: Python :: 2.7',
5858
'Programming Language :: Python :: 3',

0 commit comments

Comments
 (0)