[Zope-Checkins] CVS: Zope/lib/python/ZPublisher - Publish.py:1.165
Chris McDonough
chrism at zope.com
Tue Oct 21 10:10:47 EDT 2003
Update of /cvs-repository/Zope/lib/python/ZPublisher
In directory cvs.zope.org:/tmp/cvs-serv2472/lib/python/ZPublisher
Modified Files:
Publish.py
Log Message:
Make 'publisher-profile-file' configuration file setting work. Note that the environment variable "PROFILE_PUBLISHER" has been banished with this change. We also change the core of Publish.py to not conditionally define functions in order to have a bit more control over the process.
=== Zope/lib/python/ZPublisher/Publish.py 1.164 => 1.165 ===
--- Zope/lib/python/ZPublisher/Publish.py:1.164 Fri Apr 18 09:51:21 2003
+++ Zope/lib/python/ZPublisher/Publish.py Tue Oct 21 10:10:16 2003
@@ -142,7 +142,7 @@
else: raise
-def publish_module(module_name,
+def publish_module_standard(module_name,
stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
environ=os.environ, debug=0, request=None, response=None):
must_die=0
@@ -295,66 +295,77 @@
if auth_user is not None:
T.setUser(auth_user, request_get('AUTHENTICATION_PATH'))
+# profiling support
-# ZPublisher profiler support
-# ---------------------------
-
-if os.environ.get('PROFILE_PUBLISHER', None):
-
+_pfile = None # profiling filename
+_plock=allocate_lock() # profiling lock
+_pfunc=publish_module_standard
+_pstat=None
+
+def install_profiling(filename):
+ global _pfile
+ _pfile = filename
+
+def pm(module_name, stdin, stdout, stderr,
+ environ, debug, request, response):
+ try:
+ r=_pfunc(module_name, stdin=stdin, stdout=stdout,
+ stderr=stderr, environ=environ, debug=debug,
+ request=request, response=response)
+ except: r=None
+ sys._pr_=r
+
+def publish_module_profiled(module_name, stdin=sys.stdin, stdout=sys.stdout,
+ stderr=sys.stderr, environ=os.environ, debug=0,
+ request=None, response=None):
import profile, pstats
+ global _pstat
+ _plock.acquire()
+ try:
+ if request is not None:
+ path_info=request.get('PATH_INFO')
+ else: path_info=environ.get('PATH_INFO')
+ if path_info[-14:]=='manage_profile':
+ return _pfunc(module_name, stdin=stdin, stdout=stdout,
+ stderr=stderr, environ=environ, debug=debug,
+ request=request, response=response)
+ pobj=profile.Profile()
+ pobj.runcall(pm, module_name, stdin, stdout, stderr,
+ environ, debug, request, response)
+ result=sys._pr_
+ pobj.create_stats()
+ if _pstat is None:
+ _pstat=sys._ps_=pstats.Stats(pobj)
+ else: _pstat.add(pobj)
+ finally:
+ _plock.release()
- _pfile=os.environ['PROFILE_PUBLISHER']
- _plock=allocate_lock()
- _pfunc=publish_module
- _pstat=None
-
- def pm(module_name, stdin, stdout, stderr,
- environ, debug, request, response):
+ if result is None:
try:
- r=_pfunc(module_name, stdin=stdin, stdout=stdout,
- stderr=stderr, environ=environ, debug=debug,
- request=request, response=response)
- except: r=None
- sys._pr_=r
-
- def publish_module(module_name, stdin=sys.stdin, stdout=sys.stdout,
- stderr=sys.stderr, environ=os.environ, debug=0,
- request=None, response=None):
- global _pstat
- _plock.acquire()
- try:
- if request is not None:
- path_info=request.get('PATH_INFO')
- else: path_info=environ.get('PATH_INFO')
- if path_info[-14:]=='manage_profile':
- return _pfunc(module_name, stdin=stdin, stdout=stdout,
- stderr=stderr, environ=environ, debug=debug,
- request=request, response=response)
- pobj=profile.Profile()
- pobj.runcall(pm, module_name, stdin, stdout, stderr,
- environ, debug, request, response)
- result=sys._pr_
- pobj.create_stats()
- if _pstat is None:
- _pstat=sys._ps_=pstats.Stats(pobj)
- else: _pstat.add(pobj)
- finally:
- _plock.release()
-
- if result is None:
- try:
- error=sys.exc_info()
- file=open(_pfile, 'w')
- file.write(
- "See the url "
- "http://www.python.org/doc/current/lib/module-profile.html"
- "\n for information on interpreting profiler statistics.\n\n"
- )
- sys.stdout=file
- _pstat.strip_dirs().sort_stats('cumulative').print_stats(250)
- _pstat.strip_dirs().sort_stats('time').print_stats(250)
- file.flush()
- file.close()
- except: pass
- raise error[0], error[1], error[2]
- return result
+ error=sys.exc_info()
+ file=open(_pfile, 'w')
+ file.write(
+ "See the url "
+ "http://www.python.org/doc/current/lib/module-profile.html"
+ "\n for information on interpreting profiler statistics.\n\n"
+ )
+ sys.stdout=file
+ _pstat.strip_dirs().sort_stats('cumulative').print_stats(250)
+ _pstat.strip_dirs().sort_stats('time').print_stats(250)
+ file.flush()
+ file.close()
+ except: pass
+ raise error[0], error[1], error[2]
+ return result
+
+def publish_module(module_name,
+ stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
+ environ=os.environ, debug=0, request=None, response=None):
+ """ publish a Python module, with or without profiling enabled """
+ if _pfile: # profiling is enabled
+ return publish_module_profiled(module_name, stdin, stdout, stderr,
+ environ, debug, request, response)
+ else:
+ return publish_module_standard(module_name, stdin, stdout, stderr,
+ environ, debug, request, response)
+
More information about the Zope-Checkins
mailing list