Changeset 512
- Timestamp:
- Tue Jul 29 16:17:12 2008
- Files:
-
- trunk/mf.py (modified) (diff)
- trunk/iu.py (modified) (diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
trunk/mf.py
r490 r512 28 28 import suffixes 29 29 30 #=======================Owners==========================#31 # An Owner does imports from a particular piece of turf32 # That is, there's an Owner for each thing on sys.path33 # There are owners for directories and .pyz files.34 # There could be owners for zip files, or even URLs.35 # Note that they replace the string in sys.path,36 # but str(sys.path[n]) should yield the original string.37 38 try:39 STRINGTYPE = basestring40 except NameError:41 STRINGTYPE = type("")42 30 43 31 if not os.environ.has_key('PYTHONCASEOK') and sys.version_info >= (2, 1): … … 58 46 return 'o' 59 47 48 #=======================Owners==========================# 49 # An Owner does imports from a particular piece of turf 50 # That is, there's an Owner for each thing on sys.path 51 # There are owners for directories and .pyz files. 52 # There could be owners for zip files, or even URLs. 53 # Note that they replace the string in sys.path, 54 # but str(sys.path[n]) should yield the original string. 55 60 56 class Owner: 61 57 def __init__(self, path, target_platform=None): 62 58 self.path = path 63 59 self.target_platform = target_platform 60 64 61 def __str__(self): 65 62 return self.path 63 66 64 def getmod(self, nm): 67 65 return None … … 143 141 return mod 144 142 143 145 144 class PYZOwner(Owner): 146 145 def __init__(self, path, target_platform=None): … … 156 155 return PyModule(nm, self.path, co) 157 156 157 158 158 ZipOwner = None 159 159 if zipimport: … … 187 187 class ImportDirector(Owner): 188 188 pass 189 189 190 class BuiltinImportDirector(ImportDirector): 190 191 def __init__(self): 191 192 self.path = 'Builtins' 193 192 194 def getmod(self, nm, isbuiltin=imp.is_builtin): 193 195 if isbuiltin(nm): 194 196 return BuiltinModule(nm) 195 197 return None 198 196 199 class FrozenImportDirector(ImportDirector): 197 200 def __init__(self): 198 201 self.path = 'FrozenModules' 202 199 203 def getmod(self, nm, isfrozen=imp.is_frozen): 200 204 if isfrozen(nm): 201 205 return FrozenModule(nm) 202 206 return None 207 203 208 class RegistryImportDirector(ImportDirector): 204 209 # for Windows only … … 233 238 hkey.Close() 234 239 break 240 235 241 def getmod(self, nm): 236 242 stuff = self.map.get(nm) … … 319 325 # If it *were* importing, it would be the one-and-only ImportManager 320 326 # ie, the builtin import 327 321 328 UNTRIED = -1 322 329 … … 539 546 # here 540 547 return mod 548 541 549 def getwarnings(self): 542 550 warnings = self.warnings.keys() … … 546 554 warnings.append(w+' - %s (%s)' % (mod.__name__, mod.__file__)) 547 555 return warnings 556 548 557 def getxref(self): 549 558 mods = self.modules.items() # (nm, mod) … … 564 573 _ispkg = 0 565 574 typ = 'UNKNOWN' 575 566 576 def __init__(self, nm): 567 577 self.__name__ = nm … … 571 581 self.warnings = [] 572 582 self._xref = {} 583 573 584 def ispackage(self): 574 585 return self._ispkg 586 575 587 def doimport(self, nm): 576 588 pass 589 577 590 def xref(self, nm): 578 591 self._xref[nm] = 1 592 579 593 def __str__(self): 580 594 return "<Module %s %s %s>" % (self.__name__, self.__file__, self.imports) … … 582 596 class BuiltinModule(Module): 583 597 typ = 'BUILTIN' 598 584 599 def __init__(self, nm): 585 600 Module.__init__(self, nm) … … 587 602 class ExtensionModule(Module): 588 603 typ = 'EXTENSION' 604 589 605 def __init__(self, nm, pth): 590 606 Module.__init__(self, nm) … … 593 609 class PyModule(Module): 594 610 typ = 'PYMODULE' 611 595 612 def __init__(self, nm, pth, co): 596 613 Module.__init__(self, nm) … … 600 617 self.__file__ = self.__file__ + pyco() 601 618 self.scancode() 619 602 620 def scancode(self): 603 621 self.imports, self.warnings, allnms = scan_code(self.co) … … 607 625 class PyScript(PyModule): 608 626 typ = 'PYSOURCE' 627 609 628 def __init__(self, pth, co): 610 629 Module.__init__(self, '__main__') … … 613 632 self.scancode() 614 633 634 615 635 class PkgModule(PyModule): 616 636 typ = 'PYMODULE' 637 617 638 def __init__(self, nm, pth, co): 618 639 PyModule.__init__(self, nm, pth, co) … … 621 642 self.__path__ = [ pth ] 622 643 self._update_director(force=True) 644 623 645 def _update_director(self, force=False): 624 646 if force or self.subimporter.path != self.__path__: 625 647 self.subimporter = PathImportDirector(self.__path__) 648 626 649 def doimport(self, nm): 627 650 self._update_director() … … 637 660 self.__path__ = [ str(pyzowner) ] 638 661 self.owner = pyzowner 662 639 663 def doimport(self, nm): 640 664 mod = self.owner.getmod(self.__name__ + '.' + nm) 641 665 return mod 642 666 667 643 668 class PkgInZipModule(PyModule): 644 669 typ = 'ZIPFILE' … … 648 673 self.__path__ = [ str(pyzowner) ] 649 674 self.owner = pyzowner 675 650 676 def doimport(self, nm): 651 677 mod = self.owner.getmod(self.__name__ + '.' + nm) 652 678 return mod 653 679 680 654 681 #======================== Utility ================================# 655 682 # Scan the code object for imports, __all__ and wierd stuff -
trunk/iu.py
r485 r512 1 # 1 2 # Copyright (C) 2005, Giovanni Bajo 3 # 2 4 # Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc. 3 5 # … … 23 25 # along with this program; if not, write to the Free Software 24 26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 25 27 # 28 # 26 29 # **NOTE** This module is used during bootstrap. Import *ONLY* builtin modules. 30 # 31 27 32 import sys 28 33 import imp … … 40 45 zipimport = None 41 46 47 try: 48 STRINGTYPE = basestring 49 except NameError: 50 STRINGTYPE = type("") 51 42 52 #=======================Owners==========================# 43 53 # An Owner does imports from a particular piece of turf … … 49 59 # (or a package's __path__) is still a bunch of strings, 50 60 51 STRINGTYPE = type('')52 53 61 class OwnerError(IOError): 54 def __init__(self, msg):55 self.msg = msg56 62 def __str__(self): 57 return "<OwnerError %s>" % self.msg 63 return "<OwnerError %s>" % self.message 64 58 65 59 66 class Owner: 60 67 def __init__(self, path): 61 68 self.path = path 69 62 70 def __str__(self): 63 71 return self.path 72 64 73 def getmod(self, nm): 65 74 return None 75 76 66 77 class DirOwner(Owner): 67 78 def __init__(self, path): … … 71 82 raise OwnerError("%s is not a directory" % path) 72 83 Owner.__init__(self, path) 73 def getmod(self, nm, getsuffixes=imp.get_suffixes, loadco=marshal.loads, newmod=imp.new_module): 84 85 def getmod(self, nm, getsuffixes=imp.get_suffixes, 86 loadco=marshal.loads, newmod=imp.new_module): 74 87 pth = _os_path_join(self.path, nm) 75 88 possibles = [(pth, 0, None)] … … 125 138 return mod 126 139 140 127 141 ZipOwner = None 128 142 if zipimport: … … 157 171 class ImportDirector(Owner): 158 172 pass 173 159 174 class BuiltinImportDirector(ImportDirector): 160 175 def __init__(self): 161 176 self.path = 'Builtins' 177 162 178 def getmod(self, nm, isbuiltin=imp.is_builtin): 163 179 if isbuiltin(nm): … … 165 181 return mod 166 182 return None 183 167 184 class FrozenImportDirector(ImportDirector): 168 185 def __init__(self): 169 186 self.path = 'FrozenModules' 187 170 188 def getmod(self, nm, isfrozen=imp.is_frozen): 171 189 if isfrozen(nm): … … 175 193 return mod 176 194 return None 195 177 196 class RegistryImportDirector(ImportDirector): 178 197 # for Windows only … … 210 229 hkey.Close() 211 230 break 231 212 232 def getmod(self, nm): 213 233 stuff = self.map.get(nm) … … 236 256 self.inMakeOwner = 0 237 257 self.building = {} 258 238 259 def __str__(self): 239 260 return str(self.path) 261 240 262 def getmod(self, nm): 241 263 mod = None … … 252 274 break 253 275 return mod 276 254 277 def makeOwner(self, path): 255 278 if self.building.get(path): … … 284 307 def __init__(self, args): 285 308 self.args = args 309 286 310 def __repr__(self): 287 311 return "<%s: %s>" % (self.__name__, self.args) 288 312 313 289 314 class ImportManager: 290 315 # really the equivalent of builtin import … … 300 325 self.locker = None 301 326 self.setThreaded() 327 302 328 def setThreaded(self): 303 329 thread = sys.modules.get('thread', None) … … 307 333 self.rlock = thread.allocate_lock() 308 334 self._get_ident = thread.get_ident 335 309 336 def install(self): 310 337 import __builtin__ 311 338 __builtin__.__import__ = self.importHook 312 339 __builtin__.reload = self.reloadHook 340 313 341 def importHook(self, name, globals=None, locals=None, fromlist=None, level=-1): 314 342 # first see if we could be importing a relative name … … 398 426 #print "importHook done with %s %s %s (case 3)" % (name, globals['__name__'], fromlist) 399 427 return bottommod 428 400 429 def doimport(self, nm, parentnm, fqname, reload=0): 401 430 # Not that nm is NEVER a dotted name at this point … … 449 478 raise 450 479 if fqname == 'thread' and not self.threaded: 451 ##print "thread detected!"480 #print "thread detected!" 451 480 self.setThreaded() 452 481 else: … … 455 484 #print "..found %s" % mod, 'when looking for', fqname 456 485 return mod 486 457 487 def reloadHook(self, mod): 458 488 fqnm = mod.__name__ … … 462 492 #mod.__dict__.update(newmod.__dict__) 463 493 return newmod 494 464 495 def _acquire(self): 465 496 if self.rlock.locked(): 466 497 if self.locker == self._get_ident(): 467 498 self.lockcount = self.lockcount + 1 468 ##print "_acquire incrementing lockcount to", self.lockcount499 #print "_acquire incrementing lockcount to", self.lockcount 468 499 return 469 500 self.rlock.acquire() 470 501 self.locker = self._get_ident() 471 502 self.lockcount = 0 472 ## print "_acquire first time!" 503 #print "_acquire first time!" 504 473 505 def _release(self): 474 506 if self.lockcount: 475 507 self.lockcount = self.lockcount - 1 476 ##print "_release decrementing lockcount to", self.lockcount508 #print "_release decrementing lockcount to", self.lockcount 476 508 else: 477 509 self.locker = None 478 510 self.rlock.release() 479 ##print "_release releasing lock!"511 #print "_release releasing lock!" 479 511 480 #========= some helper functions=============================#512 #========= some helper functions =============================# 480 512 481 513 def packagename(s): … … 516 548 return (s[0] & 0170000) == 0040000 517 549 550 518 551 _os_stat = _os_path_join = _os_getcwd = _os_path_dirname = None 552 519 553 def _os_bootstrap(): 520 554 "Set up 'os' module replacement functions for use during import bootstrap." 521 555 556 global _os_stat, _os_path_join, _os_path_dirname, _os_getcwd 557 558 def join(a, b, sep=sep): 559 if a == '': 560 return b 561 lastchar = a[-1:] 562 if lastchar == '/' or lastchar == sep: 563 return a + b 564 return a + sep + b 565 566 def dirname(a, sep=sep, mindirlen=mindirlen): 567 for i in range(len(a)-1, -1, -1): 568 c = a[i] 569 if c == '/' or c == sep: 570 if i < mindirlen: 571 return a[:i+1] 572 return a[:i] 573 return '' 574 522 575 names = sys.builtin_module_names 523 576 … … 525 578 mindirlen = 0 526 579 if 'posix' in names: 580 from posix import stat, getcwd 527 581 sep = '/' 528 582 mindirlen = 1 529 from posix import stat, getcwd530 583 elif 'nt' in names: 584 from nt import stat, getcwd 531 585 sep = '\\' 532 586 mindirlen = 3 533 from nt import stat, getcwd534 587 elif 'dos' in names: 588 from dos import stat, getcwd 535 589 sep = '\\' 536 590 mindirlen = 3 537 from dos import stat, getcwd538 591 elif 'os2' in names: 539 sep = '\\'540 592 from os2 import stat, getcwd 593 sep = '\\' 541 594 elif 'mac' in names: 542 595 from mac import stat, getcwd 596 # overwrite join(): 543 597 def join(a, b): 544 598 if a == '': … … 553 607 raise ImportError, 'no os specific module found' 554 608 555 if join is None:556 def join(a, b, sep=sep):557 if a == '':558 return b559 lastchar = a[-1:]560 if lastchar == '/' or lastchar == sep:561 return a + b562 return a + sep + b563 564 if dirname is None:565 def dirname(a, sep=sep, mindirlen=mindirlen):566 for i in range(len(a)-1, -1, -1):567 c = a[i]568 if c == '/' or c == sep:569 if i < mindirlen:570 return a[:i+1]571 return a[:i]572 return ''573 574 global _os_stat575 609 _os_stat = stat 576 577 global _os_path_join 610 _os_getcwd = getcwd 578 611 _os_path_join = join 579 580 global _os_path_dirname581 612 _os_path_dirname = dirname 582 613 583 global _os_getcwd584 _os_getcwd = getcwd585 614 586 615 _string_replace = _string_join = _string_split = None 616 587 617 def _string_bootstrap(): 588 618 """ … … 593 623 For Python 1.5, we would need the string module, so we need replacements. 594 624 """ 595 s = type('')596 597 625 global _string_replace, _string_join, _string_split 598 626 599 if hasattr(s, "join"): 600 _string_join = s.join 601 else: 602 def join(sep, words): 603 res = '' 604 for w in words: 605 res = res + (sep + w) 606 return res[len(sep):] 607 _string_join = join 608 609 if hasattr(s, "split"): 610 _string_split = s.split 611 else: 612 def split(s, sep, maxsplit=0): 613 res = [] 614 nsep = len(sep) 615 if nsep == 0: 616 return [s] 617 ns = len(s) 618 if maxsplit <= 0: maxsplit = ns 619 i = j = 0 620 count = 0 621 while j+nsep <= ns: 622 if s[j:j+nsep] == sep: 623 count = count + 1 624 res.append(s[i:j]) 625 i = j = j + nsep 626 if count >= maxsplit: break 627 else: 628 j = j + 1 629 res.append(s[i:]) 630 return res 631 _string_split = split 632 633 if hasattr(s, "replace"): 634 _string_replace = s.replace 635 else: 636 def replace(str, old, new): 637 return _string_join(new, _string_split(str, old)) 638 _string_replace = replace 639 627 def join(sep, words): 628 res = '' 629 for w in words: 630 res = res + (sep + w) 631 return res[len(sep):] 632 633 def split(s, sep, maxsplit=0): 634 res = [] 635 nsep = len(sep) 636 if nsep == 0: 637 return [s] 638 ns = len(s) 639 if maxsplit <= 0: maxsplit = ns 640 i = j = 0 641 count = 0 642 while j+nsep <= ns: 643 if s[j:j+nsep] == sep: 644 count = count + 1 645 res.append(s[i:j]) 646 i = j = j + nsep 647 if count >= maxsplit: break 648 else: 649 j = j + 1 650 res.append(s[i:]) 651 return res 652 653 def replace(str, old, new): 654 return _string_join(new, _string_split(str, old)) 655 656 _string_join = getattr(STRINGTYPE, "join", join) 657 _string_split = getattr(STRINGTYPE, "split", split) 658 _string_replace = getattr(STRINGTYPE, "replace", replace) 640 659 641 660
