Skip to content

Commit 430fe4f

Browse files
authored
Julia formatter (#528)
* add .JuliaFormatter.toml * format everything --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent a1b72f7 commit 430fe4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2737
-1734
lines changed

.JuliaFormatter.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ignore = [
2+
"examples",
3+
".git",
4+
".CondaPkg",
5+
".ipynb_checkpoints",
6+
".pytest_cache",
7+
".venv",
8+
]

bump.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function bump(file, oldpat, newpat)
99
oldtext = read(file, String)
1010
newtext = replace(oldtext, oldpat => newpat)
1111
@assert newtext != oldtext
12-
write(file, newtext)
12+
write(file, newtext)
1313
end
1414

1515
function bumpver(file, pattern, oldver, newver)

docs/customdocs.jl

+7-9
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ function runner(::Type{CustomDocExpander}, node, page, doc)
5353
Dict{Symbol,Any}( # NOTE: Not sure about what to put here.
5454
:module => Main, # This is supposed to be tracking python code.
5555
:path => "",
56-
:linenumber => 0
57-
)
56+
:linenumber => 0,
57+
),
5858
)::Docs.DocStr
5959

6060
# NOTE: This was modified because the original Documenter.create_docsnode was generating unreachable links
@@ -80,12 +80,10 @@ function _parse_docs(code::AbstractString)
8080
m = match(r"^(.+?)\s*-\s*(.+?)\s*(\n[\s\S]*)$", strip(code))
8181

8282
if isnothing(m)
83-
error(
84-
"""
85-
Invalid docstring:
86-
$(code)
87-
"""
88-
)
83+
error("""
84+
Invalid docstring:
85+
$(code)
86+
""")
8987
end
9088

9189
name = Symbol(m[1])
@@ -134,4 +132,4 @@ function _create_docsnode(docstring, result, object, page, doc)
134132
return MarkdownAST.Node(docsnode)
135133
end
136134

137-
end # module CustomDocs
135+
end # module CustomDocs

docs/make.jl

+7-16
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ include("customdocs.jl")
55
makedocs(
66
sitename = "PythonCall & JuliaCall",
77
modules = [PythonCall],
8-
format = Documenter.HTML(
9-
assets = ["assets/favicon.ico"],
10-
),
8+
format = Documenter.HTML(assets = ["assets/favicon.ico"]),
119
warnonly = [:missing_docs], # avoid raising error when docs are missing
1210
pages = [
1311
"Home" => "index.md",
14-
"The Julia module PythonCall" => [
15-
"Guide" => "pythoncall.md",
16-
"Reference" => "pythoncall-reference.md",
17-
],
18-
"The Python module JuliaCall" => [
19-
"Guide" => "juliacall.md",
20-
"Reference" => "juliacall-reference.md",
21-
],
12+
"The Julia module PythonCall" =>
13+
["Guide" => "pythoncall.md", "Reference" => "pythoncall-reference.md"],
14+
"The Python module JuliaCall" =>
15+
["Guide" => "juliacall.md", "Reference" => "juliacall-reference.md"],
2216
"Conversion" => [
2317
"Julia to Python" => "conversion-to-python.md",
2418
"Python to Julia" => "conversion-to-julia.md",
@@ -27,10 +21,7 @@ makedocs(
2721
"faq.md",
2822
"pycall.md",
2923
"releasenotes.md",
30-
]
24+
],
3125
)
3226

33-
deploydocs(
34-
repo = raw"github.com/JuliaPy/PythonCall.jl.git",
35-
push_preview = true
36-
)
27+
deploydocs(repo = raw"github.com/JuliaPy/PythonCall.jl.git", push_preview = true)

src/C/C.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ using UnsafePointers: UnsafePtr
1010
using CondaPkg: CondaPkg
1111
using Pkg: Pkg
1212
using Requires: @require
13-
using Libdl: dlpath, dlopen, dlopen_e, dlclose, dlsym, dlsym_e, RTLD_LAZY, RTLD_DEEPBIND, RTLD_GLOBAL
13+
using Libdl:
14+
dlpath, dlopen, dlopen_e, dlclose, dlsym, dlsym_e, RTLD_LAZY, RTLD_DEEPBIND, RTLD_GLOBAL
1415

1516
include("consts.jl")
1617
include("pointers.jl")

src/C/context.jl

+24-20
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
A handle to a loaded instance of libpython, its interpreter, function pointers, etc.
55
"""
66
@kwdef mutable struct Context
7-
is_embedded :: Bool = false
8-
is_initialized :: Bool = false
9-
is_preinitialized :: Bool = false
10-
lib_ptr :: Ptr{Cvoid} = C_NULL
11-
exe_path :: Union{String, Missing} = missing
12-
lib_path :: Union{String, Missing} = missing
13-
dlopen_flags :: UInt32 = RTLD_LAZY | RTLD_DEEPBIND | RTLD_GLOBAL
14-
pyprogname :: Union{String, Missing} = missing
15-
pyprogname_w :: Any = missing
16-
pyhome :: Union{String, Missing} = missing
17-
pyhome_w :: Any = missing
18-
which :: Symbol = :unknown # :CondaPkg, :PyCall, :embedded or :unknown
19-
version :: Union{VersionNumber, Missing} = missing
20-
matches_pycall :: Union{Bool, Missing} = missing
7+
is_embedded::Bool = false
8+
is_initialized::Bool = false
9+
is_preinitialized::Bool = false
10+
lib_ptr::Ptr{Cvoid} = C_NULL
11+
exe_path::Union{String,Missing} = missing
12+
lib_path::Union{String,Missing} = missing
13+
dlopen_flags::UInt32 = RTLD_LAZY | RTLD_DEEPBIND | RTLD_GLOBAL
14+
pyprogname::Union{String,Missing} = missing
15+
pyprogname_w::Any = missing
16+
pyhome::Union{String,Missing} = missing
17+
pyhome_w::Any = missing
18+
which::Symbol = :unknown # :CondaPkg, :PyCall, :embedded or :unknown
19+
version::Union{VersionNumber,Missing} = missing
20+
matches_pycall::Union{Bool,Missing} = missing
2121
end
2222

2323
const CTX = Context()
@@ -60,7 +60,9 @@ function init_context()
6060
exe_path::String
6161
else
6262
# By default, we use Python installed by CondaPkg.
63-
exe_path = Sys.iswindows() ? joinpath(CondaPkg.envdir(), "python.exe") : joinpath(CondaPkg.envdir(), "bin", "python")
63+
exe_path =
64+
Sys.iswindows() ? joinpath(CondaPkg.envdir(), "python.exe") :
65+
joinpath(CondaPkg.envdir(), "bin", "python")
6466
# It's not sufficient to only activate the env while Python is initialising,
6567
# it must also be active when loading extension modules (e.g. numpy). So we
6668
# activate the environment globally.
@@ -83,7 +85,7 @@ function init_context()
8385

8486
# Ensure Python is runnable
8587
try
86-
run(pipeline(`$exe_path --version`, stdout=devnull, stderr=devnull))
88+
run(pipeline(`$exe_path --version`, stdout = devnull, stderr = devnull))
8789
catch
8890
error("Python executable $(repr(exe_path)) is not executable.")
8991
end
@@ -99,9 +101,9 @@ function init_context()
99101

100102
# Find and open Python library
101103
lib_path = something(
102-
CTX.lib_path===missing ? nothing : CTX.lib_path,
104+
CTX.lib_path === missing ? nothing : CTX.lib_path,
103105
get(ENV, "JULIA_PYTHONCALL_LIB", nothing),
104-
Some(nothing)
106+
Some(nothing),
105107
)
106108
if lib_path !== nothing
107109
lib_ptr = dlopen_e(lib_path, CTX.dlopen_flags)
@@ -112,7 +114,9 @@ function init_context()
112114
CTX.lib_ptr = lib_ptr
113115
end
114116
else
115-
for lib_path in readlines(python_cmd([joinpath(@__DIR__, "find_libpython.py"), "--list-all"]))
117+
for lib_path in readlines(
118+
python_cmd([joinpath(@__DIR__, "find_libpython.py"), "--list-all"]),
119+
)
116120
lib_ptr = dlopen_e(lib_path, CTX.dlopen_flags)
117121
if lib_ptr == C_NULL
118122
@warn "Python library $(repr(lib_path)) could not be opened."
@@ -138,7 +142,7 @@ function init_context()
138142
init_pointers()
139143

140144
# Compare libpath with PyCall
141-
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
145+
@require PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
142146

143147
# Initialize the interpreter
144148
with_gil() do

src/C/extras.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
Py_Type(x::PyPtr) = PyPtr(UnsafePtr(x).type[!])
22

3-
PyObject_Type(x::PyPtr) = (t=Py_Type(x); Py_IncRef(t); t)
3+
PyObject_Type(x::PyPtr) = (t = Py_Type(x); Py_IncRef(t); t)
44

55
Py_TypeCheck(o::PyPtr, t::PyPtr) = PyType_IsSubtype(Py_Type(o), t)
66
Py_TypeCheckFast(o::PyPtr, f::Integer) = PyType_IsSubtypeFast(Py_Type(o), f)
77

8-
PyType_IsSubtypeFast(t::PyPtr, f::Integer) = Cint(!iszero(UnsafePtr{PyTypeObject}(t).flags[] & f))
8+
PyType_IsSubtypeFast(t::PyPtr, f::Integer) =
9+
Cint(!iszero(UnsafePtr{PyTypeObject}(t).flags[] & f))
910

1011
PyMemoryView_GET_BUFFER(m::PyPtr) = Ptr{Py_buffer}(UnsafePtr{PyMemoryViewObject}(m).view)
1112

src/C/pointers.jl

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const CAPI_FUNC_SIGS = Dict{Symbol, Pair{Tuple, Type}}(
1+
const CAPI_FUNC_SIGS = Dict{Symbol,Pair{Tuple,Type}}(
22
# INITIALIZE
33
:Py_Initialize => () => Cvoid,
44
:Py_InitializeEx => (Cint,) => Cvoid,
@@ -270,29 +270,33 @@ const CAPI_OBJECTS = Set([
270270
])
271271

272272
@eval @kwdef mutable struct CAPIPointers
273-
$([:($name :: Ptr{Cvoid} = C_NULL) for name in CAPI_FUNCS]...)
274-
$([:($name :: PyPtr = C_NULL) for name in CAPI_EXCEPTIONS]...)
275-
$([:($name :: PyPtr = C_NULL) for name in CAPI_OBJECTS]...)
276-
PyOS_InputHookPtr :: Ptr{Ptr{Cvoid}} = C_NULL
273+
$([:($name::Ptr{Cvoid} = C_NULL) for name in CAPI_FUNCS]...)
274+
$([:($name::PyPtr = C_NULL) for name in CAPI_EXCEPTIONS]...)
275+
$([:($name::PyPtr = C_NULL) for name in CAPI_OBJECTS]...)
276+
PyOS_InputHookPtr::Ptr{Ptr{Cvoid}} = C_NULL
277277
end
278278

279279
const POINTERS = CAPIPointers()
280280

281-
@eval init_pointers(p::CAPIPointers=POINTERS, lib::Ptr=CTX.lib_ptr) = begin
281+
@eval init_pointers(p::CAPIPointers = POINTERS, lib::Ptr = CTX.lib_ptr) = begin
282282
$([
283283
if name == :Py_FinalizeEx
284284
:(p.$name = dlsym_e(lib, $(QuoteNode(name))))
285285
else
286286
:(p.$name = dlsym(lib, $(QuoteNode(name))))
287-
end
288-
for name in CAPI_FUNCS
287+
end for name in CAPI_FUNCS
289288
]...)
290-
$([:(p.$name = Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS]...)
289+
$(
290+
[
291+
:(p.$name =
292+
Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS
293+
]...
294+
)
291295
$([:(p.$name = dlsym(lib, $(QuoteNode(name)))) for name in CAPI_OBJECTS]...)
292296
p.PyOS_InputHookPtr = dlsym(CTX.lib_ptr, :PyOS_InputHook)
293297
end
294298

295299
for (name, (argtypes, rettype)) in CAPI_FUNC_SIGS
296-
args = [Symbol("x", i) for (i,_) in enumerate(argtypes)]
300+
args = [Symbol("x", i) for (i, _) in enumerate(argtypes)]
297301
@eval $name($(args...)) = ccall(POINTERS.$name, $rettype, ($(argtypes...),), $(args...))
298302
end

src/Compat/Compat.jl

+34-21
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,41 @@
44
Misc bits and bobs for compatibility.
55
"""
66
module Compat
7-
using ..PythonCall: PythonCall # needed for docstring cross-refs
8-
using ..Core
9-
using ..Core: Core, C, Utils, pynew, incref, getptr, pycopy!, pymodulehooks, pyisnull, pybytes_asvector, pysysmodule, pyosmodule, pystr_fromUTF8
10-
using ..Convert: pyconvert, @pyconvert
11-
using ..Wrap: PyArray, PyPandasDataFrame
12-
using Serialization: Serialization, AbstractSerializer, serialize, deserialize
13-
using Tables: Tables
14-
using Requires: @require
7+
using ..PythonCall: PythonCall # needed for docstring cross-refs
8+
using ..Core
9+
using ..Core:
10+
Core,
11+
C,
12+
Utils,
13+
pynew,
14+
incref,
15+
getptr,
16+
pycopy!,
17+
pymodulehooks,
18+
pyisnull,
19+
pybytes_asvector,
20+
pysysmodule,
21+
pyosmodule,
22+
pystr_fromUTF8
23+
using ..Convert: pyconvert, @pyconvert
24+
using ..Wrap: PyArray, PyPandasDataFrame
25+
using Serialization: Serialization, AbstractSerializer, serialize, deserialize
26+
using Tables: Tables
27+
using Requires: @require
1528

16-
include("gui.jl")
17-
include("ipython.jl")
18-
include("multimedia.jl")
19-
include("serialization.jl")
20-
include("tables.jl")
21-
include("pycall.jl")
29+
include("gui.jl")
30+
include("ipython.jl")
31+
include("multimedia.jl")
32+
include("serialization.jl")
33+
include("tables.jl")
34+
include("pycall.jl")
2235

23-
function __init__()
24-
C.with_gil() do
25-
init_gui()
26-
init_pyshow()
27-
init_tables()
28-
end
29-
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
36+
function __init__()
37+
C.with_gil() do
38+
init_gui()
39+
init_pyshow()
40+
init_tables()
3041
end
42+
@require PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
43+
end
3144
end

0 commit comments

Comments
 (0)