473,772 Members | 3,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python bytecode STORE_NAME

As part of some research I am doing a Python Virtual Machine in Java,
and the exact semantics of the STORE_NAME bytecode is unclear to be,
so I was hoping somebody here could clarify it.
The STORE_NAME bytecode is supposed to set a value for a name in the
current scope. However, the following piece of code:

def hello(who):
print "Hello", who
return hello(who)
print "Say:"
hello("World")

Results in this bytecode for the top level:
1, LOAD_CONST, 1
4, MAKE_FUNCTION, 0
7, STORE_NAME, 0
10, LOAD_CONST, 2
13, PRINT_ITEM, None
14, PRINT_NEWLINE, None
15, LOAD_NAME, 0
18, LOAD_CONST, 3
21, CALL_FUNCTION, 1
24, POP_TOP, None
25, LOAD_CONST, 0
28, RETURN_VALUE, None

And this bytecode for the hello function:
1, LOAD_CONST, 1
4, PRINT_ITEM, None
5, LOAD_FAST, 0
8, PRINT_ITEM, None
9, PRINT_NEWLINE, None
10, LOAD_GLOBAL, 1
13, LOAD_FAST, 0
16, CALL_FUNCTION, 1
19, RETURN_VALUE, None
20, LOAD_CONST, 0
23, RETURN_VALUE, None

The first column are the byte numbers, and the last column contains
the arguments to the byte codes if they take any.

The function is stored using STORE_NAME with offset 0 in the module
scope, but it is loaded from inside the hello method using LOAD_GLOBAL
with offset 1. My questions are: Does STORE_NAME add things to the
global scope when used top level? And why is the offset different?

The documentation contains nothing usable:
http://www.python.org/doc/2.5.2/lib/bytecodes.html

regards,
Mathias
Nov 19 '08 #1
3 2676
sc*****@cs.au.d k wrote:
As part of some research I am doing a Python Virtual Machine in Java,
and the exact semantics of the STORE_NAME bytecode is unclear to be,
so I was hoping somebody here could clarify it.
The STORE_NAME bytecode is supposed to set a value for a name in the
current scope. However, the following piece of code:

def hello(who):
print "Hello", who
return hello(who)
print "Say:"
hello("World")

Results in this bytecode for the top level:
1, LOAD_CONST, 1
4, MAKE_FUNCTION, 0
7, STORE_NAME, 0
10, LOAD_CONST, 2
13, PRINT_ITEM, None
14, PRINT_NEWLINE, None
15, LOAD_NAME, 0
18, LOAD_CONST, 3
21, CALL_FUNCTION, 1
24, POP_TOP, None
25, LOAD_CONST, 0
28, RETURN_VALUE, None

And this bytecode for the hello function:
1, LOAD_CONST, 1
4, PRINT_ITEM, None
5, LOAD_FAST, 0
8, PRINT_ITEM, None
9, PRINT_NEWLINE, None
10, LOAD_GLOBAL, 1
13, LOAD_FAST, 0
16, CALL_FUNCTION, 1
19, RETURN_VALUE, None
20, LOAD_CONST, 0
23, RETURN_VALUE, None

The first column are the byte numbers, and the last column contains
the arguments to the byte codes if they take any.

The function is stored using STORE_NAME with offset 0 in the module
scope, but it is loaded from inside the hello method using LOAD_GLOBAL
with offset 1. My questions are: Does STORE_NAME add things to the
global scope when used top level? And why is the offset different?
Every code object has its own co_names attribute (a tuple). The arguments
are offsets into that tuple.

Using Python 2.5 I can't reproduce your example, I get 0 offsets in both
cases. Here's a simpler one:
>>import dis
def f():
.... x
.... y
....
>>def g():
.... y
....
>>dis.dis(f)
2 0 LOAD_GLOBAL 0 (x)
3 POP_TOP

3 4 LOAD_GLOBAL 1 (y)
7 POP_TOP
8 LOAD_CONST 0 (None)
11 RETURN_VALUE
>>dis.dis(g)
2 0 LOAD_GLOBAL 0 (y)
3 POP_TOP
4 LOAD_CONST 0 (None)
7 RETURN_VALUE
>>f.func_code.c o_names
('x', 'y')
>>g.func_code.c o_names
('y',)

Peter
Nov 19 '08 #2
On 19 Nov., 10:14, Peter Otten <__pete...@web. dewrote:
>
Every code object has its own co_names attribute (a tuple). The arguments
are offsets into that tuple.

Using Python 2.5 I can't reproduce your example, I get 0 offsets in both
cases. Here's a simpler one:
>import dis
def f():

... * * x
... * * y
...>>def g():

... * * y
...>>dis.dis(f)

* 2 * * * * * 0 LOAD_GLOBAL * * * * * * *0 (x)
* * * * * * * 3 POP_TOP

* 3 * * * * * 4 LOAD_GLOBAL * * * * * * *1 (y)
* * * * * * * 7 POP_TOP
* * * * * * * 8 LOAD_CONST * * * * * * * 0 (None)
* * * * * * *11 RETURN_VALUE>>d is.dis(g)

* 2 * * * * * 0 LOAD_GLOBAL * * * * * * *0 (y)
* * * * * * * 3 POP_TOP
* * * * * * * 4 LOAD_CONST * * * * * * * 0 (None)
* * * * * * * 7 RETURN_VALUE>>f .func_code.co_n ames
('x', 'y')
>g.func_code.co _names

('y',)

Peter
Ok, thanks a lot. That helped me understand the offsets. Your
disassembly misses the code in the global scope that creates the two
methods from the code objects. That code looks like this for your
example (dis won't give you this. I use a modified version of the
byteplay disassembler which can disassemble a module without loading
it):
1, LOAD_CONST, 1 //Loads the code object for function f
4, MAKE_FUNCTION, 0 //Creates a function from the code object
7, STORE_NAME, 0 //Stores it as 'f' using STORE_NAME
10, LOAD_CONST, 2 //Loads the code object for function g
13, MAKE_FUNCTION, 0 //Creates the function
16, STORE_NAME, 1 //Stores it as 'g' using STORE_NAME
19, LOAD_CONST, 0 //Loads None
22, RETURN_VALUE, None //Exists the program with status None

The two last bytecodes are there because I didn't use the interactive
mode. I guess I narrowed down my other question to whether the
semantics are that stuff saved using STORE_NAME in the global scope
can be loaded everywhere else using LOAD_GLOBAL. What baffled me was
that there is actually a STORE_GLOBAL byte code.
Nov 19 '08 #3
sc*****@cs.au.d k wrote:
On 19 Nov., 10:14, Peter Otten <__pete...@web. dewrote:
>>
Every code object has its own co_names attribute (a tuple). The arguments
are offsets into that tuple.

Using Python 2.5 I can't reproduce your example, I get 0 offsets in both
cases. Here's a simpler one:
>>import dis
def f():

... Â* Â* x
... Â* Â* y
...>>def g():

... Â* Â* y
...>>dis.dis(f )

2 Â* Â* Â* Â* Â* 0 LOAD_GLOBAL Â* Â* Â* Â* Â* Â* Â*0 (x)
3 POP_TOP

3 Â* Â* Â* Â* Â* 4 LOAD_GLOBAL Â* Â* Â* Â* Â* Â* Â*1 (y)
7 POP_TOP
8 LOAD_CONST Â* Â* Â* Â* Â* Â* Â* 0 (None)
11 RETURN_VALUE>>d is.dis(g)

2 Â* Â* Â* Â* Â* 0 LOAD_GLOBAL Â* Â* Â* Â* Â* Â* Â*0 (y)
3 POP_TOP
4 LOAD_CONST Â* Â* Â* Â* Â* Â* Â* 0 (None)
7 RETURN_VALUE>>f .func_code.co_n ames
('x', 'y')
>>g.func_code.c o_names

('y',)

Peter

Ok, thanks a lot. That helped me understand the offsets. Your
disassembly misses the code in the global scope that creates the two
methods from the code objects. That code looks like this for your
example (dis won't give you this. I use a modified version of the
byteplay disassembler which can disassemble a module without loading
it):
1, LOAD_CONST, 1 //Loads the code object for function f
4, MAKE_FUNCTION, 0 //Creates a function from the code object
7, STORE_NAME, 0 //Stores it as 'f' using STORE_NAME
10, LOAD_CONST, 2 //Loads the code object for function g
13, MAKE_FUNCTION, 0 //Creates the function
16, STORE_NAME, 1 //Stores it as 'g' using STORE_NAME
19, LOAD_CONST, 0 //Loads None
22, RETURN_VALUE, None //Exists the program with status None
You can get it with standard tools, too:

$ cat tmp.py
def f():
x
y

def g():
x
$ python -c'import tmp'
$ python
Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:43)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>import marshal
module = marshal.loads(o pen("tmp.pyc"). read()[8:])
import dis
dis.dis(modul e)
1 0 LOAD_CONST 0 (<code object f at
0x2b3389f3d648, file "tmp.py", line 1>)
3 MAKE_FUNCTION 0
6 STORE_NAME 0 (f)

5 9 LOAD_CONST 1 (<code object g at
0x2b3389f3d828, file "tmp.py", line 5>)
12 MAKE_FUNCTION 0
15 STORE_NAME 1 (g)
18 LOAD_CONST 2 (None)
21 RETURN_VALUE

I just didn't deem it relevant.
The two last bytecodes are there because I didn't use the interactive
mode. I guess I narrowed down my other question to whether the
semantics are that stuff saved using STORE_NAME in the global scope
can be loaded everywhere else using LOAD_GLOBAL. What baffled me was
that there is actually a STORE_GLOBAL byte code.
If you are really interested in the subtleties you have to dive into the
source. After a quick look into

http://svn.python.org/view/python/tr...75&view=markup

I think STORE_NAME works with the module startup code because global and
local namespace are identical. Within a function (with distinct global and
local namespaces) you'd have to use STORE_GLOBAL.

Peter
Nov 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
3456
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully. I'm wondering if something of such had been attempted in python, as in compiling X language into .pyc. I do not understand the schematics of .pyc files but I assume that they are the so called python bytecode files. Or is there any...
114
9884
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
32
2531
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in Fortran and trying to do it in Python. I got it to work, but now I'm trying to modularize it. My fortran program uses a subroutine and I was trying to do the same thing in Python. But I'm still new so I'm having trouble understanding what I'm...
14
4345
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user intervention such as adding type declarations. It uses rather advanced static type inference techniques to deduce type information by itself. In addition, it determines whether deduced types may be parameterized, and if so, it generates...
38
2055
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this syntax return old-style class, same as "class C:", and not the new style "class C(object):" ?
11
1441
by: Michael Spencer | last post by:
Announcing: compiler2 --------------------- For all you bytecode enthusiasts: 'compiler2' is an alternative to the standard library 'compiler' package, with several advantages. Improved pure-python compiler - Produces identical bytecode* to the built-in compile function for all /Lib and Lib/test modules, including 'peephole' optimizations
5
2709
by: xkenneth | last post by:
Hi All, I'll shortly be distributing a number of python applications that use proprietary. The software is part of a much larger system and it will need to be distributed securely. How can i achieve this? Regards, Ken
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.