如果我在断点处停止,则异常消失

时间:2017-10-18 17:20:23

标签: python

Python 3.6.2

以下代码的问题是运行时会引发异常。 但是,进入调试器,它完美地工作。我在调试器中停止的位置在注释中标记为断点。

我在IDE和shell中都尝试过该命令。异常提升。所以,这个问题与IDE无关。

这种情况让我感到震惊。 我制作了一段视频:https://www.youtube.com/watch?v=OUcMpEzooDk

你能帮我一把吗?怎么会这样?

评论下面的代码(与问题无关,但仅针对最好奇的代码)。

这是一个与Django Web框架一起使用的实用程序。 用户上传文件,将它们放到媒体目录中。

当然,Django知道媒体目录的位置。 然后Django保持相对于媒体的数据库路径。像这样:

  • it_1 / 705fad82-2f68-4f3c-90c2-116da3ad9a40.txt'

  • e5474da0-0fd3-4fa4-A85F-15c767ac32d4.djvu

我想确切地知道媒体中保存的文件对应于数据库中的路径。没有额外的文件,没有短缺。

代码:

from pathlib import Path

class <Something>():
    def _reveal_lack_extra_files(self):

        path = os.path.join(settings.BASE_DIR, '../media/')

        image_files = Image.objects.values_list("file", flat=True)
        image_files = [Path(os.path.join(path, file)) for file in image_files]

        item_files = ItemFile.objects.values_list("file", flat=True)
        item_files = [Path(os.path.join(path, file)) for file in item_files]

        sheet_files = SheetFile.objects.values_list("file", flat=True)
        sheet_files = [Path(os.path.join(path, file)) for file in sheet_files]

        expected_files = set().union(image_files, item_files, sheet_files)

        real_files = set()

        glob_generator = list(Path(path).glob("**/*"))

        for posix_path in glob_generator:
            if os.path.isfile(posix_path._str): # Breakpoint
                real_files.add(posix_path)


        lack = expected_files.difference(real_files)
        extra = real_files.difference(expected_files)

        assert bool(lack) == False, "Lack of files: {}".format(lack)
        assert bool(extra) == False, "Extra files: {}".format(extra)

回溯:

/home/michael/PycharmProjects/venv/photoarchive_4/bin/python /home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py --multiproc --qt-support --client 127.0.0.1 --port 43849 --file /home/michael/PycharmProjects/photoarchive_4/manage.py checkfiles
warning: Debugger speedups using cython not found. Run '"/home/michael/PycharmProjects/venv/photoarchive_4/bin/python" "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 3840 is connecting

Connected to pydev debugger (build 171.4694.67)
Traceback (most recent call last):
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py", line 1591, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py", line 1018, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/michael/PycharmProjects/photoarchive_4/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py", line 59, in handle
    self._reveal_lack_extra_files()
  File "/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py", line 39, in _reveal_lack_extra_files
    if os.path.isfile(posix_path._str):
AttributeError: _str

Process finished with exit code 1

1 个答案:

答案 0 :(得分:2)

您在路径上使用_str属性,该属性未记录且无法保证设置。通常,下划线前缀表示这是用户代码不应使用的私有属性。如果要将路径转换为字符串,请改用str(the_path)

但在这种情况下,您不需要这样做:Path个对象有一个eclipse driver definition方法,您可以调用它。另一种可能性是将Path对象本身传递给Python {3.6支持的is_file函数。