测试游记

c2py试用(一)

来呀

欢迎关注我的公众号「测试游记」

install

  1. pip install https://github.com/nanoric/c2py/archive/master.zip

命令

  1. > c2py generate --help

  2. Usage: c2py generate [OPTIONS] MODULE_NAME [FILES]...

  3. Converts C/C++ .h files into python module source files. All matching is

  4. based on c++ qualified name, using regex.

  5. Options:

  6. -e, --encoding TEXT encoding of input files, default is utf-8

  7. -I, --include-path TEXT additional include paths

  8. -A, --additional-include TEXT additional include files. These files will

  9. be included in output cxx file, but skipped

  10. by parser.

  11. -ew, --string-encoding-windows TEXT

  12. encoding used to get & set string. This

  13. value is used to construct std::locale. use

  14. `locale -a` to show all the locates

  15. supported. default is utf-8, which is the

  16. internal encoding used by pybind11.

  17. -el, --string-encoding-linux TEXT

  18. encoding used to get & set string. This

  19. value is used to construct std::locale. use

  20. `locale -a` to show all the locates

  21. supported. default is utf-8, which is the

  22. internal encoding used by pybind11.

  23. -i, --ignore-pattern TEXT ignore symbols matched

  24. --no-callback-pattern TEXT disable generation of callback for functions

  25. matched (for some virtual method used as

  26. undocumented API)

  27. --no-transform-pattern TEXT disable applying transforms(changing its

  28. signature) into functions matched (for some

  29. virtual method used as callback only)

  30. --inout-arg-pattern TEXT make symbol(arguments only) as input_output

  31. --output-arg-pattern TEXT make symbol(arguments only) as output only

  32. --m2c / --no-m2c treat const macros as global variable

  33. --ignore-underline-prefixed / --no-ignore-underline-prefixed

  34. ignore global variables starts with

  35. underline

  36. --ignore-unsupported / --no-ignore-unsupported

  37. ignore functions that has unsupported

  38. argument

  39. --inject-symbol-name / --no-inject-symbol-name

  40. Add comment to describe every generated

  41. symbol's name

  42. -o, --output-dir PATH module source output directory

  43. -p, --pyi-output-dir PATH pyi files output directory

  44. --clear-output-dir / --no-clear-output-dir

  45. --clear-pyi-output-dir / --no-clear-pyi-output-dir

  46. --copy-c2py-includes TEXT copy all c2py include files, excluding input

  47. files to specific dir.

  48. -m, --max-lines-per-file INTEGER RANGE

  49. --generate-setup TEXT if set, generate setup.py into this location

  50. --setup-lib-dir TEXT

  51. --setup-lib TEXT

  52. --setup-use-patches / --setup-no-use-patches

  53. --enforce-version TEXT Check if c2py version matches. If not match,

  54. print error and exit. Use this to prevent

  55. generating code from incompatible version of

  56. c2py.

  57. --help Show this message and exit.

命令作用

  1. Converts C/C++ .h files into python module source files. All matching is based on c++ qualified name, using regex.

转变 C/C++的 .h文件为python模块源文件,所有匹配都基于c++限定名,使用正则表达式。

左边的抽象设计板经中间的模具,生成右边有对应接口的连接件。
把头文件里的声明想成接口图纸。包装工具据此生成供 Python 调用的接口代码;生成代码和调用目标函数不是一回事。
  1. -e, --encoding TEXT

  2. encoding of input files, default is utf-8

  • -e:文件的编码,默认 utf-8

  1. -I, --include-path TEXT

  2. additional include paths

  • -I:其他的包含路径

  1. -A, --additional-include TEXT

  2. additional include files. These files will be included in output cxx file, but skipped by parser.

  • -A:额外的包含文件。这些文件将包含在输出cxx文件中,但是解析器将跳过它们。

  1. -ew, --string-encoding-windows TEXT

  2. encoding used to get & set string. This value is used to construct std::locale. use `locale -a` to show all the locates supported. default is utf-8, which is the internal encoding used by pybind11.

  • -ew:用于获取和设置windows的字符串的编码

  1. -el, --string-encoding-linux TEXT

  2. encoding used to get & set string. This value is used to construct std::locale. use `locale -a` to show all the locates supported. default is utf-8, which is the internal encoding used by pybind11.

  • -el:用于获取和设置linux的字符串的编码

  1. -i, --ignore-pattern TEXT

  2. ignore symbols matched

  • -i:忽略符号匹配

  1. --no-callback-pattern TEXT

  2. disable generation of callback for functions matched (for some virtual method used as undocumented API)

  • --no-callback-pattern:禁用为匹配的函数生成回调(对于一些作为未文档化的API使用的虚拟方法)

  1. --no-transform-pattern TEXT

  2. disable applying transforms(changing its signature) into functions matched (for some virtual method used as callback only)

  • --no-transform-pattern:禁用将转换(更改其签名)应用到匹配的函数(对于某些仅用作回调的虚拟方法)

  1. --inout-arg-pattern TEXT

  2. make symbol(arguments only) as input_output

  • --inout-arg-pattern:将符号(仅限参数)作为input_output

    条纹块和绿块一起进入示意盒,只有条纹块也出现在输出侧。
    只读输入用于提供初始信息;输入输出参数既带入初始值,也把处理后的值带回来。图中用本地形状代替真实数据。
  1. --output-arg-pattern TEXT

  2. make symbol(arguments only) as output only

  • --output-arg-pattern:将符号(仅限参数)作为输出

  1. --m2c / --no-m2c

  2. treat const macros as global variable

  • --m2c/--no-m2c:将const宏作为全局变量

  1. --ignore-underline-prefixed / --no-ignore-underline-prefixed

  2. ignore global variables starts with underline

  • --ignore-underline-prefixed/--no-ignore-underline-prefixed:忽略以下划线开始的全局变量

  1. --ignore-unsupported / --no-ignore-unsupported

  2. ignore functions that has unsupported argument

  • --ignore-unsupported/--no-ignore-unsupported:忽略不支持参数的函数

  1. --inject-symbol-name / --no-inject-symbol-name

  2. Add comment to describe every generated symbol's name

  • --inject-symbol-name/--no-inject-symbol-name:添加注释来描述每个生成的符号的名称

  1. -o, --output-dir PATH

  2. module source output directory

  • -o:模块源输出目录

  1. -p, --pyi-output-dir PATH

  2. pyi files output directory

  • -p :pyi文件输出目录

  1. --clear-output-dir / --no-clear-output-dir

  1. --clear-pyi-output-dir / --no-clear-pyi-output-dir

  1. --copy-c2py-includes TEXT

  2. copy all c2py include files, excluding input files to specific dir.

  • --copy-c2py-includes:复制所有c2py包含的文件,不包括输入文件到特定的目录。

  1. -m, --max-lines-per-file INTEGER RANGE

  1. --generate-setup TEXT

  2. if set, generate setup.py into this location

  • --generate-setup:如果设置,则在此位置生成setup.py

  1. --setup-lib-dir TEXT

  1. --setup-lib TEXT

  1. --setup-use-patches / --setup-no-use-patches

  1. --enforce-version TEXT

  2. Check if c2py version matches. If not match, print error and exit. Use this to prevent generating code from incompatible version of c2py.

  • --enforce-version:检查c2py版本是否匹配。如果不匹配,则打印错误并退出。使用它可以防止从不兼容的c2py版本生成代码。

  1. --help

  2. Show this message and exit.

案例测试

环境:

  • Python3.7.4

  • Windows 64

  • Visual Studio 2019

https://visualstudio.microsoft.com/zh-hans/downloads

  • 测试内容&语句

c2py/tests/generate/ctp/vnctp

  1. $ c2py generate vnctp ThostFtdcMdApi.h ThostFtdcTraderApi.h ThostFtdcUserApiDataType.h ThostFtdcUserApiStruct.h -I vnctp/include/ --no-callback-pattern ".*Api::.*" --string-encoding-windows .936 --string-encoding-linux zh_CN.GB18030 --copy-c2py-includes vnctp/include/ --output-dir vnctp/generated_files/ --clear-output-dir --generate-setup . --setup-lib-dir vnctp/libs/ --setup-lib thostmduserapi --setup-lib thosttraderapi

期间会有各种找不到文件的报错,将 c2py中的文件拷贝到 python/Lib/site-packages对应路径下即可

  1. $ /e/c2py/tests/generate/ctp/generate_ctp.sh

  2. c2py 0.5.4

  3. parsing ...

  4. parsing 100.00: 2697/2697

  5. parse finished.

  6. processing result ...

  7. process finished.

  8. # of unsupported functions: 0

  9. generating cxx code ...

  10. cxx code generated.

  11. generating pyi code ...

  12. pyi code generated.

  13. # of files generated : 36

  14. wrappers.hpp

  15. module.cpp

  16. generated_functions.h

  17. generated_functions_0.cpp

  18. generated_functions_1.cpp

  19. generated_functions_2.cpp

  20. generated_functions_3.cpp

  21. generated_functions_4.cpp

  22. generated_functions_5.cpp

  23. generated_functions_6.cpp

  24. generated_functions_7.cpp

  25. generated_functions_8.cpp

  26. generated_functions_9.cpp

  27. generated_functions_10.cpp

  28. generated_functions_11.cpp

  29. generated_functions_12.cpp

  30. generated_functions_13.cpp

  31. generated_functions_14.cpp

  32. generated_functions_15.cpp

  33. generated_functions_16.cpp

  34. generated_functions_17.cpp

  35. generated_functions_18.cpp

  36. generated_functions_19.cpp

  37. generated_functions_20.cpp

  38. generated_functions_21.cpp

  39. generated_functions_22.cpp

  40. generated_functions_23.cpp

  41. generated_functions_24.cpp

  42. generated_functions_25.cpp

  43. generated_functions_26.cpp

  44. generated_functions_27.cpp

  45. generated_functions_28.cpp

  46. generated_functions_29.cpp

  47. generated_functions_30.cpp

  48. config.h

  49. module.hpp

  50. # of files generated : 1

  51. vnctp.pyi

完成之后,会在 vnctp文件下生成一个 generated_files文件夹

进入 c2py/tests/generate/ctp下有一个 setup.py执行

  1. $ python setup.py build

  2. $ python setup.py install

这个时候没安装 VisualStudio会报错,安装一下

  1. C:\Users\Administrator>C:\Python37\Scripts\pip3.exe list

  2. Package Version

  3. ---------- -------

  4. c2py 0.5.4

  5. Click 7.0

  6. pip 19.0.3

  7. setuptools 40.8.0

  8. vnctp 1.0.0

导入还存在问题,需要继续查看原因

  1. $ python3

  2. Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32

  3. Type "help", "copyright", "credits" or "license" for more information.

  4. >>> import vnctp

  5. Traceback (most recent call last):

  6. File "<stdin>", line 1, in <module>

  7. ImportError: DLL load failed: 找不到指定的模块。

  8. >>>

本文视频讲解

打开或下载 MP4 ↗

评论与讨论

使用 GitHub 账号留言,评论公开显示。

搜索文章