This commit is contained in:
Sven Riwoldt
2024-04-01 20:30:24 +02:00
parent fd333f3514
commit c7bc862c6f
6804 changed files with 1065135 additions and 0 deletions

13
asm/Add.asm Normal file
View File

@@ -0,0 +1,13 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/add/Add.asm
// Computes R0 = 2 + 3 (R0 refers to RAM[0])
@2 //R2
D=A
@3
D=D+A
@0
M=D

26
asm/Max.asm Normal file
View File

@@ -0,0 +1,26 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/max/Max.asm
// Computes R2 = max(R0, R1) (R0,R1,R2 refer to RAM[0],RAM[1],RAM[2])
@R0
D=M // D = first number
@R1
D=D-M // D = first number - second number
@OUTPUT_FIRST
D;JGT // if D>0 (first is greater) goto output_first
@R1
D=M // D = second number
@OUTPUT_D
0;JMP // goto output_d
(OUTPUT_FIRST)
@R0
D=M // D = first number
(OUTPUT_D)
@R2
M=D // M[2] = D (greatest number)
(INFINITE_LOOP)
@INFINITE_LOOP
0;JMP // infinite loop

23
asm/MaxL.asm Normal file
View File

@@ -0,0 +1,23 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/max/MaxL.asm
// Symbol-less version of the Max.asm program.
@0
D=M
@1
D=D-M
@10
D;JGT
@1
D=M
@12
0;JMP
@0
D=M
@2
M=D
@14
0;JMP

28375
asm/Pong.asm Normal file

File diff suppressed because it is too large Load Diff

27490
asm/PongL.asm Normal file

File diff suppressed because it is too large Load Diff

35
asm/Rect.asm Normal file
View File

@@ -0,0 +1,35 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/rect/Rect.asm
// Draws a rectangle at the top-left corner of the screen.
// The rectangle is 16 pixels wide and R0 pixels high.
@0
D=M
@INFINITE_LOOP
D;JLE
@counter
M=D
@SCREEN
D=A
@address
M=D
(LOOP)
@address
A=M
M=-1
@address
D=M
@32
D=D+A
@address
M=D
@counter
MD=M-1
@LOOP
D;JGT
(INFINITE_LOOP)
@INFINITE_LOOP
0;JMP

0
asm/Rect.hack Normal file
View File

32
asm/RectL.asm Normal file
View File

@@ -0,0 +1,32 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/rect/RectL.asm
// Symbol-less version of the Rect.asm program.
@0
D=M
@23
D;JLE
@16
M=D
@16384
D=A
@17
M=D
@17
A=M
M=-1
@17
D=M
@32
D=D+A
@17
M=D
@16
MD=M-1
@10
D;JGT
@23
0;JMP

BIN
asm/asm.zip Normal file

Binary file not shown.

192
asm/asm01.py Normal file
View File

@@ -0,0 +1,192 @@
"""Assembler for the hack computer
Usage:
python assembler.py file
Loads an assembly file and translate it into machine language for the hack
computer as specified in project 6 of the nand2tetris course.
# Assembler implementation details
This assembler works in 3 steps:
1. Load and clean the assembly file
2. Construct a symbol table referencing the user-defined labels and variables
3. Translate the asm file to binary code
The file follows this pattern:
File parsing (line 147)
Symbol table (line 207)
Assembling (line 322)
??
# Assembly language specifications:
## Note on registers
The Hack computer has three 16-bit registers: the D-, A- and M-registers.
The D-register is used to store "data" that can be used as input for the ALU.
The A-register can be used in the same way but it also has a second role:
The RAM use it as address input. So any read/write instruction to the RAM is
done on the register which has the value of the A-register as address.
The M-register represents this register in the RAM that is 'pointed' onto by the
A-register. Reading/writing to it consists actually in reading/writing to
the RAM.
## Assembly instructions
There are three types of assembly instructions: A-instructions, C-instructions
and labels. Indents and blanks are ignored. Comments can only be in-line, start
with "//" and are ignored.
## A-instructions
- `"@" integer` where integer is a number in the range 0->32768. Sets the A
register to contain the specified integer. Ex: @42
- `"@" label` where label is a user-defined label. Sets the A register to
contain the code address corresponding to the label.
Labels are upper-cased by convention, with "_" as word separator. Ex: @MAIN
- `"@" variable` where variable is a user-defined variable. Sets the A register
to contain the RAM adress corresponding to the variable. If a variable is
encountered for the first time, it is automatically assigned an address.
The address assignment starts at RAM address 16 and increments.
Variables are lowercased by convention, with "_" as word separator. Ex: @i
## C-instructions
`(Dest-code "=")? op-code (";" jump-code)?`
- op-code:
Only the op-code is mandatory. It represents an instruction to be performed
by the ALU. Available codes and their associated outputs are:
- 0 -> the constant 0
- 1 -> the constant 1
- -1 -> the constant -1
- D -> the value contained in the D-register
- A -> the value contained in the A-register
- M -> the value contained in the M-Register
- !D -> bit-wise negation of the D-register
- !A -> bit-wise negation of the A-register
- !M -> bit-wise negation of the M-register
- -D -> numerical negation of the D-register using 2's complement
- -A -> numerical negation of the A-register using 2's complement
- -M -> numerical negation of the M-register using 2's complement
- D+1 -> 1 + value of the D-register
- A+1 -> 1 + value of the A-register
- M+1 -> 1 + value of the M-register
- D-1 -> -1 + value of the D-register
- A-1 -> -1 + value of the A-register
- M-1 -> -1 + value of the M-register
- D+A -> value of the D-register + value of the A-register
- D+M -> value of the D-register + value of the M-register
- D-A -> value of the D-register - value of the A-register
- D-M -> value of the D-register - value of the M-register
- A-D -> value of the A-register - value of the D-register
- M-D -> value of the M-register - value of the D-register
- D&A -> bit-wise AND of the values of the D and A registers
- D&M -> bit-wise AND of the values of the D and M registers
- D|A -> bit-wise OR of the values of the D and A registers
- D|M -> bit-wise OR of the values of the D and M registers
- dest-code:
If specified, should be followed with a "=" character. Available codes are:
- D -> write the ALU instruction's output to the D-register
- A -> write the ALU instruction's output to the A-register
- M -> write the ALU instruction's output to the M-register
- AD -> write the ALU instruction's output to the A- and D-registers
- AM -> write the ALU instruction's output to the A- and M-registers
- MD -> write the DLU instruction's output to the D- and M-registers
- ADM -> write the DLU instruction's output to the A-, D- and M-registers
- jump-code:
If specified, should be preceded by a ";" character. The computer is fed
with a programm containing one binary instruction per line. Each of those
instructions should be seen as having a number, starting at 0 and increasing
by one. The jump-code lets the computer jump to the instruction of which the
address is contained in the A-register if the result of the current
operation satisfies a certain condition. Available codes and corresponding
conditions are:
- JEQ -> jump if the output is equal to 0
- JLT -> jump if the output is lower than 0
- JLE -> jump if the output is lower than 0 or equal to 0
- JGT -> jump if the output is greater than 0
- JGE -> jump if the output is greater than 0 or equal to 0
- JNE -> jump if the output is not 0
- JMP -> just jump wathever the output
- Examples:
@3 // Set A to 3
0;JMP // unconditional jump to code line 3.
@42 // Set A to 42
D=D-A;JEQ: // Set D to D-A. if D-A == 0, jump to code line nb 42.
@i // Point onto var i, the real RAM address is handled by the assembler
M=A // Set corresponding value to it's own address
A=A+1 // Point to the RAM address just after i
## Labels
`"(" LABEL_NAME ")"`
When performing a jump, the appropriate line of code should be put in the
A-register. Setting directly the line number with a `@integer` instruction
is delicate since one has to figure out the line number ignoring comments,
blank lines, etc... And all the addresses have to be updated if the beginning of
the assembly code is edited afterward.
So the assembly language proposes to mark lines with a label using the `(LABEL)`
syntax. The assembler will then automatically adjust any `@LABEL` instruction
to match the desired code line at assembly time.
Example:
// This code runs a loop 42 times and then stops in an infinite empty loop
00 @MAIN // @2
01 0;JMP
(MAIN)
02 @42 // Set D to 42
03 D=A
04 @DECREMENT // @6
05 0;JMP
(DECREMENT)
06 D=D-1 // Decrement D
07 @END // @11
08 D;JEQ // Go there if D==0
09 @DECREMENT // Or continue the loop
10 0;JMP
(END)
11 @END // Infinity loop to end the programm
12 0;JMP
"""
import sys
import re
def create_symbol_table():
# Erzeugen eines dict
return {
key: (value)
for key, value in {
**{'@SP': 0,
'@LCL': 1,
'@ARG': 2,
'@THIS': 3,
'@THAT': 4,
'@SCREEN': 0x4000,
'@KBD': 0x6000,},
**{f'@R{i}': i
for i in range(16)}
}.items()}
def asm(file):
symbol_table = create_symbol_table()
print(symbol_table)
regex = r"^\n"
asmfile = open(file, 'r')
asmlines = asmfile.readlines()
asm =[]
#print(asmlines)
for l in asmlines:
if l.startswith('//'):
pass
elif re.match(regex, l):
pass
else:
asm.append(l.replace('\n', ''))
print(l)
print(asm)
print (sys.argv)
asm_file = asm(sys.argv[1])

117
asm/asm02.py Normal file
View File

@@ -0,0 +1,117 @@
'''
1. Kreieren der Symboltabelle
2. Einlesen der Datei
a. Entfernen der Kommentare -- erledigt
b. Suchen der (Label) und Ablegen dieser in die Symboltabelle -- erledigt
c. Entfernen der (Label) aus dem ASM-File
d. HACK-Code erzeugen
'''
import sys
from pathlib import Path
import re
from icecream import ic
symboltable = {}
asm = []
def appendTable(table, id, item):
print("appendTable ",id, " ", item )
table["@"+ str(item)] = id
symboltable = table
def createSymboltable():
return {
key: (value)
for key, value in {
**{'@SP': 0,
'@LCL': 1,
'@ARG': 2,
'@THIS': 3,
'@THAT': 4,
'@SCREEN': 0x4000,
'@KBD': 0x6000,},
**{f'@R{i}': i
for i in range(16)}
}.items()}
def load_asm_file(filename):
fp = open(filename, 'r')
comment = r"^/"
eol = r"^\n"
tmp = []
t = fp.readlines()
for l in t:
if re.match(comment, l):
pass
elif re.match(eol, l):
pass
else:
tmp.append(re.sub('\s', '', l).split('//')[0]) # Wenn noch ein Kommentar hinter der Anweisung steht dann Abtrenen
fp.close()
#ic("tmp = ", tmp)
return tmp
def label_to_symboltable(data):
#ic(len(data))
i = 0
tmp = {}
for l in data:
if (l.startswith('(') and l.endswith(')')):
l = l[:-1]
l = l.lstrip('(')
tmp['@'+str(l)] = i
#ic(l)
else:
i=i+1
if len(tmp) != 0:
symboltable.update(tmp)
#ic(symboltable)
#ic (data)
# c. Löschen der (Label) aus der Liste
count = 0
for i in tmp:
#ic(tmp[i]) # Zeilennummern
data.pop(tmp[i]-count)
#ic(len(data))
searchSymbols(data)
def searchSymbols(asm):
'''
wenn Zeile mit @ beginnt schauen ob Variable in Symboltable
sonst
ist Variable @x ein int, dann ohne @ in die Symboltable übernehmen
'''
#ic(asm)
i = 0
for line in asm:
if line.startswith('@'):
ic (i, " ",line[1:])
if line[1:].isdigit():
ic(line[1:], " ist ein int")
if __name__ == "__main__":
#filename = sys.argv[1]
filename = "Rect.asm"
symboltable = createSymboltable()
asm = load_asm_file(filename)
label_to_symboltable(asm)
searchSymbols(asm)
#print("Symtable = ",symboltable)

549
asm/assembler02.py Normal file
View File

@@ -0,0 +1,549 @@
"""Assembler for the hack computer
Usage:
python assembler.py file
Loads an assembly file and translate it into machine language for the hack
computer as specified in project 6 of the nand2tetris course.
# Assembler implementation details
This assembler works in 3 steps:
1. Load and clean the assembly file
2. Construct a symbol table referencing the user-defined labels and variables
3. Translate the asm file to binary code
The file follows this pattern:
File parsing (line 147)
Symbol table (line 207)
Assembling (line 322)
??
# Assembly language specifications:
## Note on registers
The Hack computer has three 16-bit registers: the D-, A- and M-registers.
The D-register is used to store "data" that can be used as input for the ALU.
The A-register can be used in the same way but it also has a second role:
The RAM use it as address input. So any read/write instruction to the RAM is
done on the register which has the value of the A-register as address.
The M-register represents this register in the RAM that is 'pointed' onto by the
A-register. Reading/writing to it consists actually in reading/writing to
the RAM.
## Assembly instructions
There are three types of assembly instructions: A-instructions, C-instructions
and labels. Indents and blanks are ignored. Comments can only be in-line, start
with "//" and are ignored.
## A-instructions
- `"@" integer` where integer is a number in the range 0->32768. Sets the A
register to contain the specified integer. Ex: @42
- `"@" label` where label is a user-defined label. Sets the A register to
contain the code address corresponding to the label.
Labels are upper-cased by convention, with "_" as word separator. Ex: @MAIN
- `"@" variable` where variable is a user-defined variable. Sets the A register
to contain the RAM adress corresponding to the variable. If a variable is
encountered for the first time, it is automatically assigned an address.
The address assignment starts at RAM address 16 and increments.
Variables are lowercased by convention, with "_" as word separator. Ex: @i
## C-instructions
`(Dest-code "=")? op-code (";" jump-code)?`
- op-code:
Only the op-code is mandatory. It represents an instruction to be performed
by the ALU. Available codes and their associated outputs are:
- 0 -> the constant 0
- 1 -> the constant 1
- -1 -> the constant -1
- D -> the value contained in the D-register
- A -> the value contained in the A-register
- M -> the value contained in the M-Register
- !D -> bit-wise negation of the D-register
- !A -> bit-wise negation of the A-register
- !M -> bit-wise negation of the M-register
- -D -> numerical negation of the D-register using 2's complement
- -A -> numerical negation of the A-register using 2's complement
- -M -> numerical negation of the M-register using 2's complement
- D+1 -> 1 + value of the D-register
- A+1 -> 1 + value of the A-register
- M+1 -> 1 + value of the M-register
- D-1 -> -1 + value of the D-register
- A-1 -> -1 + value of the A-register
- M-1 -> -1 + value of the M-register
- D+A -> value of the D-register + value of the A-register
- D+M -> value of the D-register + value of the M-register
- D-A -> value of the D-register - value of the A-register
- D-M -> value of the D-register - value of the M-register
- A-D -> value of the A-register - value of the D-register
- M-D -> value of the M-register - value of the D-register
- D&A -> bit-wise AND of the values of the D and A registers
- D&M -> bit-wise AND of the values of the D and M registers
- D|A -> bit-wise OR of the values of the D and A registers
- D|M -> bit-wise OR of the values of the D and M registers
- dest-code:
If specified, should be followed with a "=" character. Available codes are:
- D -> write the ALU instruction's output to the D-register
- A -> write the ALU instruction's output to the A-register
- M -> write the ALU instruction's output to the M-register
- AD -> write the ALU instruction's output to the A- and D-registers
- AM -> write the ALU instruction's output to the A- and M-registers
- MD -> write the DLU instruction's output to the D- and M-registers
- ADM -> write the DLU instruction's output to the A-, D- and M-registers
- jump-code:
If specified, should be preceded by a ";" character. The computer is fed
with a programm containing one binary instruction per line. Each of those
instructions should be seen as having a number, starting at 0 and increasing
by one. The jump-code lets the computer jump to the instruction of which the
address is contained in the A-register if the result of the current
operation satisfies a certain condition. Available codes and corresponding
conditions are:
- JEQ -> jump if the output is equal to 0
- JLT -> jump if the output is lower than 0
- JLE -> jump if the output is lower than 0 or equal to 0
- JGT -> jump if the output is greater than 0
- JGE -> jump if the output is greater than 0 or equal to 0
- JNE -> jump if the output is not 0
- JMP -> just jump wathever the output
- Examples:
@3 // Set A to 3
0;JMP // unconditional jump to code line 3.
@42 // Set A to 42
D=D-A;JEQ: // Set D to D-A. if D-A == 0, jump to code line nb 42.
@i // Point onto var i, the real RAM address is handled by the assembler
M=A // Set corresponding value to it's own address
A=A+1 // Point to the RAM address just after i
## Labels
`"(" LABEL_NAME ")"`
When performing a jump, the appropriate line of code should be put in the
A-register. Setting directly the line number with a `@integer` instruction
is delicate since one has to figure out the line number ignoring comments,
blank lines, etc... And all the addresses have to be updated if the beginning of
the assembly code is edited afterward.
So the assembly language proposes to mark lines with a label using the `(LABEL)`
syntax. The assembler will then automatically adjust any `@LABEL` instruction
to match the desired code line at assembly time.
Example:
// This code runs a loop 42 times and then stops in an infinite empty loop
00 @MAIN // @2
01 0;JMP
(MAIN)
02 @42 // Set D to 42
03 D=A
04 @DECREMENT // @6
05 0;JMP
(DECREMENT)
06 D=D-1 // Decrement D
07 @END // @11
08 D;JEQ // Go there if D==0
09 @DECREMENT // Or continue the loop
10 0;JMP
(END)
11 @END // Infinity loop to end the programm
12 0;JMP
"""
import sys
from pathlib import Path
import re
#import pdb
from icecream import ic
##############
# File parsing
##############
def load_asm_file():
"""Read the asm file and preprocess it
The path to the file is treated as a global variable.
Preprocessing includes:
- Remove carriage returns and split file on new lines
- Remove comments and blanks in the lines
- Remove empty lines
"""
def read_lines():
"""Read the file given as script argument and split on new lines
Carriage returns are removed.
"""
ic(Path(sys.argv[1]).expanduser().read_text().replace(
'\r', '').split('\n'))
return Path(sys.argv[1]).expanduser().read_text().replace(
'\r', '').split('\n')
def filter_comment_and_blank_in_lines(lines):
"""Remove blanks and trailing comments from each line
Anything inside a line, after a "//" sequence is a comment.
"""
def filter_comment_and_blank_in_line(l):
ic(re.sub('\s', '', l).split('//')[0])
return re.sub('\s', '', l).split('//')[0]
ic([filter_comment_and_blank_in_line(l) for l in lines])
return [filter_comment_and_blank_in_line(l) for l in lines]
def remove_empty_lines(file):
ic("remove_empty_lines ",[ l for l in file if len(l) > 0])
return [l for l in file if len(l) > 0]
#ic("vor remove empty lines")
ic(remove_empty_lines(filter_comment_and_blank_in_lines(read_lines())))
return remove_empty_lines(
filter_comment_and_blank_in_lines(
read_lines()))
def is_label(line):
ic(line)
"""Recognise "label" declarations
A label is an line in the form `"(" LABEL_NAME ")"`
"""
ic(line.startswith('(') and line.endswith(')'))
return line.startswith('(') and line.endswith(')')
def extract_label_name(label_declaration):
ic("extract_label_name ", label_declaration.strip('()'))
"""Extract the label name from a label instruction"""
return label_declaration.strip('()')
def is_a_instruction(line):
"""Recognise "A-instructions"
An A-instruction starts with "@"
"""
ic("is_a_instruction ",line.startswith('@'))
return line.startswith('@')
##############
# Symbol table
##############
def default_symbol_table():
"""Construct a symbol table containing the pre-defined variables
Those variables are:
- SP: VM stack-pointer, RAM[0]
- LCL: VM local variable pointer, RAM[1]
- ARG: VM function argument pointer, RAM[2]
- THIS: VM object pointer, RAM[3]
- THAT: VM array pointer, RAM[4]
- SCREEN: base address for the screen memory-map, RAM[0x4000]
- KBD: address of the keyboard memory-map, RAM[0x6000]
- R0 -> R15: Shortcuts for the first 16 RAM locations
"""
return {
key: (value)
for key, value in {
**{'@SP': 0,
'@LCL': 1,
'@ARG': 2,
'@THIS': 3,
'@THAT': 4,
'@SCREEN': 0x4000,
'@KBD': 0x6000,},
**{f'@R{i}': i
for i in range(16)}
}.items()}
def inc_p_c(line, program_counter):
ic("inc_p_c ", line, program_counter )
"""Increment `program_counter` if `line` is an instruction"""
if is_label(line):
return program_counter
return program_counter + 1
def insert_into(symbol_table, label, value):
"""Return a copy of `symbol_table` with the `label: value` pair added"""
#ic("insert_into ",{**symbol_table, **{label: value}})
return {**symbol_table,
**{label: value}}
def add_label(label, symbol_table, program_counter):
"""Add a label to the symbol table"""
if label in symbol_table:
raise ValueError(f'Duplicate attemp at '
f'declaring label {label} '
f'before line {program_counter+1}')
#ic("0--- ", symbol_table, '@' + label, program_counter)
return insert_into(symbol_table, '@' + label, program_counter)
def find_and_add_labels(line, program_counter, symbol_table):
#ic("1 --- ", line, program_counter, symbol_table)
"""Look for a label declaration in `line` and add it to the symbol table"""
if is_label(line):
return add_label(extract_label_name(line), symbol_table,
program_counter)
return symbol_table
def add_user_labels(asm_lines, program_counter, symbol_table):
#ic("2 --- ", asm_lines,program_counter,symbol_table)
"""Add the user-defined labels in `asm_lines` to `symbol_table`"""
for line in asm_lines:
symbol_table = find_and_add_labels(line, program_counter,
symbol_table)
program_counter = inc_p_c(line, program_counter)
return symbol_table
def is_int(string):
"""Test if string is an int"""
try:
int(string)
return True
except ValueError:
return False
def find_and_add_variables(line, variable_counter, symbol_table):
"""Recognise if line declares a new variable and add it to `symbol_table`
This function assumes that labels have already been added to the
symbol-table. So any `@var` instruction where `var` is not in `symbol_table`
is a new variable.
"""
if not is_a_instruction(line):
return variable_counter, symbol_table
if line in symbol_table:
return variable_counter, symbol_table
if is_int(line[1:]):
return variable_counter, insert_into(symbol_table, line,
int(line[1:]))
return variable_counter+1, insert_into(symbol_table, line,
variable_counter)
def add_user_variables(asm_lines, variable_counter, symbol_table):
"""Add the user-defined variables to the symbol_table
This function assumes that labels have already been added to `symbol_table`.
"""
for line in asm_lines:
variable_counter, symbol_table = find_and_add_variables(line,
variable_counter, symbol_table)
return symbol_table
############
# Assembling
############
def int_to_binary(integer, bits=15):
"""Convert an integer to it's binary representation, as string"""
if bits < 0:
return ''
high_bit_value = 2**bits
return (
'1' if integer >= high_bit_value else '0'
) + int_to_binary(integer % high_bit_value, bits-1)
def get_dest(c_instruction):
"""Return the destination part of a c-instruction
C-instruction format:
`(Dest-code "=")? op-code (";" jump-code)?`
"""
return c_instruction.split('=')[0] if '=' in c_instruction else ''
def assemble_dest(dest):
"""Convert an assembly destination to its binary counterpart
The legal assembly destinations are: A, D, M, AD, AM, MD, ADM.
The binary representation of the destination is:
X X X
^ ^ ^
| | Write to M
| | ----------
| Write to D
| ----------
Write to A
"""
if dest not in ['', "A", "D", "M", "AD", "AM", "MD", "ADM"]:
raise ValueError(f"Unrecognised c-instruction destination: '{dest}'")
return (
('1' if 'A' in dest else '0') +
('1' if 'D' in dest else '0') +
('1' if 'M' in dest else '0'))
def get_jump(c_instruction):
"""Return the jump part of a c-instruction
C-instruction format:
`(Dest-code "=")? op-code (";" jump-code)?`
"""
return c_instruction.split(';')[1] if ';' in c_instruction else ''
def assemble_jump(jump):
"""Convert an assembly jump code to its binary counterpart
The legal assembly destinations are: JMP, JEQ, JLT, JLE, JGT, JGE, JNE
The binary representation of the jump is:
X X X
^ ^ ^
| | Jump if output is greater than 0
| | --------------------------------
| Jump if output is equal to 0
| ----------------------------
Jump if output is lower than 0
"""
if len(jump) == 0:
return '000'
if jump not in ["JMP", "JEQ", "JLT", "JLE", "JGT", "JGE", "JNE"]:
raise ValueError(f"Unrecognized jump instruction: {jump}")
if jump == 'JMP':
return '111'
return (
str(1 * ('L' in jump or jump == 'JNE')) +
str(1 * ('E' in jump and jump != 'JNE')) +
str(1 * ('G' in jump or jump == 'JNE'))
)
def get_op_code(c_instruction):
"""Return the op-code part of a c-instruction
C-instruction format:
`(Dest-code "=")? op-code (";" jump-code)?`
"""
if '=' in c_instruction:
return c_instruction.split('=')[1].split(';')[0]
return c_instruction.split(';')[0]
def assemble_op_code_no_M(op_code):
ic(op_code)
"""Convert an assembly op code to its binary counterpart
Note that this method assumes that the A/M switch is made. It
will thus only recognise operations on the A register. Any "M" has to be
replaced with "A".
The legal assembly destinations are: 0, 1, -1, D, M, !D, !A, -D, -A, D+1,
A+1, D-1, A-1, D+A, D-A, A-D, D&A, D|A.
The binary representation of the op-code is:
X X X X X X
^ ^ ^ ^ ^ ^
| | | | | Flip the output bits
| | | | | --------------------
| | | | operation switch (0->`AND`, 1->`+`)
| | | | -----------------------------------
| | | flip the A/M input's bits
| | | -------------------------
| | Zero the A/M input
| | ------------------
| Flip the D input's bits
| -----------------------
Zero the D input
"""
if op_code == '0':
return '101010'
if op_code == '1':
return '111111'
if op_code == '-1':
return '111010'
if op_code == 'D':
return '001100'
if op_code == 'A':
return '110000'
if op_code == '!D':
return '001101'
if op_code == '!A':
return '110001'
if op_code == '-D':
return '001111'
if op_code == '-A':
return '110011'
if op_code == 'D+1':
return '011111'
if op_code == 'A+1':
return '110111'
if op_code == 'D-1':
return '001110'
if op_code == 'A-1':
return '110010'
if op_code == 'D+A':
return '000010'
if op_code == 'D-A':
return '010011'
if op_code == 'A-D':
return '000111'
if op_code == 'D&A':
return '000000'
if op_code == 'D|A':
return '010101'
raise ValueError(f'Unrecognized op code: {op_code}')
def assemble_op_code(op_code):
ic("assemble_op_code ", op_code)
"""Assemble the A/M switch and the op-code"""
return ('1' if 'M' in op_code else '0') + \
assemble_op_code_no_M(op_code.replace('M', 'A'))
def assemble_c_instruction(c_instruction):
ic("assemble_c_instruction ", c_instruction)
"""Assemble a c-instruction
The binary representation of a c-instruction is
111 a.ffff.ff dd.d jjj
^^^ ^ ^^^^ ^^ ^^ ^ ^^^
||| | |||| || || | jump instruction
||| | |||| || || | ----------------
||| | |||| || Destination instruction
||| | |||| || -----------------------
||| | Operation instruction
||| | ---------------------
||| A/M switch (0->A, 1->M)
||| -----------------------
c-instruction marker
"""
return (
'111' +
assemble_op_code(get_op_code(c_instruction)) +
assemble_dest(get_dest(c_instruction)) +
assemble_jump(get_jump(c_instruction)))
def assemble_line(line, symbol_table):
"""Recognize if a line is a label, A- or C-instruction and assemble it"""
if is_label(line):
return ''
if is_a_instruction(line):
return int_to_binary(symbol_table[line]) + '\n'
return assemble_c_instruction(line) + '\n'
def assemble_lines(asm_lines, symbol_table):
return ''.join([
assemble_line(line, symbol_table)
for line in asm_lines
])
asm_file = load_asm_file()
print(assemble_lines(asm_file,
add_user_variables(asm_file, 16,
add_user_labels(asm_file, 0, default_symbol_table()))), end='')
#print(default_symbol_table())

267
asm/assembler03.py Normal file
View File

@@ -0,0 +1,267 @@
#!/usr/bin/env python
'''
Hack Assembler
Daniel Kronovet
kronovet@gmail.com
This implementation sticks closely to the API defined by the authors.
That said, I'm not thrilled with this implementation. The API given by the
authors felt a bit heavy; if time permits it would be worth reimplementing
this with a thinner interface, specifically by minimizing the SymbolTable API
and integrating Assembler and Parser into a single object.
'''
class Assembler(object):
def __init__(self, parser, symbol_table, code):
self.parser = parser
self.symbol_table = symbol_table
self.code = code
def assemble(self, asm_filename):
self.prepare_files(asm_filename)
parser = self.parser
# First pass to build label table
while parser.has_more_commands:
parser.advance()
if parser.command_type == 'L_COMMAND':
self.write_L(parser.symbol)
# Second pass to write .hack file
parser.reset_file()
self.ram_address = 16
while parser.has_more_commands:
parser.advance()
if parser.command_type == 'A_COMMAND':
self.write_A(parser.symbol)
elif parser.command_type == 'C_COMMAND':
self.write_C(parser.dest, parser.comp, parser.jump)
parser.close_asm()
self.hack.close()
def prepare_files(self, asm_filename):
assert '.asm' in asm_filename, 'Must pass .asm file!'
self.parser.load_file(asm_filename)
hack_filename = asm_filename.replace('.asm', '.hack')
self.hack = open(hack_filename, 'w')
def create_address(self, symbol):
address = '{0:b}'.format(int(symbol))
base = (15 - len(address)) * '0'
return base + address
def write(self, instruction):
self.hack.write(instruction + '\n')
def write_A(self, symbol):
instruction = '0'
try:
int(symbol)
except ValueError:
if not self.symbol_table.contains(symbol): # Build table on first pass
address = self.create_address(self.ram_address)
self.symbol_table.add_entry(symbol, address)
self.ram_address += 1
instruction += self.symbol_table.get_address(symbol)
else:
instruction += self.create_address(symbol)
self.write(instruction)
def write_L(self, symbol):
address = self.create_address(self.parser.instruction_num+1)
self.symbol_table.add_entry(symbol, address)
def write_C(self, dest, comp, jump):
instruction = '111'
instruction += self.code.comp(comp)
instruction += self.code.dest(dest)
instruction += self.code.jump(jump)
self.write(instruction)
class Parser(object):
def load_file(self, asm_filename):
self.asm = open(asm_filename, 'r')
self.reset_file()
self.symbol = None
self.dest = None
self.comp = None
self.jump = None
self.command_type = None
def reset_file(self):
self.asm.seek(0)
line = self.asm.readline().strip()
while self.is_not_instruction(line):
line = self.asm.readline().strip()
self.curr_instruction = line
self.instruction_num = -1 # 0 once first instruction is parsed.
def close_asm(self):
self.asm.close()
def is_not_instruction(self, line):
return not line or line[:2] == '//'
@property
def has_more_commands(self):
return bool(self.curr_instruction)
def get_next_instruction(self):
line = self.asm.readline().strip()
line = line.split('//')[0]
line = line.strip()
self.curr_instruction = line
def advance(self):
'''Parse current instruction and load next instruction
'''
ci = self.curr_instruction
if ci[0] == '@':
self.parse_A(ci)
self.instruction_num += 1
elif ci[0] == '(':
self.parse_L(ci)
else:
self.parse_C(ci)
self.instruction_num += 1
self.get_next_instruction()
def parse_A(self, instruction):
'''A instruction format: @address
'''
self.symbol = instruction[1:]
self.command_type = 'A_COMMAND'
def parse_L(self, instruction):
'''L instruction format: (LABEL)
'''
self.symbol = instruction[1:-1]
self.command_type = 'L_COMMAND'
def parse_C(self, instruction):
'''C instruction format: dest=comp;jump
'''
self.dest, self.comp, self.jump = None, None, None
parts = instruction.split(';')
remainder = parts[0]
if len(parts) == 2:
self.jump = parts[1]
parts = remainder.split('=')
if len(parts) == 2:
self.dest = parts[0]
self.comp = parts[1]
else:
self.comp = parts[0]
self.command_type = 'C_COMMAND'
class Code(object):
def dest(self, mnemonic):
'''Alt: Enumerate all possibilities and do dictionary lookup.
Current implemention is more flexible,
but slower (max 9 comparisons vs 1 hashing)
'''
bin = ['0', '0', '0']
if mnemonic is None:
return ''.join(bin)
if 'A' in mnemonic:
bin[0] = '1'
if 'D' in mnemonic:
bin[1] = '1'
if 'M' in mnemonic:
bin[2] = '1'
return ''.join(bin)
def comp(self, mnemonic):
comp_dict = {
'0': '101010',
'1': '111111',
'-1': '111010',
'D': '001100',
'A': '110000',
'!D': '001101',
'!A': '110001',
'-D': '001111',
'-A': '110011',
'D+1': '011111',
'A+1': '110111',
'D-1': '001110',
'A-1': '110010',
'D+A': '000010',
'D-A': '010011',
'A-D': '000111',
'D&A': '000000',
'D|A': '010101',
}
a_bit = '0'
if 'M' in mnemonic:
a_bit = '1'
mnemonic = mnemonic.replace('M', 'A')
c_bit = comp_dict.get(mnemonic, '000000')
return a_bit + c_bit
def jump(self, mnemonic):
jump_dict = {
'JGT': '001',
'JEQ': '010',
'JGE': '011',
'JLT': '100',
'JNE': '101',
'JLE': '110',
'JMP': '111',
}
return jump_dict.get(mnemonic, '000')
class SymbolTable(object):
def __init__(self):
self.symbol_dict = self.base_table()
self.ram_position = 16 # 0-15 have preset values
def get_address(self, symbol):
return self.symbol_dict[symbol]
def contains(self, symbol):
return symbol in self.symbol_dict
def add_entry(self, symbol, address):
self.symbol_dict[symbol] = address
def base_table(self): # 15 bit addresses, 32K locations
return {
'SP': '000000000000000',
'LCL': '000000000000001',
'ARG': '000000000000010',
'THIS': '000000000000011',
'THAT': '000000000000100',
'R0': '000000000000000',
'R1': '000000000000001',
'R2': '000000000000010',
'R3': '000000000000011',
'R4': '000000000000100',
'R5': '000000000000101',
'R6': '000000000000110',
'R7': '000000000000111',
'R8': '000000000001000',
'R9': '000000000001001',
'R10': '000000000001010',
'R11': '000000000001011',
'R12': '000000000001100',
'R13': '000000000001101',
'R14': '000000000001110',
'R15': '000000000001111',
'SCREEN': '100000000000000',
'KBD': '110000000000000',
}
if __name__ == '__main__':
import sys
#asm_filename = sys.argv[1]
asm_filename = "Rect.asm"
assembler = Assembler(Parser(), SymbolTable(), Code())
assembler.assemble(asm_filename)

247
asm/venv/bin/Activate.ps1 Normal file
View File

@@ -0,0 +1,247 @@
<#
.Synopsis
Activate a Python virtual environment for the current PowerShell session.
.Description
Pushes the python executable for a virtual environment to the front of the
$Env:PATH environment variable and sets the prompt to signify that you are
in a Python virtual environment. Makes use of the command line switches as
well as the `pyvenv.cfg` file values present in the virtual environment.
.Parameter VenvDir
Path to the directory that contains the virtual environment to activate. The
default value for this is the parent of the directory that the Activate.ps1
script is located within.
.Parameter Prompt
The prompt prefix to display when this virtual environment is activated. By
default, this prompt is the name of the virtual environment folder (VenvDir)
surrounded by parentheses and followed by a single space (ie. '(.venv) ').
.Example
Activate.ps1
Activates the Python virtual environment that contains the Activate.ps1 script.
.Example
Activate.ps1 -Verbose
Activates the Python virtual environment that contains the Activate.ps1 script,
and shows extra information about the activation as it executes.
.Example
Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
Activates the Python virtual environment located in the specified location.
.Example
Activate.ps1 -Prompt "MyPython"
Activates the Python virtual environment that contains the Activate.ps1 script,
and prefixes the current prompt with the specified string (surrounded in
parentheses) while the virtual environment is active.
.Notes
On Windows, it may be required to enable this Activate.ps1 script by setting the
execution policy for the user. You can do this by issuing the following PowerShell
command:
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
For more information on Execution Policies:
https://go.microsoft.com/fwlink/?LinkID=135170
#>
Param(
[Parameter(Mandatory = $false)]
[String]
$VenvDir,
[Parameter(Mandatory = $false)]
[String]
$Prompt
)
<# Function declarations --------------------------------------------------- #>
<#
.Synopsis
Remove all shell session elements added by the Activate script, including the
addition of the virtual environment's Python executable from the beginning of
the PATH variable.
.Parameter NonDestructive
If present, do not remove this function from the global namespace for the
session.
#>
function global:deactivate ([switch]$NonDestructive) {
# Revert to original values
# The prior prompt:
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
}
# The prior PYTHONHOME:
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
}
# The prior PATH:
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
Remove-Item -Path Env:_OLD_VIRTUAL_PATH
}
# Just remove the VIRTUAL_ENV altogether:
if (Test-Path -Path Env:VIRTUAL_ENV) {
Remove-Item -Path env:VIRTUAL_ENV
}
# Just remove VIRTUAL_ENV_PROMPT altogether.
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
}
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
}
# Leave deactivate function in the global namespace if requested:
if (-not $NonDestructive) {
Remove-Item -Path function:deactivate
}
}
<#
.Description
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
given folder, and returns them in a map.
For each line in the pyvenv.cfg file, if that line can be parsed into exactly
two strings separated by `=` (with any amount of whitespace surrounding the =)
then it is considered a `key = value` line. The left hand string is the key,
the right hand is the value.
If the value starts with a `'` or a `"` then the first and last character is
stripped from the value before being captured.
.Parameter ConfigDir
Path to the directory that contains the `pyvenv.cfg` file.
#>
function Get-PyVenvConfig(
[String]
$ConfigDir
) {
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
# An empty map will be returned if no config file is found.
$pyvenvConfig = @{ }
if ($pyvenvConfigPath) {
Write-Verbose "File exists, parse `key = value` lines"
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
$pyvenvConfigContent | ForEach-Object {
$keyval = $PSItem -split "\s*=\s*", 2
if ($keyval[0] -and $keyval[1]) {
$val = $keyval[1]
# Remove extraneous quotations around a string value.
if ("'""".Contains($val.Substring(0, 1))) {
$val = $val.Substring(1, $val.Length - 2)
}
$pyvenvConfig[$keyval[0]] = $val
Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
}
}
}
return $pyvenvConfig
}
<# Begin Activate script --------------------------------------------------- #>
# Determine the containing directory of this script
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$VenvExecDir = Get-Item -Path $VenvExecPath
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
# Set values required in priority: CmdLine, ConfigFile, Default
# First, get the location of the virtual environment, it might not be
# VenvExecDir if specified on the command line.
if ($VenvDir) {
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
}
else {
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
Write-Verbose "VenvDir=$VenvDir"
}
# Next, read the `pyvenv.cfg` file to determine any required value such
# as `prompt`.
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
# Next, set the prompt from the command line, or the config file, or
# just use the name of the virtual environment folder.
if ($Prompt) {
Write-Verbose "Prompt specified as argument, using '$Prompt'"
}
else {
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
$Prompt = $pyvenvCfg['prompt'];
}
else {
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
$Prompt = Split-Path -Path $venvDir -Leaf
}
}
Write-Verbose "Prompt = '$Prompt'"
Write-Verbose "VenvDir='$VenvDir'"
# Deactivate any currently active virtual environment, but leave the
# deactivate function in place.
deactivate -nondestructive
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine
# that there is an activated venv.
$env:VIRTUAL_ENV = $VenvDir
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
Write-Verbose "Setting prompt to '$Prompt'"
# Set the prompt to include the env name
# Make sure _OLD_VIRTUAL_PROMPT is global
function global:_OLD_VIRTUAL_PROMPT { "" }
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
function global:prompt {
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
_OLD_VIRTUAL_PROMPT
}
$env:VIRTUAL_ENV_PROMPT = $Prompt
}
# Clear PYTHONHOME
if (Test-Path -Path Env:PYTHONHOME) {
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
Remove-Item -Path Env:PYTHONHOME
}
# Add the venv to the PATH
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"

63
asm/venv/bin/activate Normal file
View File

@@ -0,0 +1,63 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# Call hash to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
hash -r 2> /dev/null
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
unset VIRTUAL_ENV_PROMPT
if [ ! "${1:-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="/Volumes/Daten01/Documents/Python/asm/venv"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
PS1="(venv) ${PS1:-}"
export PS1
VIRTUAL_ENV_PROMPT="(venv) "
export VIRTUAL_ENV_PROMPT
fi
# Call hash to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
hash -r 2> /dev/null

26
asm/venv/bin/activate.csh Normal file
View File

@@ -0,0 +1,26 @@
# This file must be used with "source bin/activate.csh" *from csh*.
# You cannot run it directly.
# Created by Davide Di Blasi <davidedb@gmail.com>.
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate'
# Unset irrelevant variables.
deactivate nondestructive
setenv VIRTUAL_ENV "/Volumes/Daten01/Documents/Python/asm/venv"
set _OLD_VIRTUAL_PATH="$PATH"
setenv PATH "$VIRTUAL_ENV/bin:$PATH"
set _OLD_VIRTUAL_PROMPT="$prompt"
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
set prompt = "(venv) $prompt"
setenv VIRTUAL_ENV_PROMPT "(venv) "
endif
alias pydoc python -m pydoc
rehash

View File

@@ -0,0 +1,69 @@
# This file must be used with "source <venv>/bin/activate.fish" *from fish*
# (https://fishshell.com/); you cannot run it directly.
function deactivate -d "Exit virtual environment and return to normal shell environment"
# reset old environment variables
if test -n "$_OLD_VIRTUAL_PATH"
set -gx PATH $_OLD_VIRTUAL_PATH
set -e _OLD_VIRTUAL_PATH
end
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
set -e _OLD_VIRTUAL_PYTHONHOME
end
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
set -e _OLD_FISH_PROMPT_OVERRIDE
# prevents error when using nested fish instances (Issue #93858)
if functions -q _old_fish_prompt
functions -e fish_prompt
functions -c _old_fish_prompt fish_prompt
functions -e _old_fish_prompt
end
end
set -e VIRTUAL_ENV
set -e VIRTUAL_ENV_PROMPT
if test "$argv[1]" != "nondestructive"
# Self-destruct!
functions -e deactivate
end
end
# Unset irrelevant variables.
deactivate nondestructive
set -gx VIRTUAL_ENV "/Volumes/Daten01/Documents/Python/asm/venv"
set -gx _OLD_VIRTUAL_PATH $PATH
set -gx PATH "$VIRTUAL_ENV/bin" $PATH
# Unset PYTHONHOME if set.
if set -q PYTHONHOME
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
set -e PYTHONHOME
end
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
# fish uses a function instead of an env var to generate the prompt.
# Save the current fish_prompt function as the function _old_fish_prompt.
functions -c fish_prompt _old_fish_prompt
# With the original prompt function renamed, we can override with our own.
function fish_prompt
# Save the return status of the last command.
set -l old_status $status
# Output the venv prompt; color taken from the blue of the Python logo.
printf "%s%s%s" (set_color 4B8BBE) "(venv) " (set_color normal)
# Restore the return status of the previous command.
echo "exit $old_status" | .
# Output the original/"old" prompt.
_old_fish_prompt
end
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
set -gx VIRTUAL_ENV_PROMPT "(venv) "
end

8
asm/venv/bin/ipython Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from IPython import start_ipython
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())

8
asm/venv/bin/ipython3 Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from IPython import start_ipython
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())

8
asm/venv/bin/jupyter Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from jupyter_core.command import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

8
asm/venv/bin/jupyter-kernel Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from jupyter_client.kernelapp import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from jupyter_client.kernelspecapp import KernelSpecApp
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(KernelSpecApp.launch_instance())

8
asm/venv/bin/jupyter-migrate Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from jupyter_core.migrate import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

8
asm/venv/bin/jupyter-run Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from jupyter_client.runapp import RunApp
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(RunApp.launch_instance())

View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from jupyter_core.troubleshoot import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

8
asm/venv/bin/pip Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

8
asm/venv/bin/pip3 Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

8
asm/venv/bin/pip3.11 Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

8
asm/venv/bin/pygmentize Executable file
View File

@@ -0,0 +1,8 @@
#!/Volumes/Daten01/Documents/Python/asm/venv/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from pygments.cmdline import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

1
asm/venv/bin/python Symbolic link
View File

@@ -0,0 +1 @@
python3.11

1
asm/venv/bin/python3 Symbolic link
View File

@@ -0,0 +1 @@
python3.11

1
asm/venv/bin/python3.11 Symbolic link
View File

@@ -0,0 +1 @@
/opt/homebrew/opt/python@3.11/bin/python3.11

View File

@@ -0,0 +1,163 @@
# PYTHON_ARGCOMPLETE_OK
"""
IPython: tools for interactive and parallel computing in Python.
https://ipython.org
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2008-2011, IPython Development Team.
# Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
# Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
# Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys
#-----------------------------------------------------------------------------
# Setup everything
#-----------------------------------------------------------------------------
# Don't forget to also update setup.py when this changes!
if sys.version_info < (3, 10):
raise ImportError(
"""
IPython 8.19+ supports Python 3.10 and above, following SPEC0.
IPython 8.13+ supports Python 3.9 and above, following NEP 29.
IPython 8.0-8.12 supports Python 3.8 and above, following NEP 29.
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
Python 3.3 and 3.4 were supported up to IPython 6.x.
Python 3.5 was supported with IPython 7.0 to 7.9.
Python 3.6 was supported with IPython up to 7.16.
Python 3.7 was still supported with the 7.x branch.
See IPython `README.rst` file for more information:
https://github.com/ipython/ipython/blob/main/README.rst
"""
)
#-----------------------------------------------------------------------------
# Setup the top level names
#-----------------------------------------------------------------------------
from .core.getipython import get_ipython
from .core import release
from .core.application import Application
from .terminal.embed import embed
from .core.interactiveshell import InteractiveShell
from .utils.sysinfo import sys_info
from .utils.frame import extract_module_locals
__all__ = ["start_ipython", "embed", "start_kernel", "embed_kernel"]
# Release data
__author__ = '%s <%s>' % (release.author, release.author_email)
__license__ = release.license
__version__ = release.version
version_info = release.version_info
# list of CVEs that should have been patched in this release.
# this is informational and should not be relied upon.
__patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"}
def embed_kernel(module=None, local_ns=None, **kwargs):
"""Embed and start an IPython kernel in a given scope.
If you don't want the kernel to initialize the namespace
from the scope of the surrounding function,
and/or you want to load full IPython configuration,
you probably want `IPython.start_kernel()` instead.
Parameters
----------
module : types.ModuleType, optional
The module to load into IPython globals (default: caller)
local_ns : dict, optional
The namespace to load into IPython user namespace (default: caller)
**kwargs : various, optional
Further keyword args are relayed to the IPKernelApp constructor,
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
allowing configuration of the kernel (see :ref:`kernel_options`). Will only have an effect
on the first embed_kernel call for a given process.
"""
(caller_module, caller_locals) = extract_module_locals(1)
if module is None:
module = caller_module
if local_ns is None:
local_ns = caller_locals
# Only import .zmq when we really need it
from ipykernel.embed import embed_kernel as real_embed_kernel
real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
def start_ipython(argv=None, **kwargs):
"""Launch a normal IPython instance (as opposed to embedded)
`IPython.embed()` puts a shell in a particular calling scope,
such as a function or method for debugging purposes,
which is often not desirable.
`start_ipython()` does full, regular IPython initialization,
including loading startup files, configuration, etc.
much of which is skipped by `embed()`.
This is a public API method, and will survive implementation changes.
Parameters
----------
argv : list or None, optional
If unspecified or None, IPython will parse command-line options from sys.argv.
To prevent any command-line parsing, pass an empty list: `argv=[]`.
user_ns : dict, optional
specify this dictionary to initialize the IPython user namespace with particular values.
**kwargs : various, optional
Any other kwargs will be passed to the Application constructor,
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
allowing configuration of the instance (see :ref:`terminal_options`).
"""
from IPython.terminal.ipapp import launch_new_instance
return launch_new_instance(argv=argv, **kwargs)
def start_kernel(argv=None, **kwargs):
"""Launch a normal IPython kernel instance (as opposed to embedded)
`IPython.embed_kernel()` puts a shell in a particular calling scope,
such as a function or method for debugging purposes,
which is often not desirable.
`start_kernel()` does full, regular IPython initialization,
including loading startup files, configuration, etc.
much of which is skipped by `embed_kernel()`.
Parameters
----------
argv : list or None, optional
If unspecified or None, IPython will parse command-line options from sys.argv.
To prevent any command-line parsing, pass an empty list: `argv=[]`.
user_ns : dict, optional
specify this dictionary to initialize the IPython user namespace with particular values.
**kwargs : various, optional
Any other kwargs will be passed to the Application constructor,
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
allowing configuration of the kernel (see :ref:`kernel_options`).
"""
import warnings
warnings.warn(
"start_kernel is deprecated since IPython 8.0, use from `ipykernel.kernelapp.launch_new_instance`",
DeprecationWarning,
stacklevel=2,
)
from ipykernel.kernelapp import launch_new_instance
return launch_new_instance(argv=argv, **kwargs)

View File

@@ -0,0 +1,15 @@
# PYTHON_ARGCOMPLETE_OK
# encoding: utf-8
"""Terminal-based IPython entry point.
"""
# -----------------------------------------------------------------------------
# Copyright (c) 2012, IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# -----------------------------------------------------------------------------
from IPython import start_ipython
start_ipython()

View File

@@ -0,0 +1,87 @@
import builtins
import inspect
import os
import pathlib
import shutil
import sys
import types
import pytest
# Must register before it gets imported
pytest.register_assert_rewrite("IPython.testing.tools")
from .testing import tools
def pytest_collection_modifyitems(items):
"""This function is automatically run by pytest passing all collected test
functions.
We use it to add asyncio marker to all async tests and assert we don't use
test functions that are async generators which wouldn't make sense.
"""
for item in items:
if inspect.iscoroutinefunction(item.obj):
item.add_marker("asyncio")
assert not inspect.isasyncgenfunction(item.obj)
def get_ipython():
from .terminal.interactiveshell import TerminalInteractiveShell
if TerminalInteractiveShell._instance:
return TerminalInteractiveShell.instance()
config = tools.default_config()
config.TerminalInteractiveShell.simple_prompt = True
# Create and initialize our test-friendly IPython instance.
shell = TerminalInteractiveShell.instance(config=config)
return shell
@pytest.fixture(scope='session', autouse=True)
def work_path():
path = pathlib.Path("./tmp-ipython-pytest-profiledir")
os.environ["IPYTHONDIR"] = str(path.absolute())
if path.exists():
raise ValueError('IPython dir temporary path already exists ! Did previous test run exit successfully ?')
path.mkdir()
yield
shutil.rmtree(str(path.resolve()))
def nopage(strng, start=0, screen_lines=0, pager_cmd=None):
if isinstance(strng, dict):
strng = strng.get("text/plain", "")
print(strng)
def xsys(self, cmd):
"""Replace the default system call with a capturing one for doctest.
"""
# We use getoutput, but we need to strip it because pexpect captures
# the trailing newline differently from commands.getoutput
print(self.getoutput(cmd, split=False, depth=1).rstrip(), end="", file=sys.stdout)
sys.stdout.flush()
# for things to work correctly we would need this as a session fixture;
# unfortunately this will fail on some test that get executed as _collection_
# time (before the fixture run), in particular parametrized test that contain
# yields. so for now execute at import time.
#@pytest.fixture(autouse=True, scope='session')
def inject():
builtins.get_ipython = get_ipython
builtins._ip = get_ipython()
builtins.ip = get_ipython()
builtins.ip.system = types.MethodType(xsys, ip)
builtins.ip.builtin_trap.activate()
from .core import page
page.pager_page = nopage
# yield
inject()

View File

@@ -0,0 +1,12 @@
"""
Shim to maintain backwards compatibility with old IPython.consoleapp imports.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from warnings import warn
warn("The `IPython.consoleapp` package has been deprecated since IPython 4.0."
"You should import from jupyter_client.consoleapp instead.", stacklevel=2)
from jupyter_client.consoleapp import *

View File

@@ -0,0 +1,267 @@
# encoding: utf-8
"""
System command aliases.
Authors:
* Fernando Perez
* Brian Granger
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import re
import sys
from traitlets.config.configurable import Configurable
from .error import UsageError
from traitlets import List, Instance
from logging import error
import typing as t
#-----------------------------------------------------------------------------
# Utilities
#-----------------------------------------------------------------------------
# This is used as the pattern for calls to split_user_input.
shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
def default_aliases() -> t.List[t.Tuple[str, str]]:
"""Return list of shell aliases to auto-define.
"""
# Note: the aliases defined here should be safe to use on a kernel
# regardless of what frontend it is attached to. Frontends that use a
# kernel in-process can define additional aliases that will only work in
# their case. For example, things like 'less' or 'clear' that manipulate
# the terminal should NOT be declared here, as they will only work if the
# kernel is running inside a true terminal, and not over the network.
if os.name == 'posix':
default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'),
('cat', 'cat'),
]
# Useful set of ls aliases. The GNU and BSD options are a little
# different, so we make aliases that provide as similar as possible
# behavior in ipython, by passing the right flags for each platform
if sys.platform.startswith('linux'):
ls_aliases = [('ls', 'ls -F --color'),
# long ls
('ll', 'ls -F -o --color'),
# ls normal files only
('lf', 'ls -F -o --color %l | grep ^-'),
# ls symbolic links
('lk', 'ls -F -o --color %l | grep ^l'),
# directories or links to directories,
('ldir', 'ls -F -o --color %l | grep /$'),
# things which are executable
('lx', 'ls -F -o --color %l | grep ^-..x'),
]
elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'):
# OpenBSD, NetBSD. The ls implementation on these platforms do not support
# the -G switch and lack the ability to use colorized output.
ls_aliases = [('ls', 'ls -F'),
# long ls
('ll', 'ls -F -l'),
# ls normal files only
('lf', 'ls -F -l %l | grep ^-'),
# ls symbolic links
('lk', 'ls -F -l %l | grep ^l'),
# directories or links to directories,
('ldir', 'ls -F -l %l | grep /$'),
# things which are executable
('lx', 'ls -F -l %l | grep ^-..x'),
]
else:
# BSD, OSX, etc.
ls_aliases = [('ls', 'ls -F -G'),
# long ls
('ll', 'ls -F -l -G'),
# ls normal files only
('lf', 'ls -F -l -G %l | grep ^-'),
# ls symbolic links
('lk', 'ls -F -l -G %l | grep ^l'),
# directories or links to directories,
('ldir', 'ls -F -G -l %l | grep /$'),
# things which are executable
('lx', 'ls -F -l -G %l | grep ^-..x'),
]
default_aliases = default_aliases + ls_aliases
elif os.name in ['nt', 'dos']:
default_aliases = [('ls', 'dir /on'),
('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
]
else:
default_aliases = []
return default_aliases
class AliasError(Exception):
pass
class InvalidAliasError(AliasError):
pass
class Alias(object):
"""Callable object storing the details of one alias.
Instances are registered as magic functions to allow use of aliases.
"""
# Prepare blacklist
blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
def __init__(self, shell, name, cmd):
self.shell = shell
self.name = name
self.cmd = cmd
self.__doc__ = "Alias for `!{}`".format(cmd)
self.nargs = self.validate()
def validate(self):
"""Validate the alias, and return the number of arguments."""
if self.name in self.blacklist:
raise InvalidAliasError("The name %s can't be aliased "
"because it is a keyword or builtin." % self.name)
try:
caller = self.shell.magics_manager.magics['line'][self.name]
except KeyError:
pass
else:
if not isinstance(caller, Alias):
raise InvalidAliasError("The name %s can't be aliased "
"because it is another magic command." % self.name)
if not (isinstance(self.cmd, str)):
raise InvalidAliasError("An alias command must be a string, "
"got: %r" % self.cmd)
nargs = self.cmd.count('%s') - self.cmd.count('%%s')
if (nargs > 0) and (self.cmd.find('%l') >= 0):
raise InvalidAliasError('The %s and %l specifiers are mutually '
'exclusive in alias definitions.')
return nargs
def __repr__(self):
return "<alias {} for {!r}>".format(self.name, self.cmd)
def __call__(self, rest=''):
cmd = self.cmd
nargs = self.nargs
# Expand the %l special to be the user's input line
if cmd.find('%l') >= 0:
cmd = cmd.replace('%l', rest)
rest = ''
if nargs==0:
if cmd.find('%%s') >= 1:
cmd = cmd.replace('%%s', '%s')
# Simple, argument-less aliases
cmd = '%s %s' % (cmd, rest)
else:
# Handle aliases with positional arguments
args = rest.split(None, nargs)
if len(args) < nargs:
raise UsageError('Alias <%s> requires %s arguments, %s given.' %
(self.name, nargs, len(args)))
cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
self.shell.system(cmd)
#-----------------------------------------------------------------------------
# Main AliasManager class
#-----------------------------------------------------------------------------
class AliasManager(Configurable):
default_aliases: List = List(default_aliases()).tag(config=True)
user_aliases: List = List(default_value=[]).tag(config=True)
shell = Instance(
"IPython.core.interactiveshell.InteractiveShellABC", allow_none=True
)
def __init__(self, shell=None, **kwargs):
super(AliasManager, self).__init__(shell=shell, **kwargs)
# For convenient access
if self.shell is not None:
self.linemagics = self.shell.magics_manager.magics["line"]
self.init_aliases()
def init_aliases(self):
# Load default & user aliases
for name, cmd in self.default_aliases + self.user_aliases:
if (
cmd.startswith("ls ")
and self.shell is not None
and self.shell.colors == "NoColor"
):
cmd = cmd.replace(" --color", "")
self.soft_define_alias(name, cmd)
@property
def aliases(self):
return [(n, func.cmd) for (n, func) in self.linemagics.items()
if isinstance(func, Alias)]
def soft_define_alias(self, name, cmd):
"""Define an alias, but don't raise on an AliasError."""
try:
self.define_alias(name, cmd)
except AliasError as e:
error("Invalid alias: %s" % e)
def define_alias(self, name, cmd):
"""Define a new alias after validating it.
This will raise an :exc:`AliasError` if there are validation
problems.
"""
caller = Alias(shell=self.shell, name=name, cmd=cmd)
self.shell.magics_manager.register_function(caller, magic_kind='line',
magic_name=name)
def get_alias(self, name):
"""Return an alias, or None if no alias by that name exists."""
aname = self.linemagics.get(name, None)
return aname if isinstance(aname, Alias) else None
def is_alias(self, name):
"""Return whether or not a given name has been defined as an alias"""
return self.get_alias(name) is not None
def undefine_alias(self, name):
if self.is_alias(name):
del self.linemagics[name]
else:
raise ValueError('%s is not an alias' % name)
def clear_aliases(self):
for name, _ in self.aliases:
self.undefine_alias(name)
def retrieve_alias(self, name):
"""Retrieve the command to which an alias expands."""
caller = self.get_alias(name)
if caller:
return caller.cmd
else:
raise ValueError('%s is not an alias' % name)

View File

@@ -0,0 +1,492 @@
# encoding: utf-8
"""
An application for IPython.
All top-level applications should use the classes in this module for
handling configuration and creating configurables.
The job of an :class:`Application` is to create the master configuration
object and then create the configurable objects, passing the config to them.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import atexit
from copy import deepcopy
import logging
import os
import shutil
import sys
from pathlib import Path
from traitlets.config.application import Application, catch_config_error
from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader
from IPython.core import release, crashhandler
from IPython.core.profiledir import ProfileDir, ProfileDirError
from IPython.paths import get_ipython_dir, get_ipython_package_dir
from IPython.utils.path import ensure_dir_exists
from traitlets import (
List, Unicode, Type, Bool, Set, Instance, Undefined,
default, observe,
)
if os.name == "nt":
programdata = os.environ.get("PROGRAMDATA", None)
if programdata is not None:
SYSTEM_CONFIG_DIRS = [str(Path(programdata) / "ipython")]
else: # PROGRAMDATA is not defined by default on XP.
SYSTEM_CONFIG_DIRS = []
else:
SYSTEM_CONFIG_DIRS = [
"/usr/local/etc/ipython",
"/etc/ipython",
]
ENV_CONFIG_DIRS = []
_env_config_dir = os.path.join(sys.prefix, 'etc', 'ipython')
if _env_config_dir not in SYSTEM_CONFIG_DIRS:
# only add ENV_CONFIG if sys.prefix is not already included
ENV_CONFIG_DIRS.append(_env_config_dir)
_envvar = os.environ.get('IPYTHON_SUPPRESS_CONFIG_ERRORS')
if _envvar in {None, ''}:
IPYTHON_SUPPRESS_CONFIG_ERRORS = None
else:
if _envvar.lower() in {'1','true'}:
IPYTHON_SUPPRESS_CONFIG_ERRORS = True
elif _envvar.lower() in {'0','false'} :
IPYTHON_SUPPRESS_CONFIG_ERRORS = False
else:
sys.exit("Unsupported value for environment variable: 'IPYTHON_SUPPRESS_CONFIG_ERRORS' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}."% _envvar )
# aliases and flags
base_aliases = {}
if isinstance(Application.aliases, dict):
# traitlets 5
base_aliases.update(Application.aliases)
base_aliases.update(
{
"profile-dir": "ProfileDir.location",
"profile": "BaseIPythonApplication.profile",
"ipython-dir": "BaseIPythonApplication.ipython_dir",
"log-level": "Application.log_level",
"config": "BaseIPythonApplication.extra_config_file",
}
)
base_flags = dict()
if isinstance(Application.flags, dict):
# traitlets 5
base_flags.update(Application.flags)
base_flags.update(
dict(
debug=(
{"Application": {"log_level": logging.DEBUG}},
"set log level to logging.DEBUG (maximize logging output)",
),
quiet=(
{"Application": {"log_level": logging.CRITICAL}},
"set log level to logging.CRITICAL (minimize logging output)",
),
init=(
{
"BaseIPythonApplication": {
"copy_config_files": True,
"auto_create": True,
}
},
"""Initialize profile with default config files. This is equivalent
to running `ipython profile create <profile>` prior to startup.
""",
),
)
)
class ProfileAwareConfigLoader(PyFileConfigLoader):
"""A Python file config loader that is aware of IPython profiles."""
def load_subconfig(self, fname, path=None, profile=None):
if profile is not None:
try:
profile_dir = ProfileDir.find_profile_dir_by_name(
get_ipython_dir(),
profile,
)
except ProfileDirError:
return
path = profile_dir.location
return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path)
class BaseIPythonApplication(Application):
name = "ipython"
description = "IPython: an enhanced interactive Python shell."
version = Unicode(release.version)
aliases = base_aliases
flags = base_flags
classes = List([ProfileDir])
# enable `load_subconfig('cfg.py', profile='name')`
python_config_loader_class = ProfileAwareConfigLoader
# Track whether the config_file has changed,
# because some logic happens only if we aren't using the default.
config_file_specified = Set()
config_file_name = Unicode()
@default('config_file_name')
def _config_file_name_default(self):
return self.name.replace('-','_') + u'_config.py'
@observe('config_file_name')
def _config_file_name_changed(self, change):
if change['new'] != change['old']:
self.config_file_specified.add(change['new'])
# The directory that contains IPython's builtin profiles.
builtin_profile_dir = Unicode(
os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default')
)
config_file_paths = List(Unicode())
@default('config_file_paths')
def _config_file_paths_default(self):
return []
extra_config_file = Unicode(
help="""Path to an extra config file to load.
If specified, load this config file in addition to any other IPython config.
""").tag(config=True)
@observe('extra_config_file')
def _extra_config_file_changed(self, change):
old = change['old']
new = change['new']
try:
self.config_files.remove(old)
except ValueError:
pass
self.config_file_specified.add(new)
self.config_files.append(new)
profile = Unicode(u'default',
help="""The IPython profile to use."""
).tag(config=True)
@observe('profile')
def _profile_changed(self, change):
self.builtin_profile_dir = os.path.join(
get_ipython_package_dir(), u'config', u'profile', change['new']
)
add_ipython_dir_to_sys_path = Bool(
False,
"""Should the IPython profile directory be added to sys path ?
This option was non-existing before IPython 8.0, and ipython_dir was added to
sys path to allow import of extensions present there. This was historical
baggage from when pip did not exist. This now default to false,
but can be set to true for legacy reasons.
""",
).tag(config=True)
ipython_dir = Unicode(
help="""
The name of the IPython directory. This directory is used for logging
configuration (through profiles), history storage, etc. The default
is usually $HOME/.ipython. This option can also be specified through
the environment variable IPYTHONDIR.
"""
).tag(config=True)
@default('ipython_dir')
def _ipython_dir_default(self):
d = get_ipython_dir()
self._ipython_dir_changed({
'name': 'ipython_dir',
'old': d,
'new': d,
})
return d
_in_init_profile_dir = False
profile_dir = Instance(ProfileDir, allow_none=True)
@default('profile_dir')
def _profile_dir_default(self):
# avoid recursion
if self._in_init_profile_dir:
return
# profile_dir requested early, force initialization
self.init_profile_dir()
return self.profile_dir
overwrite = Bool(False,
help="""Whether to overwrite existing config files when copying"""
).tag(config=True)
auto_create = Bool(False,
help="""Whether to create profile dir if it doesn't exist"""
).tag(config=True)
config_files = List(Unicode())
@default('config_files')
def _config_files_default(self):
return [self.config_file_name]
copy_config_files = Bool(False,
help="""Whether to install the default config files into the profile dir.
If a new profile is being created, and IPython contains config files for that
profile, then they will be staged into the new directory. Otherwise,
default config files will be automatically generated.
""").tag(config=True)
verbose_crash = Bool(False,
help="""Create a massive crash report when IPython encounters what may be an
internal error. The default is to append a short message to the
usual traceback""").tag(config=True)
# The class to use as the crash handler.
crash_handler_class = Type(crashhandler.CrashHandler)
@catch_config_error
def __init__(self, **kwargs):
super(BaseIPythonApplication, self).__init__(**kwargs)
# ensure current working directory exists
try:
os.getcwd()
except:
# exit if cwd doesn't exist
self.log.error("Current working directory doesn't exist.")
self.exit(1)
#-------------------------------------------------------------------------
# Various stages of Application creation
#-------------------------------------------------------------------------
def init_crash_handler(self):
"""Create a crash handler, typically setting sys.excepthook to it."""
self.crash_handler = self.crash_handler_class(self)
sys.excepthook = self.excepthook
def unset_crashhandler():
sys.excepthook = sys.__excepthook__
atexit.register(unset_crashhandler)
def excepthook(self, etype, evalue, tb):
"""this is sys.excepthook after init_crashhandler
set self.verbose_crash=True to use our full crashhandler, instead of
a regular traceback with a short message (crash_handler_lite)
"""
if self.verbose_crash:
return self.crash_handler(etype, evalue, tb)
else:
return crashhandler.crash_handler_lite(etype, evalue, tb)
@observe('ipython_dir')
def _ipython_dir_changed(self, change):
old = change['old']
new = change['new']
if old is not Undefined:
str_old = os.path.abspath(old)
if str_old in sys.path:
sys.path.remove(str_old)
if self.add_ipython_dir_to_sys_path:
str_path = os.path.abspath(new)
sys.path.append(str_path)
ensure_dir_exists(new)
readme = os.path.join(new, "README")
readme_src = os.path.join(
get_ipython_package_dir(), "config", "profile", "README"
)
if not os.path.exists(readme) and os.path.exists(readme_src):
shutil.copy(readme_src, readme)
for d in ("extensions", "nbextensions"):
path = os.path.join(new, d)
try:
ensure_dir_exists(path)
except OSError as e:
# this will not be EEXIST
self.log.error("couldn't create path %s: %s", path, e)
self.log.debug("IPYTHONDIR set to: %s", new)
def load_config_file(self, suppress_errors=IPYTHON_SUPPRESS_CONFIG_ERRORS):
"""Load the config file.
By default, errors in loading config are handled, and a warning
printed on screen. For testing, the suppress_errors option is set
to False, so errors will make tests fail.
`suppress_errors` default value is to be `None` in which case the
behavior default to the one of `traitlets.Application`.
The default value can be set :
- to `False` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '0', or 'false' (case insensitive).
- to `True` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '1' or 'true' (case insensitive).
- to `None` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '' (empty string) or leaving it unset.
Any other value are invalid, and will make IPython exit with a non-zero return code.
"""
self.log.debug("Searching path %s for config files", self.config_file_paths)
base_config = 'ipython_config.py'
self.log.debug("Attempting to load config file: %s" %
base_config)
try:
if suppress_errors is not None:
old_value = Application.raise_config_file_errors
Application.raise_config_file_errors = not suppress_errors;
Application.load_config_file(
self,
base_config,
path=self.config_file_paths
)
except ConfigFileNotFound:
# ignore errors loading parent
self.log.debug("Config file %s not found", base_config)
pass
if suppress_errors is not None:
Application.raise_config_file_errors = old_value
for config_file_name in self.config_files:
if not config_file_name or config_file_name == base_config:
continue
self.log.debug("Attempting to load config file: %s" %
self.config_file_name)
try:
Application.load_config_file(
self,
config_file_name,
path=self.config_file_paths
)
except ConfigFileNotFound:
# Only warn if the default config file was NOT being used.
if config_file_name in self.config_file_specified:
msg = self.log.warning
else:
msg = self.log.debug
msg("Config file not found, skipping: %s", config_file_name)
except Exception:
# For testing purposes.
if not suppress_errors:
raise
self.log.warning("Error loading config file: %s" %
self.config_file_name, exc_info=True)
def init_profile_dir(self):
"""initialize the profile dir"""
self._in_init_profile_dir = True
if self.profile_dir is not None:
# already ran
return
if 'ProfileDir.location' not in self.config:
# location not specified, find by profile name
try:
p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
except ProfileDirError:
# not found, maybe create it (always create default profile)
if self.auto_create or self.profile == 'default':
try:
p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
except ProfileDirError:
self.log.fatal("Could not create profile: %r"%self.profile)
self.exit(1)
else:
self.log.info("Created profile dir: %r"%p.location)
else:
self.log.fatal("Profile %r not found."%self.profile)
self.exit(1)
else:
self.log.debug("Using existing profile dir: %r", p.location)
else:
location = self.config.ProfileDir.location
# location is fully specified
try:
p = ProfileDir.find_profile_dir(location, self.config)
except ProfileDirError:
# not found, maybe create it
if self.auto_create:
try:
p = ProfileDir.create_profile_dir(location, self.config)
except ProfileDirError:
self.log.fatal("Could not create profile directory: %r"%location)
self.exit(1)
else:
self.log.debug("Creating new profile dir: %r"%location)
else:
self.log.fatal("Profile directory %r not found."%location)
self.exit(1)
else:
self.log.debug("Using existing profile dir: %r", p.location)
# if profile_dir is specified explicitly, set profile name
dir_name = os.path.basename(p.location)
if dir_name.startswith('profile_'):
self.profile = dir_name[8:]
self.profile_dir = p
self.config_file_paths.append(p.location)
self._in_init_profile_dir = False
def init_config_files(self):
"""[optionally] copy default config files into profile dir."""
self.config_file_paths.extend(ENV_CONFIG_DIRS)
self.config_file_paths.extend(SYSTEM_CONFIG_DIRS)
# copy config files
path = Path(self.builtin_profile_dir)
if self.copy_config_files:
src = self.profile
cfg = self.config_file_name
if path and (path / cfg).exists():
self.log.warning(
"Staging %r from %s into %r [overwrite=%s]"
% (cfg, src, self.profile_dir.location, self.overwrite)
)
self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
else:
self.stage_default_config_file()
else:
# Still stage *bundled* config files, but not generated ones
# This is necessary for `ipython profile=sympy` to load the profile
# on the first go
files = path.glob("*.py")
for fullpath in files:
cfg = fullpath.name
if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
# file was copied
self.log.warning("Staging bundled %s from %s into %r"%(
cfg, self.profile, self.profile_dir.location)
)
def stage_default_config_file(self):
"""auto generate default config file, and stage it into the profile."""
s = self.generate_config_file()
config_file = Path(self.profile_dir.location) / self.config_file_name
if self.overwrite or not config_file.exists():
self.log.warning("Generating default config file: %r", (config_file))
config_file.write_text(s, encoding="utf-8")
@catch_config_error
def initialize(self, argv=None):
# don't hook up crash handler before parsing command-line
self.parse_command_line(argv)
self.init_crash_handler()
if self.subapp is not None:
# stop here if subapp is taking over
return
# save a copy of CLI config to re-load after config files
# so that it has highest priority
cl_config = deepcopy(self.config)
self.init_profile_dir()
self.init_config_files()
self.load_config_file()
# enforce cl-opts override configfile opts:
self.update_config(cl_config)

View File

@@ -0,0 +1,156 @@
"""
Async helper function that are invalid syntax on Python 3.5 and below.
This code is best effort, and may have edge cases not behaving as expected. In
particular it contain a number of heuristics to detect whether code is
effectively async and need to run in an event loop or not.
Some constructs (like top-level `return`, or `yield`) are taken care of
explicitly to actually raise a SyntaxError and stay as close as possible to
Python semantics.
"""
import ast
import asyncio
import inspect
from functools import wraps
_asyncio_event_loop = None
def get_asyncio_loop():
"""asyncio has deprecated get_event_loop
Replicate it here, with our desired semantics:
- always returns a valid, not-closed loop
- not thread-local like asyncio's,
because we only want one loop for IPython
- if called from inside a coroutine (e.g. in ipykernel),
return the running loop
.. versionadded:: 8.0
"""
try:
return asyncio.get_running_loop()
except RuntimeError:
# not inside a coroutine,
# track our own global
pass
# not thread-local like asyncio's,
# because we only track one event loop to run for IPython itself,
# always in the main thread.
global _asyncio_event_loop
if _asyncio_event_loop is None or _asyncio_event_loop.is_closed():
_asyncio_event_loop = asyncio.new_event_loop()
return _asyncio_event_loop
class _AsyncIORunner:
def __call__(self, coro):
"""
Handler for asyncio autoawait
"""
return get_asyncio_loop().run_until_complete(coro)
def __str__(self):
return "asyncio"
_asyncio_runner = _AsyncIORunner()
class _AsyncIOProxy:
"""Proxy-object for an asyncio
Any coroutine methods will be wrapped in event_loop.run_
"""
def __init__(self, obj, event_loop):
self._obj = obj
self._event_loop = event_loop
def __repr__(self):
return f"<_AsyncIOProxy({self._obj!r})>"
def __getattr__(self, key):
attr = getattr(self._obj, key)
if inspect.iscoroutinefunction(attr):
# if it's a coroutine method,
# return a threadsafe wrapper onto the _current_ asyncio loop
@wraps(attr)
def _wrapped(*args, **kwargs):
concurrent_future = asyncio.run_coroutine_threadsafe(
attr(*args, **kwargs), self._event_loop
)
return asyncio.wrap_future(concurrent_future)
return _wrapped
else:
return attr
def __dir__(self):
return dir(self._obj)
def _curio_runner(coroutine):
"""
handler for curio autoawait
"""
import curio
return curio.run(coroutine)
def _trio_runner(async_fn):
import trio
async def loc(coro):
"""
We need the dummy no-op async def to protect from
trio's internal. See https://github.com/python-trio/trio/issues/89
"""
return await coro
return trio.run(loc, async_fn)
def _pseudo_sync_runner(coro):
"""
A runner that does not really allow async execution, and just advance the coroutine.
See discussion in https://github.com/python-trio/trio/issues/608,
Credit to Nathaniel Smith
"""
try:
coro.send(None)
except StopIteration as exc:
return exc.value
else:
# TODO: do not raise but return an execution result with the right info.
raise RuntimeError(
"{coro_name!r} needs a real async loop".format(coro_name=coro.__name__)
)
def _should_be_async(cell: str) -> bool:
"""Detect if a block of code need to be wrapped in an `async def`
Attempt to parse the block of code, it it compile we're fine.
Otherwise we wrap if and try to compile.
If it works, assume it should be async. Otherwise Return False.
Not handled yet: If the block of code has a return statement as the top
level, it will be seen as async. This is a know limitation.
"""
try:
code = compile(
cell, "<>", "exec", flags=getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0)
)
return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE
except (SyntaxError, MemoryError):
return False

View File

@@ -0,0 +1,70 @@
# encoding: utf-8
"""
Autocall capabilities for IPython.core.
Authors:
* Brian Granger
* Fernando Perez
* Thomas Kluyver
Notes
-----
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
class IPyAutocall(object):
""" Instances of this class are always autocalled
This happens regardless of 'autocall' variable state. Use this to
develop macro-like mechanisms.
"""
_ip = None
rewrite = True
def __init__(self, ip=None):
self._ip = ip
def set_ip(self, ip):
"""Will be used to set _ip point to current ipython instance b/f call
Override this method if you don't want this to happen.
"""
self._ip = ip
class ExitAutocall(IPyAutocall):
"""An autocallable object which will be added to the user namespace so that
exit, exit(), quit or quit() are all valid ways to close the shell."""
rewrite = False
def __call__(self):
self._ip.ask_exit()
class ZMQExitAutocall(ExitAutocall):
"""Exit IPython. Autocallable, so it needn't be explicitly called.
Parameters
----------
keep_kernel : bool
If True, leave the kernel alive. Otherwise, tell the kernel to exit too
(default).
"""
def __call__(self, keep_kernel=False):
self._ip.keepkernel_on_exit = keep_kernel
self._ip.ask_exit()

View File

@@ -0,0 +1,86 @@
"""
A context manager for managing things injected into :mod:`builtins`.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import builtins as builtin_mod
from traitlets.config.configurable import Configurable
from traitlets import Instance
class __BuiltinUndefined(object): pass
BuiltinUndefined = __BuiltinUndefined()
class __HideBuiltin(object): pass
HideBuiltin = __HideBuiltin()
class BuiltinTrap(Configurable):
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
allow_none=True)
def __init__(self, shell=None):
super(BuiltinTrap, self).__init__(shell=shell, config=None)
self._orig_builtins = {}
# We define this to track if a single BuiltinTrap is nested.
# Only turn off the trap when the outermost call to __exit__ is made.
self._nested_level = 0
self.shell = shell
# builtins we always add - if set to HideBuiltin, they will just
# be removed instead of being replaced by something else
self.auto_builtins = {'exit': HideBuiltin,
'quit': HideBuiltin,
'get_ipython': self.shell.get_ipython,
}
def __enter__(self):
if self._nested_level == 0:
self.activate()
self._nested_level += 1
# I return self, so callers can use add_builtin in a with clause.
return self
def __exit__(self, type, value, traceback):
if self._nested_level == 1:
self.deactivate()
self._nested_level -= 1
# Returning False will cause exceptions to propagate
return False
def add_builtin(self, key, value):
"""Add a builtin and save the original."""
bdict = builtin_mod.__dict__
orig = bdict.get(key, BuiltinUndefined)
if value is HideBuiltin:
if orig is not BuiltinUndefined: #same as 'key in bdict'
self._orig_builtins[key] = orig
del bdict[key]
else:
self._orig_builtins[key] = orig
bdict[key] = value
def remove_builtin(self, key, orig):
"""Remove an added builtin and re-set the original."""
if orig is BuiltinUndefined:
del builtin_mod.__dict__[key]
else:
builtin_mod.__dict__[key] = orig
def activate(self):
"""Store ipython references in the __builtin__ namespace."""
add_builtin = self.add_builtin
for name, func in self.auto_builtins.items():
add_builtin(name, func)
def deactivate(self):
"""Remove any builtins which might have been added by add_builtins, or
restore overwritten ones to their previous values."""
remove_builtin = self.remove_builtin
for key, val in self._orig_builtins.items():
remove_builtin(key, val)
self._orig_builtins.clear()
self._builtins_added = False

View File

@@ -0,0 +1,214 @@
"""Compiler tools with improved interactive support.
Provides compilation machinery similar to codeop, but with caching support so
we can provide interactive tracebacks.
Authors
-------
* Robert Kern
* Fernando Perez
* Thomas Kluyver
"""
# Note: though it might be more natural to name this module 'compiler', that
# name is in the stdlib and name collisions with the stdlib tend to produce
# weird problems (often with third-party tools).
#-----------------------------------------------------------------------------
# Copyright (C) 2010-2011 The IPython Development Team.
#
# Distributed under the terms of the BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Stdlib imports
import __future__
from ast import PyCF_ONLY_AST
import codeop
import functools
import hashlib
import linecache
import operator
import time
from contextlib import contextmanager
#-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------
# Roughly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h,
# this is used as a bitmask to extract future-related code flags.
PyCF_MASK = functools.reduce(operator.or_,
(getattr(__future__, fname).compiler_flag
for fname in __future__.all_feature_names))
#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------
def code_name(code, number=0):
""" Compute a (probably) unique name for code for caching.
This now expects code to be unicode.
"""
hash_digest = hashlib.sha1(code.encode("utf-8")).hexdigest()
# Include the number and 12 characters of the hash in the name. It's
# pretty much impossible that in a single session we'll have collisions
# even with truncated hashes, and the full one makes tracebacks too long
return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12])
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class CachingCompiler(codeop.Compile):
"""A compiler that caches code compiled from interactive statements.
"""
def __init__(self):
codeop.Compile.__init__(self)
# Caching a dictionary { filename: execution_count } for nicely
# rendered tracebacks. The filename corresponds to the filename
# argument used for the builtins.compile function.
self._filename_map = {}
def ast_parse(self, source, filename='<unknown>', symbol='exec'):
"""Parse code to an AST with the current compiler flags active.
Arguments are exactly the same as ast.parse (in the standard library),
and are passed to the built-in compile function."""
return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
def reset_compiler_flags(self):
"""Reset compiler flags to default state."""
# This value is copied from codeop.Compile.__init__, so if that ever
# changes, it will need to be updated.
self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
@property
def compiler_flags(self):
"""Flags currently active in the compilation process.
"""
return self.flags
def get_code_name(self, raw_code, transformed_code, number):
"""Compute filename given the code, and the cell number.
Parameters
----------
raw_code : str
The raw cell code.
transformed_code : str
The executable Python source code to cache and compile.
number : int
A number which forms part of the code's name. Used for the execution
counter.
Returns
-------
The computed filename.
"""
return code_name(transformed_code, number)
def format_code_name(self, name):
"""Return a user-friendly label and name for a code block.
Parameters
----------
name : str
The name for the code block returned from get_code_name
Returns
-------
A (label, name) pair that can be used in tracebacks, or None if the default formatting should be used.
"""
if name in self._filename_map:
return "Cell", "In[%s]" % self._filename_map[name]
def cache(self, transformed_code, number=0, raw_code=None):
"""Make a name for a block of code, and cache the code.
Parameters
----------
transformed_code : str
The executable Python source code to cache and compile.
number : int
A number which forms part of the code's name. Used for the execution
counter.
raw_code : str
The raw code before transformation, if None, set to `transformed_code`.
Returns
-------
The name of the cached code (as a string). Pass this as the filename
argument to compilation, so that tracebacks are correctly hooked up.
"""
if raw_code is None:
raw_code = transformed_code
name = self.get_code_name(raw_code, transformed_code, number)
# Save the execution count
self._filename_map[name] = number
# Since Python 2.5, setting mtime to `None` means the lines will
# never be removed by `linecache.checkcache`. This means all the
# monkeypatching has *never* been necessary, since this code was
# only added in 2010, at which point IPython had already stopped
# supporting Python 2.4.
#
# Note that `linecache.clearcache` and `linecache.updatecache` may
# still remove our code from the cache, but those show explicit
# intent, and we should not try to interfere. Normally the former
# is never called except when out of memory, and the latter is only
# called for lines *not* in the cache.
entry = (
len(transformed_code),
None,
[line + "\n" for line in transformed_code.splitlines()],
name,
)
linecache.cache[name] = entry
return name
@contextmanager
def extra_flags(self, flags):
## bits that we'll set to 1
turn_on_bits = ~self.flags & flags
self.flags = self.flags | flags
try:
yield
finally:
# turn off only the bits we turned on so that something like
# __future__ that set flags stays.
self.flags &= ~turn_on_bits
def check_linecache_ipython(*args):
"""Deprecated since IPython 8.6. Call linecache.checkcache() directly.
It was already not necessary to call this function directly. If no
CachingCompiler had been created, this function would fail badly. If
an instance had been created, this function would've been monkeypatched
into place.
As of IPython 8.6, the monkeypatching has gone away entirely. But there
were still internal callers of this function, so maybe external callers
also existed?
"""
import warnings
warnings.warn(
"Deprecated Since IPython 8.6, Just call linecache.checkcache() directly.",
DeprecationWarning,
stacklevel=2,
)
linecache.checkcache()

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,377 @@
# encoding: utf-8
"""Implementations for various useful completers.
These are all loaded by default by IPython.
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2010-2011 The IPython Development Team.
#
# Distributed under the terms of the BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Stdlib imports
import glob
import inspect
import os
import re
import sys
from importlib import import_module
from importlib.machinery import all_suffixes
# Third-party imports
from time import time
from zipimport import zipimporter
# Our own imports
from .completer import expand_user, compress_user
from .error import TryNext
from ..utils._process_common import arg_split
# FIXME: this should be pulled in with the right call via the component system
from IPython import get_ipython
from typing import List
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
_suffixes = all_suffixes()
# Time in seconds after which the rootmodules will be stored permanently in the
# ipython ip.db database (kept in the user's .ipython dir).
TIMEOUT_STORAGE = 2
# Time in seconds after which we give up
TIMEOUT_GIVEUP = 20
# Regular expression for the python import statement
import_re = re.compile(r'(?P<name>[^\W\d]\w*?)'
r'(?P<package>[/\\]__init__)?'
r'(?P<suffix>%s)$' %
r'|'.join(re.escape(s) for s in _suffixes))
# RE for the ipython %run command (python + ipython scripts)
magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$')
#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------
def module_list(path: str) -> List[str]:
"""
Return the list containing the names of the modules available in the given
folder.
"""
# sys.path has the cwd as an empty string, but isdir/listdir need it as '.'
if path == '':
path = '.'
# A few local constants to be used in loops below
pjoin = os.path.join
if os.path.isdir(path):
# Build a list of all files in the directory and all files
# in its subdirectories. For performance reasons, do not
# recurse more than one level into subdirectories.
files: List[str] = []
for root, dirs, nondirs in os.walk(path, followlinks=True):
subdir = root[len(path)+1:]
if subdir:
files.extend(pjoin(subdir, f) for f in nondirs)
dirs[:] = [] # Do not recurse into additional subdirectories.
else:
files.extend(nondirs)
else:
try:
files = list(zipimporter(path)._files.keys()) # type: ignore
except Exception:
files = []
# Build a list of modules which match the import_re regex.
modules = []
for f in files:
m = import_re.match(f)
if m:
modules.append(m.group('name'))
return list(set(modules))
def get_root_modules():
"""
Returns a list containing the names of all the modules available in the
folders of the pythonpath.
ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
"""
ip = get_ipython()
if ip is None:
# No global shell instance to store cached list of modules.
# Don't try to scan for modules every time.
return list(sys.builtin_module_names)
if getattr(ip.db, "_mock", False):
rootmodules_cache = {}
else:
rootmodules_cache = ip.db.get("rootmodules_cache", {})
rootmodules = list(sys.builtin_module_names)
start_time = time()
store = False
for path in sys.path:
try:
modules = rootmodules_cache[path]
except KeyError:
modules = module_list(path)
try:
modules.remove('__init__')
except ValueError:
pass
if path not in ('', '.'): # cwd modules should not be cached
rootmodules_cache[path] = modules
if time() - start_time > TIMEOUT_STORAGE and not store:
store = True
print("\nCaching the list of root modules, please wait!")
print("(This will only be done once - type '%rehashx' to "
"reset cache!)\n")
sys.stdout.flush()
if time() - start_time > TIMEOUT_GIVEUP:
print("This is taking too long, we give up.\n")
return []
rootmodules.extend(modules)
if store:
ip.db['rootmodules_cache'] = rootmodules_cache
rootmodules = list(set(rootmodules))
return rootmodules
def is_importable(module, attr, only_modules):
if only_modules:
return inspect.ismodule(getattr(module, attr))
else:
return not(attr[:2] == '__' and attr[-2:] == '__')
def is_possible_submodule(module, attr):
try:
obj = getattr(module, attr)
except AttributeError:
# Is possilby an unimported submodule
return True
except TypeError:
# https://github.com/ipython/ipython/issues/9678
return False
return inspect.ismodule(obj)
def try_import(mod: str, only_modules=False) -> List[str]:
"""
Try to import given module and return list of potential completions.
"""
mod = mod.rstrip('.')
try:
m = import_module(mod)
except:
return []
m_is_init = '__init__' in (getattr(m, '__file__', '') or '')
completions = []
if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
completions.extend( [attr for attr in dir(m) if
is_importable(m, attr, only_modules)])
m_all = getattr(m, "__all__", [])
if only_modules:
completions.extend(attr for attr in m_all if is_possible_submodule(m, attr))
else:
completions.extend(m_all)
if m_is_init:
file_ = m.__file__
file_path = os.path.dirname(file_) # type: ignore
if file_path is not None:
completions.extend(module_list(file_path))
completions_set = {c for c in completions if isinstance(c, str)}
completions_set.discard('__init__')
return list(completions_set)
#-----------------------------------------------------------------------------
# Completion-related functions.
#-----------------------------------------------------------------------------
def quick_completer(cmd, completions):
r""" Easily create a trivial completer for a command.
Takes either a list of completions, or all completions in string (that will
be split on whitespace).
Example::
[d:\ipython]|1> import ipy_completers
[d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
[d:\ipython]|3> foo b<TAB>
bar baz
[d:\ipython]|3> foo ba
"""
if isinstance(completions, str):
completions = completions.split()
def do_complete(self, event):
return completions
get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
def module_completion(line):
"""
Returns a list containing the completion possibilities for an import line.
The line looks like this :
'import xml.d'
'from xml.dom import'
"""
words = line.split(' ')
nwords = len(words)
# from whatever <tab> -> 'import '
if nwords == 3 and words[0] == 'from':
return ['import ']
# 'from xy<tab>' or 'import xy<tab>'
if nwords < 3 and (words[0] in {'%aimport', 'import', 'from'}) :
if nwords == 1:
return get_root_modules()
mod = words[1].split('.')
if len(mod) < 2:
return get_root_modules()
completion_list = try_import('.'.join(mod[:-1]), True)
return ['.'.join(mod[:-1] + [el]) for el in completion_list]
# 'from xyz import abc<tab>'
if nwords >= 3 and words[0] == 'from':
mod = words[1]
return try_import(mod)
#-----------------------------------------------------------------------------
# Completers
#-----------------------------------------------------------------------------
# These all have the func(self, event) signature to be used as custom
# completers
def module_completer(self,event):
"""Give completions after user has typed 'import ...' or 'from ...'"""
# This works in all versions of python. While 2.5 has
# pkgutil.walk_packages(), that particular routine is fairly dangerous,
# since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
# of possibly problematic side effects.
# This search the folders in the sys.path for available modules.
return module_completion(event.line)
# FIXME: there's a lot of logic common to the run, cd and builtin file
# completers, that is currently reimplemented in each.
def magic_run_completer(self, event):
"""Complete files that end in .py or .ipy or .ipynb for the %run command.
"""
comps = arg_split(event.line, strict=False)
# relpath should be the current token that we need to complete.
if (len(comps) > 1) and (not event.line.endswith(' ')):
relpath = comps[-1].strip("'\"")
else:
relpath = ''
#print("\nev=", event) # dbg
#print("rp=", relpath) # dbg
#print('comps=', comps) # dbg
lglob = glob.glob
isdir = os.path.isdir
relpath, tilde_expand, tilde_val = expand_user(relpath)
# Find if the user has already typed the first filename, after which we
# should complete on all files, since after the first one other files may
# be arguments to the input script.
if any(magic_run_re.match(c) for c in comps):
matches = [f.replace('\\','/') + ('/' if isdir(f) else '')
for f in lglob(relpath+'*')]
else:
dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
pys = [f.replace('\\','/')
for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')]
matches = dirs + pys
#print('run comp:', dirs+pys) # dbg
return [compress_user(p, tilde_expand, tilde_val) for p in matches]
def cd_completer(self, event):
"""Completer function for cd, which only returns directories."""
ip = get_ipython()
relpath = event.symbol
#print(event) # dbg
if event.line.endswith('-b') or ' -b ' in event.line:
# return only bookmark completions
bkms = self.db.get('bookmarks', None)
if bkms:
return bkms.keys()
else:
return []
if event.symbol == '-':
width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
# jump in directory history by number
fmt = '-%0' + width_dh +'d [%s]'
ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
if len(ents) > 1:
return ents
return []
if event.symbol.startswith('--'):
return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
# Expand ~ in path and normalize directory separators.
relpath, tilde_expand, tilde_val = expand_user(relpath)
relpath = relpath.replace('\\','/')
found = []
for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
if os.path.isdir(f)]:
if ' ' in d:
# we don't want to deal with any of that, complex code
# for this is elsewhere
raise TryNext
found.append(d)
if not found:
if os.path.isdir(relpath):
return [compress_user(relpath, tilde_expand, tilde_val)]
# if no completions so far, try bookmarks
bks = self.db.get('bookmarks',{})
bkmatches = [s for s in bks if s.startswith(event.symbol)]
if bkmatches:
return bkmatches
raise TryNext
return [compress_user(p, tilde_expand, tilde_val) for p in found]
def reset_completer(self, event):
"A completer for %reset magic"
return '-f -s in out array dhist'.split()

Some files were not shown because too many files have changed in this diff Show More