1

I am running code on my command-line interface with python 3. However, I am confused about why dis.distb() does not give me an assembly language code for an error in print("Hello World) with missing quotes.

C:\Users\jarvis>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> print("Hello World)
  File "<stdin>", line 1
    print("Hello World)
                  ^
SyntaxError: EOL while scanning string literal
>>> dis.distb()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jarvis\AppData\Local\Programs\Python\Python37-32\lib\dis.py", line 86, in distb
    while tb.tb_next: tb = tb.tb_next
AttributeError: 'NoneType' object has no attribute 'tb_next'

This code gives me an assembly code for the print function that misses the letter 't'

>>> prin("Hello World")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'prin' is not defined
>>> dis.distb()
  1 -->       0 LOAD_NAME                0 (prin)
              2 LOAD_CONST               0 ('Hello World')
              4 CALL_FUNCTION            1
              6 PRINT_EXPR
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

 >>>

What is the difference in the error types above? I am trying to understand how python works in this situation.

myverdict
  • 622
  • 8
  • 24

2 Answers2

0

The reason you are not getting a dissasembly of bytecode is simply because there is none: the Python interpreter hit a serious error before it could create any bytecode.

Here is an all-too-common analog, but it is useable hee. Suppose I want to translate:

"Whose woods these are, I think I know"

into Spanish. Basically you'd understand the words (or if someone speaks the words instead, in your head you might break up the sounds into phonemes and figure out words).

Then you'd piece together the words into a sentence, and then after that you might parse the sentence into a subject-phrase and and object phrase.) With all of this you are in a good position to make a translation. If you can't hear or don't understand one word you might be able to figure out what was meant, but that's extra work, and you might guess the wrong word or meaning.

Now, if instead I were to ask you to translate:

"fafdsasdSFEFafef094qt43qtu08rpiekjsfdrshgoirtfhejtr4qr5841u0toiqjhwekfnsajf"

There are no descernable words there that make up a reasonabe sentence and that you could parse. The best you could do is spit out the same thing which is what, for example, google translate does.

When it comes to real-world production compilers or interpreters, these programs are pretty exacting. (In the olden days there were special compilers for languages that were targeted for beginners who would make mistakes, and the compiler might try to fill in the details).

But in CPython, if one double quote is missing, as in your situation, CPython gives you a message about the invalid syntax with a pointer of where the problem is and what might be done about it. But it won't try to go further and create bytecode. It's missin unlike google translate is not about trying to figure out what you might have meant given partial information.

rocky
  • 7,226
  • 3
  • 33
  • 74
0

The cause maybe be calling values or types in the Python interpreter's stack or memory management. On the other hand, it shows that dis.py cannot handle the SyntaxError exception in such cases as the one that occurred here (EOL while scanning string literal). Deep diving goes back to the sys.last_traceback, which traces the previous exception if it happened in an interactive environment to do some post-mortem debugging. One of the parameters of the method distb() is the variable tb, but the default value of this parameter is None. Conversely, when the tb is None, the method will raise an AttributeError. So, this process indicates that in the case of your problem, in the CMD or terminal, tb will be None, but not in jupyter notebook.

For example, if you do the same thing in jupyter notebook, dis.distb() works appropriately in both cases. But in the command prompt,t not.

I ran both cases in jupyter notebook and got the following result.

print("Hello World)

# result:
  File "C:\Users\itpro\AppData\Local\Temp/ipykernel_21836/2771583741.py", line 1
    print("Hello World)
                       ^
SyntaxError: EOL while scanning string literal
# ---

dis.distb()

# result:
101           0 LOAD_GLOBAL              0 (compile)
              2 LOAD_FAST                1 (source)
              4 LOAD_FAST                2 (filename)
              6 LOAD_FAST                3 (symbol)
              8 LOAD_FAST                0 (self)
             10 LOAD_ATTR                1 (flags)
             12 LOAD_GLOBAL              2 (PyCF_ONLY_AST)
             14 BINARY_OR
             16 LOAD_CONST               1 (1)
    -->      18 CALL_FUNCTION            5
             20 RETURN_VALUE

And for the second case:

prin("Hello World")

# result:
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21836/2802362923.py in <module>
----> 1 prin("Hello World")

NameError: name 'prin' is not defined

# ---

dis.distb()

# result:

  1 -->       0 LOAD_NAME                0 (prin)
              2 LOAD_CONST               0 ('Hello World')
              4 CALL_FUNCTION            1
              6 PRINT_EXPR
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE
Reza K Ghazi
  • 347
  • 2
  • 9