[Python-Dev] SF patch 864863: Bisect C implementation

Samuele Pedroni pedronis at bluewin.ch
Fri Jan 2 10:31:28 EST 2004


At 15:47 02.01.2004 +0100, Samuele Pedroni wrote:

>import heapq # import likely the C code
>test(heapq) # or heapq could be a global
>heapq = test.support.get_pure_python_version(heapq)

oops, that should have been test_support

>test(heapq)


here's a tentative minimally tested(*) version of get_pure_python_version:

def get_pure_python_version(mod,bltin_mod_name=None):
   DUMMY = object()
   mod_name = mod.__name__
   if bltin_mod_name is None:
     bltin_mod_name = '_'+mod_name
   import inspect,sys,new
   pure_py_ver = new.module('pure-python-'+mod_name) # ?
   saved_bltin_mod = sys.modules.get(bltin_mod_name,DUMMY)

   # import _<mod_name> should fail momentarily
   sys.modules[bltin_mod_name] = None # or an empty module
   execfile(inspect.getsourcefile(mod),pure_py_ver.__dict__)

   if saved_bltin_mod is DUMMY:
     del sys.modules[bltin_mod_name]
   else:
     sys.modules[bltin_mod_name] = saved_bltin_mod

   return pure_py_ver



we could also detect the case were there's no builtin version, which means 
that the first test likely already tested just the pure Python version.

*: tested with:

<array2.py>
class array(object):
   def __init__(*args,**kws):
     pass

   def __repr__(self):
     return "<fake pure python array>"

try:
   from array import array
except ImportError:
   pass
</array2.py>

import array2
ppy = get_pure_python_version(array2)






More information about the Python-Dev mailing list