Skip to content

Commit

Permalink
Fix func name
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Apr 23, 2024
1 parent 666a64e commit 5d63d37
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/coredumpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datetime
import os
import re


def get_dump_filename(frame, path, directory):
Expand All @@ -15,6 +16,8 @@ def get_dump_filename(frame, path, directory):
return os.path.abspath(path)

funcname = os.path.basename(frame.f_code.co_name)
funcname = re.sub(r"[^a-zA-Z0-9_]", "", funcname)

d = datetime.datetime.now()
filename = f"coredumpy_{funcname}_{d.strftime('%Y%m%d_%H%M%S_%f')}.dump"

Expand Down
4 changes: 2 additions & 2 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def run_script(self, script, expected_returncode=0):
process = subprocess.Popen(normalize_commands(["python", f"{tmpdir}/script.py"]),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
self.assertEqual(process.returncode, expected_returncode,
f"script failed with return code {process.returncode}\n{stderr}")
stdout = stdout.decode()
stderr = stderr.decode()
self.assertEqual(process.returncode, expected_returncode,
f"script failed with return code {process.returncode}\n{stderr}")
return stdout, stderr
25 changes: 17 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
from .base import TestBase


class TestUtils(TestBase):
def test_get_dump_filename(self):
class FakeFrame:
def __init__(self, name):
self.f_code = FakeCode(name)
class FakeCode:
def __init__(self, name):
self.co_name = name


class FakeCode:
def __init__(self, name):
self.co_name = name
class FakeFrame:
def __init__(self, name):
self.f_code = FakeCode(name)


class TestUtils(TestBase):

def test_get_dump_filename(self):
frame = FakeFrame("test_get_dump_filename")
filename = get_dump_filename(frame, None, None)
self.assertEqual(filename, os.path.abspath(filename))
Expand All @@ -36,3 +39,9 @@ def __init__(self, name):

with self.assertRaises(ValueError):
filename = get_dump_filename(frame, "test.dump", "dir")

def test_escape_name(self):
frame = FakeFrame("<module>")
filename = get_dump_filename(frame, None, None)
self.assertNotIn("<", filename)
self.assertNotIn(">", filename)

0 comments on commit 5d63d37

Please sign in to comment.