Skip to content

Commit

Permalink
Attempt to tighten path stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Mar 11, 2024
1 parent b720d00 commit 629ee78
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/ctypesgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def api_main(args):

args_str = str(pformat(args))
for p, x in get_priv_paths():
args_str = args_str.replace(p, x)
args_str = args_str.replace(str(p), x)
return main_impl(real_args, f"ctypesgen.api_main(\n{args_str}\n)")


Expand Down
20 changes: 10 additions & 10 deletions src/ctypesgen/printer_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ def paragraph_ctx(txt):

@functools.lru_cache(maxsize=1)
def get_priv_paths():
priv_paths = [(str(Path.home()), "~")]
priv_paths = [(Path.home(), "~")]
if Path.cwd() != Path("/"): # don't strip unix root
priv_paths += [(str(Path.cwd()), ".")]
priv_paths += [(Path.cwd(), ".")]
# sort descending by length to avoid interference
priv_paths.sort(key=lambda x: len(x[0]), reverse=True)
priv_paths.sort(key=lambda x: len(str(x[0])), reverse=True)
return priv_paths

def txtpath(s):
def txtpath(p):
# Returns a path string suitable for embedding into the output, with private paths stripped
# FIXME if e.g. Path.home() is /home/a, this would wrongly strip from /home/alice as well - need a tighter matching strategy
s = str(s)
for p, x in get_priv_paths():
if s.startswith(p):
return x + s[len(p):]
return s
p = Path(p)
for strip_p, x in get_priv_paths():
# should be equivalent to `p.is_relative_to(strip_p)` or `p.parts[:len(strip_p.parts)] == strip_p.parts`
if strip_p in p.parents or p == strip_p:
return x + str(p)[len(str(strip_p)):]
return str(p)


# Important: Concerning newlines handling, please read docs/dev_comments.md
Expand Down

0 comments on commit 629ee78

Please sign in to comment.