Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:25:44+00:00 2026-06-13T23:25:44+00:00

The following code produces bytecode output as string. I want to replace some lines

  • 0

The following code produces bytecode output as string. I want to replace some lines of the bytecode and reassemble and exec it. Do I need something like the ByteCodeAssember or can I do this with PyCode New?

http://docs.python.org/2/c-api/code.html#PyCode_New

http://pypi.python.org/pypi/BytecodeAssembler

output

**********      code    **********                                                                                   

type: <type 'str'>                                                                                                   
def test(a,b):                                                                                                       
    return a*b                                                                                                       
test(2,3)                                                                                                            

**********      compiles into the bytecode      **********                                                           

type: <type 'code'>                                                                                                  
  1           0 LOAD_CONST               0 (<code object test at 0x101d1ca30, file "<string>", line 1>)              
              3 MAKE_FUNCTION            0                                                                           
              6 STORE_NAME               0 (test)                                                                    

  3           9 LOAD_NAME                0 (test)                                                                    
             12 LOAD_CONST               1 (2)                                                                       
             15 LOAD_CONST               2 (3)                                                                       
             18 CALL_FUNCTION            2                                                                           
             21 POP_TOP                                                                                              
             22 LOAD_CONST               3 (None)                                                                    
             25 RETURN_VALUE 3  

**********  bytecode    **********

'd\x00\x00\x84\x00\x00Z\x00\x00e\x00\x00d\x01\x00d\x02\x00\x83\x02\x00\x01d\x03\x00S'

code

import dis                                                                                                           
import sys                                                                                                           
import inspect                                                                                                       
import new                                                                                                           

class writer:                                                                                                        
    def __init__(self):                                                                                              
        self.s = ""                                                                                                  

    def write(self,text):                                                                                            
        #print ': ',text                                                                                             
        self.s += text                                                                                               

#save stdout                                                                                                                     
origstdout = sys.stdout                                                                                              
w = writer()                                                                                                         
sys.stdout = w                                                                                                       


s = "def test(a,b):\n\treturn a*b\ntest(2,3)"                                                                        

c = compile(s,'<string>','exec')                                                                                     
# dis calls stdout, so output is in w                                                                                                                         
bytecode = dis.dis(c)                                                                                                

sys.stdout = origstdout                                                                                              

def h(x):                                                                                                            
    print '*'*10 + '\t' + x + '\t' + '*'*10 + '\n'*1                                                                 

h('code')                                                                                                            
print 'type: '+str(type(s))                                                                                          
print s + '\n'                                                                                                       

h('compiles into the bytecode')                                                                                      
print 'type: '+str(type(c))                                                                                          
print w.s

h('bytecode')                                                                                                        
print repr(c.co_code)  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:25:45+00:00Added an answer on June 13, 2026 at 11:25 pm

    Byteplay is a nice wrapper for python code objects. It has its own code class. Code objects is CPython specific and more complicated. To get started certainly better to play with byteplay first.

    from byteplay import *                                                                                 
    
    def foo():                                                                                             
        x = 10                                                                                             
        print 'inside ',x                                                                                  
        return 42                                                                                          
    
    c = Code.from_code(foo.func_code)                                                                      
    print c.code                                                                                           
    
    ret = foo()                                                                                            
    print 'outside: ',ret                                                                                  
    
    c.code[10] = (LOAD_CONST,1000)                                                                         
    
    foo.func_code = c.to_code()                                                                            
    print '*'*20                                                                                           
    print c.code                                                                                           
    
    ret = foo()                                                                                            
    
    print 'outside: ',ret  
    

    produces

      4           1 LOAD_CONST           10
                  2 STORE_FAST           x
    
      5           4 LOAD_CONST           'inside '
                  5 PRINT_ITEM           
                  6 LOAD_FAST            x
                  7 PRINT_ITEM           
                  8 PRINT_NEWLINE        
    
      6          10 LOAD_CONST           42
                 11 RETURN_VALUE         
    
    inside  10
    outside:  42
    ********************
    
      4           1 LOAD_CONST           10
                  2 STORE_FAST           x
    
      5           4 LOAD_CONST           'inside '
                  5 PRINT_ITEM           
                  6 LOAD_FAST            x
                  7 PRINT_ITEM           
                  8 PRINT_NEWLINE        
    
      6          10 LOAD_CONST           1000
                 11 RETURN_VALUE         
    
    inside  10
    outside:  1000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code produces each result as text. I would like to add some
The following code produces wrong and inconsistent output with gcc (4.1.2 20080704) but correct
The following code produces the output xyz a = %w{x y z} print a.to_s
The following code produces an error, any ideas why? string dateFormatString = dd.MM.yyyy HH:mm:ss;
The following code produces the shown output and I'm confused ... I'm using Intel
The following code produces pretty much what I want, but the scrollable pane on
I would like to except the error the following code produces, but I don't
The following code produces the output of 46104728: using System; namespace TestApplication { internal
The following code produces the issue Cannot implicitly convert type 'System.Collections.Generic.IEnumberable<DataField<string>>' to 'DataFields'. An
Following code produces a nested array as a result for keys containing three items:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.