# Funny experience of using trace module to trace function call

I came across this issue in httpie and started my investigation.

The problem is while pretty printing the json, output is alpha sorted because keys are hashed and user wanted to preserve the order. Then I made 3 comments to the issue. First comment was half correct and explained why it isn’t possible to get the desired output, quickly I figured my assumptions were wrong and second comment explained what is actually happening, finally I proposed the solution. Since I made wrong assumptions and to make further debugging easy, I want to find easiest way to trace all functions/methods invocations.

I remember Noufal sharing small snippet in twitter, but I wanted to use trace and wrote small snippet(final one).

 #! -*- coding: utf-8 -*-

from httpie.core import main
#import pdb
import sys
#import os
#import trace

#pdb.set_trace()
main(args=sys.argv[1:])


First I ran the command python -m trace –trace httpie_test.py –pretty=all http://httpbin.org/get and it printed some 20 million lines. Then I got clever idea of using pdb.

I debugged with pdb for half an hour pressing s key and fed up. The pdb was beautiful like her, I was enjoying each line it was printing, it was like watching her speak and I was mesemerized. After half an hour I gave up and went back to trace command. Finally I figured out I can use --ignore-module from command line.

After one hour of spending time, final command looked like(scroll completely)

 ➜  snippets  python -m trace --trace --ignore- module=os,sre_compile,sre_parse,zipfile,text_file,sysconfig,pkg_resources,re,posixpath,genericpath,decoder,hex_codec,socket,httplib,pkgutil,stat,token,style,calendar,spawn,util,collections,abc,decimal,lexer,StringIO,plist,argparse,_abcoll,structures,Queue,threading,_collections,urlparse,cookielib,platform,cookielib,terminal256,formatter,__init__,stringprep,_weakrefset,filter,encoder,codecs,latin,connectionpool,plugin,html,pygmentplugin,htmlentitydefs,__future__,weakref,UserDict,atexit,functools,base64,struct,hashlib,ssl,textwrap,six,exceptions,warnings,mimetools,tempfile,random,tempfile,rfc822,urllib,filepost,uuid,_endian,dyld,dylib,framework,io,poolmanager,pyopenssl,utils,cgi,netrc,shlex,compat,copy,numbers,locale,locale,unicode_escape,urllib2,Cookie,_LWPCookieJar,_MozillaCookieJar,ordered_dict,cookies,certs,ascii,status_codes,gettext,scanner,config,minicompat,domreg,minidom,xmlbuilder,NodeFilter,shutil,copy_reg,string,plistlib,_mapping,bbcode,img,Image,FixTk,ImageMode,ImagePalette,ImageColor,ImageDraw,ImageFont,latex,other,console,rtf,svg,terminal,solarized,getpass,pprint,downloads,idna,agile,web,unistring,functional,jvm,compiled,mimetypes httpie_test.py --pretty=all --verbose http://headers.jsontest.com/ >> op.txt
➜  snippets  wc -l op.txt
2344 op.txt


The essay is 1269 characters to terminal and the result of it is pull request.