cpython

Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes

一笑奈何 提交于 2019-12-23 11:00:40
问题 This question is related to a previous question I asked. Namely this one if anyone is interested. Basically, what I want to do is to expose a C array to Python using a Py_buffer wrapped in a memoryview -object. I've gotten it to work using PyBuffer_FillInfo (work = I can manipulate the data in Python and write it to stdout in C), but if I try to roll my own buffer I get a segfault after the C function returns. I need to create my own buffer because PyBuffer_FillInfo assumes that the format is

Remove doc strings but not asserts from compiled CPython

好久不见. 提交于 2019-12-23 10:24:22
问题 I want to remove doc strings from a Python program but leave asserts (and __debug__ sections) in. I have been using the -OO flag to generate .pyo files but according to the documentation that removes both asserts and doc strings. I'm using CPython 2.7. Clarification: I'm removing the docstrings as a cheap obfuscation method. Management made that decision and I think whether that is a useful thing to do is outside the scope of this question. 回答1: You can't have half of -O . It removes

Retrieving address of native base class with ctypes

醉酒当歌 提交于 2019-12-23 07:07:34
问题 I want to be able to pass a certificate to Python's ssl library without requiring a temporary file. It seems that the Python ssl module cannot do that. To work around this problem I want to retrieve the underlying SSL_CTX struct stored in the ssl._ssl._SSLContext class from the native _ssl module. Using ctypes I could then manually call the respective SSL_CTX_* functions from libssl with that context. How to do that in C is shown here and I would do the same thing via ctypes. Unfortunately, I

How can we pass a C structure to Python?

。_饼干妹妹 提交于 2019-12-22 10:43:24
问题 I'm a new in both C and Python. These days, I'm learning to embed Python in C. As I am calling Python functions from C, I'd like to know, how can we pass a C structure to Python? thanks alot in advance! 回答1: The cleanest thing to do is probably to create a new type and implement tp_getattr , either returning the appropriate member of the struct or raising an exception depending on the argument passed. 回答2: This documentation on extending and/or embedding Python might get you started. There is

PyFile_Type replaced by ..?

試著忘記壹切 提交于 2019-12-22 10:12:59
问题 I'm tyring to compile Yenc for Python 3.2. I noticed that gcc complained about a non-declared function PyString_Type , so I replaced it with its replacement PyBytes_Type as according to the documentation. However, gcc also complained about an undeclared function called PyFile_Type . I googled a bit and found: Python 3.x replaces the PyFile_Type extension type with an abstract interface and specific implementation types. Unfortunately it doesn't make any of this directly available with a C

How to get the address of mmap-ed memory in Python?

こ雲淡風輕ζ 提交于 2019-12-22 08:57:19
问题 I cannot figure how to get the virtual address of the standard mmap objects in Python (from the mmap module). The documented methods only seem to access the memory as array of bytes or as character strings. But I need to access the mmap'ped memory by precisely 2 or 4 bytes at once - because this memory in my application is mapped to hardware registers (think /dev/mem or GPIO or such). Accessing memory in this way is possible with ctypes module - but for this I need the pointer - or virtual

Large memory footprint of integers compared with result of sys.getsizeof()

ぃ、小莉子 提交于 2019-12-22 07:59:43
问题 Python-Integer-objects in the range [1,2^30) need 28 byte, as provided by sys.getsizeof() and explained for example in this SO-post. However, when I measure the memory footprint with the following script: #int_list.py: import sys N=int(sys.argv[1]) lst=[0]*N # no overallocation for i in range(N): lst[i]=1000+i # ints not from integer pool via /usr/bin/time -fpeak_used_memory:%M python3 int_list.py <N> I get the following peak memory values (Linux-x64, Python 3.6.2): N Peak memory in Kb bytes

Large memory footprint of integers compared with result of sys.getsizeof()

你说的曾经没有我的故事 提交于 2019-12-22 07:59:12
问题 Python-Integer-objects in the range [1,2^30) need 28 byte, as provided by sys.getsizeof() and explained for example in this SO-post. However, when I measure the memory footprint with the following script: #int_list.py: import sys N=int(sys.argv[1]) lst=[0]*N # no overallocation for i in range(N): lst[i]=1000+i # ints not from integer pool via /usr/bin/time -fpeak_used_memory:%M python3 int_list.py <N> I get the following peak memory values (Linux-x64, Python 3.6.2): N Peak memory in Kb bytes

Why is copying a list using a slice[:] faster than using the obvious way?

跟風遠走 提交于 2019-12-22 01:53:47
问题 Why is shallow-copying a list using a slice so much faster than using list builtin? In [1]: x = range(10) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 83.2 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best of 3: 147 ns per loop Usually when I see weird things like this, they're fixed in python3 - but this discrepancy is still there: In [1]: x = list(range(10)) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 100 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best

Python list.clear() time and space complexity?

荒凉一梦 提交于 2019-12-21 16:36:06
问题 I am writing a blogpost on Python list.clear() method where I also want to mention about the time and space complexity of the underlying algorithm. I expected the time complexity to be O(N) , iterate over the elements and free the memory? But, I found an article where it is mentioned that it is actually an O(1) operation. Then, I searched the source code of the method in CPython implementation and found a method which I believe is the actual internal implementation of list.clear() , however,