问题标签 [cpython]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
338 浏览

python - 防止从 Python 调用 C 类型

防止从 Python 调用(创建实例)C 类型的正确方法是什么?

我考虑过提供tp_init引发异常的 a ,但据我了解,它仍然允许__new__直接在类型上调用。

AC 函数返回这种类型的实例——这是创建这种类型实例的唯一方法。

编辑:我的意图是,如果他们不小心错误地使用了我的类型的用户,他们会得到一个例外。C 代码是这样的,在从 Python 错误创建的对象上调用函数会崩溃。我意识到这很不寻常:到目前为止,我所有的 C 扩展类型在从 Python 实例化时都运行良好。我的问题是是否有通常的方法来提供这种限制。

0 投票
5 回答
2087 浏览

python - Python 对象没有引用但仍可通过弱引用访问?

gc.get_referrers(obj) 是否可以为对象返回一个空列表,但仍然可以通过弱引用访问该对象?

如果是这样,我将如何开始尝试确定该对象未被垃圾收集的原因?

编辑:我不确定在这种情况下代码示例将如何帮助 - 显然某处有很强的参考,但如果我能找到它,我会被诅咒的。我的印象是对对象的所有强引用都将由 get_referrers() 标识。

编辑:已解决。我发现该变量具有强引用 - 它在游戏事件循环中,但不是类变量,因此 get_referrers 没有选择它。

0 投票
4 回答
5071 浏览

python - CPython 还是 IronPython?

对于全新的跨平台 GUI 应用程序,CPython 或 IronPython,您会使用什么?

怎么样
- 许可/自由
- 开发
- - 文档
- - 编辑器
- - 工具
- 库
- 性能
- 可移植性

你可以用其中一个做最好的事情吗?
- 网络
- 数据库
- GUI
- 系统
- 多线程/处理

0 投票
10 回答
70255 浏览

python - IronPython 与 Python .NET

我想从 Python 代码访问一些用 C# 编写的 .NET 程序集。

一项小研究表明我有两个选择:

两种解决方案之间的权衡是什么?

0 投票
2 回答
339 浏览

python - 何时使用替代 Python 发行版?

我已经用 Python 编程了几年,并且一直使用 CPython 而没有考虑它。我读过的书籍和文档也总是提到 CPython。

什么时候使用替代发行版(PyPy、Stackless 等)才有意义?

谢谢!

0 投票
3 回答
2846 浏览

c++ - CPython 是字节码解释器?

在 CPython 的上下文中,我并没有真正理解“字节码解释器”的概念。有人可以对整个图片有所了解吗?

这是否意味着CPython将编译并执行pyc文件(字节码文件?)。那么什么编译py文件到pyc文件呢?Jython 与 CPython 有何不同(除了它们是用不同的语言实现的)。

我还在某处读到 Python 是 C++ 解释。这个对吗?那是什么意思?

我对 Python 还是很陌生,所以如果我问愚蠢的问题,请原谅我......非常感谢!

0 投票
6 回答
6999 浏览

python - Overriding the newline generation behaviour of Python's print statement

I have a bunch of legacy code for encoding raw emails that contains a lot of print statements such as

This is all well and good for emails, but we're now leveraging the same code for outputting HTTP request. The problem is that the Python print statement outputs '\n' whilst HTTP requires '\r\n'.

It looks like Python (2.6.4 at least) generates a trailing PRINT_NEWLINE byte code for a print statement which is implemented as

Thus it appears there's no easy way to override the default newline behaviour of print. I have considered the following solutions

  • After writing the output simply do a .replace('\n', '\r\n'). This will interfere with HTTP messages that use multipart encoding.
  • Create a wrapper around the destination file object and proxy the .write method
  • Write a regular expression that translates print >>f, text to f.write(text + line_end) where line_end can be '\n' or '\r\n'.
  • I believe the third option would be the most appropriate. It would be interesting to hear what your Pythonic approach to the problem would be.

    0 投票
    2 回答
    362 浏览

    python - 访问 xrange 内部结构

    我正在尝试使用 ctypes 从内部 python 结构中提取数据。即,我正在尝试读取 xrange 中的 4 个字段:

    是否有任何标准方法可以在 python 本身中获取这些字段?

    0 投票
    2 回答
    607 浏览

    python - Qt Creator 调试器中 PyString 的内容?

    我有一个 PyString* 对象,我想查看它的内容。有没有办法使用 Qt Creator 的调试器查看 PyString 的文本?

    如果它有所作为,Qt Creator 就是 GDB 的前端。

    0 投票
    3 回答
    564 浏览

    python - IronPython 可以用作 CPython 的替代品吗?

    IronPython 是否已经到了可以将其作为 CPython 的替代品的地步?

    澄清一下:我的意思是 IronPython 可以运行最初为 CPython 编写的应用程序(当然不涉及 .NET)