cpython

When to Py_INCREF?

我的梦境 提交于 2020-01-02 07:20:28
问题 I'm working on a C extension and am at the point where I want to track down memory leaks. From reading Python's documentation it's hard to understand when to increment / decrement reference count of Python objects. Also, after couple days spending trying to embed Python interpreter (in order to compile the extension as a standalone program), I had to give up this endeavor. So, tools like Valgrind are helpless here. So far, by trial and error I learned that, for example, Py_DECREF(Py_None) is

Python's _winapi module

…衆ロ難τιáo~ 提交于 2020-01-02 05:33:10
问题 I was trying to write some python code that requires calls to native WINAPI functions. At first I came across the pypiwin32 package. Then, somewhere on the internet I saw someone using the _winapi module. I found no proper documentation for this module, only this link to cpython 's GitHub page. Is this a cpython -specific module? That is, is it not guaranteed that other implementations have this module? Do I need the pypiwin32 package if the functions I need are already implemented in the

Calling Python code from a C thread

杀马特。学长 韩版系。学妹 提交于 2020-01-02 01:41:31
问题 I'm very confused as to how exactly I can ensure thread-safety when calling Python code from a C (or C++) thread. The Python documentation seems to be saying that the usual idiom to do so is: PyGILState_STATE gstate; gstate = PyGILState_Ensure(); /* Perform Python actions here. */ result = CallSomeFunction(); /* evaluate result or handle exception */ /* Release the thread. No Python API allowed beyond this point. */ PyGILState_Release(gstate); And indeed, this stackoverflow answer seems to

Change what dictionary serves as a function's global scope

拜拜、爱过 提交于 2020-01-01 06:55:13
问题 I want to make an @pure decorator for Python, part of this is being able to selectively disallow access to the global scope of the function. Is there a way to programmatically change which dictionary thing serves as a function's global/external scope? So for instance in the following I want to be able to intercept the access to f in h and throw an error, but I want to allow access to g because it's a pure function. def f(): print("Non-pure function") @pure def g(i): return i + 1 @pure def h(i

Docstrings in C extensions to Python?

大兔子大兔子 提交于 2019-12-31 12:23:12
问题 When creating a C extension to Python, is it possible to be able to somehow write comments that are exposed as docstrings to users of the extension? 回答1: Docstrings for types can be included as the tp_doc member in the PyTypeObject structure, see an example in the docs. Docstrings for functions can be included in the ml_doc field of the module's method table. If you want your docstrings to be "physically close" to the actual functions, you could include string constants above your function

“ ==”和“是”之间有区别吗?

瘦欲@ 提交于 2019-12-30 18:37:12
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 这篇文章是 社区维基 。 编辑现有答案以改善此职位。 它当前不接受新的答案。 我的 Google Fu 使我失败了。 在Python中,以下两个相等测试是否等效? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' 对于要比较实例的对象( list 说),这是否成立? 好的,这样可以回答我的问题: L = [] L.append(1) if L == [1]: print 'Yay!' # Holds true, but... if L is [1]: print 'Yay!' # Doesn't. 因此 == 测试值,其中 is 看测试是否是同一个对象? #1楼 看一下Stack Overflow问题, Python的“ is”运算符在使用整数时表现异常 。 它主要归结为“ is ”检查它们是否是同一对象,而不只是彼此相等(小于256的数字是特例)。 #2楼 有一条简单的经验法则可以告诉您何时使用 == 或 is 。 == 用于 价值平等 。 当您想知道两个对象是否具有相同的值时,请使用它。 is 为 参考平等 。 当您想知道两个引用是否引用同一对象时,请使用它。 通常,在将某事物与简单类型进行比较时

CPython from Java?

你。 提交于 2019-12-30 10:37:33
问题 I need to call CPython code from Java. What tools/APIs/libraries exist out there to help me do this? Jython is not an option since the Python code is heavily dependent upon numpy . edit 1: The main() function should be Java, not Python (i.e. I need to embed CPython into Java, not vice versa.) edit 2: I should also mention that I'll be passing large numeric arrays between Java and Python and therefore a solution that brings the two into the same process space would be preferable (but not

Using SQL Alchemy and pyodbc with IronPython 2.6.1

元气小坏坏 提交于 2019-12-30 05:27:06
问题 I'm using IronPython and the clr module to retrieve SQL Server information via SMO. I'd like to retrieve/store this data in a SQL Server database using SQL Alchemy, but am having some trouble loading the pyodbc module. Here's the setup: IronPython 2.6.1 (installed at D:\Program Files\IronPython) CPython 2.6.5 (installed at D:\Python26) SQL Alchemy 0.6.1 (installed at D:\Python26\Lib\site-packages\sqlalchemy) pyodbc 2.1.7 (installed at D:\Python26\Lib\site-packages) I have these entries in the

Python: GIL context - switching

◇◆丶佛笑我妖孽 提交于 2019-12-30 04:43:08
问题 So, I generally have a pretty good understanding of how the Global Interpreter Lock (GIL) in Python works. Essentially, while the interpreter is running, one thread holds the GIL for N ticks (where N can be set using sys.setcheckinterval ), at which point the GIL is released and another thread can acquire the GIL. The also happens if one thread begins an I/O operation. What I'm a bit confused about is how this all works with C extension modules. If you have a C extension module that acquires

Python--windows下安装python

我是研究僧i 提交于 2019-12-29 18:50:07
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近有爬虫方面的需求,就学习下python。windows安装python步骤如下 1.下载python python官网下载路径: https://www.python.org/downloads/release/python-372/ 由图可知,我下载的是3.7.2版本。 2.安装python python的安装非常简单,双击exe文件,选中 Add Python 3.7 to PATH ,后续一直点击下去就行了。 3.使用python 在windows控制台中输入 python,详见如下图 出现如上界面,说明python安装成功。同时进入了python交互式环境中,此时可以输入python代码,回车即可得到执行结果。输入exit()并回车,即可退出python交互环境。 4.python解释器 当我们编写python代码时,得到的是一个包含python代码且以 .py 为扩展名的文本文件。要运行python代码,就必须要python解释器去执行.py文件。常用的python解释器是CPython,当我们从官网下载python之后,解释器也一并自带下载了,所以CPython也是官方指定解释器。 5.第一个python程序 在python交互式环境中输入python代码是练习使用的