Skip to content

Commit cda5299

Browse files
authored
Merge pull request #250 from tdyas/visibility_keywords
allow all visibility keywords on classes and functions
2 parents 06e6109 + e3cc38d commit cda5299

File tree

13 files changed

+974
-247
lines changed

13 files changed

+974
-247
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,13 @@ rustflags = [
112112
```
113113

114114
For `setup.py` integration, see https://github.com/PyO3/setuptools-rust
115+
116+
# Development
117+
118+
To build the crate, run: `make build`
119+
120+
To test the crate, run: `make test`
121+
122+
Note: This crate has several files that are auto-generated using scripts. Using the Makefile ensures that these
123+
files are re-generated as needed.
124+
>>>>>>> 9ce2de8 (Add comment explaining how to regenerate)

python27-sys/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![no_std]
2-
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case, unused_parens)]
2+
#![allow(
3+
non_camel_case_types,
4+
non_upper_case_globals,
5+
non_snake_case,
6+
unused_parens
7+
)]
38

49
pub use crate::boolobject::*;
510
pub use crate::bufferobject::*;

python3-sys/src/initconfig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This entire module is Python 3.8+ and !Py_LIMITED_API only.
22

33
use crate::pyport::Py_ssize_t;
4-
use libc::{c_char, c_int, c_ulong, wchar_t};
54
#[cfg(Py_3_9)]
65
use libc::c_void;
6+
use libc::{c_char, c_int, c_ulong, wchar_t};
77

88
#[repr(C)]
99
#[derive(Copy, Clone)]

python3-sys/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![no_std]
2-
#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, unused_parens)]
2+
#![allow(
3+
non_camel_case_types,
4+
non_snake_case,
5+
non_upper_case_globals,
6+
unused_parens
7+
)]
38
#![cfg_attr(Py_LIMITED_API, allow(unused_imports))]
49

510
// old: marked with TODO

python3-sys/src/methodobject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extern "C" {
9494
arg2: *mut PyObject,
9595
arg3: *mut PyObject,
9696
) -> *mut PyObject;
97-
97+
9898
#[cfg(Py_3_9)]
9999
pub fn PyCMethod_New(
100100
arg1: *mut PyMethodDef,

python3-sys/src/modsupport.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use libc::{c_char, c_int, c_long};
22

33
#[cfg(Py_3_5)]
44
use crate::methodobject::PyMethodDef;
5-
#[cfg(Py_3_9)]
6-
use crate::object::PyTypeObject;
75
use crate::moduleobject::PyModuleDef;
86
use crate::object::PyObject;
7+
#[cfg(Py_3_9)]
8+
use crate::object::PyTypeObject;
99
use crate::pyport::Py_ssize_t;
1010

1111
#[cfg_attr(windows, link(name = "pythonXY"))]

python3-sys/src/object.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,12 @@ extern "C" {
724724
#[cfg(Py_3_4)]
725725
pub fn PyType_GetSlot(arg1: *mut PyTypeObject, arg2: c_int) -> *mut c_void;
726726

727-
728727
#[cfg(Py_3_9)]
729-
pub fn PyType_FromModuleAndSpec(arg1: *mut PyObject, arg2: *mut PyType_Spec, arg3: *mut PyObject) -> *mut PyObject;
728+
pub fn PyType_FromModuleAndSpec(
729+
arg1: *mut PyObject,
730+
arg2: *mut PyType_Spec,
731+
arg3: *mut PyObject,
732+
) -> *mut PyObject;
730733

731734
#[cfg(Py_3_9)]
732735
pub fn PyType_GetModule(arg1: *mut PyTypeObject) -> *mut PyObject;

python3-sys/src/pystate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use libc;
22

3-
use crate::moduleobject::PyModuleDef;
43
#[cfg(Py_3_9)]
54
use crate::frameobject::PyFrameObject;
5+
use crate::moduleobject::PyModuleDef;
66
use crate::object::PyObject;
77

88
#[cfg(Py_3_6)]

src/py_class/py_class.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ instances of that Python class from Rust.
2525
# Syntax
2626
`py_class!(pub class MyType |py| { ... })`
2727
28-
* `pub` makes the generated Rust struct visible outside the current module. It has no effect on the visibility from Python.
28+
* `pub` makes the generated Rust struct visible outside the current module. It has no effect on
29+
the visibility from Python. You may use any Rust visibility keyword. For example, `pub(crate)`
30+
would also be valid.
2931
* `MyType` is the name of the Python class.
3032
* `py` is an identifier that will be made available as a variable of type `Python`
3133
in all function bodies.
@@ -89,6 +91,7 @@ impl MyType {
8991
* The generated type implements a number of traits from the `cpython` crate.
9092
* The inherent `create_instance` method can create new Python objects
9193
given the values for the data fields.
94+
- Note: Any visibility keyword on the class will also be used for this method.
9295
* Private accessors functions are created for the data fields.
9396
* All functions callable from Python are also exposed as public Rust functions.
9497
* To convert from `MyType` to `PyObject`, use `as_object()` or `into_object()` (from the `PythonObject` trait).
@@ -139,16 +142,21 @@ impl MyType {
139142
140143
## Instance methods
141144
`def method_name(&self, parameter-list) -> PyResult<...> { ... }`
145+
`pub(crate) def method_name(&self, parameter-list) -> PyResult<...> { ... }`
142146
143147
Declares an instance method callable from Python.
144148
145149
* Because Python objects are potentially shared, the self parameter must always
146150
be a shared reference (`&self`).
147151
* For details on `parameter-list`, see the documentation of `py_argparse!()`.
148152
* The return type must be `PyResult<T>` for some `T` that implements `ToPyObject`.
153+
* Visibility of the method in Rust defaults to `pub`. You may specify a visibility keyword
154+
before the `def` to change the visibility, for example, to `pub(crate)`. Changing visibility
155+
in Rust does not affect visibility in Python.
149156
150157
## Class methods
151158
`@classmethod def method_name(cls, parameter-list) -> PyResult<...> { ... }`
159+
`@classmethod pub(crate) def method_name(cls, parameter-list) -> PyResult<...> { ... }`
152160
153161
Declares a class method callable from Python.
154162
@@ -157,14 +165,21 @@ Declares a class method callable from Python.
157165
* The first parameter implicitly has type `&PyType`. This type must not be explicitly specified.
158166
* For details on `parameter-list`, see the documentation of `py_argparse!()`.
159167
* The return type must be `PyResult<T>` for some `T` that implements `ToPyObject`.
168+
* Visibility of the method in Rust defaults to `pub`. You may specify a visibility keyword
169+
before the `def` to change the visibility, for example, to `pub(crate)`. Changing visibility
170+
in Rust does not affect visibility in Python.
160171
161172
## Static methods
162173
`@staticmethod def method_name(parameter-list) -> PyResult<...> { ... }`
174+
`@staticmethod pub(crate) def method_name(parameter-list) -> PyResult<...> { ... }`
163175
164176
Declares a static method callable from Python.
165177
166178
* For details on `parameter-list`, see the documentation of `py_argparse!()`.
167179
* The return type must be `PyResult<T>` for some `T` that implements `ToPyObject`.
180+
* Visibility of the method in Rust defaults to `pub`. You may specify a visibility keyword
181+
before the `def` to change the visibility, for example, to `pub(crate)`. Changing visibility
182+
in Rust does not affect visibility in Python.
168183
169184
## Properties
170185
`@property def property_name(&self) -> PyResult<...> { ... }`
@@ -475,14 +490,14 @@ macro_rules! py_class {
475490
/* props: */ { [ /* getters */ ] [ /* setters */ ] }
476491
}
477492
);
478-
(pub class $class:ident |$py: ident| { $( $body:tt )* }) => (
493+
($visibility:vis class $class:ident |$py: ident| { $( $body:tt )* }) => (
479494
$crate::py_class_impl! {
480495
{ $( $body )* }
481496
$class $py
482497
/* info: */ {
483498
/* base_type: */ $crate::PyObject,
484499
/* size: */ <$crate::PyObject as $crate::py_class::BaseObject>::size(),
485-
/* class_visibility: */ {pub},
500+
/* class_visibility: */ {$visibility},
486501
/* gc: */ {
487502
/* traverse_proc: */ None,
488503
/* traverse_data: */ [ /*name*/ ]
@@ -510,11 +525,11 @@ macro_rules! py_class {
510525
#[macro_export]
511526
#[doc(hidden)]
512527
macro_rules! py_class_impl_item {
513-
{ $class:ident, $py:ident, $name:ident( $( $selfarg:tt )* )
528+
{ $class:ident, $py:ident, $visibility:vis, $name:ident( $( $selfarg:tt )* )
514529
$res_type:ty; $body:block [ $( { $pname:ident : $ptype:ty = $detail:tt } )* ]
515530
} => { $crate::py_coerce_item! {
516531
impl $class {
517-
pub fn $name($( $selfarg )* $py: $crate::Python $( , $pname: $ptype )* )
532+
$visibility fn $name($( $selfarg )* $py: $crate::Python $( , $pname: $ptype )* )
518533
-> $res_type $body
519534
}
520535
}}

0 commit comments

Comments
 (0)