[Python-checkins] r42269 - in python/trunk: Doc/lib/lib.tex Doc/lib/libhotshot.tex Doc/lib/libprofile.tex Lib/cProfile.py Lib/pstats.py Lib/test/output/test_cProfile Lib/test/output/test_profile Lib/test/test_cProfile.py Lib/test/test_profile.py

Neal Norwitz nnorwitz at gmail.com
Thu Feb 9 05:24:51 CET 2006


On 2/8/06, armin.rigo <python-checkins at python.org> wrote:
> Added: python/trunk/Lib/cProfile.py
>
> +# Backwards compatibility.
> +def help():
> +    print "Documentation for the profile/cProfile modules can be found "
> +    print "in the Python Library Reference, section 'The Python Profiler'."

Should this generate a warning?  When should support for help be removed?

> +def main():
> +    import os, sys
> +    from optparse import OptionParser
> +    usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
> +    parser = OptionParser(usage=usage)
> +    parser.allow_interspersed_args = False
> +    parser.add_option('-o', '--outfile', dest="outfile",
> +        help="Save stats to <outfile>", default=None)
> +    parser.add_option('-s', '--sort', dest="sort",
> +        help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
> +
> +    if not sys.argv[1:]:
> +        parser.print_usage()
> +        sys.exit(2)
> +
> +    (options, args) = parser.parse_args()
> +    sys.argv[:] = args
> +
> +    if (len(sys.argv) > 0):
> +        sys.path.insert(0, os.path.dirname(sys.argv[0]))
> +        run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
> +    else:
> +        parser.print_usage()
> +    return parser

Why does main() return a parser?  Is that useful?


> Added: python/trunk/Modules/_lsprof.c

> +static void
> +ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
> +{
> +       /* entering a call to the function identified by 'key'
> +          (which can be a PyCodeObject or a PyMethodDef pointer) */
> +       ProfilerObject *pObj = (ProfilerObject*)self;
> +       ProfilerEntry *profEntry;
> +       ProfilerContext *pContext;
> +
> +       profEntry = getEntry(pObj, key);
> +       if (profEntry == NULL) {
> +               profEntry = newProfilerEntry(pObj, key, userObj);
> +               if (profEntry == NULL)

Should you  do
      pObj->flags |= POF_NOMEMORY;
like below?

Why don't ptrace_enter_call() and ptrace_leave_call() return an error,
would that be easier?

> +                       return;
> +       }
> +       /* grab a ProfilerContext out of the free list */
> +       pContext = pObj->freelistProfilerContext;
> +       if (pContext) {
> +               pObj->freelistProfilerContext = pContext->previous;
> +       }
> +       else {
> +               /* free list exhausted, allocate a new one */
> +               pContext = (ProfilerContext*)
> +                       malloc(sizeof(ProfilerContext));
> +               if (pContext == NULL) {
> +                       pObj->flags |= POF_NOMEMORY;
> +                       return;
> +               }
> +       }
> +       initContext(pObj, pContext, profEntry);
> +}
> +
> +static void
> +ptrace_leave_call(PyObject *self, void *key)
> +{
> +       /* leaving a call to the function identified by 'key' */
> +       ProfilerObject *pObj = (ProfilerObject*)self;
> +       ProfilerEntry *profEntry;
> +       ProfilerContext *pContext;
> +
> +       pContext = pObj->currentProfilerContext;
> +       if (pContext == NULL)

Should you  do
      pObj->flags |= POF_NOMEMORY;
like above?

> +               return;
> +       profEntry = getEntry(pObj, key);
> +       if (profEntry) {
> +               Stop(pObj, pContext, profEntry);
> +       }
> +       else {
> +               pObj->currentProfilerContext = pContext->previous;
> +       }
> +       /* put pContext into the free list */
> +       pContext->previous = pObj->freelistProfilerContext;
> +       pObj->freelistProfilerContext = pContext;
> +}

> +PyMODINIT_FUNC
> +init_lsprof(void)
> +{
> +       PyObject *module, *d;
> +       module = Py_InitModule3("_lsprof", moduleMethods, "Fast profiler");
> +       d = PyModule_GetDict(module);

need to check return result of module and d, since they could fail.

Sorry, this is out of order, but already deleted it:

+static int
+pending_exception(ProfilerObject *pObj)
+{
+       if (pObj->flags & POF_NOMEMORY) {
+               pObj->flags -= POF_NOMEMORY;

The -= looks funny b/c it's the only place that is done.  Everywhere
else you do &= ~POF_NOMEMORY.

n


More information about the Python-checkins mailing list