How to extend/overload range() function from Python 2.7 to accept floats? -


our core application changed python 2.6 python 2.7 maybe python 3 in later release.

range function python changed (quote python 2.7 changelog).

the range() function processes arguments more consistently; call __int__() on non-float.

we allow users add expressions / python code based on results process further.

now how change range function? of them using float argument failing in python 2.7.

as code written users cannot change in python code/expression. there 1000s of files. users may have own files.

  1. is there way extend range() function python, such take float arguments?

  2. another alternative parse python code , change float int. time consuming requires lot of sting manipulation , range calls have formulas parameter.

our application build in c++, , evaluate python expression using c++ python apis

you have serious migration issue. if want switch python 2.7 , python 3, there no easy way around refactoring existing (user) code base eventually. imho, have following options.

  1. provide permanent (non-optional) 2.6 compatible interface users. means not have change anything, kind of defeats purpose of upgrading python 2.7 since users still have satisfy python 2.6 semantics. verdict: not recommended.

  2. provide temporary (non-optional) 2.6 compatible interface limited amount of time. in case existing code needs refactored eventually. verdict: not recommended.

  3. make users include flag in code (e.g. magic comment can identified safely without executing file # *$$ supertool-pythonversion: 2.7 $$*), python version code expects run , use 2.6 compatibility files have not been flagged python 2.7. way, can whatever compatibility hacks needed run old files , run new files way are. verdict: increases complexity, helps doing migration. recommended.

however, in convenient position of calling python c++. can control environment scripts run via globals , locals dictionary passed pyeval_evalcode. in order implement scenario 3, after checking compatibility flag file, can put custom range function supports float arguments gloabls dictionary before calling pyeval_evalcode "enable" compatibility mode.

i not proficient python's c api, in python (and possible same via c api):

range27 = range  def range26(start=none, stop=none, step=none):     if start not none , not isinstance(start, int):         start = int(start)     if stop not none , not isinstance(stop, int):         stop = int(stop)     if step not none , not isinstance(step, int):         step = int(step)     return range27(start, stop, step)  def execute_user_code(user_file):     ...     src = read(user_file)     global_dict = {}     local_dict = {}     ...      if check_magic_version_comment(src) in (none, '2.6'):         global_dict['range'] = range26         global_dict['range27'] = range27         # last line needed because call         # of range27 resolved against global_dict         # when user code executed      eval_code(src, global_dict, local_dict)     ... 

Comments