Skip to content

Commit fa5275f

Browse files
authored
Merge pull request #26 from Deric-W/compiler-subsystem
Compiler subsystem
2 parents 4abb7ac + efdee0b commit fa5275f

30 files changed

+1447
-343
lines changed

.github/workflows/Tests.yaml

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
Test:
1111
strategy:
1212
matrix:
13-
python-version: ["3.5", "3.6", "3.7","3.8", "3.9", "3.10.0-alpha.4", "pypy3"]
13+
python-version: ["3.7", "3.8", "3.9", "3.10.0-alpha.4", "pypy-3.7"]
1414
os: [ubuntu-latest, windows-latest]
1515

1616
runs-on: ${{ matrix.os }}
@@ -50,34 +50,29 @@ jobs:
5050
uses: codecov/codecov-action@v1
5151
with:
5252
flags: ${{ runner.os }}
53+
54+
Typecheck:
55+
runs-on: ubuntu-latest
56+
steps:
57+
# setup
58+
- name: Checkout Repository
59+
uses: actions/checkout@v2
60+
61+
- name: Set up Python ${{ matrix.python-version }}
62+
uses: actions/setup-python@v2
63+
with:
64+
python-version: "3.9"
5365

66+
# dependencies
67+
- name: Restore pip cache
68+
uses: actions/cache@v2
69+
with:
70+
path: ~/.cache/pip
71+
key: ${{ runner.os }}-pip-${{ hashFiles('setup.cfg') }}
72+
73+
- name: Install test dependencies
74+
run: python -m pip --cache-dir ~/.cache/pip install --quiet mypy
5475

55-
# Typecheck: # disabled until we have type hints
56-
# runs-on: ubuntu-latest
57-
# strategy:
58-
# matrix:
59-
# python-version: ["3.5", "3.6", "3.7", "3.8", "3.9", "3.10.0-alpha.4", "pypy3"]
60-
#
61-
# steps:
62-
# # setup
63-
# - name: Checkout Repository
64-
# uses: actions/checkout@v2
65-
#
66-
# - name: Set up Python ${{ matrix.python-version }}
67-
# uses: actions/setup-python@v2
68-
# with:
69-
# python-version: ${{ matrix.python-version }}
70-
#
71-
# # dependencies
72-
# - name: Restore pip cache
73-
# uses: actions/cache@v2
74-
# with:
75-
# path: ~/.cache/pip
76-
# key: ${{ runner.os }}-pip-${{ hashFiles('setup.cfg') }}
77-
#
78-
# - name: Install lint dependencies
79-
# run: python -m pip --cache-dir ~/.cache/pip install --quiet mypy
80-
#
81-
# # lint
82-
# - name: run MyPy
83-
# run: python -m mypy --config-file setupg.cfg .
76+
# check
77+
- name: run MyPy
78+
run: python -m mypy --config-file setup.cfg -p pyhp

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*/__pycache__
2+
*.pyc
3+
.coverage
4+
.htmlcov
25
dist
3-
notes.txt
4-
Test.html
5-
6+
**/.vscode/**

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ The script is called either by the configuration of the web server or a shebang
4444
initialization parameters and provides the following methods:
4545
- `is_available`, wich returns a boolean indicating if the handler can be used
4646
- `is_outdated`, wich returns a boolean indicating if the cache needs to be renewed
47-
- `save`, wich takes an iterator as argument and saves it in the cache
48-
- `load`, wich loads an iterator from the cache
49-
- `close`, wich does cleanup tasks
50-
- note that the iterator may contain code objects which can't be pickled
47+
- `save`, wich takes an code object as an argument and saves it in the cache
48+
- `load`, wich loads an code object from the cache
49+
- `close`, wich does cleanup tasks and gets called when used as a context manager
50+
- note that code objects have to support the pickle protocol
5151
- examples are available in the *cache_handlers* directory
5252

5353
## Installation

cache_handlers/files_mtime.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""PyHP cache handler (files with modification time)"""
44

5-
import marshal # not pickle because only marshal supports code objects
5+
import pickle
66
import os.path
77
from os import makedirs
88
from time import time
@@ -16,6 +16,12 @@ def __init__(self, cache_path, file_path, config):
1616
self.ttl = config.getint("ttl")
1717
self.max_size = config.getint("max_size")
1818

19+
def __enter__(self):
20+
return self
21+
22+
def __exit__(self, type, value, traceback):
23+
self.close()
24+
1925
def get_cachedir_size(self): # get size of cache directory (with all sub directories) in Mbytes
2026
size = 0
2127
for dirpath, dirnames, filenames in os.walk(self.cache_prefix, followlinks=False):
@@ -40,14 +46,13 @@ def is_outdated(self): # return True if cache is not created or needs refre
4046

4147
def load(self): # load sections
4248
with open(self.cache_path, "rb") as cache:
43-
code = marshal.load(cache)
44-
return code
49+
return pickle.load(cache)
4550

4651
def save(self, code): # save sections
4752
if not os.path.isdir(os.path.dirname(self.cache_path)): # directories not already created
4853
makedirs(os.path.dirname(self.cache_path), exist_ok=True) # ignore already created directories
4954
with open(self.cache_path, "wb") as cache:
50-
marshal.dump(code, cache)
55+
pickle.dump(code, cache)
5156

5257
def close(self):
5358
pass # nothing to do

debian/changelog

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
pyhp (2.1-1) stable; urgency=low
2+
3+
* fifth release
4+
* add bytecode code object implementation
5+
* change CacheHandlers to support the context manager protocol
6+
* change CacheHandlers to work with code objects directly
7+
* replace pyhp.embed with pyhp.compiler
8+
* fix errors during compilation and execution having wrong line numbers
9+
* fix code objects lacking proper module constants
10+
11+
-- Eric Wolf <[email protected]> Wed, 27 Jan 2021 19:17:00 +0100
12+
113
pyhp (2.0-1) stable; urgency=low
214

315
* fourth release

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Version: {0}
33
Architecture: all
44
Maintainer: Eric Wolf <[email protected]>
55
Installed-Size: {1}
6-
Depends: python3:any (>= 3.5)
6+
Depends: python3:any (>= 3.7)
77
Suggests: apache2
88
Section: web
99
Priority: optional

debian/copyright

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
22
Upstream-Name: pyhp
33
Upstream-Contact: Eric Wolf <[email protected]>
44
Source: https://github.com/Deric-W/PyHP
5-
Copyright: 2019 Eric Wolf
6-
License: Expat
5+
Copyright: 2021 Eric Wolf
6+
License: GPLv3
77

88
Files: *
9-
Copyright: 2019 Eric Wolf
10-
License: Expat
9+
Copyright: 2021 Eric Wolf
10+
License: GPLv3

debian/pyhp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
#!/usr/bin/python3
22

3-
# script to support the pyhp command
3+
"""Script to support the pyhp command"""
4+
# This script is part of PyHP (https://github.com/Deric-W/PyHP)
5+
# Copyright (C) 2021 Eric Wolf
6+
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, version 3.
10+
11+
# You should have received a copy of the GNU General Public License
12+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
13+
# SPDX-License-Identifier: GPL-3.0-only
414

515
import sys
6-
from pyhp.main import main, get_args
16+
from pyhp.main import main, argparser
717

818
# get cli arguments
9-
args = get_args()
19+
args = argparser.parse_args()
1020

11-
# execute main with file_path as normal argument and the rest as keyword arguments
12-
sys.exit(main(args.pop("file_path"), **args))
21+
# execute main
22+
sys.exit(
23+
main(
24+
args.file,
25+
args.caching,
26+
args.config
27+
)
28+
)

pyhp.conf

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
# This file uses the INI syntax
33

44
[parser]
5-
# regex to isolate the code
6-
# escape sequences are processed
7-
regex = \\<\\?pyhp[\\s](.*?)[\\s]\\?\\>
5+
# regex matching the start and end of a code section
6+
# escape sequences are processed (at the cost of missing unicode support)
7+
start = <\\?pyhp\\s
8+
end = \\s\\?>
9+
10+
# if python code should be stripped of a starting indentation
11+
dedent = True
12+
13+
[compiler]
14+
# optimization level to be used with the builtin compile()
15+
# all levels: https://docs.python.org/3/library/functions.html#compile
16+
optimization_level = -1
817

918
[request]
1019
# order to fill up REQUEST, starting left and missing methods are not filled in
@@ -40,39 +49,3 @@ path = ~/.cache/pyhp
4049

4150
# path to handler
4251
handler_path = /lib/pyhp/cache_handlers/files_mtime.py
43-
44-
[sessions]
45-
enable = True
46-
auto_start = False
47-
48-
# path argument for handler
49-
path = ~/.pyhp/sessions
50-
51-
# session handler + directory containing the session handler
52-
handler = files
53-
handler_path = /lib/pyhp/session_handlers
54-
55-
# lenght of the session id
56-
sid_length = 32
57-
58-
# how to serialize/unserialize session data, pickle or json
59-
serialize_handler = pickle
60-
61-
# config for session cookie
62-
name = PyHPSESSID
63-
cookie_lifetime = 0
64-
cookie_path = /
65-
cookie_domain =
66-
cookie_secure = True
67-
cookie_httponly = False
68-
cookie_samesite =
69-
70-
# probability/divisor = probability for carrying out a garbage collection at startup
71-
gc_probability = 1
72-
gc_divisor = 100
73-
74-
# max lifetime of session since last use
75-
gc_maxlifetime = 1440
76-
77-
# write only if data has changed
78-
lazy_write = True

pyhp/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
# package metadata
1515
# needs to be defined before .main is imported
16-
__version__ = "2.0"
16+
__version__ = "2.1"
1717
__author__ = "Eric Wolf"
1818
__maintainer__ = "Eric Wolf"
19-
__license__ = "MIT"
19+
__license__ = "GPLv3"
2020
__email__ = "[email protected]" # please dont use for spam :(
2121
__contact__ = "https://github.com/Deric-W/PyHP"
2222

23-
# import all submodules
24-
from . import embed
25-
from . import libpyhp
26-
from . import main
23+
__all__ = (
24+
"compiler",
25+
"libpyhp",
26+
"main"
27+
)

0 commit comments

Comments
 (0)