Python Forum
NameError: NameError: global name 'BPLInstruction' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: NameError: global name 'BPLInstruction' is not defined
#1
Hello I am receiving these messages when running my code:

Error:
File "/home/leopoldo/cpu.py", line 28, in process_instructions instruction = self.identify_instruction (byte) File "/home/leopoldo/cpu.py", line 18, in identify_instruction instruction = BPLInstruction (byte) NameError: global name 'BPLInstruction' is not defined
The code:

def process_instructions (self):
		counter=0
		for byte in self.rom.data_bytes:
			byte = self.rom.get_byte (self.pc)
			instruction = self.identify_instruction (byte)
			if instruction is None:
				raise Exception ("Instruction not found")
			instruction.process ()
			print ("self.pc, antes: ", self.pc)
			self.pc+= instruction.instruction_length
			print ("counter: ", counter)
			print ("self.pc, depois: ", self.pc)
			print ("")
			counter = counter + 1
def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = LDAInstruction (byte)
		elif byte==0xD8:
			instruction = SEIInstruction (byte)
		elif byte==0xA9:
			instruction = CLDInstruction (byte)
		elif byte==0x10:
			instruction = BPLInstruction (byte)
		else:
			instruction = None
		return instruction
class LDAInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: LDAInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 2

class SEIInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: SEIInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class CLDInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: CLDInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class BPLinstruction (Instruction):
	def process (self):
		print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1
I can't identify the source of trouble because I see no difference between the code related to BPL, whether is its class definition or its elif clause on "identify_instruction" to the others instruction's code.

So I don't see what is not defined there, especially when python does not complain about any other instruction. Thanks for the help.
Reply
#2
in class definition, name is BPLinstruction (small i)
when instantiated you use capital I
Reply
#3
(Oct-21-2019, 12:51 AM)Larz60+ Wrote: in class definition, name is BPLinstruction (small i)
when instantiated you use capital I

Thanks. I did changed it to
class BPLInstruction (Instruction):
instead of
class BPLinstruction (Instruction):
Nonetheless, the error message continued, exactly as before. What else can be?
Reply
#4
it's line 18!
print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)
Reply
#5
(Oct-23-2019, 11:29 PM)Larz60+ Wrote: it's line 18!
print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)

Between the quotes? Well, I changed it but the error remains.
Reply
#6
show your full code
Reply
#7
I somewhat was able to fix it through the correction of a logical bug.

The code that fix it is:

        if byte==0x78:	
			instruction = SEIInstruction (byte)
		elif byte==0xD8:
			instruction = CLDInstruction (byte)
		elif byte==0xA9:
			instruction = LDAInstruction (byte)
and not

def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = LDAInstruction (byte)
		elif byte==0xD8:
			instruction = SEIInstruction (byte)
		elif byte==0xA9:
			instruction = CLDInstruction (byte)
However I added code for a new instruction with

class STAabsInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: STAabsInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 3
and code to deal with it
elif byte==0x8D:
			instruction = STAabsInstruction (byte)
and now I am receiving
Error:
File "/home/leopoldo/cpu.py", line 32, in process_instructions instruction = self.identify_instruction (byte) File "/home/leopoldo/cpu.py", line 20, in identify_instruction instruction = STAabsInstruction (byte) NameError: global name 'STAabsInstruction' is not defined
Full code for the two classes that use Instructions:

from rom import ROM
from instruction import LDAInstruction, SEIInstruction, CLDInstruction

class CPU (object):
	def __init__(self, rom_bytes):
		self.registers = []
		self.rom = ROM (rom_bytes)
		self.pc = 0		

	def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = SEIInstruction (byte)
		elif byte==0xD8:
			instruction = CLDInstruction (byte)
		elif byte==0xA9:
			instruction = LDAInstruction (byte)
		elif byte==0x10:
			instruction = BPLInstruction (byte)
		elif byte==0x8D:
			instruction = STAabsInstruction (byte)
		else:
			instruction = None
		return instruction
		

	def process_instructions (self):
		counter=0
		for byte in self.rom.data_bytes:
			byte = self.rom.get_byte (self.pc)
			print ("self.pc, antes: ", self.pc)
			print ("byte: ", hex (byte), byte)
			instruction = self.identify_instruction (byte)
			if instruction is None:
				raise Exception ("Instruction not found")
			instruction.process ()
			self.pc+= instruction.instruction_length
			print ("counter: ", counter)
			print ("self.pc, depois: ", self.pc)
			print ("")
			if self.pc>=5:
				break
			
from abc import ABCMeta, abstractmethod, abstractproperty

class Instruction (object):
	__metaclass_ = ABCMeta
		
	def __init__ (self,identification_byte):
		self.identity_byte = identification_byte

	@abstractproperty
	def instruction_length (self):
		return 1

	@abstractmethod
	def process (self):
		pass
		
class SEIInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: SEIInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class CLDInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: CLDInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class LDAInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: LDAInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 2

class STAabsInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: STAabsInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 3
Reply
#8
I assume these classes are in separate files, and each named the same as the class that they hold? (you don't show the module names)
on line 2 of cpu.py, you don't import STAabsInstruction, thus the error on line 20.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 181 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,057 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,776 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  NameError nafshar 3 945 Oct-18-2022, 02:13 PM
Last Post: deanhystad
  NameError: azizrasul 12 1,808 Oct-02-2022, 03:15 AM
Last Post: azizrasul
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,205 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,457 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  Why the NameError? Mark17 4 1,905 May-17-2022, 08:14 PM
Last Post: Mark17
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,866 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,188 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020