搁置的问题非常奇怪(python)

发布于 2024-08-20 02:26:22 字数 743 浏览 10 评论 0原文

我创建一个名为 foo_module.py 的文件,其中包含以下代码:

import shelve, whichdb, os

from foo_package.g import g

g.shelf = shelve.open("foo_path")
g.shelf.close() 

print whichdb.whichdb("foo_path")  # => dbhash
os.remove("foo_path")

在该文件旁边,我创建一个名为 foo_package 的目录,其中包含一个空的 __init__.py > 文件和一个名为 g.py 的文件,其中仅包含:

class g:
    pass

现在,当我运行 foo_module.py 时,我收到一条奇怪的错误消息:

Exception TypeError: "'NoneType ' object is not callable" inignored

但是,如果我将目录从 foo_package 重命名为 foo,并更改 foo_module.txt 中的导入行。 py,我没有收到任何错误。这是怎么回事?

在 WinXP 上运行 Python 2.6.4。

I create a file called foo_module.py containing the following code:

import shelve, whichdb, os

from foo_package.g import g

g.shelf = shelve.open("foo_path")
g.shelf.close() 

print whichdb.whichdb("foo_path")  # => dbhash
os.remove("foo_path")

Next to that file I create a directory called foo_package than contains an empty __init__.py file and a file called g.py that just contains:

class g:
    pass

Now when I run foo_module.py I get a weird error message:

Exception TypeError: "'NoneType' object is not callable" in ignored

But then, if I rename the directory from foo_package to foo, and change the import line in foo_module.py, I don't get any error. Wtf is going on here?

Running Python 2.6.4 on WinXP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吃不饱 2024-08-27 02:26:22

我认为您在 2.6.4 的代码中遇到了一个与程序结束时的清理相关的小错误。如果您运行 python -v ,您可以准确地看到错误发生在清理的哪个点:

# cleanup[1] foo_package.g
Exception TypeError: "'NoneType' object is not callable" in  ignored

Python 在程序结束时的清理过程中设置了对 None 的引用,并且它看起来它对 g.shelf 的状态感到困惑。作为解决方法,您可以在 close 之后设置 g.shelf = None。我还建议在 Python 的 bug 跟踪器中打开一个 bug!

I think you've hit a minor bug in 2.6.4's code related to the cleanup at end of program. If you run python -v you can see exactly at what point of the cleanup the error comes:

# cleanup[1] foo_package.g
Exception TypeError: "'NoneType' object is not callable" in  ignored

Python sets references to None during the cleanup at the end of program, and it looks like it's getting confused about the status of g.shelf. As a workaround you could set g.shelf = None after the close. I would also recommend opening a bug in Python's bug tracker!

鸠魁 2024-08-27 02:26:22

头发掉了好几天,终于用atexit功能成功了:

  import atexit
  ...
  cache = shelve.open(path)
  atexit.register(cache.close)

打开后立即注册最合适。这适用于多个并发架子。

(python 2.6.5 on lucid)

After days of hair loss, I finally had success using an atexit function:

  import atexit
  ...
  cache = shelve.open(path)
  atexit.register(cache.close)

It's most appropriate to register right after opening. This works with multiple concurrent shelves.

(python 2.6.5 on lucid)

月亮邮递员 2024-08-27 02:26:22

这确实是一个 Python 错误,我已经针对您打开的跟踪器问题发布了一个补丁(感谢您这样做)。

问题是 shelve 的 del 方法调用其 close 方法,但如果 shelve 模块已完成清理,则 close 方法将失败并显示您看到的消息。

您可以通过在 g.shelf.close 之后添加“del g.shelf”来避免代码中出现该消息。只要 g.shelf 是对架子的唯一引用,这将导致 CPython 在解释器清理阶段之前立即调用架子的 del 方法,从而避免错误消息。

This is indeed a Python bug, and I've posted a patch to the tracker issue you opened (thanks for doing that).

The problem is that shelve's del method calls its close method, but if the shelve module has already been through cleanup, the close method fails with the message you see.

You can avoid the message in your code by adding 'del g.shelf' after g.shelf.close. As long as g.shelf is the only reference to the shelf, this will result in CPython calling the shelve's del method right away, before the interpreter cleanup phase, and thus avoid the error message.

你的笑 2024-08-27 02:26:22

这似乎是 shelve 模块注册的关闭函数中的异常。 “忽略”部分来自关闭系统,根据问题 6294,有时可能会改进其措辞。不过,我仍然希望得到有关如何消除异常本身的答案......

It seems to be an exception in a shutdown function registered by the shelve module. The "ignored" part is from the shutdown system, and might get its wording improved sometime, per Issue 6294. I'm still hoping for an answer on how to eliminate the exception itself, though...

罗罗贝儿 2024-08-27 02:26:22

对我来说,一个简单的 shelve.close() 就完成了未关闭的工作。

shelve.open('somefile') 返回一个“用于读写的持久字典”对象,我在应用程序的整个运行时使用该对象。
当我终止应用程序时,我收到了如上所述的“TypeError”异常。
我在终止序列中调用了“close()”,这似乎解决了问题。

例如
shelveObj = shelve.open('文件名')
...
shelveObj.close()

for me a simple shelve.close() on an unclosed one did the job.

shelve.open('somefile') returns a "persistent dictionary for reading and writing" object which i used throughout the app's runtime.
when I terminated the app I received the "TypeError" Exception as mentioned.
I plased a 'close()' call in my termination sequence and that seemed to fix the problem.

e.g.
shelveObj = shelve.open('fileName')
...
shelveObj.close()

独闯女儿国 2024-08-27 02:26:22

OverByThere于2018年7月17日发表评论

这似乎是简而言之

,打开 /usr/lib/python3.5/weakref.py 并将第 109 行更改为:

 def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):

将第 117 行更改为:

_atomic_removal(d, wr.key)

请注意,您需要使用空格而不是制表符来执行此操作,因为这会导致其他错误。

OverByThere commented on Jul 17, 2018

This seems to be fixable.

In short open /usr/lib/python3.5/weakref.py and change line 109 to:

 def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):

And line 117 to:

_atomic_removal(d, wr.key)

Note you need to do this with spaces, not tabs as this will cause other errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文