banner.gif adie's blog
主页 博客 胭脂泪,相留醉,几时重,自是人生长恨水长东
统计
日志总数: 127
评论总数: 123
日志分类
日志归档
最近日志
最近评论
订阅
rss2.gif

atom.gif

google_rss
yc.gif 【技术资料】 阅读 9302 次

定制 Python 嵌入 C++: (四) 定制 Python 内建模块

2011-07-19 14:15:32

Python 中除了独立编译成 dll 的模块外, 在解释器的内部还有内置的, 编译到解释器中的模块.  默认的 python 解释器的 Release 版 python27.dll 编译后 2M 多, 对于某些应用来说, 还是显得过大了. 要精简 python 解释器, 首先就可以去掉解释器内部那些不用的模块.

 

Python 内建模块的配置在 config.c 文件中. 工程位置为 pythoncore/PC/config.c , 文件位置为 Python2.7.2/PC/config.c

该文件中默认的模块配置如下:

  1.     {"array", initarray},  
  2.     {"_ast", init_ast},  
  3. #ifdef MS_WINDOWS  
  4. #ifndef MS_WINI64  
  5.     {"audioop", initaudioop},  
  6. #endif  
  7. #endif  
  8.     {"binascii", initbinascii},  
  9.     {"cmath", initcmath},  
  10.     {"errno", initerrno},  
  11.     {"future_builtins", initfuture_builtins},  
  12.     {"gc", initgc},  
  13. #ifndef MS_WINI64  
  14.     {"imageop", initimageop},  
  15. #endif  
  16.     {"math", initmath},  
  17.     {"_md5", init_md5},  
  18.     {"nt", initnt}, /* Use the NT os functions, not posix */  
  19.     {"operator", initoperator},  
  20.     {"signal", initsignal},  
  21.     {"_sha", init_sha},  
  22.     {"_sha256", init_sha256},  
  23.     {"_sha512", init_sha512},  
  24.     {"strop", initstrop},  
  25.     {"time", inittime},  
  26. #ifdef WITH_THREAD  
  27.     {"thread", initthread},  
  28. #endif  
  29.     {"cStringIO", initcStringIO},  
  30.     {"cPickle", initcPickle},  
  31. #ifdef WIN32  
  32.     {"msvcrt", initmsvcrt},  
  33.     {"_locale", init_locale},  
  34. #endif  
  35.     /* XXX Should _subprocess go in a WIN32 block?  not WIN64? */  
  36.     {"_subprocess", init_subprocess},  
  37.   
  38.     {"_codecs", init_codecs},  
  39.     {"_weakref", init_weakref},  
  40.     {"_hotshot", init_hotshot},  
  41.     {"_random", init_random},  
  42.     {"_bisect", init_bisect},  
  43.     {"_heapq", init_heapq},  
  44.     {"_lsprof", init_lsprof},  
  45.     {"itertools", inititertools},  
  46.     {"_collections", init_collections},  
  47.     {"_symtable", init_symtable},  
  48.     {"mmap", initmmap},  
  49.     {"_csv", init_csv},  
  50.     {"_sre", init_sre},  
  51.     {"parser", initparser},  
  52.     {"_winreg", init_winreg},  
  53.     {"_struct", init_struct},  
  54.     {"datetime", initdatetime},  
  55.     {"_functools", init_functools},  
  56.     {"_json", init_json},  
  57.   
  58.     {"xxsubtype", initxxsubtype},  
  59.     {"zipimport", initzipimport},  
  60.     {"zlib", initzlib},  
  61.   
  62.     /* CJK codecs */  
  63.     {"_multibytecodec", init_multibytecodec},  
  64.     {"_codecs_cn", init_codecs_cn},  
  65.     {"_codecs_hk", init_codecs_hk},  
  66.     {"_codecs_iso2022", init_codecs_iso2022},  
  67.     {"_codecs_jp", init_codecs_jp},  
  68.     {"_codecs_kr", init_codecs_kr},  
  69.     {"_codecs_tw", init_codecs_tw},  
  70.   
  71. /* tools/freeze/makeconfig.py marker for additional "_inittab" entries */  
  72. /* -- ADDMODULE MARKER 2 -- */  
  73.   
  74.     /* This module "lives in" with marshal.c */  
  75.     {"marshal", PyMarshal_Init},  
  76.   
  77.     /* This lives it with import.c */  
  78.     {"imp", initimp},  
  79.   
  80.     /* These entries are here for sys.builtin_module_names */  
  81.     {"__main__", NULL},  
  82.     {"__builtin__", NULL},  
  83.     {"sys", NULL},  
  84.     {"exceptions", NULL},  
  85.     {"_warnings", _PyWarnings_Init},  
  86.   
  87.     {"_io", init_io},  

使用中发现, 单是去掉这个文件中的模块配置还不能减小生成的解释器大小, 必须把对应模块的实现文件从工程中移除才可以. 下面我们来具体看看这些模块, 并找到实现他们的代码:

  1. array (Modules/arraymodule.c) (http://docs.python.org/library/array.html) 一个可以存放基本类型的高效数组, 提供了和序列类似的操作. 使用放法类似于 a = array.array('b', [10, 20, 30]), 不常使用, 可以考虑去除. 
  2. _ast (Python/Python-ast.c) (http://docs.python.org/library/ast.html) 抽象语法树, 供 Python 程序解析处理 Python 语法相关的库, 这个模块的源代码是由脚本自动生成的. 由于 Python-ast.c 本身还会被解释器的其它地方引用, 不能删除, 所以, 如果是为了压缩解释器大小, 保留这个库也没关系. 如果是为了定制 python 的功能, 也可以屏蔽这个库, 但是源代码需要保留, 不能从工程中删掉. 
  3. audioop (Modules/audioop.c) (http://docs.python.org/library/audioop.html) 一个音频处理的库, 仅 Win32 平台有效.
  4. binascii (Modules/binascii.c) (http://docs.python.org/library/binascii.html) 提供二进制和 ASCII 码的转换, 会被 uu, base64, binhex 这些库引用. 建议保留.
  5. cmath (Modules/cmathmodule.c) (http://docs.python.org/library/cmath.html) 提供复数操作的函数
  6. errno (Modules/errnomodule.c) (http://docs.python.org/library/errno.html) 提供标准的错误码定义, 在很多地方中都会使用, 需要保留.
  7. future_builtins (Modules/future_builtins.c) (http://docs.python.org/library/future_builtins.html) 对那些在 Python2.x 和 Python3 中都有但是意义不一样的函数提供的包装. 使用这里面的函数可以保证调用了正确的版本的函数.
  8. gc (Modules/gcmodule.c) (http://docs.python.org/library/gc.html) Python 的垃圾收集接口. 当然保留.
  9. imageop (Modules/imageop.c) (http://docs.python.org/library/imageop.html) 一些图像处理的函数.
  10. math (Modules/mathmodule.c) (http://docs.python.org/library/math.html) 提供了 C 标准库中的那些数学函数.
  11. _md5 (Modules/md5module.c) 提供了 MD5 算法.
  12. nt (Modules/posixmodule.c) 一些操作系统习惯的函数, 比如打开文件等等.
  13. operator (Modules/operator.c) (http://docs.python.org/library/operator.html) 提供了操作符的等价函数
  14. signal (Modules/signalmodule.c) (http://docs.python.org/library/signal.html) 信号机制, 提供异步事件的回调.
  15. _sha, _sha256, _sha512 三种 SHA 的加密算法模块.
  16. strop (Modules/stropmodule.c) 提供了一些优化的字符串操作.
  17. time (Modules/timemodule.c) (http://docs.python.org/library/time.html) 时间操作库.
  18. thread (Modules/threadmodule.c) Python 线程的底层模块, threading 会使用 thread 库.
  19. cStringIO (Modules/cStringIO.c) (http://docs.python.org/library/stringio.html) StringIO 的高效版本.
  20. cPickle (Modules/cPickle.c) (http://docs.python.org/library/pickle.html) Python 的序列化模块.
  21. msvcrt (PC/msvcrtmodule.c) (http://docs.python.org/library/msvcrt.html) VC 运行时库的包装, 包括一些文件和屏幕操作函数.
  22. _locale (Modules/_localemodule.c) 提供本地化支持的模块.
  23. _subprocess (PC/_subprocess.c) (http://docs.python.org/library/subprocess.html) 操作子进程的库, 平台相关的.
  24. _codecs (Modules/_codecsmodule.c)  (http://docs.python.org/library/codecs.html) 定义了 Python 的编码器相关接口.
  25. _weakref (Modules/_weakref.c) (http://docs.python.org/library/weakref.html) 创建对象的弱引用.
  26. _hotshot (Modules/_hotshot.c) (http://docs.python.org/library/hotshot.html) 类似于 Profiler 模块, 而且将来可能被移除, 现在把它去掉也不错.
  27. _random (Modules/_randommodule.c) 随机数模块.
  28. _bisect (Modules/_bisectmodule.c) (http://docs.python.org/library/bisect.html)  一个基于二分算法, 可以让插入一个数据岛排序的序列后序列仍然有序的库.
  29. _heapq (Modules/_heapqmodule.c) (http://docs.python.org/library/heapq.html) 实现堆栈数据结构算法的库.
  30. _lsprof (Modules/_lsprof.c) (http://docs.python.org/library/profile.html) Profiler 模块, 统计程序执行的性能.
  31. itertools (Modules/itertoolsmodule.c) (http://docs.python.org/library/itertools.html) 一些迭代器操作的模块.
  32. _collections (Modules/_collectionsmodule.c) (http://docs.python.org/library/collections.html) 提供了几个高级的容器类.
  33. _symtable (Modules/symtablemodule.c) (http://docs.python.org/library/symtable.html) 符号表管理模块.
  34. mmap (Modules/mmapmodule.c) (http://docs.python.org/library/mmap.html) 文件内存映射支持模块.
  35. _csv (Modules/_csv.c) (http://docs.python.org/library/csv.html) 为 CSV 模块的内部支持. CSV 模块提供了读写 CSV 文件的功能.
  36. _sre (Modules/_sre.c)  正则表达式的匹配引擎.
  37. parser (Modules/parsermodule.c) (http://docs.python.org/library/parser.html) 操作 Python 语法树的模块.
  38. _winreg (PC/_winreg.c) Windows 注册表操作模块.
  39. _struct (Modules/_struct.c) 提供在 Python 和 C 之间转换数据类型的功能.
  40. datetime (Modules/datetimemodule.c) (http://docs.python.org/library/datetime.html) 日期时间操作函数.
  41. _functools (Modules/_functoolsmodule.c) (http://docs.python.org/library/functools.html) 函数相关操作模块.
  42. _json (Modules/_json.c) (http://docs.python.org/library/json.html) JSON 数据格式操作模块.
  43. xxsubtype (Modules/xxsubtype.c)  这是一个测试相关的模块. 运行 test_descr.py 时会用到.
  44. zipimport (Modules/zipimport.c) 这个模块主要用于从 zip 文件中导入 Python 的模块.
  45. zlib (Modules/zlibmodule.c) 这个模块提供了 zip 压缩和解压功能, 基于 GNU zip 实现.
  46. _multibytecodec, _codecs_cn, _codecs_hk, _codecs_iso2022, _codecs_jp, _codecs_kr, _codecs_tw (Modules/cjkcodecs/*) 这些模块提供了 CJK(中日韩统一表意文字) 的编码和解码. 去掉这部分可以减小 python 解释器 600 多 K.
  47. marshal (Python/marshal.c) (http://docs.python.org/library/marshal.html) 为 Python 对象提供序列化的模块.
  48. imp (Python/import.c) (http://docs.python.org/library/imp.html) 这个模块提供了 Python 里的 import 语句的实现.
  49. __main__, __builtin__, sys, exceptions, _warnings 这部分模块在 config.c 设置里只是一个名字占位符.
  50. _io (Modules/_iomodule.c) (http://docs.python.org/library/io.html) 新版本的 Python 输入输出模块, 在 Python 3 中为默认的输入输出处理方法.
以上就是可以在 config.c 中进行定制的模块,  当然有些模块如果去掉了会对很多功能造成影响, 但是也有很多是平时很少用, 去掉了也无所谓的. 通过定制这些模块就可以大概定制出一个自己需要的 Python 解释器了. 当然, 更详细的定制还需要对 Python 的源代码做更多的研究.

▲评论

X 正在回复:
姓 名: 留下更多信息
性 别:
邮 件:
主 页:
Q Q:
来 自:
职 业:
评 论:
验 证:


Valid HTML 4.01 Strict Valid CSS!
Copyleft.A!die Software Studio.ADSS
Power by webmaster@adintr.com