[Python-ideas] easy thread-safety [was: fork]
Ron Adam
ron3200 at gmail.com
Thu Aug 20 18:27:55 CEST 2015
On 08/20/2015 11:45 AM, Sven R. Kunze wrote:
> I had absolutely not idea what you mean when saying: "co_mutables".
> Reading the byte code and the examples above though, I just can agree.
> Thanks a lot for this.
When a bytecode to load an object is executed such as LOAD_FAST, it gets
it's reference to the object from the function's list of names in it's
code object.
>>> def foo(x):
... y = 2
... return x + y
...
>>> from dis import dis
>>> dis(foo)
2 0 LOAD_CONST 1 (2)
3 STORE_FAST 1 (y)
3 6 LOAD_FAST 0 (x)
9 LOAD_FAST 1 (y)
12 BINARY_ADD
13 RETURN_VALUE
>>> dir(foo.__code__)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code',
'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars',
'co_kwonlyargcount', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals',
'co_stacksize', 'co_varnames']
>>> foo.__code__.co_varnames
('x', 'y')
LOAD_FAST 0, reads __code__.co_varnames[0]
LOAD_FAST 1, reads __code__.co_varnames[1]
Adding a co_mutables name list to the __code__ attribute, along with new
bytecodes to access them, it would create a way to keep private local
names without changing how the other bytecodes work.
LOAD_MUTABLE 0, would get the first reference in __code__.co_mutables.
Look for co_name, and co_freevars here to see how they relate to other
byte codes.
https://docs.python.org/3.5/library/dis.html#python-bytecode-instructions
Hope that helps.
Cheers,
Ron
> Yesterday, I used "shared" in my posts to illustrate that. "mutable" is
> another word for it.
More information about the Python-ideas
mailing list