Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mongodb/mongo-python-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Dec 3, 2024
2 parents edb5419 + ce1c49a commit ffa3d0d
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ functions:
# Make an evergreen expansion file with dynamic values
- command: subprocess.exec
params:
include_expansions_in_env: ["is_patch", "project", "version_id", "AUTH", "SSL", "test_encryption", "test_encryption_pyopenssl", "test_crypt_shared", "test_pyopenssl", "SETDEFAULTENCODING", "test_loadbalancer", "test_serverless", "SKIP_CSOT_TESTS", "MONGODB_STARTED", "DISABLE_TEST_COMMANDS", "GREEN_FRAMEWORK", "NO_EXT", "COVERAGE", "COMPRESSORS", "TEST_SUITES", "MONGODB_API_VERSION", "SKIP_HATCH", "skip_crypt_shared", "VERSION", "TOPOLOGY", "STORAGE_ENGINE", "ORCHESTRATION_FILE", "REQUIRE_API_VERSION", "LOAD_BALANCER", "skip_web_identity_auth_test", "skip_ECS_auth_test"]
include_expansions_in_env: ["is_patch", "project", "version_id", "AUTH", "SSL", "test_encryption", "test_encryption_pyopenssl", "test_crypt_shared", "test_pyopenssl", "SETDEFAULTENCODING", "test_loadbalancer", "test_serverless", "SKIP_CSOT_TESTS", "MONGODB_STARTED", "DISABLE_TEST_COMMANDS", "GREEN_FRAMEWORK", "NO_EXT", "COVERAGE", "COMPRESSORS", "TEST_SUITES", "MONGODB_API_VERSION", "skip_crypt_shared", "VERSION", "TOPOLOGY", "STORAGE_ENGINE", "ORCHESTRATION_FILE", "REQUIRE_API_VERSION", "LOAD_BALANCER", "skip_web_identity_auth_test", "skip_ECS_auth_test"]
binary: bash
working_dir: "src"
args:
Expand Down
9 changes: 0 additions & 9 deletions .evergreen/generated_configs/variants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ buildvariants:
- rhel79-small
batchtime: 10080
expansions:
SKIP_HATCH: "true"
PYTHON_BINARY: /opt/python/3.9/bin/python3
- name: other-hosts-rhel9-fips
tasks:
Expand All @@ -17,35 +16,27 @@ buildvariants:
run_on:
- rhel92-fips
batchtime: 10080
expansions:
SKIP_HATCH: "true"
- name: other-hosts-rhel8-zseries
tasks:
- name: .6.0 .standalone !.sync_async
display_name: Other hosts RHEL8-zseries
run_on:
- rhel8-zseries-small
batchtime: 10080
expansions:
SKIP_HATCH: "true"
- name: other-hosts-rhel8-power8
tasks:
- name: .6.0 .standalone !.sync_async
display_name: Other hosts RHEL8-POWER8
run_on:
- rhel8-power-small
batchtime: 10080
expansions:
SKIP_HATCH: "true"
- name: other-hosts-rhel8-arm64
tasks:
- name: .6.0 .standalone !.sync_async
display_name: Other hosts RHEL8-arm64
run_on:
- rhel82-arm64-small
batchtime: 10080
expansions:
SKIP_HATCH: "true"

# Atlas connect tests
- name: atlas-connect-rhel8-python3.9
Expand Down
46 changes: 3 additions & 43 deletions .evergreen/hatch.sh
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
#!/bin/bash
set -o errexit # Exit the script with error if any of the commands fail
set -x
set -eu

. .evergreen/utils.sh

if [ -z "$PYTHON_BINARY" ]; then
PYTHON_BINARY=$(find_python3)
fi

# Check if we should skip hatch and run the tests directly.
if [ -n "$SKIP_HATCH" ]; then
ENV_NAME=testenv-$RANDOM
createvirtualenv "$PYTHON_BINARY" $ENV_NAME
# shellcheck disable=SC2064
trap "deactivate; rm -rf $ENV_NAME" EXIT HUP
python -m pip install -e ".[test]"
run_hatch() {
bash ./.evergreen/run-tests.sh
}
else # Set up virtualenv before installing hatch
# Use a random venv name because the encryption tasks run this script multiple times in the same run.
ENV_NAME=hatchenv-$RANDOM
createvirtualenv "$PYTHON_BINARY" $ENV_NAME
# shellcheck disable=SC2064
trap "deactivate; rm -rf $ENV_NAME" EXIT HUP
python -m pip install -q hatch

# Ensure hatch does not write to user or global locations.
touch hatch_config.toml
HATCH_CONFIG=$(pwd)/hatch_config.toml
if [ "Windows_NT" = "${OS:-}" ]; then # Magic variable in cygwin
HATCH_CONFIG=$(cygpath -m "$HATCH_CONFIG")
fi
export HATCH_CONFIG
hatch config restore
hatch config set dirs.data "$(pwd)/.hatch/data"
hatch config set dirs.cache "$(pwd)/.hatch/cache"

run_hatch() {
python -m hatch run "$@"
}
fi

run_hatch "${@:1}"
. .evergreen/scripts/ensure-hatch.sh
hatch run "$@"
3 changes: 3 additions & 0 deletions .evergreen/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ cp ${PROJECT_DIRECTORY}/test/certificates/* ${DRIVERS_TOOLS}/.evergreen/x509gen/
# Replace MongoOrchestration's client certificate.
cp ${PROJECT_DIRECTORY}/test/certificates/client.pem ${MONGO_ORCHESTRATION_HOME}/lib/client.pem

# Ensure hatch is installed.
bash ${PROJECT_DIRECTORY}/scripts/ensure-hatch.sh

if [ -w /etc/hosts ]; then
SUDO=""
else
Expand Down
6 changes: 3 additions & 3 deletions .evergreen/run-mongodb-aws-ecs-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ set -o xtrace

# Install python with pip.
PYTHON_VER="python3.9"
apt-get update
apt-get install $PYTHON_VER python3-pip build-essential $PYTHON_VER-dev -y
apt-get -qq update < /dev/null > /dev/null
apt-get -qq install $PYTHON_VER $PYTHON_VER-venv build-essential $PYTHON_VER-dev -y < /dev/null > /dev/null

export PYTHON_BINARY=$PYTHON_VER
export TEST_AUTH_AWS=1
export AUTH="auth"
export SET_XTRACE_ON=1
cd src
$PYTHON_BINARY -m pip install -q --user hatch
rm -rf .venv
bash .evergreen/hatch.sh test:test-eg
4 changes: 3 additions & 1 deletion .evergreen/scripts/configure-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ fi

PROJECT_DIRECTORY="$(pwd)"
DRIVERS_TOOLS="$(dirname $PROJECT_DIRECTORY)/drivers-tools"
CARGO_HOME=${CARGO_HOME:-${DRIVERS_TOOLS}/.cargo}

# Python has cygwin path problems on Windows. Detect prospective mongo-orchestration home directory
if [ "Windows_NT" = "$OS" ]; then # Magic variable in cygwin
DRIVERS_TOOLS=$(cygpath -m $DRIVERS_TOOLS)
PROJECT_DIRECTORY=$(cygpath -m $PROJECT_DIRECTORY)
CARGO_HOME=$(cygpath -m $CARGO_HOME)
fi

SCRIPT_DIR="$PROJECT_DIRECTORY/.evergreen/scripts"
Expand Down Expand Up @@ -47,13 +49,13 @@ export NO_EXT="${NO_EXT:-}"
export COVERAGE="${COVERAGE:-}"
export COMPRESSORS="${COMPRESSORS:-}"
export MONGODB_API_VERSION="${MONGODB_API_VERSION:-}"
export SKIP_HATCH="${SKIP_HATCH:-}"
export skip_crypt_shared="${skip_crypt_shared:-}"
export STORAGE_ENGINE="${STORAGE_ENGINE:-}"
export REQUIRE_API_VERSION="${REQUIRE_API_VERSION:-}"
export skip_web_identity_auth_test="${skip_web_identity_auth_test:-}"
export skip_ECS_auth_test="${skip_ECS_auth_test:-}"
export CARGO_HOME="$CARGO_HOME"
export TMPDIR="$MONGO_ORCHESTRATION_HOME/db"
export PATH="$MONGODB_BINARIES:$PATH"
# shellcheck disable=SC2154
Expand Down
55 changes: 55 additions & 0 deletions .evergreen/scripts/ensure-hatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

set -eu

# Ensure hatch is available.
if [ ! -x "$(command -v hatch)" ]; then
# Install a virtual env with "hatch"
# Ensure there is a python venv.
. .evergreen/utils.sh

if [ -z "${PYTHON_BINARY:-}" ]; then
PYTHON_BINARY=$(find_python3)
fi
VENV_DIR=.venv
if [ ! -d $VENV_DIR ]; then
echo "Creating virtual environment..."
createvirtualenv "$PYTHON_BINARY" .venv
echo "Creating virtual environment... done."
fi
if [ -f $VENV_DIR/Scripts/activate ]; then
. $VENV_DIR/Scripts/activate
else
. $VENV_DIR/bin/activate
fi

python --version

echo "Installing hatch..."
python -m pip install -U pip
python -m pip install hatch || {
# Install rust and try again.
CARGO_HOME=${CARGO_HOME:-${DRIVERS_TOOLS}/.cargo}
# Handle paths on Windows.
if [ "Windows_NT" = "${OS:-}" ]; then # Magic variable in cygwin
CARGO_HOME=$(cygpath -m $CARGO_HOME)
fi
export RUSTUP_HOME="${CARGO_HOME}/.rustup"
${DRIVERS_TOOLS}/.evergreen/install-rust.sh
source "${CARGO_HOME}/env"
python -m pip install hatch
}
# Ensure hatch does not write to user or global locations.
touch hatch_config.toml
HATCH_CONFIG=$(pwd)/hatch_config.toml
if [ "Windows_NT" = "${OS:-}" ]; then # Magic variable in cygwin
HATCH_CONFIG=$(cygpath -m "$HATCH_CONFIG")
fi
export HATCH_CONFIG
hatch config restore
hatch config set dirs.data "$(pwd)/.hatch/data"
hatch config set dirs.cache "$(pwd)/.hatch/cache"

echo "Installing hatch... done."
fi
hatch --version
7 changes: 2 additions & 5 deletions .evergreen/scripts/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,19 +744,17 @@ def create_aws_auth_variants():


def create_alternative_hosts_variants():
expansions = dict(SKIP_HATCH="true")
batchtime = BATCHTIME_WEEK
variants = []

host = HOSTS["rhel7"]
variants.append(
create_variant(
[".5.0 .standalone !.sync_async"],
get_display_name("OpenSSL 1.0.2", host, python=CPYTHONS[0], **expansions),
get_display_name("OpenSSL 1.0.2", host, python=CPYTHONS[0]),
host=host,
python=CPYTHONS[0],
batchtime=batchtime,
expansions=expansions,
)
)

Expand All @@ -765,8 +763,7 @@ def create_alternative_hosts_variants():
variants.append(
create_variant(
[".6.0 .standalone !.sync_async"],
display_name=get_display_name("Other hosts", host, **expansions),
expansions=expansions,
display_name=get_display_name("Other hosts", host),
batchtime=batchtime,
host=host,
)
Expand Down
3 changes: 2 additions & 1 deletion .evergreen/scripts/run-enterprise-auth-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

# Disable xtrace for security reasons (just in case it was accidentally set).
set +x
bash "${DRIVERS_TOOLS}"/.evergreen/auth_aws/setup_secrets.sh drivers/enterprise_auth
# Use the default python to bootstrap secrets.
PYTHON_BINARY="" bash "${DRIVERS_TOOLS}"/.evergreen/auth_aws/setup_secrets.sh drivers/enterprise_auth
TEST_ENTERPRISE_AUTH=1 AUTH=auth bash "${PROJECT_DIRECTORY}"/.evergreen/hatch.sh test:test-eg
1 change: 0 additions & 1 deletion .evergreen/scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@ GREEN_FRAMEWORK=${GREEN_FRAMEWORK} \
TEST_DATA_LAKE=${TEST_DATA_LAKE:-} \
TEST_SUITES=${TEST_SUITES:-} \
MONGODB_API_VERSION=${MONGODB_API_VERSION} \
SKIP_HATCH=${SKIP_HATCH} \
bash "${PROJECT_DIRECTORY}"/.evergreen/hatch.sh test:test-eg
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ secrets-export.sh
libmongocrypt.tar.gz
libmongocrypt/
libmongocrypt_git/
hatch_config.toml
.venv

# Lambda temp files
test/lambda/.aws-sam
Expand Down
5 changes: 4 additions & 1 deletion bson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,10 @@ def _dict_to_bson(
elements.append(_name_value_to_bson(b"_id\x00", doc["_id"], check_keys, opts))
for key, value in doc.items():
if not top_level or key != "_id":
elements.append(_element_to_bson(key, value, check_keys, opts))
try:
elements.append(_element_to_bson(key, value, check_keys, opts))
except InvalidDocument as err:
raise InvalidDocument(f"Invalid document {doc} | {err}") from err
except AttributeError:
raise TypeError(f"encoder expected a mapping type but got: {doc!r}") from None

Expand Down
35 changes: 35 additions & 0 deletions bson/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,41 @@ int write_dict(PyObject* self, buffer_t buffer,
while (PyDict_Next(dict, &pos, &key, &value)) {
if (!decode_and_write_pair(self, buffer, key, value,
check_keys, options, top_level)) {
if (PyErr_Occurred()) {
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
PyErr_Fetch(&etype, &evalue, &etrace);
PyObject *InvalidDocument = _error("InvalidDocument");

if (top_level && InvalidDocument && PyErr_GivenExceptionMatches(etype, InvalidDocument)) {

Py_DECREF(etype);
etype = InvalidDocument;

if (evalue) {
PyObject *msg = PyObject_Str(evalue);
Py_DECREF(evalue);

if (msg) {
// Prepend doc to the existing message
PyObject *dict_str = PyObject_Str(dict);
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", PyUnicode_AsUTF8(dict_str), PyUnicode_AsUTF8(msg));
Py_DECREF(dict_str);

if (new_msg) {
evalue = new_msg;
}
else {
evalue = msg;
}
}
}
PyErr_NormalizeException(&etype, &evalue, &etrace);
}
else {
Py_DECREF(InvalidDocument);
}
PyErr_Restore(etype, evalue, etrace);
}
return 0;
}
}
Expand Down
1 change: 1 addition & 0 deletions doc/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ The following is a list of people who have contributed to
- Ivan Lukyanchikov (ilukyanchikov)
- Terry Patterson
- Romain Morotti
- Navjot Singh (navjots18)
13 changes: 13 additions & 0 deletions test/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,19 @@ def __repr__(self):
):
encode({"t": Wrapper(1)})

def test_doc_in_invalid_document_error_message(self):
class Wrapper:
def __init__(self, val):
self.val = val

def __repr__(self):
return repr(self.val)

self.assertEqual("1", repr(Wrapper(1)))
doc = {"t": Wrapper(1)}
with self.assertRaisesRegex(InvalidDocument, f"Invalid document {doc}"):
encode(doc)


class TestCodecOptions(unittest.TestCase):
def test_document_class(self):
Expand Down

0 comments on commit ffa3d0d

Please sign in to comment.