#NEWLINE# def normalize_path( base , filename ) : #NEWLINE# #INDENT# "\n        Join and normalize a base path and filename.\n\n       `base` may be relative, in which case it's converted to absolute.\n\n        Args:\n            base (str): Base path\n            filename (str): Filename (optionally including path relative to\n                            base)\n\n        Returns:\n            str: The joined and normalized path\n\n        Raises:\n            PathTraversalException: if an attempt to escape the base path is\n                                    detected\n    " #NEWLINE# abs_path = os . path . abspath ( base ) #NEWLINE# joined = os . path . join ( abs_path , joined ) #NEWLINE# normalized = os . path . normpath ( joined ) #NEWLINE# if normalized . startswith ( os . path . join ( abs_path , '' ) ) : #NEWLINE# #INDENT# return normalized #NEWLINE# #UNINDENT# raise PathTraversalException ( 'Path Traversal detected' )