-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Configures pytest to compile our example modules using the same infrastructure as pydust would for user projects. * We pass pyconf into pydust and the user's code so we can switch on various bits of functionality (and configure limited API)
- Loading branch information
Showing
14 changed files
with
154 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ __pycache__/ | |
|
||
# C extensions | ||
*.so | ||
*.so.o | ||
|
||
# Distribution / packaging | ||
.Python | ||
|
This file contains 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,5 @@ | ||
{ | ||
"[python]": { | ||
"editor.defaultFormatter": "ms-python.black-formatter" | ||
}, | ||
} |
This file contains 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 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,44 @@ | ||
const std = @import("std"); | ||
const py = @import("pydust"); | ||
|
||
const Self = @This(); | ||
|
||
pub const __doc__ = | ||
\\A docstring for the example module. | ||
\\ | ||
\\With lots of lines... | ||
\\ | ||
\\One day we'll parse these from Zig doc comments in the AST :) | ||
; | ||
|
||
/// Internal module state can be declared as struct fields. | ||
/// | ||
/// Default values must be set inline, or in the __new__ function if | ||
/// they cannot be defaulted at comptime. | ||
count: u32 = 0, | ||
name: py.PyString, | ||
|
||
pub fn __new__() !Self { | ||
return .{ .name = try py.PyString.fromSlice("Nick") }; | ||
} | ||
|
||
pub fn hello() !py.PyString { | ||
return try py.PyString.fromSlice("Hello!"); | ||
} | ||
|
||
pub fn whoami(self: *const Self) !py.PyString { | ||
return self.name; | ||
} | ||
|
||
/// Functions taking a "self" parameter are passed the module state. | ||
pub fn increment(self: *Self) void { | ||
self.count += 1; | ||
} | ||
|
||
pub fn count(self: *const Self) u32 { | ||
return self.count; | ||
} | ||
|
||
comptime { | ||
py.module(@This()); | ||
} |
This file contains 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,3 @@ | ||
/// This pyconf module is used during our own tests to represent the typically auto-generated pyconf module. | ||
pub const limited_api = true; | ||
pub const hexversion = "0x030B0000"; // 3.11 |
This file contains 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 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 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,6 +1,10 @@ | ||
// Export the Limited Python C API for use within PyDust. | ||
const pyconf = @import("pyconf"); | ||
|
||
pub usingnamespace @cImport({ | ||
@cDefine("Py_LIMITED_API", "0x030A0000"); // 3.10 | ||
if (pyconf.limited_api) { | ||
@cDefine("Py_LIMITED_API", pyconf.hexversion); | ||
} | ||
@cDefine("PY_SSIZE_T_CLEAN", {}); | ||
@cInclude("Python.h"); | ||
}); |
This file contains 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 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 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 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,6 @@ | ||
from pydust import build | ||
|
||
|
||
def pytest_collection(session): | ||
"""We use the same pydust build system for our example modules, but we trigger it from a pytest hook.""" | ||
build.build() |
This file contains 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,20 @@ | ||
from example import modules | ||
|
||
|
||
def test_module_docstring(): | ||
assert modules.__doc__.startswith("A docstring for the example module.") | ||
|
||
|
||
def test_modules_function(): | ||
assert modules.hello() == "Hello!" | ||
|
||
|
||
def test_modules_state(): | ||
assert modules.whoami() == "Nick" | ||
|
||
|
||
def test_modules_mutable_state(): | ||
assert modules.count() == 0 | ||
modules.increment() | ||
modules.increment() | ||
assert modules.count() == 2 |