cpython

Defining enums in Cython code that will be used in the C part of code

风流意气都作罢 提交于 2019-12-12 17:50:19
问题 I have defined and enum in cython header file api.pxd : ctypedef enum InstructionType: default = 0 end_if = 1 end_loop = 2 backward_jump_here = 4 I also have checked if turning ctypedef to cdef would work (and it didn't). And I want to use value from this enum in __cinit__ method fo some class: from api cimport Instruction, CLinVM, InstructionType # (...) some other classes cdef class EndIf(Noop): def __cinit__(self): self.type = InstructionType.end_if And I get compilation error: self.type =

Tuple slicing not returning a new object as opposed to list slicing

♀尐吖头ヾ 提交于 2019-12-12 08:23:00
问题 In Python (2 and 3). Whenever we use list slicing it returns a new object, e.g.: l1 = [1,2,3,4] print(id(l1)) l2 = l1[:] print(id(l2)) Output >>> 140344378384464 >>> 140344378387272 If the same thing is repeated with tuple, the same object is returned, e.g.: t1 = (1,2,3,4) t2 = t1[:] print(id(t1)) print(id(t2)) Output >>> 140344379214896 >>> 140344379214896 It would be great if someone can shed some light on why this is happening, throughout my Python experience I was under the impression

Where does Python store the name binding of function closure?

天涯浪子 提交于 2019-12-12 07:58:08
问题 So recently I understand the concept of function closure. def outer(): somevar = [] assert "somevar" in locals() and not "somevar" in globals() def inner(): assert "somevar" in locals() and not "somevar" in globals() somevar.append(5) return somevar return inner function = outer() somevar_returned = function() assert id(somevar_returned) == id(function.func_closure[0].cell_contents) As much as I understand, the objective of function closure is to keep an active reference to the object, in

Default __str__ method for an instance

余生长醉 提交于 2019-12-11 15:27:19
问题 I have a class without a str or repr method. And when I call it: >>> from ingest.tpr import TPR >>> t=TPR() >>> t # that is, "repr(t)" <ingest.tpr.TPR instance at 0x101eb8518> The default representation here seems to be something like: "<${path_to_class} instance at ${memory_address}>" Where is this default implementation supplied? And is the above the __repr__ method or the __str__ method -- I know it calls the __repr__ method in the above, but are both the __str__ and __repr__ methods

Benchmarks for CPython

喜欢而已 提交于 2019-12-11 13:38:09
问题 I'm investigating a potential change in the CPython code base which I'm hoping may provide some performance benefits. However, though there are plenty of functionality tests in the build system, I can find nothing to do with performance testing. I would think that this would be ideal in order to check both whether: any proposed performance improvements actually deliver what they intend; and whether any functionality changes cause serious performance issues. Have I missed something in the repo

Can't get rid of “value has been optimized out” in GDB

醉酒当歌 提交于 2019-12-11 12:51:59
问题 I am debugging the CPython executable by GDB and can't get the value of some variables despite of disabling all GCC optimizations: (gdb) print *co value has been optimized out (gdb) frame #0 _PyEval_EvalFrameDefault ( f=Frame 0x7ffff7f36050, for file <frozen importlib._bootstrap>, line 8, in <module> (), throwflag=<optimized out>) at Python/ceval.c:1057 1057 switch (opcode) { (gdb) info locals stack_pointer = 0x7ffff7f361c8 next_instr = 0x555555aff422 opcode = 100 oparg = 0 fastlocals =

Forcing modules to run in PyPy from a CPython script (run PyPy on part of the code)?

£可爱£侵袭症+ 提交于 2019-12-11 10:48:10
问题 Is there a way to import modules from a CPython script, but run them in PyPy? The problem is that I have a code that uses lots of SciPy (and NumPy), but there are parts of the code that could be optimized with PyPy. Here's a random example of what I would like to do: sub_run_in_pypy.py module: #assuming this can be optimized with PyPy def function_a(foo): return foo**2 main_run_in_cpython.py module: import scipy.stats as stats #assuming this exists: import import_function_for_pypy pypy

Why does setting a descriptor on a class overwrite the descriptor?

百般思念 提交于 2019-12-11 06:35:20
问题 Simple repro: class VocalDescriptor(object): def __get__(self, obj, objtype): print('__get__, obj={}, objtype={}'.format(obj, objtype)) def __set__(self, obj, val): print('__set__') class B(object): v = VocalDescriptor() B.v # prints "__get__, obj=None, objtype=<class '__main__.B'>" B.v = 3 # does not print "__set__", evidently does not trigger descriptor B.v # does not print anything, we overwrote the descriptor This question has an effective duplicate, but the duplicate was not answered,

Fast and scalable RPC between C# and CPython

爷,独闯天下 提交于 2019-12-11 02:55:59
问题 I need some feedback on C# - cPython integration for scientific application. The scenario is the following: C# for data acquisition and visualization - CPython for data manipulation using several and changing third part library to implement domain specific tasks. General use case: C# code get realtime data from device C# code pass data to cPython code and ask for elaboration Cpython code does the magic Cpython code pass result back to c# code C# visualizes data in a WPF application Both c#

Why does deleting a global variable named __builtins__ prevent only the REPL from accessing builtins?

左心房为你撑大大i 提交于 2019-12-11 01:17:32
问题 I have a python script with the following contents: # foo.py __builtins__ = 3 del __builtins__ print(int) # <- this still works Curiously, executing this script with the -i flag prevents only the REPL from accessing builtins: aran-fey@starlight ~> python3 -i foo.py <class 'int'> >>> print(int) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'print' is not defined How come the script can access builtins, but the REPL can't? 回答1: CPython doesn't look up _