1 import pyximport; pyximport.install(reload_support=True) 2 import os, sys 3 import time, shutil 4 import tempfile 5 6 def make_tempdir(): 7 tempdir = os.path.join(tempfile.gettempdir(), "pyrex_temp") 8 if os.path.exists(tempdir): 9 remove_tempdir(tempdir) 10 11 os.mkdir(tempdir) 12 return tempdir 13 14 def remove_tempdir(tempdir): 15 shutil.rmtree(tempdir, 0, on_remove_file_error) 16 17 def on_remove_file_error(func, path, excinfo): 18 print "Sorry! Could not remove a temp file:", path 19 print "Extra information." 20 print func, excinfo 21 print "You may want to delete this yourself when you get a chance." 22 23 def test(): 24 pyximport._test_files = [] 25 tempdir = make_tempdir() 26 sys.path.append(tempdir) 27 filename = os.path.join(tempdir, "dummy.pyx") 28 open(filename, "w").write("print 'Hello world from the Pyrex install hook'") 29 import dummy 30 reload(dummy) 31 32 depend_filename = os.path.join(tempdir, "dummy.pyxdep") 33 depend_file = open(depend_filename, "w") 34 depend_file.write("*.txt\nfoo.bar") 35 depend_file.close() 36 37 build_filename = os.path.join(tempdir, "dummy.pyxbld") 38 build_file = open(build_filename, "w") 39 build_file.write(""" 40 from distutils.extension import Extension 41 def make_ext(name, filename): 42 return Extension(name=name, sources=[filename]) 43 """) 44 build_file.close() 45 46 open(os.path.join(tempdir, "foo.bar"), "w").write(" ") 47 open(os.path.join(tempdir, "1.txt"), "w").write(" ") 48 open(os.path.join(tempdir, "abc.txt"), "w").write(" ") 49 reload(dummy) 50 assert len(pyximport._test_files)==1, pyximport._test_files 51 reload(dummy) 52 53 time.sleep(1) # sleep a second to get safer mtimes 54 open(os.path.join(tempdir, "abc.txt"), "w").write(" ") 55 print "Here goes the reolad" 56 reload(dummy) 57 assert len(pyximport._test_files) == 1, pyximport._test_files 58 59 reload(dummy) 60 assert len(pyximport._test_files) ==0, pyximport._test_files 61 remove_tempdir(tempdir) 62 63 if __name__=="__main__": 64 test() 65 66