/* * tkImgPPM.c -- * * A photo image file handler for PPM (Portable PixMap) files. * * Copyright (c) 1994 The Australian National University. * Copyright (c) 1994-1997 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * * Author: Paul Mackerras (paulus@cs.anu.edu.au), * Department of Computer Science, * Australian National University. * * RCS: @(#) $Id: tkImgPPM.c,v 1.23 2010/01/18 20:43:38 nijtmans Exp $ */ #include "tkInt.h" /* * The maximum amount of memory to allocate for data read from the file. If we * need more than this, we do it in pieces. */ #define MAX_MEMORY 10000 /* don't allocate > 10KB */ /* * Define PGM and PPM, i.e. gray images and color images. */ #define PGM 1 #define PPM 2 /* * The format record for the PPM file format: */ static int FileMatchPPM(Tcl_Channel chan, const char *fileName, Tcl_Obj *format, int *widthPtr, int *heightPtr, Tcl_Interp *interp); static int FileReadPPM(Tcl_Interp *interp, Tcl_Channel chan, const char *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle, int destX, int destY, int width, int height, int srcX, int srcY); static int FileWritePPM(Tcl_Interp *interp, const char *fileName, Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr); static int StringWritePPM(Tcl_Interp *interp, Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr); static int StringMatchPPM(Tcl_Obj *dataObj, Tcl_Obj *format, int *widthPtr, int *heightPtr, Tcl_Interp *interp); static int StringReadPPM(Tcl_Interp *interp, Tcl_Obj *dataObj, Tcl_Obj *format, Tk_PhotoHandle imageHandle, int destX, int destY, int width, int height, int srcX, int srcY); Tk_PhotoImageFormat tkImgFmtPPM = { "ppm", /* name */ FileMatchPPM, /* fileMatchProc */ StringMatchPPM, /* stringMatchProc */ FileReadPPM, /* fileReadProc */ StringReadPPM, /* stringReadProc */ FileWritePPM, /* fileWriteProc */ StringWritePPM, /* stringWriteProc */ NULL }; /* * Prototypes for local functions defined in this file: */ static int ReadPPMFileHeader(Tcl_Channel chan, int *widthPtr, int *heightPtr, int *maxIntensityPtr); static int ReadPPMStringHeader(Tcl_Obj *dataObj, int *widthPtr, int *heightPtr, int *maxIntensityPtr, unsigned char **dataBufferPtr, int *dataSizePtr); /* *---------------------------------------------------------------------- * * FileMatchPPM -- * * This function is invoked by the photo image type to see if a file * contains image data in PPM format. * * Results: * The return value is >0 if the first characters in file "f" look like * PPM data, and 0 otherwise. * * Side effects: * The access position in f may change. * *---------------------------------------------------------------------- */ static int FileMatchPPM( Tcl_Channel chan, /* The image file, open for reading. */ const char *fileName, /* The name of the image file. */ Tcl_Obj *format, /* User-specified format string, or NULL. */ int *widthPtr, int *heightPtr, /* The dimensions of the image are returned * here if the file is a valid raw PPM * file. */ Tcl_Interp *interp) /* unused */ { int dummy; return ReadPPMFileHeader(chan, widthPtr, heightPtr, &dummy); } /* *---------------------------------------------------------------------- * * FileReadPPM -- * * This function is called by the photo image type to read PPM format * data from a file and write it into a given photo image. * * Results: * A standard TCL completion code. If TCL_ERROR is returned then an error * message is left in the interp's result. * * Side effects: * The access position in file f is changed, and new data is added to the * image given by imageHandle. * *---------------------------------------------------------------------- */ static int FileReadPPM( Tcl_Interp *interp, /* Interpreter to use for reporting errors. */ Tcl_Channel chan, /* The image file, open for reading. */ const char *fileName, /* The name of the image file. */ Tcl_Obj *format, /* User-specified format string, or NULL. */ Tk_PhotoHandle imageHandle, /* The photo image to write into. */ int destX, int destY, /* Coordinates of top-left pixel in photo * image to be written to. */ int width, int height, /* Dimensions of block of photo image to be * written to. */ int srcX, int srcY) /* Coordinates of top-left pixel to be used in * image being read. */ { int fileWidth, fileHeight, maxIntensity; int nLines, nBytes, h, type, count; unsigned char *pixelPtr; Tk_PhotoImageBlock block; type = ReadPPMFileHeader(chan, &fileWidth, &fileHeight, &maxIntensity); if (type == 0) { Tcl_AppendResult(interp, "couldn't read raw PPM header from file \"", fileName, "\"", NULL); return TCL_ERROR; } if ((fileWidth <= 0) || (fileHeight <= 0)) { Tcl_AppendResult(interp, "PPM image file \"", fileName, "\" has dimension(s) <= 0", NULL); return TCL_ERROR; } if ((maxIntensity <= 0) || (maxIntensity >= 256)) { char buffer[TCL_INTEGER_SPACE]; sprintf(buffer, "%d", maxIntensity); Tcl_AppendResult(interp, "PPM image file \"", fileName, "\" has bad maximum intensity value ", buffer, NULL); return TCL_ERROR; } if ((srcX + width) > fileWidth) { width = fileWidth - srcX; } if ((srcY + height) > fileHeight) { height = fileHeight - srcY; } if ((width <= 0) || (height <= 0) || (srcX >= fileWidth) || (srcY >= fileHeight)) { return TCL_OK; } if (type == PGM) { block.pixelSize = 1; block.offset[0] = 0; block.offset[1] = 0; block.offset[2] = 0; } else { block.pixelSize = 3; block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; } block.offset[3] = 0; block.width = width; block.pitch = block.pixelSize * fileWidth; if (Tk_PhotoExpand(interp, imageHandle, destX + width, destY + height) != TCL_OK) { return TCL_ERROR; } if (srcY > 0) { Tcl_Seek(chan, (Tcl_WideInt)(srcY * block.pitch), SEEK_CUR); } nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch; if (nLines > height) { nLines = height; } if (nLines <= 0) { nLines = 1; } nBytes = nLines * block.pitch; pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes); block.pixelPtr = pixelPtr + srcX * block.pixelSize; for (h = height; h > 0; h -= nLines) { if (nLines > h) { nLines = h; nBytes = nLines * block.pitch; } count = Tcl_Read(chan, (char *) pixelPtr, nBytes); if (count != nBytes) { Tcl_AppendResult(interp, "error reading PPM image file \"", fileName, "\": ", Tcl_Eof(chan) ? "not enough data" : Tcl_PosixError(interp), NULL); ckfree((char *) pixelPtr); return TCL_ERROR; } if (maxIntensity != 255) { unsigned char *p; for (p = pixelPtr; count > 0; count--, p++) { *p = (((int) *p) * 255)/maxIntensity; } } block.height = nLines; if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY, width, nLines, TK_PHOTO_COMPOSITE_SET) != TCL_OK) { ckfree((char *) pixelPtr); return TCL_ERROR; } destY += nLines; } ckfree((char *) pixelPtr); return TCL_OK; } /* *---------------------------------------------------------------------- * * FileWritePPM -- * * This function is invoked to write image data to a file in PPM format * (although we can read PGM files, we never write them). * * Results: * A standard TCL completion code. If TCL_ERROR is returned then an error * message is left in the interp's result. * * Side effects: * Data is written to the file given by "fileName". * *---------------------------------------------------------------------- */ static int FileWritePPM( Tcl_Interp *interp, const char *fileName, Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr) { Tcl_Channel chan; int w, h, greenOffset, blueOffset, nBytes; unsigned char *pixelPtr, *pixLinePtr; char header[16 + TCL_INTEGER_SPACE * 2]; chan = Tcl_OpenFileChannel(interp, fileName, "w", 0666); if (chan == NULL) { return TCL_ERROR; } if (Tcl_SetChannelOption(interp, chan, "-translation", "binary") != TCL_OK) { Tcl_Close(NULL, chan); return TCL_ERROR; } if (Tcl_SetChannelOption(interp, chan, "-encoding", "binary") != TCL_OK) { Tcl_Close(NULL, chan); return TCL_ERROR; } sprintf(header, "P6\n%d %d\n255\n", blockPtr->width, blockPtr->height); Tcl_Write(chan, header, -1); pixLinePtr = blockPtr->pixelPtr + blockPtr->offset[0]; greenOffset = blockPtr->offset[1] - blockPtr->offset[0]; blueOffset = blockPtr->offset[2] - blockPtr->offset[0]; if ((greenOffset == 1) && (blueOffset == 2) && (blockPtr->pixelSize == 3) && (blockPtr->pitch == (blockPtr->width * 3))) { nBytes = blockPtr->height * blockPtr->pitch; if (Tcl_Write(chan, (char *) pixLinePtr, nBytes) != nBytes) { goto writeerror; } } else { for (h = blockPtr->height; h > 0; h--) { pixelPtr = pixLinePtr; for (w = blockPtr->width; w > 0; w--) { if ( Tcl_Write(chan,(char *)&pixelPtr[0], 1) == -1 || Tcl_Write(chan,(char *)&pixelPtr[greenOffset],1)==-1 || Tcl_Write(chan,(char *)&pixelPtr[blueOffset],1) ==-1) { goto writeerror; } pixelPtr += blockPtr->pixelSize; } pixLinePtr += blockPtr->pitch; } } if (Tcl_Close(NULL, chan) == 0) { return TCL_OK; } chan = NULL; writeerror: Tcl_AppendResult(interp, "error writing \"", fileName, "\": ", Tcl_PosixError(interp), NULL); if (chan != NULL) { Tcl_Close(NULL, chan); } return TCL_ERROR; } /* *---------------------------------------------------------------------- * * StringWritePPM -- * * This function is invoked to write image data to a string in PPM * format. * * Results: * A standard TCL completion code. If TCL_ERROR is returned then an error * message is left in the interp's result. * * Side effects: * None. * *---------------------------------------------------------------------- */ static int StringWritePPM( Tcl_Interp *interp, Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr) { int w, h, size, greenOffset, blueOffset; unsigned char *pixLinePtr, *byteArray; char header[16 + TCL_INTEGER_SPACE * 2]; Tcl_Obj *byteArrayObj; sprintf(header, "P6\n%d %d\n255\n", blockPtr->width, blockPtr->height); /* * Construct a byte array of the right size with the header and * get a pointer to the data part of it. */ size = strlen(header); byteArrayObj = Tcl_NewByteArrayObj((unsigned char *)header, size); byteArray = Tcl_SetByteArrayLength(byteArrayObj, size + 3*blockPtr->width*blockPtr->height); byteArray += size; pixLinePtr = blockPtr->pixelPtr + blockPtr->offset[0]; greenOffset = blockPtr->offset[1] - blockPtr->offset[0]; blueOffset = blockPtr->offset[2] - blockPtr->offset[0]; /* * Check if we can do the data move in single action. */ if ((greenOffset == 1) && (blueOffset == 2) && (blockPtr->pixelSize == 3) && (blockPtr->pitch == (blockPtr->width * 3))) { memcpy(byteArray, pixLinePtr, (unsigned)blockPtr->height * blockPtr->pitch); } else { for (h = blockPtr->height; h > 0; h--) { unsigned char *pixelPtr = pixLinePtr; for (w = blockPtr->width; w > 0; w--) { *byteArray++ = pixelPtr[0]; *byteArray++ = pixelPtr[greenOffset]; *byteArray++ = pixelPtr[blueOffset]; pixelPtr += blockPtr->pixelSize; } pixLinePtr += blockPtr->pitch; } } /* * Return the object in the interpreter result. */ Tcl_SetObjResult(interp, byteArrayObj); return TCL_OK; } /* *---------------------------------------------------------------------- * * StringMatchPPM -- * * This function is invoked by the photo image type to see if a string * contains image data in PPM format. * * Results: * The return value is >0 if the first characters in file "f" look like * PPM data, and 0 otherwise. * * Side effects: * The access position in f may change. * *---------------------------------------------------------------------- */ static int StringMatchPPM( Tcl_Obj *dataObj, /* The image data. */ Tcl_Obj *format, /* User-specified format string, or NULL. */ int *widthPtr, int *heightPtr, /* The dimensions of the image are returned * here if the file is a valid raw PPM * file. */ Tcl_Interp *interp) /* unused */ { int dummy; return ReadPPMStringHeader(dataObj, widthPtr, heightPtr, &dummy, NULL, NULL); } /* *---------------------------------------------------------------------- * * StringReadPPM -- * * This function is called by the photo image type to read PPM format * data from a string and write it into a given photo image. * * Results: * A standard TCL completion code. If TCL_ERROR is returned then an error * message is left in the interp's result. * * Side effects: * New data is added to the image given by imageHandle. * *---------------------------------------------------------------------- */ static int StringReadPPM( Tcl_Interp *interp, /* Interpreter to use for reporting errors. */ Tcl_Obj *dataObj, /* The image data. */ Tcl_Obj *format, /* User-specified format string, or NULL. */ Tk_PhotoHandle imageHandle, /* The photo image to write into. */ int destX, int destY, /* Coordinates of top-left pixel in photo * image to be written to. */ int width, int height, /* Dimensions of block of photo image to be * written to. */ int srcX, int srcY) /* Coordinates of top-left pixel to be used in * image being read. */ { int fileWidth, fileHeight, maxIntensity; int nLines, nBytes, h, type, count, dataSize; unsigned char *pixelPtr, *dataBuffer; Tk_PhotoImageBlock block; type = ReadPPMStringHeader(dataObj, &fileWidth, &fileHeight, &maxIntensity, &dataBuffer, &dataSize); if (type == 0) { Tcl_AppendResult(interp, "couldn't read raw PPM header from string", NULL); return TCL_ERROR; } if ((fileWidth <= 0) || (fileHeight <= 0)) { Tcl_AppendResult(interp, "PPM image data has dimension(s) <= 0", NULL); return TCL_ERROR; } if ((maxIntensity <= 0) || (maxIntensity >= 256)) { char buffer[TCL_INTEGER_SPACE]; sprintf(buffer, "%d", maxIntensity); Tcl_AppendResult(interp, "PPM image data has bad maximum intensity value ", buffer, NULL); return TCL_ERROR; } if ((srcX + width) > fileWidth) { width = fileWidth - srcX; } if ((srcY + height) > fileHeight) { height = fileHeight - srcY; } if ((width <= 0) || (height <= 0) || (srcX >= fileWidth) || (srcY >= fileHeight)) { return TCL_OK; } if (type == PGM) { block.pixelSize = 1; block.offset[0] = 0; block.offset[1] = 0; block.offset[2] = 0; } else { block.pixelSize = 3; block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; } block.offset[3] = 0; block.width = width; block.pitch = block.pixelSize * fileWidth; if (srcY > 0) { dataBuffer += srcY * block.pitch; dataSize -= srcY * block.pitch; } if (maxIntensity == 255) { /* * We have all the data in memory, so write everything in one go. */ if (block.pitch*height > dataSize) { Tcl_AppendResult(interp, "truncated PPM data", NULL); return TCL_ERROR; } block.pixelPtr = dataBuffer + srcX * block.pixelSize; block.height = height; return Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY, width, height, TK_PHOTO_COMPOSITE_SET); } if (Tk_PhotoExpand(interp, imageHandle, destX + width, destY + height) != TCL_OK) { return TCL_ERROR; } nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch; if (nLines > height) { nLines = height; } if (nLines <= 0) { nLines = 1; } nBytes = nLines * block.pitch; pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes); block.pixelPtr = pixelPtr + srcX * block.pixelSize; for (h = height; h > 0; h -= nLines) { unsigned char *p; if (nLines > h) { nLines = h; nBytes = nLines * block.pitch; } if (dataSize < nBytes) { ckfree((char *) pixelPtr); Tcl_AppendResult(interp, "truncated PPM data", NULL); return TCL_ERROR; } for (p=pixelPtr,count=nBytes ; count>0 ; count--,p++,dataBuffer++) { *p = (((int) *dataBuffer) * 255)/maxIntensity; } dataSize -= nBytes; block.height = nLines; if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY, width, nLines, TK_PHOTO_COMPOSITE_SET) != TCL_OK) { ckfree((char *) pixelPtr); return TCL_ERROR; } destY += nLines; } ckfree((char *) pixelPtr); return TCL_OK; } /* *---------------------------------------------------------------------- * * ReadPPMFileHeader -- * * This function reads the PPM header from the beginning of a PPM file * and returns information from the header. * * Results: * The return value is PGM if file "f" appears to start with a valid PGM * header, PPM if "f" appears to start with a valid PPM header, and 0 * otherwise. If the header is valid, then *widthPtr and *heightPtr are * modified to hold the dimensions of the image and *maxIntensityPtr is * modified to hold the value of a "fully on" intensity value. * * Side effects: * The access position in f advances. * *---------------------------------------------------------------------- */ static int ReadPPMFileHeader( Tcl_Channel chan, /* Image file to read the header from. */ int *widthPtr, int *heightPtr, /* The dimensions of the image are returned * here. */ int *maxIntensityPtr) /* The maximum intensity value for the image * is stored here. */ { #define BUFFER_SIZE 1000 char buffer[BUFFER_SIZE], c; int i, numFields, type = 0; /* * Read 4 space-separated fields from the file, ignoring comments (any * line that starts with "#"). */ if (Tcl_Read(chan, &c, 1) != 1) { return 0; } i = 0; for (numFields = 0; numFields < 4; numFields++) { /* * Skip comments and white space. */ while (1) { while (isspace(UCHAR(c))) { if (Tcl_Read(chan, &c, 1) != 1) { return 0; } } if (c != '#') { break; } do { if (Tcl_Read(chan, &c, 1) != 1) { return 0; } } while (c != '\n'); } /* * Read a field (everything up to the next white space). */ while (!isspace(UCHAR(c))) { if (i < (BUFFER_SIZE-2)) { buffer[i] = c; i++; } if (Tcl_Read(chan, &c, 1) != 1) { goto done; } } if (i < (BUFFER_SIZE-1)) { buffer[i] = ' '; i++; } } done: buffer[i] = 0; /* * Parse the fields, which are: id, width, height, maxIntensity. */ if (strncmp(buffer, "P6 ", 3) == 0) { type = PPM; } else if (strncmp(buffer, "P5 ", 3) == 0) { type = PGM; } else { return 0; } if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr) != 3) { return 0; } return type; } /* *---------------------------------------------------------------------- * * ReadPPMStringHeader -- * * This function reads the PPM header from the beginning of a PPM-format * string and returns information from the header. * * Results: * The return value is PGM if the string appears to start with a valid * PGM header, PPM if the string appears to start with a valid PPM * header, and 0 otherwise. If the header is valid, then *widthPtr and * *heightPtr are modified to hold the dimensions of the image and * *maxIntensityPtr is modified to hold the value of a "fully on" * intensity value. * * Side effects: * None * *---------------------------------------------------------------------- */ static int ReadPPMStringHeader( Tcl_Obj *dataPtr, /* Object to read the header from. */ int *widthPtr, int *heightPtr, /* The dimensions of the image are returned * here. */ int *maxIntensityPtr, /* The maximum intensity value for the image * is stored here. */ unsigned char **dataBufferPtr, int *dataSizePtr) { #define BUFFER_SIZE 1000 char buffer[BUFFER_SIZE], c; int i, numFields, dataSize, type = 0; unsigned char *dataBuffer; dataBuffer = Tcl_GetByteArrayFromObj(dataPtr, &dataSize); /* * Read 4 space-separated fields from the string, ignoring comments (any * line that starts with "#"). */ if (dataSize-- < 1) { return 0; } c = (char) (*dataBuffer++); i = 0; for (numFields = 0; numFields < 4; numFields++) { /* * Skip comments and white space. */ while (1) { while (isspace(UCHAR(c))) { if (dataSize-- < 1) { return 0; } c = (char) (*dataBuffer++); } if (c != '#') { break; } do { if (dataSize-- < 1) { return 0; } c = (char) (*dataBuffer++); } while (c != '\n'); } /* * Read a field (everything up to the next white space). */ while (!isspace(UCHAR(c))) { if (i < (BUFFER_SIZE-2)) { buffer[i] = c; i++; } if (dataSize-- < 1) { goto done; } c = (char) (*dataBuffer++); } if (i < (BUFFER_SIZE-1)) { buffer[i] = ' '; i++; } } done: buffer[i] = 0; /* * Parse the fields, which are: id, width, height, maxIntensity. */ if (strncmp(buffer, "P6 ", 3) == 0) { type = PPM; } else if (strncmp(buffer, "P5 ", 3) == 0) { type = PGM; } else { return 0; } if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr) != 3) { return 0; } if (dataBufferPtr != NULL) { *dataBufferPtr = dataBuffer; *dataSizePtr = dataSize; } return type; } /* * Local Variables: * mode: c * c-basic-offset: 4 * fill-column: 78 * End: */ ss="hl str"> 2) A "module loader" class provides an interface to search for a module in a search path and to load it. It defines a method which searches for a module in a single directory; by overriding this method one can redefine the details of the search. If the directory is None, built-in and frozen modules are searched instead. Two module loader class are defined, both implementing the search strategy used by the built-in __import__ function: ModuleLoader uses the imp module's find_module interface, while HookableModuleLoader uses a file system hooks class to interact with the file system. Both use the imp module's load_* interfaces to actually load the module. 3) A "module importer" class provides an interface to import a module, as well as interfaces to reload and unload a module. It also provides interfaces to install and uninstall itself instead of the default __import__ and reload (and unload) functions. One module importer class is defined (ModuleImporter), which uses a module loader instance passed in (by default HookableModuleLoader is instantiated). The classes defined here should be used as base classes for extended functionality along those lines. If a module importer class supports dotted names, its import_module() must return a different value depending on whether it is called on behalf of a "from ... import ..." statement or not. (This is caused by the way the __import__ hook is used by the Python interpreter.) It would also do wise to install a different version of reload(). """ import __builtin__ import imp import os import sys __all__ = ["BasicModuleLoader","Hooks","ModuleLoader","FancyModuleLoader", "BasicModuleImporter","ModuleImporter","install","uninstall"] VERBOSE = 0 from imp import C_EXTENSION, PY_SOURCE, PY_COMPILED from imp import C_BUILTIN, PY_FROZEN, PKG_DIRECTORY BUILTIN_MODULE = C_BUILTIN FROZEN_MODULE = PY_FROZEN class _Verbose: def __init__(self, verbose = VERBOSE): self.verbose = verbose def get_verbose(self): return self.verbose def set_verbose(self, verbose): self.verbose = verbose # XXX The following is an experimental interface def note(self, *args): if self.verbose: self.message(*args) def message(self, format, *args): if args: print format%args else: print format class BasicModuleLoader(_Verbose): """Basic module loader. This provides the same functionality as built-in import. It doesn't deal with checking sys.modules -- all it provides is find_module() and a load_module(), as well as find_module_in_dir() which searches just one directory, and can be overridden by a derived class to change the module search algorithm when the basic dependency on sys.path is unchanged. The interface is a little more convenient than imp's: find_module(name, [path]) returns None or 'stuff', and load_module(name, stuff) loads the module. """ def find_module(self, name, path = None): if path is None: path = [None] + self.default_path() for dir in path: stuff = self.find_module_in_dir(name, dir) if stuff: return stuff return None def default_path(self): return sys.path def find_module_in_dir(self, name, dir): if dir is None: return self.find_builtin_module(name) else: try: return imp.find_module(name, [dir]) except ImportError: return None def find_builtin_module(self, name): # XXX frozen packages? if imp.is_builtin(name): return None, '', ('', '', BUILTIN_MODULE) if imp.is_frozen(name): return None, '', ('', '', FROZEN_MODULE) return None def load_module(self, name, stuff): file, filename, info = stuff try: return imp.load_module(name, file, filename, info) finally: if file: file.close() class Hooks(_Verbose): """Hooks into the filesystem and interpreter. By deriving a subclass you can redefine your filesystem interface, e.g. to merge it with the URL space. This base class behaves just like the native filesystem. """ # imp interface def get_suffixes(self): return imp.get_suffixes() def new_module(self, name): return imp.new_module(name) def is_builtin(self, name): return imp.is_builtin(name) def init_builtin(self, name): return imp.init_builtin(name) def is_frozen(self, name): return imp.is_frozen(name) def init_frozen(self, name): return imp.init_frozen(name) def get_frozen_object(self, name): return imp.get_frozen_object(name) def load_source(self, name, filename, file=None): return imp.load_source(name, filename, file) def load_compiled(self, name, filename, file=None): return imp.load_compiled(name, filename, file) def load_dynamic(self, name, filename, file=None): return imp.load_dynamic(name, filename, file) def load_package(self, name, filename, file=None): return imp.load_module(name, file, filename, ("", "", PKG_DIRECTORY)) def add_module(self, name): d = self.modules_dict() if name in d: return d[name] d[name] = m = self.new_module(name) return m # sys interface def modules_dict(self): return sys.modules def default_path(self): return sys.path def path_split(self, x): return os.path.split(x) def path_join(self, x, y): return os.path.join(x, y) def path_isabs(self, x): return os.path.isabs(x) # etc. def path_exists(self, x): return os.path.exists(x) def path_isdir(self, x): return os.path.isdir(x) def path_isfile(self, x): return os.path.isfile(x) def path_islink(self, x): return os.path.islink(x) # etc. def openfile(self, *x): return open(*x) openfile_error = IOError def listdir(self, x): return os.listdir(x) listdir_error = os.error # etc. class ModuleLoader(BasicModuleLoader): """Default module loader; uses file system hooks. By defining suitable hooks, you might be able to load modules from other sources than the file system, e.g. from compressed or encrypted files, tar files or (if you're brave!) URLs. """ def __init__(self, hooks = None, verbose = VERBOSE): BasicModuleLoader.__init__(self, verbose) self.hooks = hooks or Hooks(verbose) def default_path(self): return self.hooks.default_path() def modules_dict(self): return self.hooks.modules_dict() def get_hooks(self): return self.hooks def set_hooks(self, hooks): self.hooks = hooks def find_builtin_module(self, name): # XXX frozen packages? if self.hooks.is_builtin(name): return None, '', ('', '', BUILTIN_MODULE) if self.hooks.is_frozen(name): return None, '', ('', '', FROZEN_MODULE) return None def find_module_in_dir(self, name, dir, allow_packages=1): if dir is None: return self.find_builtin_module(name) if allow_packages: fullname = self.hooks.path_join(dir, name) if self.hooks.path_isdir(fullname): stuff = self.find_module_in_dir("__init__", fullname, 0) if stuff: file = stuff[0] if file: file.close() return None, fullname, ('', '', PKG_DIRECTORY) for info in self.hooks.get_suffixes(): suff, mode, type = info fullname = self.hooks.path_join(dir, name+suff) try: fp = self.hooks.openfile(fullname, mode) return fp, fullname, info except self.hooks.openfile_error: pass return None def load_module(self, name, stuff): file, filename, info = stuff (suff, mode, type) = info try: if type == BUILTIN_MODULE: return self.hooks.init_builtin(name) if type == FROZEN_MODULE: return self.hooks.init_frozen(name) if type == C_EXTENSION: m = self.hooks.load_dynamic(name, filename, file) elif type == PY_SOURCE: m = self.hooks.load_source(name, filename, file) elif type == PY_COMPILED: m = self.hooks.load_compiled(name, filename, file) elif type == PKG_DIRECTORY: m = self.hooks.load_package(name, filename, file) else: raise ImportError, "Unrecognized module type (%r) for %s" % \ (type, name) finally: if file: file.close() m.__file__ = filename return m class FancyModuleLoader(ModuleLoader): """Fancy module loader -- parses and execs the code itself.""" def load_module(self, name, stuff): file, filename, (suff, mode, type) = stuff realfilename = filename path = None if type == PKG_DIRECTORY: initstuff = self.find_module_in_dir("__init__", filename, 0) if not initstuff: raise ImportError, "No __init__ module in package %s" % name initfile, initfilename, initinfo = initstuff initsuff, initmode, inittype = initinfo if inittype not in (PY_COMPILED, PY_SOURCE): if initfile: initfile.close() raise ImportError, \ "Bad type (%r) for __init__ module in package %s" % ( inittype, name) path = [filename] file = initfile realfilename = initfilename type = inittype if type == FROZEN_MODULE: code = self.hooks.get_frozen_object(name) elif type == PY_COMPILED: import marshal file.seek(8) code = marshal.load(file) elif type == PY_SOURCE: data = file.read() code = compile(data, realfilename, 'exec') else: return ModuleLoader.load_module(self, name, stuff) m = self.hooks.add_module(name) if path: m.__path__ = path m.__file__ = filename try: exec code in m.__dict__ except: d = self.hooks.modules_dict() if name in d: del d[name] raise return m class BasicModuleImporter(_Verbose): """Basic module importer; uses module loader. This provides basic import facilities but no package imports. """ def __init__(self, loader = None, verbose = VERBOSE): _Verbose.__init__(self, verbose) self.loader = loader or ModuleLoader(None, verbose) self.modules = self.loader.modules_dict() def get_loader(self): return self.loader def set_loader(self, loader): self.loader = loader def get_hooks(self): return self.loader.get_hooks() def set_hooks(self, hooks): return self.loader.set_hooks(hooks) def import_module(self, name, globals={}, locals={}, fromlist=[]): name = str(name) if name in self.modules: return self.modules[name] # Fast path stuff = self.loader.find_module(name) if not stuff: raise ImportError, "No module named %s" % name return self.loader.load_module(name, stuff) def reload(self, module, path = None): name = str(module.__name__) stuff = self.loader.find_module(name, path) if not stuff: raise ImportError, "Module %s not found for reload" % name return self.loader.load_module(name, stuff) def unload(self, module): del self.modules[str(module.__name__)] # XXX Should this try to clear the module's namespace? def install(self): self.save_import_module = __builtin__.__import__ self.save_reload = __builtin__.reload if not hasattr(__builtin__, 'unload'): __builtin__.unload = None self.save_unload = __builtin__.unload __builtin__.__import__ = self.import_module __builtin__.reload = self.reload __builtin__.unload = self.unload def uninstall(self): __builtin__.__import__ = self.save_import_module __builtin__.reload = self.save_reload __builtin__.unload = self.save_unload if not __builtin__.unload: del __builtin__.unload class ModuleImporter(BasicModuleImporter): """A module importer that supports packages.""" def import_module(self, name, globals=None, locals=None, fromlist=None): parent = self.determine_parent(globals) q, tail = self.find_head_package(parent, str(name)) m = self.load_tail(q, tail) if not fromlist: return q if hasattr(m, "__path__"): self.ensure_fromlist(m, fromlist) return m def determine_parent(self, globals): if not globals or not "__name__" in globals: return None pname = globals['__name__'] if "__path__" in globals: parent = self.modules[pname] assert globals is parent.__dict__ return parent if '.' in pname: i = pname.rfind('.') pname = pname[:i] parent = self.modules[pname] assert parent.__name__ == pname return parent return None def find_head_package(self, parent, name): if '.' in name: i = name.find('.') head = name[:i] tail = name[i+1:] else: head = name tail = "" if parent: qname = "%s.%s" % (parent.__name__, head) else: qname = head q = self.import_it(head, qname, parent) if q: return q, tail if parent: qname = head parent = None q = self.import_it(head, qname, parent) if q: return q, tail raise ImportError, "No module named " + qname def load_tail(self, q, tail): m = q while tail: i = tail.find('.') if i < 0: i = len(tail) head, tail = tail[:i], tail[i+1:] mname = "%s.%s" % (m.__name__, head) m = self.import_it(head, mname, m) if not m: raise ImportError, "No module named " + mname return m def ensure_fromlist(self, m, fromlist, recursive=0): for sub in fromlist: if sub == "*": if not recursive: try: all = m.__all__ except AttributeError: pass else: self.ensure_fromlist(m, all, 1) continue if sub != "*" and not hasattr(m, sub): subname = "%s.%s" % (m.__name__, sub) submod = self.import_it(sub, subname, m) if not submod: raise ImportError, "No module named " + subname def import_it(self, partname, fqname, parent, force_load=0): if not partname: raise ValueError, "Empty module name" if not force_load: try: return self.modules[fqname] except KeyError: pass try: path = parent and parent.__path__ except AttributeError: return None partname = str(partname) stuff = self.loader.find_module(partname, path) if not stuff: return None fqname = str(fqname) m = self.loader.load_module(fqname, stuff) if parent: setattr(parent, partname, m) return m def reload(self, module): name = str(module.__name__) if '.' not in name: return self.import_it(name, name, None, force_load=1) i = name.rfind('.') pname = name[:i] parent = self.modules[pname] return self.import_it(name[i+1:], name, parent, force_load=1) default_importer = None current_importer = None def install(importer = None): global current_importer current_importer = importer or default_importer or ModuleImporter() current_importer.install() def uninstall(): global current_importer current_importer.uninstall()