cpython

Slice endpoints invisibly truncated

烂漫一生 提交于 2019-12-05 07:15:27
>>> class Potato(object): ... def __getslice__(self, start, stop): ... print start, stop ... >>> sys.maxint 9223372036854775807 >>> x = sys.maxint + 69 >>> print x 9223372036854775876 >>> Potato()[123:x] 123 9223372036854775807 Why the call to getslice doesn't respect the stop I sent in, instead silently substituting 2^63 - 1? Does it mean that implementing __getslice__ for your own syntax will generally be unsafe with longs? I can do whatever I need with __getitem__ anyway, I'm just wondering why __getslice__ is apparently broken. Edit: Where is the code in CPython which truncates the slice?

python function(or a code block) runs much slower with a time interval in a loop

拜拜、爱过 提交于 2019-12-05 06:53:40
问题 I notice a case in python, when a block of code, nested in a loop, runs continuously, it is much faster than running with some .sleep() time interval. I wonder the reason and a possible solution . I guess it's related to CPU-cache or some mechanism of cPython VM. ''' Created on Aug 22, 2015 @author: doge ''' import numpy as np import time import gc gc.disable() t = np.arange(100000) for i in xrange(100): #np.sum(t) time.sleep(1) #--> if you comment this line, the following lines will be much

Why can't I access builtins if I use a custom dict as a function's globals?

烂漫一生 提交于 2019-12-05 06:50:54
I have a dict subclass like this: class MyDict(dict): def __getitem__(self, name): return globals()[name] This class can be used with eval and exec without issues: >>> eval('bytearray', MyDict()) <class 'bytearray'> >>> exec('print(bytearray)', MyDict()) <class 'bytearray'> But if I instantiate a function object with the types.FunctionType constructor, the function can't access any builtins: import types func = lambda: bytearray func_copy = types.FunctionType(func.__code__, MyDict(), func.__name__, func.__defaults__, func.__closure__) print(func_copy()) # Traceback (most recent call last): #

Calling Python code from a C thread

♀尐吖头ヾ 提交于 2019-12-05 03:42:05
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 confirm as much. But a commenter (with a very high reputation) says otherwise. The commenter says you

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

可紊 提交于 2019-12-04 23:36:29
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 of 3: 178 ns per loop The difference is in additional function call (just SLICE+0 vs CALL_FUNCTION 1

getting the C python exec argument string or accessing the evaluation stack

一个人想着一个人 提交于 2019-12-04 19:24:26
In my python debugger I have a way of remapping a string to a filename so that when you are stepping through an exec'd function inside the debugger you can list lines pygmentized, or view them along inside an editor like Emacs via realgud . So I'd like to be able to extract the string in an exec statement when CPython is stopped inside evaluating that. I already have a mechanism that can look back in the call frame to see if the caller was an EXEC_STMT and I can look back one instruction to see if the previous instruction was say DUP_TOP . So I'd be home free if I could just figure out a way

Extracting SWIG wrapped C++ instance/pointer for use in Cython

荒凉一梦 提交于 2019-12-04 15:02:32
I have an instance of a class from SWIG-wrapped C++ library from which I would like to extract its reference, in order to be able to use it inside a Cython file, in which I am directly linking to the same C++ library by using a more lightweight self-made Cython wrapper of the same class. I know it would not be as easy as accessing some hidden attribute, but I imagine there might be some function within SWIG or CPython that could potentially do that if linked to from within Cython (some PyObject_*, perhaps?). Unfortunately, I don't know enough about SWIG or CPython internals to know how to do

Python: Lifetime of module-global variables

坚强是说给别人听的谎言 提交于 2019-12-04 12:56:27
I have a shared resource with high initialisation cost and thus I want to access it across the system (it's used for some instrumentation basically, so has to be light weight). So I created a module managing the setup and access to it. It does a lazy initialise of the resource and stores it in a module global variable. I then use functions of this module across the system to operate on the resource. - Now I am wondering whether (or how often) I will have to reinitialise the resource? - I know objects are garbage collected in CPython on (or better around) zero reference count, but is storing in

Can I embed CPython inside PyPy?

南笙酒味 提交于 2019-12-04 09:21:36
I'd like to write a performance-sensitive application in Python, so executing it under PyPy is a natural choice. However, a significant portion of my code depends on numpy, scipy, and scikit-learn. Would it be possible to embed a CPython instance within a running PyPy program in order to call array-oriented code? If not, what's the easiest way to make PyPy and CPython talk to each other? No, you can't embed CPython inside PyPy AFAIK. You can, however, use distributed/parallel execution systems to make PyPy talk to CPython. Both execnet and Pyro mention this precise PyPy <-> CPython use case.

C++ vector to Python 3.3

橙三吉。 提交于 2019-12-04 08:53:21
问题 I would like to get a python list, say, [1,2,3,4] , from a C++ script. I wrote the C++ script, which returns a vector. How to connect the ends without SWIG/SIP/Cython/and others? Could it be easier to just compile the C++ to an .exe or elf file and then call from command line, have the .exe create a .txt containing a vector and read it in with python? My point is, I only need a really small function from C++ to do the heavy calculations on huge data. What would be the least painful and