Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMarcelino committed Sep 27, 2022
2 parents b934a4e + a8df62e commit 32e0e09
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 121 deletions.
4 changes: 2 additions & 2 deletions py2many/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
JuliaImportRewriter,
JuliaAugAssignRewriter,
JuliaGeneratorRewriter,
JuliaConditionRewriter,
JuliaBoolOpRewriter,
JuliaIndexingRewriter,
JuliaMainRewriter,
JuliaMethodCallRewriter,
Expand Down Expand Up @@ -520,7 +520,7 @@ def julia_settings(args, env=os.environ):
JuliaClassWrapper(),
JuliaMethodCallRewriter(),
JuliaAugAssignRewriter(),
JuliaConditionRewriter(),
JuliaBoolOpRewriter(),
VariableScopeRewriter(),
JuliaIORewriter(),
JuliaArbitraryPrecisionRewriter(),
Expand Down
12 changes: 12 additions & 0 deletions py2many/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,12 @@ def _visit_val_branch(self, node: ast.If, ann, ann_node_id):
ann_node.annotation = ast.Name(id="None")
for n in node.orelse:
self.visit(n)
else:
node.test.annotation = ann
for n in node.body:
self.visit(n)
for n in node.orelse:
self.visit(n)

def _visit_none_branch(self, node: ast.If, ann, ann_node_id):
if self._is_optional(ann):
Expand All @@ -969,6 +975,12 @@ def _visit_none_branch(self, node: ast.If, ann, ann_node_id):
ann_node.annotation = ann.slice
for n in node.orelse:
self.visit(n)
else:
node.test.annotation = ann
for n in node.body:
self.visit(n)
for n in node.orelse:
self.visit(n)

def _is_optional(self, annotation):
is_optional = lambda x: get_id(x) == "Optional"
Expand Down
54 changes: 46 additions & 8 deletions pyjl/external/modules/ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def visit_Libdl(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,s
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = GENERIC_DISPATCH_TABLE


EXTERNAL_TYPE_MAP = {
GENERIC_EXTERNAL_TYPE_MAP = {
ctypes.c_int: lambda self: "Cint",
ctypes.c_int8: lambda self: "Cint",
ctypes.c_int16: lambda self: "Cint",
Expand Down Expand Up @@ -173,26 +173,64 @@ def visit_Libdl(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,s
ctypes.c_wchar_p: lambda self: "Cwstring", # Ptr{Cwchar_t}
ctypes.c_void_p: lambda self: "Ptr{Cvoid}",
ctypes.CDLL: lambda self: "", # TODO: Temporary
ctypes.WinDLL: lambda self: "",
ctypes.py_object: lambda self: "Ptr{Cvoid}",
}


FUNC_TYPE_MAP = {
GENERIC_FUNC_TYPE_MAP = {
ctypes.cdll.LoadLibrary: lambda self, node, vargs, kwargs: "ctypes.CDLL",
ctypes.windll.LoadLibrary: lambda self, node, vargs, kwargs: "ctypes.WinDLL",
ctypes.CDLL: lambda self, node, vargs, kwargs: "ctypes.CDLL",
ctypes.WinDLL: lambda self, node, vargs, kwargs: "ctypes.WinDLL",
ctypes.PyDLL: lambda self, node, vargs, kwargs: "ctypes.CDLL", # Hack, for now
# Why invalid syntax???
# ctypes.cast: lambda self, node, vargs, kwargs: ast.unparse(vargs[1]) if vargs else "ctypes.cast",
ctypes.cast: lambda self, node, vargs, kwargs: "ctypes._SimpleCData",
ctypes.WINFUNCTYPE: lambda self, node, vargs, kwargs: "PyObject",
ctypes.POINTER: lambda self, node, vargs, kwargs: "ctypes.POINTER",
WindowsError: lambda self, node, vargs, kwargs: "OSError",
}

IGNORED_MODULE_SET = set([
"ctypes",
"ctypes.wintypes"
])

if sys.platform.startswith('win32'):
# Import windows ctypes modules
from builtins import WindowsError
from ctypes import wintypes

WIN_SMALL_DISPATCH_MAP = {
"GetLastError": lambda node, vargs, kwargs: "Base.Libc.GetLastError"
if getattr(node, "is_attr", None)
else "Base.Libc.GetLastError()",
"ctypes.GetLastError": lambda node, vargs, kwargs: "Base.Libc.GetLastError"
if getattr(node, "is_attr", None)
else "Base.Libc.GetLastError()",
}

SMALL_DISPATCH_MAP = GENERIC_SMALL_DISPATCH_MAP | WIN_SMALL_DISPATCH_MAP

WIN_DISPATCH_TABLE = {
ctypes.WinDLL: (JuliaExternalModulePlugins.visit_load_library, True),
ctypes.windll.LoadLibrary: (JuliaExternalModulePlugins.visit_load_library, True),
# ctypes.WinDLL: (JuliaExternalModulePlugins.visit_windll, True),
# ctypes.GetLastError: (lambda self, node, vargs, kwargs: "Base.Libc.GetLastError", True),
ctypes.FormatError: (lambda self, node, vargs, kwargs: f"Base.Libc.FormatMessage({', '.join(vargs)})", True),
wintypes: (JuliaExternalModulePlugins.visit_wintypes, True),
}

WIN_EXTERNAL_TYPE_MAP = {
ctypes.WinDLL: lambda self: "",
}

WIN_FUNC_TYPE_MAP = {
ctypes.windll.LoadLibrary: lambda self, node, vargs, kwargs: "ctypes.WinDLL",
ctypes.WinDLL: lambda self, node, vargs, kwargs: "ctypes.WinDLL",
ctypes.WINFUNCTYPE: lambda self, node, vargs, kwargs: "PyObject",
WindowsError: lambda self, node, vargs, kwargs: "OSError",
}

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = GENERIC_DISPATCH_TABLE | WIN_DISPATCH_TABLE
EXTERNAL_TYPE_MAP = WIN_EXTERNAL_TYPE_MAP | GENERIC_EXTERNAL_TYPE_MAP
FUNC_TYPE_MAP = WIN_FUNC_TYPE_MAP + GENERIC_FUNC_TYPE_MAP
else:
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = GENERIC_DISPATCH_TABLE
EXTERNAL_TYPE_MAP = GENERIC_EXTERNAL_TYPE_MAP
FUNC_TYPE_MAP = GENERIC_FUNC_TYPE_MAP
22 changes: 13 additions & 9 deletions pyjl/external/modules/gzip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ast
import gzip
from typing import Callable, Dict, Tuple, Union

try:
import gzip
except ImportError:
gzip = None


class JuliaExternalModulePlugins:
def visit_gzipopen(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,str]]):
Expand Down Expand Up @@ -30,14 +34,14 @@ def visit_gzipBadGzipFile(self, node: ast.Call, vargs: list[str], kwargs: list[t
def _generic_gzip_visit(self):
self._usings.add("GZip")

if gzip:
FuncType = Union[Callable, str]

FuncType = Union[Callable, str]

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
gzip.open: (JuliaExternalModulePlugins.visit_gzipopen, True),
gzip.compress: (JuliaExternalModulePlugins.visit_gzipcompress, True),
gzip.decompress: (JuliaExternalModulePlugins.visit_gzipdecompress, True),
gzip.BadGzipFile: (JuliaExternalModulePlugins.visit_gzipBadGzipFile, True),
}
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
gzip.open: (JuliaExternalModulePlugins.visit_gzipopen, True),
gzip.compress: (JuliaExternalModulePlugins.visit_gzipcompress, True),
gzip.decompress: (JuliaExternalModulePlugins.visit_gzipdecompress, True),
gzip.BadGzipFile: (JuliaExternalModulePlugins.visit_gzipBadGzipFile, True),
}

IGNORED_MODULE_SET = set(["gzip"])
31 changes: 18 additions & 13 deletions pyjl/external/modules/pandas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

import ast
from typing import BinaryIO, Callable, Dict, Tuple, Union
import pandas
from typing import Callable, Dict, Tuple, Union

try:
import pandas
except:
pandas = None

class JuliaExternalModulePlugins:
def visit_pandas_readcsv(t_self, node: ast.Call, vargs: list[str]):
Expand All @@ -23,17 +27,18 @@ def visit_pandas_dataframe_sum(t_self, node: ast.Call, vargs: list[str]):
def _visit_pandas(t_self):
t_self._usings.add("Pandas")

FuncType = Union[Callable, str]

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
pandas.read_csv: (JuliaExternalModulePlugins.visit_pandas_readcsv, False),
pandas.DataFrame.groupby: (JuliaExternalModulePlugins.visit_pandas_groupby, False),
pandas.DataFrame.to_excel: (JuliaExternalModulePlugins.visit_pandas_toexcel, False),
pandas.core.groupby.generic.DataFrameGroupBy.sum: (
JuliaExternalModulePlugins.visit_pandas_dataframe_sum,
False,
),
}
if pandas:
FuncType = Union[Callable, str]

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
pandas.read_csv: (JuliaExternalModulePlugins.visit_pandas_readcsv, False),
pandas.DataFrame.groupby: (JuliaExternalModulePlugins.visit_pandas_groupby, False),
pandas.DataFrame.to_excel: (JuliaExternalModulePlugins.visit_pandas_toexcel, False),
pandas.core.groupby.generic.DataFrameGroupBy.sum: (
JuliaExternalModulePlugins.visit_pandas_dataframe_sum,
False,
),
}

IGNORED_MODULE_SET = set([
"pandas"
Expand Down
27 changes: 14 additions & 13 deletions pyjl/external/modules/pyproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ def visit_transform(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[s
return f"pyproj.transform({', '.join(vargs)})" \
if vargs else "pyproj.transform"

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
pyproj.Proj: (JuliaExternalModulePlugins.visit_proj, True),
pyproj.transform: (JuliaExternalModulePlugins.visit_transform, True),
}

EXTERNAL_TYPE_MAP = {
pyproj.Proj: lambda self: JuliaExternalModulePlugins.visit_proj(self, None, [], []),
pyproj.transform: lambda self: JuliaExternalModulePlugins.visit_transform(self, None, [], []),
}

IGNORED_MODULE_SET = set([
"pyproj"
])
if pyproj:
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
pyproj.Proj: (JuliaExternalModulePlugins.visit_proj, True),
pyproj.transform: (JuliaExternalModulePlugins.visit_transform, True),
}

EXTERNAL_TYPE_MAP = {
pyproj.Proj: lambda self: JuliaExternalModulePlugins.visit_proj(self, None, [], []),
pyproj.transform: lambda self: JuliaExternalModulePlugins.visit_transform(self, None, [], []),
}

IGNORED_MODULE_SET = set([
"pyproj"
])
29 changes: 17 additions & 12 deletions pyjl/external/modules/requests_mock.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import ast
from pyjl.helpers import pycall_import
import requests_mock
from typing import Callable, Dict, Tuple, Union

FuncType = Union[Callable, str]

try:
import requests_mock
except ImportError:
requests_mock = None

class JuliaExternalModulePlugins():
def visit_mock(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,str]]):
pycall_import(self, node, "requests_mock")
Expand All @@ -18,16 +22,17 @@ def visit_mocker_get(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[
kwargs_str = [f"{kw[0]}={kw[1]}" for kw in kwargs]
return f"{func_name}({', '.join(vargs[1:])}, {', '.join(kwargs_str)})"

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
requests_mock.mock: (JuliaExternalModulePlugins.visit_mock, True),
requests_mock.Mocker.get: (JuliaExternalModulePlugins.visit_mocker_get, True),
# requests_mock.mock.get
}
if requests_mock:
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
requests_mock.mock: (JuliaExternalModulePlugins.visit_mock, True),
requests_mock.Mocker.get: (JuliaExternalModulePlugins.visit_mocker_get, True),
# requests_mock.mock.get
}

IGNORED_MODULE_SET = {
"requests_mock"
}
IGNORED_MODULE_SET = {
"requests_mock"
}

FUNC_TYPE_MAP = {
requests_mock.mock: lambda self, node, vargs, kwargs: "requests_mock.Mocker",
}
FUNC_TYPE_MAP = {
requests_mock.mock: lambda self, node, vargs, kwargs: "requests_mock.Mocker",
}
35 changes: 18 additions & 17 deletions pyjl/external/modules/shapely.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ def visit_point(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,s
return f"shapely_geo.Point({', '.join(vargs)})" \
if vargs else "shapely_geo.Point"

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
BaseGeometry: (JuliaExternalModulePlugins.visit_basegeometry, True),
transform: (JuliaExternalModulePlugins.visit_transform, True),
Point: (JuliaExternalModulePlugins.visit_point, True),
}

IGNORED_MODULE_SET = set([
"shapely",
"shapely.geometry",
"shapely.geometry.base",
"shapely.ops",
])

EXTERNAL_TYPE_MAP = {
BaseGeometry: lambda self: JuliaExternalModulePlugins.visit_basegeometry(self, None, [], []),
Point: lambda self: JuliaExternalModulePlugins.visit_point(self, None, [], []),
}
if shapely:
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
BaseGeometry: (JuliaExternalModulePlugins.visit_basegeometry, True),
transform: (JuliaExternalModulePlugins.visit_transform, True),
Point: (JuliaExternalModulePlugins.visit_point, True),
}

IGNORED_MODULE_SET = set([
"shapely",
"shapely.geometry",
"shapely.geometry.base",
"shapely.ops",
])

EXTERNAL_TYPE_MAP = {
BaseGeometry: lambda self: JuliaExternalModulePlugins.visit_basegeometry(self, None, [], []),
Point: lambda self: JuliaExternalModulePlugins.visit_point(self, None, [], []),
}
25 changes: 14 additions & 11 deletions pyjl/external/modules/shutil.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import shutil
from typing import Callable, Dict, Tuple, Union
try:
import shutil
except ImportError:
shutil = None

FuncType = Union[Callable, str]

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
shutil.rmtree: (lambda self, node, vargs, kwargs: f"Base.Filesystem.rm({vargs[0]}, recursive=True)", True),
shutil.copy: (lambda self, node, vargs, kwargs: f"Base.Filesystem.cp({vargs[0]}, {vargs[1]}, follow_symlinks={vargs[2]}, force=true)"
if len(vargs) == 3 else f"Base.Filesystem.cp({vargs[0]}, {vargs[1]}, force=true)", True),
shutil.move: (lambda self, node, vargs, kwargs: f"Base.Filesystem.mv({vargs[0]}, {vargs[1]})", True),
shutil.chown: (lambda self, node, vargs, kwargs: f"Base.Filesystem.chown({vargs[0]}, {vargs[1]}, {vargs[2]})"
if len(vargs) == 3 else "Base.Filesystem.chown", True),
}
if shutil:
FuncType = Union[Callable, str]
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
shutil.rmtree: (lambda self, node, vargs, kwargs: f"Base.Filesystem.rm({vargs[0]}, recursive=True)", True),
shutil.copy: (lambda self, node, vargs, kwargs: f"Base.Filesystem.cp({vargs[0]}, {vargs[1]}, follow_symlinks={vargs[2]}, force=true)"
if len(vargs) == 3 else f"Base.Filesystem.cp({vargs[0]}, {vargs[1]}, force=true)", True),
shutil.move: (lambda self, node, vargs, kwargs: f"Base.Filesystem.mv({vargs[0]}, {vargs[1]})", True),
shutil.chown: (lambda self, node, vargs, kwargs: f"Base.Filesystem.chown({vargs[0]}, {vargs[1]}, {vargs[2]})"
if len(vargs) == 3 else "Base.Filesystem.chown", True),
}

IGNORED_MODULE_SET = set([
"shutil"
Expand Down
15 changes: 9 additions & 6 deletions pyjl/external/modules/tqdm.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

import ast
import tqdm
from typing import Callable, Dict, Tuple, Union
try:
import tqdm
except ImportError:
tqdm = None

class JuliaExternalModulePlugins():
def visit_tqdm(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,str]]):
self._usings.add("ProgressBars")
# Using tqdm alias (Identical to using ProgressBar)
return f"tqdm({', '.join(vargs)})"

if tqdm:
FuncType = Union[Callable, str]

FuncType = Union[Callable, str]

FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
tqdm.tqdm: (JuliaExternalModulePlugins.visit_tqdm, True),
}
FUNC_DISPATCH_TABLE: Dict[FuncType, Tuple[Callable, bool]] = {
tqdm.tqdm: (JuliaExternalModulePlugins.visit_tqdm, True),
}

IGNORED_MODULE_SET = set(["tqdm"])
Loading

0 comments on commit 32e0e09

Please sign in to comment.