Skip to content

Commit 8b3ec4b

Browse files
committed
Future-proof namelambda() for Python 3.8 (see PEP 570: positional-only parameters)
1 parent ff84ba7 commit 8b3ec4b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

unpythonic/misc.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from time import time
88
from copy import copy
99
from functools import partial
10+
from sys import version_info
1011

1112
from .regutil import register_decorator
1213
from .lazyutil import mark_lazy, lazycall, force
@@ -293,13 +294,24 @@ def rename(f):
293294
# that we can use to re-create the code object with the new name.
294295
# (This is no worse than what the stdlib's Lib/modulefinder.py already does.)
295296
co = f.__code__
296-
f.__code__ = CodeType(co.co_argcount, co.co_kwonlyargcount,
297-
co.co_nlocals, co.co_stacksize, co.co_flags,
298-
co.co_code, co.co_consts, co.co_names,
299-
co.co_varnames, co.co_filename,
300-
name,
301-
co.co_firstlineno, co.co_lnotab, co.co_freevars,
302-
co.co_cellvars)
297+
# https://github.com/ipython/ipython/blob/master/IPython/core/interactiveshell.py
298+
# https://www.python.org/dev/peps/pep-0570/
299+
if version_info > (3, 8, 0, 'alpha', 3):
300+
f.__code__ = CodeType(co.co_argcount, co.co_posonlyargcount, co.co_kwonlyargcount,
301+
co.co_nlocals, co.co_stacksize, co.co_flags,
302+
co.co_code, co.co_consts, co.co_names,
303+
co.co_varnames, co.co_filename,
304+
name,
305+
co.co_firstlineno, co.co_lnotab, co.co_freevars,
306+
co.co_cellvars)
307+
else:
308+
f.__code__ = CodeType(co.co_argcount, co.co_kwonlyargcount,
309+
co.co_nlocals, co.co_stacksize, co.co_flags,
310+
co.co_code, co.co_consts, co.co_names,
311+
co.co_varnames, co.co_filename,
312+
name,
313+
co.co_firstlineno, co.co_lnotab, co.co_freevars,
314+
co.co_cellvars)
303315
return f
304316
return rename
305317

0 commit comments

Comments
 (0)