-
Couldn't load subscription status.
- Fork 770
Python bindings & package #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
percevalw
wants to merge
4
commits into
zrax:master
Choose a base branch
from
percevalw:python-bindings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,33 @@ | ||
| # IDE/Filesystem | ||
| .idea | ||
| .vscode | ||
| .DS_Store | ||
|
|
||
| *.swp | ||
| *.swo | ||
| *.gcno | ||
| *.gcda | ||
| *.kdev4 | ||
| /.kdev4 | ||
|
|
||
| # Python | ||
| __pycache__ | ||
| *.egg-info | ||
| .venv | ||
| build/ | ||
|
|
||
| # Build outputs | ||
| bytes/*.cpp | ||
| *.so | ||
| *.o | ||
| *.a | ||
|
|
||
| # Test artifacts | ||
| tests/*.tok.* | ||
| tests/*.src.* | ||
| tests/*.err | ||
| tests/tests | ||
|
|
||
| # CMake/Ninja artifacts | ||
| *.cmake | ||
| cmake-build-debug/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Find the interpreter as well for byte files generation | ||
| find_package(Python COMPONENTS Interpreter Development.Module REQUIRED) | ||
|
|
||
| # Find pybind11 | ||
| execute_process( | ||
| COMMAND ${Python_EXECUTABLE} -c "import pybind11; print(pybind11.get_cmake_dir(), end='')" | ||
| OUTPUT_VARIABLE pybind11_DIR | ||
| ) | ||
| find_package(pybind11 CONFIG REQUIRED) | ||
|
|
||
| # Create C library | ||
| pybind11_add_module(bindings | ||
| bindings.cpp | ||
| ../pycdc.cpp | ||
| ../ASTree.cpp | ||
| ../ASTNode.cpp | ||
| ) | ||
|
|
||
| target_include_directories(bindings PRIVATE pybind11::headers ${Python_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}) | ||
| target_link_libraries(bindings PRIVATE pycxx) | ||
|
|
||
| if (NOT DEFINED CMAKE_LIBRARY_OUTPUT_DIRECTORY) | ||
| set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/lib") | ||
| endif () | ||
|
|
||
| target_compile_definitions( | ||
| bindings | ||
| PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import sys | ||
|
|
||
| from .bindings import decompyle as _decompyle | ||
|
|
||
| __version__ = '0.0.1' | ||
|
|
||
|
|
||
| def decompyle(code, version=(sys.version_info.major, sys.version_info.minor)): | ||
| """ | ||
| Decompyle the given code object. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| code : bytes | ||
| The code object to decompile. | ||
| version : tuple, optional | ||
| The Python version to decompile for. Defaults to the current Python version. | ||
| Use None or (0, 0) to infer the Python version from the code object. This will | ||
| not work for marshalled code objects. | ||
| """ | ||
| if version is None: | ||
| return _decompyle(code, 0, 0) | ||
| else: | ||
| return _decompyle(code, version[0], version[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #pragma clang diagnostic push | ||
| #pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions" | ||
|
|
||
| #include <Python.h> | ||
| #include <pybind11/pybind11.h> | ||
| #include <pybind11/pytypes.h> | ||
| #include <vector> | ||
| #include <cstring> | ||
| #include <ostream> | ||
| #include <sstream> | ||
| #include <optional> | ||
| #include "ASTree.h" | ||
|
|
||
| namespace py = pybind11; | ||
|
|
||
|
|
||
| #ifdef WIN32 | ||
| # define PATHSEP '\\' | ||
| #else | ||
| # define PATHSEP '/' | ||
| #endif | ||
|
|
||
| py::str decompyle_binding(py::bytes &data, int major_version, int minor_version) { | ||
| PycModule mod; | ||
| auto str = data.cast<std::string>(); | ||
| PycBuffer buffer( | ||
| reinterpret_cast<const unsigned char*>(str.c_str()), | ||
| str.size() | ||
| ); | ||
|
|
||
| if (major_version == 0 && minor_version == 0) { | ||
| mod.loadFromStream(buffer); | ||
| } | ||
| else { | ||
| mod.loadFromMarshalledStream( | ||
| buffer, | ||
| major_version, | ||
| minor_version | ||
| ); | ||
| } | ||
| std::ostringstream pyc_output; | ||
| decompyle(mod.code(), &mod, pyc_output); | ||
| return pyc_output.str(); | ||
| } | ||
|
|
||
| PYBIND11_MODULE(bindings, m) { | ||
| m.doc() = "pycdcpy bindings"; | ||
| m.def("decompyle", &decompyle_binding, "Decompile a marshalled python file"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.