Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61b87cb

Browse files
committed
ch06
1 parent e282e64 commit 61b87cb

File tree

16 files changed

+627
-5
lines changed

16 files changed

+627
-5
lines changed

‎README.md‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# 《Python 源码剖析》学习笔记
22

3-
> 我看的《Python 源码剖析》是 2008 年出版的,Python 版本是 2.5。
3+
> 《Python 源码剖析》
4+
> 作者:陈儒 Robert Chen
5+
> 出版年份:2008 年
6+
> Python 版本:2.5
47
58
在阅读《Python 源码剖析》的过程中记录的一些笔记,不是特别详细,简单记录了一些关键的地方,方便以后查看。
69

@@ -16,11 +19,36 @@ master 分支是原始代码。
1619

1720
每个 chxx 分支对应书中相应的章节,基于 master 分支修改而来。
1821

22+
## 其它资源
23+
24+
- [作者在 CSDN 的博客](https://blog.csdn.net/balabalamerobert)(不再更新)。
25+
26+
- [Extending and Embedding the Python Interpreter](https://docs.python.org/2.7/extending/index.html)
27+
28+
扩展和嵌入 Python 解析器,介绍了如何用 C/C++ 编写 Python 的扩展模块,如何在其它语言中嵌入 Python 解释器。
29+
30+
- [C API](https://docs.python.org/2.7/c-api/index.html)
31+
32+
详细介绍了 Python 内部的 C API。
33+
34+
- [Python Developer’s Guide](https://devguide.python.org/)
35+
36+
Python 开发者指南。
37+
38+
1939

2040
## 目录
2141

22-
- [ch01 - Pyhton 对象初探](ch01.md)
23-
- [ch02 - Pyhton 中的整数对象](ch02.md)
24-
- [ch03 - Pyhton 中的字符串对象](ch03.md)
25-
- [ch04 - Python 中的 List 对象](ch04.md)
42+
- 第一部分
43+
44+
- [ch01 - Pyhton 对象初探](ch01.md)
45+
- [ch02 - Pyhton 中的整数对象](ch02.md)
46+
- [ch03 - Pyhton 中的字符串对象](ch03.md)
47+
- [ch04 - Python 中的 List 对象](ch04.md)
48+
- [ch05 - Python 中的 Dict 对象](ch05.md)
49+
- [ch06 - 最简单的Python模拟——Small Python](ch06.md)
50+
51+
- 第二部分
52+
53+
- [ch07 - Python的编译结果——Code对象与pyc文件](ch07.md)
2654

‎assets/small_python/Makefile‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#####################################################
2+
#common settings
3+
#####################################################
4+
CC=g++
5+
LINK=g++
6+
INC_PREFIX=-I
7+
LIB_PREFIX=-L
8+
BUILD_PATH=./build
9+
CFLAGS=-c
10+
TARGET=small_python
11+
TEST_TARGET=test_$(TARGET)
12+
CUNIT_PATH=/home/robert/CUnit
13+
14+
#####################################################
15+
#settings for normal compile
16+
#####################################################
17+
SRC_PATH=./src
18+
SRCS=$(wildcard $(SRC_PATH)/*.cpp)
19+
OBJS=$(patsubst $(SRC_PATH)/%.cpp,$(BUILD_PATH)/%.o,$(SRCS))
20+
21+
INC_DIR=./include $(CUNIT_PATH)/include/CUnit
22+
INCS=$(addprefix $(INC_PREFIX),$(INC_DIR))
23+
24+
#####################################################
25+
#settings for compiling test cases
26+
#####################################################
27+
TEST_PATH=./test
28+
TEST_SRCS=$(wildcard $(TEST_PATH)/*.cpp)
29+
TEST_OBJS=$(patsubst $(TEST_PATH)/%.cpp,$(BUILD_PATH)/%.o,$(TEST_SRCS))
30+
31+
TEST_INC_DIR=$(INC_DIR) $(TEST_PATH) $(CUNIT_PATH)/include/CUnit
32+
TEST_LIB_DIR=$(CUNIT_PATH)/lib
33+
TEST_INCS=$(addprefix $(INC_PREFIX),$(TEST_INC_DIR))
34+
TEST_LIB_PATHS=$(addprefix $(LIB_PREFIX),$(TEST_LIB_DIR))
35+
TEST_LIBS=-lcunit
36+
37+
#####################################################
38+
# set path or make
39+
#####################################################
40+
vpath %.cpp $(SRC_PATH)
41+
vpath %.o $(BUILD_PATH)
42+
vpath %.cpp $(TEST_PATH)
43+
44+
#####################################################
45+
# normal compile
46+
#####################################################
47+
all : init $(TARGET)
48+
49+
$(TARGET) : $(OBJS)
50+
@echo "generate excutable file : $@"
51+
$(LINK) -o $@ $(OBJS)
52+
53+
$(OBJS) : $(BUILD_PATH)/%.o : $(SRC_PATH)/%.cpp
54+
$(CC) $(CFLAGS) $(INCS) -o $@ $<
55+
56+
init:
57+
@echo "create $(BUILD_PATH) dir"
58+
mkdir $(BUILD_PATH)
59+
60+
61+
#####################################################
62+
# compile for test
63+
#####################################################
64+
test: clean init compile_test run_test
65+
66+
compile_test: $(OBJS) $(TEST_OBJS)
67+
@echo "generate excutable file for testing : $(TEST_TARGET)"
68+
$(LINK) $(TEST_LIBS) $(TEST_LIB_PATHS) -o $(TEST_TARGET) $(TEST_OBJS) $(filter-out %main.o,$(OBJS))
69+
70+
$(TEST_OBJS) : $(BUILD_PATH)/%.o : $(TEST_PATH)/%.cpp
71+
$(CC) $(CFLAGS) $(TEST_INCS) -o $@ $<
72+
73+
run_test:
74+
./test_main
75+
76+
#####################################################
77+
# clean
78+
#####################################################
79+
clean:
80+
rm -rf $(BUILD_PATH)
81+
rm -f $(TEST_TARGET)
82+
rm -f $(TARGET)
83+
84+
#####################################################
85+
# robert's test
86+
#####################################################
87+
show:
88+
@echo "TEST_SRCS : $(TEST_SRCS)"
89+
@echo "TEST_INCS : $(TEST_INCS)"
90+
@echo "TEST_LIBS : $(TEST_LIBS)"
91+
@echo "TEST_OBJS : $(TEST_OBJS)"
92+
@echo "TEST_TARGET : $(TEST_TARGET)"

‎assets/small_python/include/engine.h‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#ifndef __PYTHONREADING_ENGINE_H_
2+
#define __PYTHONREADING_ENGINE_H_
3+
4+
#include "object.h"
5+
#include "intObject.h"
6+
#include "strObject.h"
7+
8+
#include <iostream>
9+
#include <string>
10+
#include <map>
11+
12+
char* info = "********** Python Research **********\nInput 'exit' to exit\n";
13+
char* prompt = ">>> ";
14+
15+
class ExcuteEngine
16+
{
17+
public:
18+
void Excute()
19+
{
20+
cout << info;
21+
cout << prompt;
22+
while(getline(cin, m_Command))
23+
{
24+
if(m_Command.size() == 0)
25+
{
26+
cout << prompt;
27+
continue;
28+
}
29+
else if(m_Command == "exit")
30+
{
31+
return;
32+
}
33+
else
34+
{
35+
ExcuteCommand(m_Command);
36+
}
37+
cout << prompt;
38+
}
39+
}
40+
41+
private:
42+
void ExcuteCommand(string& command)
43+
{
44+
string::size_type pos = 0;
45+
if((pos = command.find("print ")) != string::npos)
46+
{
47+
ExcutePrint(command.substr(6));
48+
}
49+
else if((pos = command.find(" = ")) != string::npos)
50+
{
51+
string target = command.substr(0, pos);
52+
string source = command.substr(pos+3);
53+
ExcuteAdd(target, source);
54+
}
55+
else {
56+
cout << "[Error] : Can not recognize : " << command << endl;
57+
}
58+
}
59+
60+
void ExcutePrint(string symbol)
61+
{
62+
PyObject* object = GetObjectFromSymbol(symbol);
63+
if(object != NULL)
64+
{
65+
PyTypeObject* type = object->type;
66+
type->print(object);
67+
}
68+
}
69+
70+
void ExcuteAdd(string& target, string& source)
71+
{
72+
string::size_type pos;
73+
if(IsSourceAllDigit(source))
74+
{
75+
PyObject* intObject = CreatePyIntObject(atoi(source.c_str()));
76+
if(m_Symbol2Object.find(target) == m_Symbol2Object.end()) {
77+
m_Symbol2Object.insert(map<string, PyObject*>::value_type(target, intObject));
78+
}
79+
else {
80+
m_Symbol2Object[target] = intObject;
81+
}
82+
}
83+
else if(source.find("\"") != string::npos || source.find("'") != string::npos)
84+
{
85+
PyObject* strObject = CreatePyStrObject(source.substr(1, source.size()-2).c_str());
86+
if(m_Symbol2Object.find(target) == m_Symbol2Object.end()) {
87+
m_Symbol2Object.insert(map<string, PyObject*>::value_type(target, strObject));
88+
}
89+
else {
90+
m_Symbol2Object[target] = strObject;
91+
}
92+
}
93+
else if((pos = source.find("+")) != string::npos)
94+
{
95+
PyObject* leftObject = GetObjectFromSymbol(Trim(source.substr(0, pos)));
96+
PyObject* rightObject = GetObjectFromSymbol(Trim(source.substr(pos+1)));
97+
if(leftObject != NULL && right != NULL && leftObject->type == rightObject->type)
98+
{
99+
PyObject* result = (leftObject->type)->add(leftObject, rightObject);
100+
m_Symbol2Object.insert(map<string, PyObject*>::value_type(target, result));
101+
}
102+
}
103+
}
104+
105+
bool IsSourceAllDigit(string& source)
106+
{
107+
string::size_type len = source.size();
108+
for(string::size_type i = 0; i < len; ++i)
109+
{
110+
if(!isdigit(source[i]))
111+
{
112+
return false;
113+
}
114+
}
115+
return true;
116+
}
117+
118+
PyObject* GetObjectFromSymbol(string symbol)
119+
{
120+
map<string, PyObject*>::iterator it = m_Symbol2Object.find(symbol);
121+
if(it == m_Symbol2Object.end())
122+
{
123+
cout << "[Error] : " << symbol << " is not defined!!" << endl;
124+
return NULL;
125+
}
126+
return it->second;
127+
}
128+
129+
string Trim(string symbol) {
130+
int length = symbol.length();
131+
132+
int start = 0;
133+
while(symbol[start] == ' ') {
134+
++start;
135+
}
136+
137+
int end = length-1;
138+
while(symbol[end] == ' ') {
139+
--end;
140+
}
141+
return symbol.substr(start, end+1);
142+
}
143+
private:
144+
string m_Command;
145+
map<string, PyObject*> m_Symbol2Object;
146+
};
147+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef __PYTHONREADING_INTOBJECT_H_
2+
#define __PYTHONREADING_INTOBJECT_H_
3+
4+
#include "object.h"
5+
6+
typedef struct tagPyIntObject
7+
{
8+
PyObject_HEAD;
9+
int value;
10+
}PyIntObject;
11+
12+
extern PyTypeObject PyInt_Type;
13+
14+
PyObject* CreatePyIntObject(int value);
15+
#endif

‎assets/small_python/include/object.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __PYTHONREADING_OBJECT_H_
2+
#define __PYTHONREADING_OBJECT_H_
3+
4+
//definition of PyObject
5+
#define PyObject_HEAD \
6+
int refCount;\
7+
struct tagPyTypeObject *type
8+
9+
#define PyObject_HEAD_INIT(typePtr)\
10+
0, typePtr
11+
12+
typedef struct tagPyObject
13+
{
14+
PyObject_HEAD;
15+
}PyObject;
16+
17+
//definition of PyTypeObject
18+
typedef void (*PrintFun)(PyObject* object);
19+
typedef PyObject* (*AddFun)(PyObject* left, PyObject* right);
20+
21+
typedef struct tagPyTypeObject
22+
{
23+
PyObject_HEAD;
24+
char* name;
25+
PrintFun print;
26+
AddFun add;
27+
}PyTypeObject;
28+
29+
extern PyTypeObject PyType_Type;
30+
31+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef __PYTHONREADING_STROBJECT_H_
2+
#define __PYTHONREADING_STROBJECT_H_
3+
4+
#include "object.h"
5+
6+
typedef struct tagPyStrObject
7+
{
8+
PyObject_HEAD;
9+
char value[50];
10+
}PyStrObject;
11+
12+
extern PyTypeObject PyStr_Type;
13+
14+
PyObject* CreatePyStrObject(const char* value);
15+
#endif

‎assets/small_python/samll_python.ncb‎

27 KB
Binary file not shown.

‎assets/small_python/samll_python.sln‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Microsoft Visual Studio Solution File, Format Version 8.00
2+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "samll_python", "samll_python.vcproj", "{CE93E66B-1A83-4AEB-850A-7E07AF661E62}"
3+
ProjectSection(ProjectDependencies) = postProject
4+
EndProjectSection
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfiguration) = preSolution
8+
Debug = Debug
9+
Release = Release
10+
EndGlobalSection
11+
GlobalSection(ProjectConfiguration) = postSolution
12+
{CE93E66B-1A83-4AEB-850A-7E07AF661E62}.Debug.ActiveCfg = Debug|Win32
13+
{CE93E66B-1A83-4AEB-850A-7E07AF661E62}.Debug.Build.0 = Debug|Win32
14+
{CE93E66B-1A83-4AEB-850A-7E07AF661E62}.Release.ActiveCfg = Release|Win32
15+
{CE93E66B-1A83-4AEB-850A-7E07AF661E62}.Release.Build.0 = Release|Win32
16+
EndGlobalSection
17+
GlobalSection(ExtensibilityGlobals) = postSolution
18+
EndGlobalSection
19+
GlobalSection(ExtensibilityAddIns) = postSolution
20+
EndGlobalSection
21+
EndGlobal

‎assets/small_python/samll_python.suo‎

8 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /