cpython

Python source code for math exponent function?

好久不见. 提交于 2019-11-28 07:40:12
问题 When I do math computation in Python which library are we using. E.g. >>> 2**0.5 1.4142135623730951 How can I find the source code that was used? Is this just the math.pow() function? Unfortunately, inspect.getsource(pow) returns a kind of error. Searching on Github narrows it down to 13 possible files. And I don't fully understand how cPython is constructed. /*[clinic input] math.pow x: double y: double / Return x**y (x to the power of y). [clinic start generated code]*/ static PyObject *

What is python-dev package used for

会有一股神秘感。 提交于 2019-11-28 04:19:56
I recently installed lxml . Before that I had to install all the dependencies for that. So I tried to install liblxml2-dev , liblxslt1-dev and python-dev (google searched for what packages are required for lxml ) but even after that I could not able to install lxml by using the command pip install lxml . However as because I am using Python 3.4.0, I thought that may be there are different version of python-dev (thought came due to facing some similar version conflict problem). So I tried to install python3-dev . Then I tried to install lxml using the same command said earlier and that worked!!

Overriding the newline generation behaviour of Python's print statement

允我心安 提交于 2019-11-28 03:33:26
问题 I have a bunch of legacy code for encoding raw emails that contains a lot of print statements such as print >>f, "Content-Type: text/plain" 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 ceval.c:1582: err = PyFile

Why does str.split not take keyword arguments?

有些话、适合烂在心里 提交于 2019-11-27 21:51:37
I came across this - in my view - strange behaviour: "a b c".split(maxsplit=1) TypeError: split() takes no keyword arguments Why does str.split() not take keyword arguments, even though it would make sense? I found this behavior both in Python2 and Python3. See this bug and its superseder . str.split() is a native function in CPython, and as such exhibits the behavior described here : CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot

Is a variable swap guaranteed to be atomic in python?

北慕城南 提交于 2019-11-27 13:54:25
With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following: (x, y) = (y, x) will be guaranteed atomic in cPython. (x and y are both python variables) Let's see: >>> x = 1 >>> y = 2 >>> def swap_xy(): ... global x, y ... (x, y) = (y, x) ... >>> dis.dis(swap_xy) 3 0 LOAD_GLOBAL 0 (y) 3 LOAD_GLOBAL 1 (x) 6 ROT_TWO 7 STORE_GLOBAL 1 (x) 10 STORE_GLOBAL 0 (y) 13 LOAD_CONST 0 (None) 16 RETURN_VALUE It doesn't appear that they're atomic: the values of x and y could be changed by another thread

C Python: Running Python code within a context

我的未来我决定 提交于 2019-11-27 13:52:50
问题 The Python C API function PyEval_EvalCode let's you execute compiled Python code. I want to execute a block of Python code as if it were executing within the scope of a function , so that it has its own dictionary of local variables which don't affect the global state. This seems easy enough to do, since PyEval_EvalCode lets you provide a Global and Local dictionary: PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) The problem I run into has to do with how

CPython is bytecode interpreter?

纵饮孤独 提交于 2019-11-27 12:56:17
I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture? Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they are implemented in different languages). I also read somewhere that Python is C++ interpretation. Is this correct? And what does that mean? I'm still very new to Python, so forgive me if I ask the dumb questions... Thank you so much! CPython is the implementation of Python in C. It's the

CPython memory allocation

强颜欢笑 提交于 2019-11-27 11:50:51
This is a post inspired from this comment about how memory is allocated for objects in CPython. Originally, this was in the context of creating a list and appending to it in a for loop vis a vis a list comprehension. So here are my questions: how many different allocaters are there in CPython? what is the function of each? when is malloc acutally called? (a list comprehension may not result in a call to malloc , based on what's said in this comment How much memory does python allocate for itself at startup? are there rules governing which data structures get first "dibs" on this memory? What

Why shouldn't I use PyPy over CPython if PyPy is 6.3 times faster?

纵然是瞬间 提交于 2019-11-27 09:55:42
I've been hearing a lot about the PyPy project. They claim it is 6.3 times faster than the CPython interpreter on their site . Whenever we talk about dynamic languages like Python, speed is one of the top issues. To solve this, they say PyPy is 6.3 times faster. The second issue is parallelism, the infamous Global Interpreter Lock (GIL). For this, PyPy says it can give GIL-less Python . If PyPy can solve these great challenges, what are its weaknesses that are preventing wider adoption? That is to say, what's preventing someone like me, a typical Python developer, from switching to PyPy right

Storing Python objects in a Python list vs. a fixed-length Numpy array

不问归期 提交于 2019-11-27 07:39:32
问题 In doing some bioinformatics work, I've been pondering the ramifications of storing object instances in a Numpy array rather than a Python list, but in all the testing I've done the performance was worse in every instance. I am using CPython. Does anyone know the reason why? Specifically: What are the performance impacts of using a fixed-length array numpy.ndarray(dtype=object) vs. a regular Python list? Initial tests I performed showed that accessing the Numpy array elements was slower than